Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit bd35c6b

Browse files
committed
Test ElementHandle::setAs for booleans
It was not tested. Signed-off-by: Kevin Rocard <[email protected]>
1 parent af6088d commit bd35c6b

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

parameter/include/ElementHandle.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class PARAMETER_EXPORT ElementHandle
177177

178178
/** Access (get or set) parameters as different types.
179179
*
180-
* Will fail if the element is not a paramete.
180+
* Will fail if the element is not a parameter.
181181
* Array access will fail if the parameter is not an array.
182182
*
183183
* @param value if get, the value to get (in parameter)
@@ -233,15 +233,35 @@ class PARAMETER_EXPORT ElementHandle
233233
/** @} */
234234

235235
protected:
236+
/** Friend to be constructed from CParameterMgr::createElementHandle. */
237+
friend CParameterMgr;
238+
239+
/** Protected for test purposes.
240+
* Client must not inherit from this class anyway.
241+
* @{ */
236242
ElementHandle(CConfigurableElement &element, CParameterMgr &parameterMgr);
237-
friend CParameterMgr; // So that it can build the handler
238243

239-
private:
244+
/** Access a parameter with a template type
245+
*
246+
* Template version of getAsBoolean, getAsInteger, getAsDoubleArray...
247+
*
248+
* @tparam T How to access the parameter.
249+
* Eg: use bool to call setAsBoolean
250+
* use std::vector<bool> to call setAsBooleanArray
251+
*
252+
* @note Why are those two methods not public ?
253+
* It could depreciate all the other setAs* and getAs*
254+
* @{
255+
*/
240256
template <class T>
241257
bool setAs(const T value, std::string &error) const;
242258
template <class T>
243259
bool getAs(T &value, std::string &error) const;
260+
/** @} */
244261

262+
/** @} */
263+
264+
private:
245265
template <class T>
246266
static size_t getSize(T value);
247267
template <class T>

test/functional-tests/Handle.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,33 @@ SCENARIO("Mapping handle access", "[handler][mapping]")
602602
}
603603
}
604604

605+
SCENARIO_METHOD(AllParamsPF, "Access boolean", "[handler][access]")
606+
{
607+
ElementHandle handle(*this, "/test/test/bool");
608+
struct TestVector
609+
{
610+
bool expectedValue;
611+
list<string> values;
612+
};
613+
list<TestVector> testVectors = {
614+
{true, {"1", "0x1", "001", "0x001", "true", "True", "TRUe"}},
615+
{false, {"0", "0x0", "000", "0x000", "false", "False", "FaLSe"}}};
616+
617+
for (auto testVector : testVectors) {
618+
for (auto value : testVector.values) {
619+
CAPTURE(value);
620+
auto expectedValue = testVector.expectedValue;
621+
CAPTURE(expectedValue);
622+
623+
CHECK_NOTHROW(handle.setAs(value));
624+
CHECK(handle.getAs<decltype(value)>() == std::to_string(expectedValue));
625+
CHECK(handle.getAs<decltype(expectedValue)>() == expectedValue);
626+
}
627+
}
628+
629+
for (string invalid : {"2", "-1", "0x10", "None", "faux"}) {
630+
CHECK_THROWS_AS(handle.setAs(invalid), Exception);
631+
}
632+
}
633+
605634
} // namespace parameterFramework

test/functional-tests/include/ElementHandle.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ namespace parameterFramework
4141
* Contrary to ::ElementHandle, is constructed through it's constructor
4242
* and not a factory method.
4343
* @see parameterFramework::ParameterFramework for the main PF interface.
44+
*
45+
* @fixme Should privately inherit from FailureWrapper but this can not be done
46+
* as this class uses protected ::ElementHandle::getAs and setAs methods.
47+
* See their documentation for more information.
4448
*/
45-
class ElementHandle : private FailureWrapper<::ElementHandle>
49+
class ElementHandle : protected FailureWrapper<::ElementHandle>
4650
{
4751
ElementHandle(const ElementHandle &other) = delete;
4852
ElementHandle &operator=(const ElementHandle &other) = delete;
@@ -71,6 +75,20 @@ class ElementHandle : private FailureWrapper<::ElementHandle>
7175
return value;
7276
}
7377

78+
/** Wrap EH::setAs* to throw an exception on failure. */
79+
template <class T>
80+
void setAs(T value)
81+
{
82+
mayFailCall(&EH::setAs<T>, value);
83+
}
84+
85+
/** Wrap EH::getAs* to throw an exception on failure.
86+
* @todo list allowed types. */
87+
template <class T>
88+
T getAs()
89+
{
90+
return mayFailGet(&EH::getAs<T>);
91+
}
7492
/** Wrap EH::setAsDouble to throw an exception on failure. */
7593
void setAsDouble(double value) { mayFailCall(&EH::setAsDouble, value); }
7694
/** Wrap EH::getAsDouble to throw an exception on failure. */

test/xml-generator/testVector/complex.pfw

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ domainGroup: Red sequenceAware
3737
q2.5 = 1.18750
3838
string = 12 ab @ <![CDATA[ < > \n \0 ✔ "'\
3939
component: included
40-
bool = 1
40+
bool = true

0 commit comments

Comments
 (0)