Improve testSerializeJsonString unit tests

this also removes the requirement that / is escaped, there is
no reason for doing so.
This commit is contained in:
sfan5 2022-05-01 13:43:29 +02:00
parent 4e9e230e34
commit 70dc23f996
3 changed files with 61 additions and 20 deletions

@ -80,7 +80,7 @@ class TestFailedException : public std::exception {
<< #expected << std::endl \ << #expected << std::endl \
<< " at " << fs::GetFilenameFromPath(__FILE__) << ":" \ << " at " << fs::GetFilenameFromPath(__FILE__) << ":" \
<< __LINE__ << std::endl \ << __LINE__ << std::endl \
<< " actual: " << a << std::endl << " expected: " \ << " actual : " << a << std::endl << " expected: " \
<< e << std::endl; \ << e << std::endl; \
throw TestFailedException(); \ throw TestFailedException(); \
} \ } \

@ -169,23 +169,50 @@ void TestSerialization::testDeSerializeLongString()
void TestSerialization::testSerializeJsonString() void TestSerialization::testSerializeJsonString()
{ {
std::istringstream is(std::ios::binary);
const auto reset_is = [&] (const std::string &s) {
is.clear();
is.str(s);
};
const auto assert_at_eof = [] (std::istream &is) {
is.get();
UASSERT(is.eof());
};
// Test blank string // Test blank string
UASSERT(serializeJsonString("") == "\"\""); UASSERTEQ(std::string, serializeJsonString(""), "\"\"");
reset_is("\"\"");
UASSERTEQ(std::string, deSerializeJsonString(is), "");
assert_at_eof(is);
// Test basic string // Test basic string
UASSERT(serializeJsonString("Hello world!") == "\"Hello world!\""); UASSERTEQ(std::string, serializeJsonString("Hello world!"), "\"Hello world!\"");
reset_is("\"Hello world!\"");
UASSERTEQ(std::string, deSerializeJsonString(is), "Hello world!");
assert_at_eof(is);
// MSVC fails when directly using "\\\\" // Test optional serialization
std::string backslash = "\\"; const std::pair<const char*, const char*> test_pairs[] = {
UASSERT(serializeJsonString(teststring2) == { "abc", "abc" },
mkstr("\"") + { "x y z", "\"x y z\"" },
{ "\"", "\"\\\"\"" },
};
for (auto it : test_pairs) {
UASSERTEQ(std::string, serializeJsonStringIfNeeded(it.first), it.second);
reset_is(it.second);
UASSERTEQ(std::string, deSerializeJsonStringIfNeeded(is), it.first);
assert_at_eof(is);
}
// Test all byte values
const std::string bs = "\\"; // MSVC fails when directly using "\\\\"
const std::string expected = mkstr("\"") +
"\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" + "\\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007" +
"\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" + "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
"\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" + "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
"\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" + "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
" !\\\"" + teststring2.substr(0x23, 0x2f-0x23) + " !\\\"" + teststring2.substr(0x23, 0x5c-0x23) +
"\\/" + teststring2.substr(0x30, 0x5c-0x30) + bs + bs + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
"\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" + "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
"\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" + "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
"\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" + "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
@ -202,14 +229,31 @@ void TestSerialization::testSerializeJsonString()
"\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" + "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
"\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" + "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
"\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" + "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
"\""); "\"";
std::string serialized = serializeJsonString(teststring2);
UASSERTEQ(std::string, serialized, expected);
// Test deserialize reset_is(serialized);
std::istringstream is(serializeJsonString(teststring2), std::ios::binary); UASSERTEQ(std::string, deSerializeJsonString(is), teststring2);
UASSERT(deSerializeJsonString(is) == teststring2); UASSERT(!is.eof()); // should have stopped at " so eof must not be set yet
UASSERT(!is.eof()); assert_at_eof(is);
is.get();
UASSERT(is.eof()); // Test that deserialization leaves rest of stream alone
std::string tmp;
reset_is("\"foo\"bar");
UASSERTEQ(std::string, deSerializeJsonString(is), "foo");
std::getline(is, tmp, '\0');
UASSERTEQ(std::string, tmp, "bar");
reset_is("\"x y z\"bar");
UASSERTEQ(std::string, deSerializeJsonStringIfNeeded(is), "x y z");
std::getline(is, tmp, '\0');
UASSERTEQ(std::string, tmp, "bar");
reset_is("foo bar");
UASSERTEQ(std::string, deSerializeJsonStringIfNeeded(is), "foo");
std::getline(is, tmp, '\0');
UASSERTEQ(std::string, tmp, " bar");
} }

@ -136,9 +136,6 @@ std::string serializeJsonString(const std::string &plain)
case '\\': case '\\':
os << "\\\\"; os << "\\\\";
break; break;
case '/':
os << "\\/";
break;
case '\b': case '\b':
os << "\\b"; os << "\\b";
break; break;