This commit is contained in:
Aleksandar Fabijanic
2018-02-08 09:08:42 -06:00
committed by Alex Fabijanic
parent 4a7ab78686
commit bd81aec779
13 changed files with 610 additions and 106 deletions

View File

@@ -16,6 +16,7 @@
#include "Poco/MemoryStream.h"
#include "Poco/Stopwatch.h"
#include "Poco/Exception.h"
#include "Poco/JSONString.h"
#include <iostream>
#include <iomanip>
#include <cstdio>
@@ -57,6 +58,7 @@ using Poco::CILess;
using Poco::MemoryInputStream;
using Poco::Stopwatch;
using Poco::RangeException;
using Poco::toJSON;
StringTest::StringTest(const std::string& name): CppUnit::TestCase(name)
@@ -1078,6 +1080,68 @@ void StringTest::benchmarkFloatToStr()
}
void StringTest::testJSONString()
{
assert (toJSON("\\", false) == "\\\\");
assert (toJSON("\"", false) == "\\\"");
assert (toJSON("/", false) == "\\/");
assert (toJSON("\a", false) == "\\a");
assert (toJSON("\b", false) == "\\b");
assert (toJSON("\f", false) == "\\f");
assert (toJSON("\n", false) == "\\n");
assert (toJSON("\r", false) == "\\r");
assert (toJSON("\t", false) == "\\t");
assert (toJSON("\v", false) == "\\v");
assert (toJSON("a", false) == "a");
assert (toJSON("\xD0\x82", false) == "\xD0\x82");
assert (toJSON("\xD0\x82", false, true) == "\\u0402");
// ??? on MSVC, the assert macro expansion
// fails to compile when this string is inline ???
std::string str = "\"foo\\\\\"";
assert (toJSON("foo\\") == str);
assert (toJSON("bar/") == "\"bar\\/\"");
assert (toJSON("baz") == "\"baz\"");
assert (toJSON("q\"uote\"d") == "\"q\\\"uote\\\"d\"");
assert (toJSON("bs\b") == "\"bs\\b\"");
assert (toJSON("nl\n") == "\"nl\\n\"");
assert (toJSON("tb\t") == "\"tb\\t\"");
assert (toJSON("\xD0\x82", true) == "\"\xD0\x82\"");
assert (toJSON("\xD0\x82", true, true) == "\"\\u0402\"");
std::ostringstream ostr;
toJSON("foo\\", ostr);
assert(ostr.str() == str);
ostr.str("");
toJSON("foo\\", ostr);
assert(toJSON("bar/") == "\"bar\\/\"");
ostr.str("");
toJSON("baz", ostr);
assert(ostr.str() == "\"baz\"");
ostr.str("");
toJSON("q\"uote\"d", ostr);
assert(ostr.str() == "\"q\\\"uote\\\"d\"");
ostr.str("");
toJSON("bs\b", ostr);
assert(ostr.str() == "\"bs\\b\"");
ostr.str("");
toJSON("nl\n", ostr);
assert(ostr.str() == "\"nl\\n\"");
ostr.str("");
toJSON("tb\t", ostr);
assert(ostr.str() == "\"tb\\t\"");
ostr.str("");
toJSON("\xD0\x82", ostr);
assert(ostr.str() == "\"\xD0\x82\"");
ostr.str("");
toJSON("\xD0\x82", ostr, true, true);
assert(ostr.str() == "\"\\u0402\"");
ostr.str("");
}
void StringTest::setUp()
{
}
@@ -1118,6 +1182,7 @@ CppUnit::Test* StringTest::suite()
CppUnit_addTest(pSuite, StringTest, testIntToString);
CppUnit_addTest(pSuite, StringTest, testFloatToString);
//CppUnit_addTest(pSuite, StringTest, benchmarkFloatToStr);
CppUnit_addTest(pSuite, StringTest, testJSONString);
return pSuite;
}