Skip to content

Commit

Permalink
catch a BadCastException and return false from getValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Dec 23, 2020
1 parent 3a78377 commit fbd1061
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 20 deletions.
14 changes: 12 additions & 2 deletions modules/c++/xml.lite/include/xml/lite/Attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,18 @@ inline bool getValue_(const Attributes& attributes, const K& key, T& result)
{
return false;
}

result = str::toType<T>(value);
if (value.empty())
{
return false; // call Attributes::getValue() directly to get an empty string
}
try
{
result = str::toType<T>(value);
}
catch (const except::BadCastException&)
{
return false;
}
return true;
}

Expand Down
10 changes: 8 additions & 2 deletions modules/c++/xml.lite/include/xml/lite/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,14 @@ inline bool getValue(const Element& element, T& value)
{
return false; // call getCharacterData() to get an empty string
}

value = str::toType<T>(characterData);
try
{
value = str::toType<T>(characterData);
}
catch (const except::BadCastException&)
{
return false;
}
return true;
}

Expand Down
53 changes: 42 additions & 11 deletions modules/c++/xml.lite/unittests/test_xmlattribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static const std::string strXml = R"(
<root>
<doc name="doc">
<a a="a">TEXT</a>
<values int="314" double="3.14" string="314"/>
<values int="314" double="3.14" string="abc" empty=""/>
</doc>
</root>)";

Expand Down Expand Up @@ -125,7 +125,7 @@ TEST_CASE(test_getAttributeValue)
std::string value;
const auto result = getValue(attributes, "string", value);
TEST_ASSERT_TRUE(result);
TEST_ASSERT_EQ("314", value);
TEST_ASSERT_EQ("abc", value);
}

{
Expand All @@ -135,6 +135,33 @@ TEST_CASE(test_getAttributeValue)
}
}

TEST_CASE(test_getAttributeValueFailure)
{
test_MinidomParser xmlParser;
const auto root = xmlParser.getRootElement();

const auto& values = root->getElementByTagName("values", true /*recurse*/);
const auto& attributes = values.getAttributes();

{
int value;
const auto result = getValue(attributes, "string", value);
TEST_ASSERT_FALSE(result);
}
{
double value;
const auto result = getValue(attributes, "string", value);
TEST_ASSERT_FALSE(result);
}
{
std::string value;
const auto result = getValue(attributes, "empty", value);
TEST_ASSERT_FALSE(result);
value = attributes.getValue("empty");
TEST_ASSERT_TRUE(value.empty());
}
}

TEST_CASE(test_getAttributeValueByIndex)
{
test_MinidomParser xmlParser;
Expand All @@ -159,13 +186,15 @@ TEST_CASE(test_getAttributeValueByIndex)
std::string value;
const auto result = getValue(attributes, 2, value);
TEST_ASSERT_TRUE(result);
TEST_ASSERT_EQ("314", value);
TEST_ASSERT_EQ("abc", value);
}

{
std::string value;
const auto result = getValue(attributes, 999, value);
auto result = getValue(attributes, -1, value);
TEST_ASSERT_FALSE(result);
result = getValue(attributes, 999, value);
TEST_ASSERT_FALSE(result);

}
}

Expand Down Expand Up @@ -194,12 +223,12 @@ TEST_CASE(test_setAttributeValue)
TEST_ASSERT_EQ(1.23, value);
}
{
auto result = setValue(attributes, "string", "123");
auto result = setValue(attributes, "string", "xyz");
TEST_ASSERT_TRUE(result);
std::string value;
result = getValue(attributes, "string", value);
TEST_ASSERT_TRUE(result);
TEST_ASSERT_EQ("123", value);
TEST_ASSERT_EQ("xyz", value);
}

{
Expand Down Expand Up @@ -233,16 +262,17 @@ TEST_CASE(test_setAttributeValueByIndex)
TEST_ASSERT_EQ(1.23, value);
}
{
auto result = setValue(attributes, 2, "123");
auto result = setValue(attributes, 2, "xyz");
TEST_ASSERT_TRUE(result);
std::string value;
result = getValue(attributes, 2, value);
TEST_ASSERT_TRUE(result);
TEST_ASSERT_EQ("123", value);
TEST_ASSERT_EQ("xyz", value);
}

{
const auto result = setValue(attributes, 999, 999);
auto result = setValue(attributes, -1, -1);
TEST_ASSERT_FALSE(result);
result = setValue(attributes, 999, 999);
TEST_ASSERT_FALSE(result);
}
}
Expand All @@ -253,6 +283,7 @@ int main(int, char**)
TEST_CHECK(test_getAttributeByIndex);
TEST_CHECK(test_getAttributeNotFound);
TEST_CHECK(test_getAttributeValue);
TEST_CHECK(test_getAttributeValueFailure);
TEST_CHECK(test_getAttributeValueByIndex);
TEST_CHECK(test_setAttributeValue);
TEST_CHECK(test_setAttributeValueByIndex);
Expand Down
40 changes: 35 additions & 5 deletions modules/c++/xml.lite/unittests/test_xmlelement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ static const std::string strXml1_ = R"(
<doc name="doc">
<a a="a">)";
static const std::string strXml2_ = R"(</a><b/><b/>
<values int="314" double="3.14" string="314"/>
<values int="314" double="3.14" string="abc"/>
<int>314</int>
<double>3.14</double>
<string>314</string>
<string>abc</string>
<empty></empty>
</doc>
</root>)";
static const auto strXml = strXml1_ + text + strXml2_;
Expand Down Expand Up @@ -196,7 +197,35 @@ TEST_CASE(test_getValue)
std::string value;
const auto result = getValue(e, value);
TEST_ASSERT_TRUE(result);
TEST_ASSERT_EQ("314", value);
TEST_ASSERT_EQ("abc", value);
}
{
const auto& e = root->getElementByTagName("empty", true /*recurse*/);
std::string value;
auto result = getValue(e, value);
TEST_ASSERT_FALSE(result);
value = e.getCharacterData();
TEST_ASSERT_TRUE(value.empty());
}
}


TEST_CASE(test_getValueFailure)
{
test_MinidomParser xmlParser;
const auto root = xmlParser.getRootElement();

{
const auto& e = root->getElementByTagName("string", true /*recurse*/);
int value;
const auto result = getValue(e, value);
TEST_ASSERT_FALSE(result);
}
{
const auto& e = root->getElementByTagName("string", true /*recurse*/);
double value;
const auto result = getValue(e, value);
TEST_ASSERT_FALSE(result);
}
}

Expand All @@ -223,11 +252,11 @@ TEST_CASE(test_setValue)
}
{
auto& e = root->getElementByTagName("string", true /*recurse*/);
setValue(e, "123");
setValue(e, "xyz");
std::string value;
const auto result = getValue(e, value);
TEST_ASSERT_TRUE(result);
TEST_ASSERT_EQ("123", value);
TEST_ASSERT_EQ("xyz", value);
}
}

Expand All @@ -241,5 +270,6 @@ int main(int, char**)
TEST_CHECK(test_getElementByTagName_b);

TEST_CHECK(test_getValue);
TEST_CHECK(test_getValueFailure);
TEST_CHECK(test_setValue);
}

0 comments on commit fbd1061

Please sign in to comment.