Skip to content

Commit b69334b

Browse files
author
Tim Aitken
committed
add me some unit tests
1 parent de2778d commit b69334b

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/test_lib_json/main.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,6 +3556,87 @@ JSONTEST_FIXTURE_LOCAL(CharReaderAllowZeroesTest, issue176) {
35563556
}
35573557
}
35583558

3559+
struct CharReaderAllowHexadecimal : JsonTest::TestCase {};
3560+
3561+
JSONTEST_FIXTURE_LOCAL(CharReaderAllowHexadecimal, disallowHex) {
3562+
Json::CharReaderBuilder b;
3563+
CharReaderPtr reader(b.newCharReader());
3564+
Json::Value root;
3565+
Json::String errs;
3566+
{
3567+
char const doc[] = R"({"a": 0x01})";
3568+
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3569+
JSONTEST_ASSERT(!ok);
3570+
JSONTEST_ASSERT_STRING_EQUAL(
3571+
"* Line 1, Column 7\n"
3572+
" Syntax error: value, object or array expected.\n",
3573+
errs);
3574+
}
3575+
}
3576+
3577+
JSONTEST_FIXTURE_LOCAL(CharReaderAllowHexadecimal, hexObject) {
3578+
Json::CharReaderBuilder b;
3579+
b.settings_["allowHexadecimal"] = true;
3580+
CharReaderPtr reader(b.newCharReader());
3581+
{
3582+
Json::Value root;
3583+
Json::String errs;
3584+
char const doc[] = R"({
3585+
"a":0x9,
3586+
"b":0xf,
3587+
"c":0xF
3588+
})";
3589+
bool ok = reader->parse(doc, doc + std::strlen(doc), &root, &errs);
3590+
JSONTEST_ASSERT(ok);
3591+
JSONTEST_ASSERT_STRING_EQUAL("", errs);
3592+
JSONTEST_ASSERT_EQUAL(3u, root.size());
3593+
JSONTEST_ASSERT_EQUAL(0x9u, root.get("a", 0).asUInt());
3594+
JSONTEST_ASSERT_EQUAL(0xfu, root.get("b", 0).asUInt());
3595+
JSONTEST_ASSERT_EQUAL(0xFu, root.get("c", 0).asUInt());
3596+
}
3597+
}
3598+
3599+
JSONTEST_FIXTURE_LOCAL(CharReaderAllowHexadecimal, hexNumbers) {
3600+
Json::CharReaderBuilder b;
3601+
b.settings_["allowHexadecimal"] = true;
3602+
CharReaderPtr reader(b.newCharReader());
3603+
3604+
struct TestData {
3605+
bool ok;
3606+
Json::String in;
3607+
Json::LargestUInt out;
3608+
};
3609+
const TestData test_data[] = {
3610+
{true, "9", 9}, // regular number
3611+
{true, "0x00", 0x00}, // zero
3612+
{true, "0x0123456789", 0x0123456789}, // numeric hex
3613+
{true, "0xABCDEF", 0xABCDEF}, // uppercase-letter hex
3614+
{true, "0xabcdef", 0xabcdef}, // lowercase-letter hex
3615+
{false, "x", 0 }, // leading x
3616+
{false, "0xx", 0 }, // extra x
3617+
{false, "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 0} // too long
3618+
};
3619+
for (const auto& td : test_data) {
3620+
Json::Value root;
3621+
Json::String errs;
3622+
const char* c0 = td.in.c_str();
3623+
const char* c1 = c0 + td.in.size();
3624+
bool ok = reader->parse(c0, c1, &root, &errs);
3625+
JSONTEST_ASSERT_EQUAL(td.ok, ok);
3626+
if (td.ok)
3627+
{
3628+
JSONTEST_ASSERT_EQUAL(0u, errs.size());
3629+
JSONTEST_ASSERT(root.isConvertibleTo(Json::ValueType::uintValue));
3630+
if (root.isConvertibleTo(Json::ValueType::uintValue))
3631+
JSONTEST_ASSERT_EQUAL(root.asLargestUInt(), td.out);
3632+
}
3633+
else
3634+
{
3635+
JSONTEST_ASSERT(errs.size() > 0);
3636+
}
3637+
}
3638+
}
3639+
35593640
struct CharReaderAllowSpecialFloatsTest : JsonTest::TestCase {};
35603641

35613642
JSONTEST_FIXTURE_LOCAL(CharReaderAllowSpecialFloatsTest, specialFloat) {

0 commit comments

Comments
 (0)