Skip to content

Commit

Permalink
enh(JSON): Add unit test testBasicJson to test basic functionality of…
Browse files Browse the repository at this point in the history
… the JSON parser. #3331 (#4315)
  • Loading branch information
matejk authored Nov 30, 2023
1 parent 57bc0bb commit 4cfa96c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
38 changes: 38 additions & 0 deletions JSON/testsuite/src/JSONTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,43 @@ void JSONTest::testVarConvert()
assertTrue(cvt == "[1,2,3]");
}

void JSONTest::testBasicJson()
{
// Tests basic JSON structure and accessibility of members

const auto json = R"(
{
"clientConfig" : "Franky",
"arrayMember": [1, "A", 3.5],
"objectMember": {
"a": 1,
"b": "B"
}
}
)";
Poco::JSON::Parser jsonParser;
Poco::Dynamic::Var jsonObject = jsonParser.parse(json);

Poco::JSON::Object::Ptr jsonPtr = jsonObject.extract<Poco::JSON::Object::Ptr>();

assertFalse(jsonPtr->get("clientConfig").isEmpty());
const auto testStr = jsonPtr->getValue<std::string>("clientConfig");
assertEqual(testStr, "Franky");

const auto testArr = jsonPtr->getArray("arrayMember");
assertFalse(testArr.isNull());
assertFalse(testArr->empty());
assertEqual(testArr->size(), 3);
assertEqual(testArr->getElement<int>(0), 1);
assertEqual(testArr->getElement<std::string>(1), "A");

const auto testObj = jsonPtr->getObject("objectMember");
assertFalse(testObj.isNull());
assertEqual(testObj->size(), 2);
assertEqual(testObj->getValue<int>("a"), 1);
assertEqual(testObj->getValue<std::string>("b"), "B");

}

void JSONTest::testValidJanssonFiles()
{
Expand Down Expand Up @@ -2375,6 +2412,7 @@ CppUnit::Test* JSONTest::suite()
CppUnit_addTest(pSuite, JSONTest, testStringifyNaN);
CppUnit_addTest(pSuite, JSONTest, testStringifyPreserveOrder);
CppUnit_addTest(pSuite, JSONTest, testVarConvert);
CppUnit_addTest(pSuite, JSONTest, testBasicJson);
CppUnit_addTest(pSuite, JSONTest, testValidJanssonFiles);
CppUnit_addTest(pSuite, JSONTest, testInvalidJanssonFiles);
CppUnit_addTest(pSuite, JSONTest, testInvalidUnicodeJanssonFiles);
Expand Down
2 changes: 2 additions & 0 deletions JSON/testsuite/src/JSONTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class JSONTest: public CppUnit::TestCase
void testStringifyPreserveOrder();
void testVarConvert();

void testBasicJson();

void testValidJanssonFiles();
void testInvalidJanssonFiles();
void testTemplate();
Expand Down

0 comments on commit 4cfa96c

Please sign in to comment.