]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Improve testSerializeJsonString unit tests
authorsfan5 <sfan5@live.de>
Sun, 1 May 2022 11:43:29 +0000 (13:43 +0200)
committersfan5 <sfan5@live.de>
Sat, 21 May 2022 15:46:10 +0000 (17:46 +0200)
this also removes the requirement that / is escaped, there is
no reason for doing so.

src/unittest/test.h
src/unittest/test_serialization.cpp
src/util/serialize.cpp

index 1102f6d33b2b77c41d5984f6f27dc66a954cd69e..79ea0947101a3ca130c2eb7e8803de14e4cffc09 100644 (file)
@@ -80,7 +80,7 @@ class TestFailedException : public std::exception {
                        << #expected << std::endl                                     \
                        << "    at " << fs::GetFilenameFromPath(__FILE__) << ":"      \
                        << __LINE__ << std::endl                                      \
-                       << "    actual:   " << a << std::endl << "    expected: "     \
+                       << "    actual  : " << a << std::endl << "    expected: "     \
                        << e << std::endl;                                            \
                throw TestFailedException();                                      \
        }                                                                     \
index 660d77d02fe9eecf2d8e8597550bd9cec519cb08..ff6b57507be6c2ffd3b2e10913cc7a7161d8a141 100644 (file)
@@ -169,23 +169,50 @@ void TestSerialization::testDeSerializeLongString()
 
 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
-       UASSERT(serializeJsonString("") == "\"\"");
+       UASSERTEQ(std::string, serializeJsonString(""), "\"\"");
+       reset_is("\"\"");
+       UASSERTEQ(std::string, deSerializeJsonString(is), "");
+       assert_at_eof(is);
 
        // 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);
+
+       // Test optional serialization
+       const std::pair<const char*, const char*> test_pairs[] = {
+               { "abc", "abc" },
+               { "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);
+       }
 
-       // MSVC fails when directly using "\\\\"
-       std::string backslash = "\\";
-       UASSERT(serializeJsonString(teststring2) ==
-               mkstr("\"") +
+       // 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" +
                "\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f" +
                "\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017" +
                "\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f" +
-               " !\\\"" + teststring2.substr(0x23, 0x2f-0x23) +
-               "\\/" + teststring2.substr(0x30, 0x5c-0x30) +
-               backslash + backslash + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
+               " !\\\"" + teststring2.substr(0x23, 0x5c-0x23) +
+               bs + bs + teststring2.substr(0x5d, 0x7f-0x5d) + "\\u007f" +
                "\\u0080\\u0081\\u0082\\u0083\\u0084\\u0085\\u0086\\u0087" +
                "\\u0088\\u0089\\u008a\\u008b\\u008c\\u008d\\u008e\\u008f" +
                "\\u0090\\u0091\\u0092\\u0093\\u0094\\u0095\\u0096\\u0097" +
@@ -202,14 +229,31 @@ void TestSerialization::testSerializeJsonString()
                "\\u00e8\\u00e9\\u00ea\\u00eb\\u00ec\\u00ed\\u00ee\\u00ef" +
                "\\u00f0\\u00f1\\u00f2\\u00f3\\u00f4\\u00f5\\u00f6\\u00f7" +
                "\\u00f8\\u00f9\\u00fa\\u00fb\\u00fc\\u00fd\\u00fe\\u00ff" +
-               "\"");
-
-       // Test deserialize
-       std::istringstream is(serializeJsonString(teststring2), std::ios::binary);
-       UASSERT(deSerializeJsonString(is) == teststring2);
-       UASSERT(!is.eof());
-       is.get();
-       UASSERT(is.eof());
+               "\"";
+       std::string serialized = serializeJsonString(teststring2);
+       UASSERTEQ(std::string, serialized, expected);
+
+       reset_is(serialized);
+       UASSERTEQ(std::string, deSerializeJsonString(is), teststring2);
+       UASSERT(!is.eof()); // should have stopped at " so eof must not be set yet
+       assert_at_eof(is);
+
+       // 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");
 }
 
 
index 281061229a29670148531e1f4f8780a9f56b9efe..32e0a8936c1c85a256966c56b6403383327d6f81 100644 (file)
@@ -136,9 +136,6 @@ std::string serializeJsonString(const std::string &plain)
                        case '\\':
                                os << "\\\\";
                                break;
-                       case '/':
-                               os << "\\/";
-                               break;
                        case '\b':
                                os << "\\b";
                                break;