summaryrefslogtreecommitdiff
path: root/test/test-json.cc
diff options
context:
space:
mode:
authorJoel Klinghed <the_jk@yahoo.com>2015-06-09 21:13:30 +0200
committerJoel Klinghed <the_jk@yahoo.com>2015-06-09 21:13:30 +0200
commit977c5975e4ead8e27becef8b78740fe5da631195 (patch)
tree28049d37ab4dd26cb6588ea0145a26e5f1a4b53d /test/test-json.cc
parent8d087b24cd40f609d030cfd0f2bebe8bb976086a (diff)
Add unittest for JSON and fix put/get overload for const char*
Diffstat (limited to 'test/test-json.cc')
-rw-r--r--test/test-json.cc85
1 files changed, 85 insertions, 0 deletions
diff --git a/test/test-json.cc b/test/test-json.cc
new file mode 100644
index 0000000..9344837
--- /dev/null
+++ b/test/test-json.cc
@@ -0,0 +1,85 @@
+#include "common.hh"
+
+#include <iostream>
+
+#include "json.hh"
+
+using namespace stuff;
+
+namespace {
+
+bool test_equal(const std::string& test, const std::string& value,
+ const std::string& expected) {
+ if (value == expected) return true;
+ std::cerr << test << ": got '" << value <<
+ "' expected '" << expected << "'" << std::endl;
+ return false;
+}
+
+bool test_equal(const std::string& test, const std::string& value,
+ const std::string& expected1,
+ const std::string& expected2) {
+ if (value == expected1 || value == expected2) return true;
+ std::cerr << test << ": got '" << value <<
+ "' expected '" << expected1 << "' or '" <<
+ expected2 << "'" << std::endl;
+ return false;
+}
+
+bool test_simple() {
+ auto obj = JsonObject::create();
+ if (!test_equal("simple", obj->str(), "{}"))
+ return false;
+ obj->put("foo", "bar");
+ obj->put("value", static_cast<int64_t>(12));
+ if (!test_equal("simple", obj->str(),
+ "{\"foo\":\"bar\",\"value\":12}",
+ "{\"value\":12,\"foo\":\"bar\"}"))
+ return false;
+ auto arr = JsonArray::create();
+ if (!test_equal("simple", arr->str(), "[]"))
+ return false;
+ arr->put(0, static_cast<int64_t>(42));
+ if (!test_equal("simple", arr->str(), "[42]"))
+ return false;
+ arr->put(1, static_cast<int64_t>(1234567));
+ obj->put("test", arr);
+ obj->erase("foo");
+ if (!test_equal("simple", obj->str(),
+ "{\"value\":12,\"test\":[42,1234567]}",
+ "{\"test\":[42,1234567],\"value\":12}"))
+ return false;
+ return true;
+}
+
+bool test_quote() {
+ auto obj = JsonObject::create();
+ obj->put("foo", "");
+ if (!test_equal("quote", obj->str(), "{\"foo\":\"\"}"))
+ return false;
+ obj->put("foo", "\"bar\"");
+ if (!test_equal("quote", obj->str(), "{\"foo\":\"\\\"bar\\\"\"}"))
+ return false;
+ obj->put("foo", " ");
+ if (!test_equal("quote", obj->str(), "{\"foo\":\" \"}"))
+ return false;
+ obj->put("foo", "\\\"");
+ if (!test_equal("quote", obj->str(), "{\"foo\":\"\\\\\\\"\"}"))
+ return false;
+ obj->put("foo", "\n");
+ if (!test_equal("quote", obj->str(), "{\"foo\":\"\\n\"}"))
+ return false;
+ return true;
+}
+
+} // namespace
+
+int main() {
+ unsigned int ok = 0, tot = 0;
+
+ tot++; if (test_simple()) ok++;
+ tot++; if (test_quote()) ok++;
+
+ std::cout << "OK " << ok << "/" << tot << std::endl;
+ return ok == tot ? EXIT_SUCCESS : EXIT_FAILURE;
+}