Skip to content
Snippets Groups Projects
Commit f7e9f2da authored by Christoph Lehmann's avatar Christoph Lehmann
Browse files

Added tests for ConfigTree

parent 896f553b
No related branches found
No related tags found
No related merge requests found
...@@ -353,8 +353,6 @@ public: ...@@ -353,8 +353,6 @@ public:
* The return value is suitable to be used with range-base for-loops. * The return value is suitable to be used with range-base for-loops.
* *
* \pre \c param must not have been read before from this ConfigTree. * \pre \c param must not have been read before from this ConfigTree.
*
* \todo write tests
*/ */
Range<ParameterIterator> Range<ParameterIterator>
getConfParamList(std::string const& param) const; getConfParamList(std::string const& param) const;
...@@ -382,8 +380,6 @@ public: ...@@ -382,8 +380,6 @@ public:
* \return the requested attribute * \return the requested attribute
* *
* \pre \c param must not have been read before from this ConfigTree. * \pre \c param must not have been read before from this ConfigTree.
*
* \todo write tests
*/ */
template<typename T> boost::optional<T> template<typename T> boost::optional<T>
getConfAttributeOptional(std::string const& attr) const; getConfAttributeOptional(std::string const& attr) const;
......
...@@ -129,7 +129,8 @@ TEST(BaseLibConfigTree, Get) ...@@ -129,7 +129,8 @@ TEST(BaseLibConfigTree, Get)
" <ignored2/>" " <ignored2/>"
"</sub>" "</sub>"
"<x>Y</x>" "<x>Y</x>"
"<z attr=\"0.5\">32.0</z>"; "<z attr=\"0.5\" optattr=\"false\">32.0</z>"
;
auto const ptree = readXml(xml); auto const ptree = readXml(xml);
Callbacks cbs; Callbacks cbs;
...@@ -220,12 +221,19 @@ TEST(BaseLibConfigTree, Get) ...@@ -220,12 +221,19 @@ TEST(BaseLibConfigTree, Get)
EXPECT_ERR_WARN(cbs, false, false); EXPECT_ERR_WARN(cbs, false, false);
EXPECT_EQ(0.5, z.getConfAttribute<double>("attr")); EXPECT_EQ(0.5, z.getConfAttribute<double>("attr"));
EXPECT_ERR_WARN(cbs, false, false); EXPECT_ERR_WARN(cbs, false, false);
RUN_SAFE(z.getConfAttribute<double>("attr")); // getting attr twice RUN_SAFE(z.getConfAttribute<double>("attr")); // getting attribute twice
EXPECT_ERR_WARN(cbs, true, false); EXPECT_ERR_WARN(cbs, true, false);
RUN_SAFE(z.getConfAttribute<double>("not_an_attr")); // nonexistent attribute RUN_SAFE(z.getConfAttribute<double>("not_an_attr")); // nonexistent attribute
EXPECT_ERR_WARN(cbs, true, false); EXPECT_ERR_WARN(cbs, true, false);
EXPECT_EQ(32.0, z.getValue<double>()); EXPECT_EQ(32.0, z.getValue<double>());
EXPECT_ERR_WARN(cbs, false, false); EXPECT_ERR_WARN(cbs, false, false);
auto const opt = z.getConfAttributeOptional<bool>("optattr");
EXPECT_TRUE(!!opt); EXPECT_FALSE(*opt);
EXPECT_ERR_WARN(cbs, false, false);
//RUN_SAFE(z.getConfAttributeOptional<bool>("optattr")); // getting attribute twice
//EXPECT_ERR_WARN(cbs, true, false);
EXPECT_FALSE(z.getConfAttributeOptional<bool>("also_not_an_attr")); // nonexisting attribute
EXPECT_ERR_WARN(cbs, false, false);
} }
EXPECT_ERR_WARN(cbs, false, false); EXPECT_ERR_WARN(cbs, false, false);
} // ConfigTree destroyed here } // ConfigTree destroyed here
...@@ -328,6 +336,13 @@ TEST(BaseLibConfigTree, GetSubtreeList) ...@@ -328,6 +336,13 @@ TEST(BaseLibConfigTree, GetSubtreeList)
{ {
auto const conf = makeConfigTree(ptree, cbs); auto const conf = makeConfigTree(ptree, cbs);
for (auto p : conf.getConfSubtreeList("nonexistent_list"))
{
(void) p;
FAIL() << "Expected empty list";
}
EXPECT_ERR_WARN(cbs, false, false);
int i = 0; int i = 0;
for (auto ct : conf.getConfSubtreeList("val")) for (auto ct : conf.getConfSubtreeList("val"))
{ {
...@@ -339,6 +354,58 @@ TEST(BaseLibConfigTree, GetSubtreeList) ...@@ -339,6 +354,58 @@ TEST(BaseLibConfigTree, GetSubtreeList)
EXPECT_ERR_WARN(cbs, false, false); EXPECT_ERR_WARN(cbs, false, false);
} }
TEST(BaseLibConfigTree, GetParamList)
{
const char xml[] =
"<int>0</int>"
"<int>1</int>"
"<int>2</int>"
"<int2 a=\"b\">3</int2>"
"<int3>4<error/></int3>";
auto const ptree = readXml(xml);
Callbacks cbs;
{
auto const conf = makeConfigTree(ptree, cbs);
for (auto p : conf.getConfParamList("nonexistent_list"))
{
(void) p;
FAIL() << "Expected empty list";
}
EXPECT_ERR_WARN(cbs, false, false);
int i = 0;
for (auto p : conf.getConfParamList("int"))
{
EXPECT_EQ(i, p.getValue<int>());
EXPECT_ERR_WARN(cbs, false, false);
++i;
}
for (auto p : conf.getConfParamList("int2"))
{
EXPECT_EQ(i, p.getValue<int>());
EXPECT_ERR_WARN(cbs, false, false);
++i;
}
EXPECT_ERR_WARN(cbs, false, true); // attribute "a" not read
{
auto range = conf.getConfParamList("int3");
EXPECT_ERR_WARN(cbs, false, false);
RUN_SAFE(*range.begin());
// Error because of child tag, raises exception, thus
// a temporary ConfigTree gets destroyed producing a warning.
EXPECT_ERR_WARN(cbs, true, true);
} // range destroyed here
EXPECT_ERR_WARN(cbs, false, false);
} // ConfigTree destroyed here
EXPECT_ERR_WARN(cbs, false, false);
}
TEST(BaseLibConfigTree, GetValueList) TEST(BaseLibConfigTree, GetValueList)
{ {
...@@ -352,6 +419,13 @@ TEST(BaseLibConfigTree, GetValueList) ...@@ -352,6 +419,13 @@ TEST(BaseLibConfigTree, GetValueList)
{ {
auto const conf = makeConfigTree(ptree, cbs); auto const conf = makeConfigTree(ptree, cbs);
for (auto p : conf.getConfParamList<int>("nonexistent_list"))
{
(void) p;
FAIL() << "Expected empty list";
}
EXPECT_ERR_WARN(cbs, false, false);
int n = 0; int n = 0;
for (auto i : conf.getConfParamList<int>("int")) for (auto i : conf.getConfParamList<int>("int"))
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment