diff options
| author | Joel Klinghed <the_jk@yahoo.com> | 2015-06-09 21:13:30 +0200 |
|---|---|---|
| committer | Joel Klinghed <the_jk@yahoo.com> | 2015-06-09 21:13:30 +0200 |
| commit | 977c5975e4ead8e27becef8b78740fe5da631195 (patch) | |
| tree | 28049d37ab4dd26cb6588ea0145a26e5f1a4b53d /test | |
| parent | 8d087b24cd40f609d030cfd0f2bebe8bb976086a (diff) | |
Add unittest for JSON and fix put/get overload for const char*
Diffstat (limited to 'test')
| -rw-r--r-- | test/.gitignore | 1 | ||||
| -rw-r--r-- | test/Makefile.am | 5 | ||||
| -rw-r--r-- | test/test-json.cc | 85 |
3 files changed, 90 insertions, 1 deletions
diff --git a/test/.gitignore b/test/.gitignore index d859557..2099e3a 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -4,3 +4,4 @@ /test-multipart-formdata-parser /test-query-parser /test-args +/test-json diff --git a/test/Makefile.am b/test/Makefile.am index 5711aa4..9636062 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -3,7 +3,7 @@ MAINTAINERCLEANFILES = Makefile.in AM_CPPFLAGS = @DEFINES@ -I$(top_builddir)/src TESTS = test-query-parser test-header-parser test-multipart-formdata-parser \ - test-args + test-args test-json check_PROGRAMS = $(TESTS) @@ -18,3 +18,6 @@ test_multipart_formdata_parser_LDADD = $(top_srcdir)/src/libcgi.la test_args_SOURCES = test-args.cc test_args_LDADD = $(top_srcdir)/src/libcgi.la + +test_json_SOURCES = test-json.cc +test_json_LDADD = $(top_srcdir)/src/libjson.la 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; +} |
