summaryrefslogtreecommitdiff
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
parent8d087b24cd40f609d030cfd0f2bebe8bb976086a (diff)
Add unittest for JSON and fix put/get overload for const char*
-rw-r--r--src/.gitignore1
-rw-r--r--src/Makefile.am9
-rw-r--r--src/json.cc50
-rw-r--r--src/json.hh12
-rw-r--r--test/.gitignore1
-rw-r--r--test/Makefile.am5
-rw-r--r--test/test-json.cc85
7 files changed, 159 insertions, 4 deletions
diff --git a/src/.gitignore b/src/.gitignore
index 146a3bd..13d1e3c 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -4,6 +4,7 @@
/stamp-h1
/libcgi.la
/libdb.la
+/libjson.la
/libutil.la
/libsender_client.la
/event
diff --git a/src/Makefile.am b/src/Makefile.am
index c85aca3..1f8b819 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -5,18 +5,21 @@ AM_CPPFLAGS = @DEFINES@ -DLOCALSTATEDIR='"@localstatedir@/stuff"' \
bin_PROGRAMS = event sender
noinst_PROGRAMS = test-sender
-noinst_LTLIBRARIES = libdb.la libcgi.la libutil.la libsender_client.la
+noinst_LTLIBRARIES = libdb.la libcgi.la libutil.la libsender_client.la \
+ libjson.la
event_SOURCES = event.cc event.hh event_main.cc common.hh cgi.hh db.hh
event_LDADD = libdb.la libcgi.la libsender_client.la
-sender_SOURCES = common.hh sender.cc json.hh json.cc
+sender_SOURCES = common.hh sender.cc
sender_CPPFLAGS = $(AM_CPPFLAGS) @CURL_CFLAGS@
-sender_LDADD = libutil.la @CURL_LIBS@
+sender_LDADD = libjson.la libutil.la @CURL_LIBS@
test_sender_SOURCES = common.hh test_sender.cc sender_client.hh
test_sender_LDADD = libsender_client.la
+libjson_la_SOURCES = json.hh json.cc common.hh
+
libcgi_la_SOURCES = cgi.hh common.hh cgi.cc \
query_parser.hh query_parser.cc \
header_parser.hh header_parser.cc \
diff --git a/src/json.cc b/src/json.cc
index 6e11c8f..d37cba0 100644
--- a/src/json.cc
+++ b/src/json.cc
@@ -108,6 +108,14 @@ public:
}
}
+ bool erase(const std::string& name) override {
+ return data_.erase(name);
+ }
+
+ void clear() override {
+ data_.clear();
+ }
+
bool contains(const std::string& name) const override {
return data_.find(name) != data_.end();
}
@@ -122,6 +130,14 @@ public:
return it->second->type == JsonType::STRING ?
static_cast<StringJsonValue*>(it->second.get())->str : fallback;
}
+ const char* get(const std::string& name,
+ const char* fallback) const override {
+ auto it = data_.find(name);
+ if (it == data_.end() || !it->second) return fallback;
+ return it->second->type == JsonType::STRING ?
+ static_cast<StringJsonValue*>(it->second.get())->str.c_str() :
+ fallback;
+ }
double get(const std::string& name, double fallback) const override {
auto it = data_.find(name);
if (it == data_.end() || !it->second) return fallback;
@@ -209,6 +225,10 @@ public:
}
}
+ void erase(size_t index) {
+ data_.erase(data_.begin() + index);
+ }
+
bool is_null(size_t index) const override {
return index < data_.size() && !data_[index];
}
@@ -218,6 +238,12 @@ public:
return data_[index]->type == JsonType::STRING ?
static_cast<StringJsonValue*>(data_[index].get())->str : fallback;
}
+ const char* get(size_t index, const char* fallback) const override {
+ if (index >= data_.size() || !data_[index]) return fallback;
+ return data_[index]->type == JsonType::STRING ?
+ static_cast<StringJsonValue*>(data_[index].get())->str.c_str() :
+ fallback;
+ }
double get(size_t index, double fallback) const override {
if (index >= data_.size() || !data_[index]) return fallback;
return data_[index]->type == JsonType::DOUBLE ?
@@ -352,10 +378,30 @@ std::shared_ptr<JsonObject> JsonObject::create() {
return std::make_shared<JsonObjectImpl>();
}
+void JsonObject::put(const std::string& name, const char* value) {
+ if (value) {
+ put(name, std::string(value));
+ } else {
+ put(name, nullptr);
+ }
+}
+
+void JsonArray::put(size_t index, const char* value) {
+ if (value) {
+ put(index, std::string(value));
+ } else {
+ put(index, nullptr);
+ }
+}
+
void JsonArray::add(const std::string& value) {
put(size(), value);
}
+void JsonArray::add(const char* value) {
+ put(size(), value);
+}
+
void JsonArray::add(double value) {
put(size(), value);
}
@@ -380,6 +426,10 @@ void JsonArray::add(std::shared_ptr<JsonArray> arr) {
put(size(), arr);
}
+void JsonArray::clear() {
+ resize(0);
+}
+
// static
std::shared_ptr<JsonArray> JsonArray::create() {
return std::make_shared<JsonArrayImpl>();
diff --git a/src/json.hh b/src/json.hh
index e14982e..5d5aa38 100644
--- a/src/json.hh
+++ b/src/json.hh
@@ -12,6 +12,7 @@ class JsonObject {
public:
virtual ~JsonObject() {}
virtual void put(const std::string& name, const std::string& value) = 0;
+ void put(const std::string& name, const char* value);
virtual void put(const std::string& name, double value) = 0;
virtual void put(const std::string& name, int64_t value) = 0;
virtual void put(const std::string& name, bool value) = 0;
@@ -21,11 +22,16 @@ public:
virtual void put(const std::string& name,
std::shared_ptr<JsonArray> arr) = 0;
+ virtual bool erase(const std::string& name) = 0;
+ virtual void clear() = 0;
+
virtual bool contains(const std::string& name) const = 0;
virtual bool is_null(const std::string& name) const = 0;
virtual const std::string& get(const std::string& name,
const std::string& fallback
= std::string()) const = 0;
+ virtual const char* get(const std::string& name,
+ const char* fallback) const = 0;
virtual double get(const std::string& name,
double fallback = 0.0) const = 0;
virtual int64_t get(const std::string& name,
@@ -54,6 +60,7 @@ public:
virtual void resize(size_t size) = 0;
virtual void put(size_t index, const std::string& value) = 0;
+ void put(size_t index, const char* value);
virtual void put(size_t index, double value) = 0;
virtual void put(size_t index, int64_t value) = 0;
virtual void put(size_t index, bool value) = 0;
@@ -63,7 +70,11 @@ public:
virtual void put(size_t index,
std::shared_ptr<JsonArray> arr) = 0;
+ virtual void erase(size_t index) = 0;
+ void clear();
+
void add(const std::string& value);
+ void add(const char* value);
void add(double value);
void add(int64_t value);
void add(bool value);
@@ -75,6 +86,7 @@ public:
virtual const std::string& get(size_t index,
const std::string& fallback
= std::string()) const = 0;
+ virtual const char* get(size_t index, const char* fallback) const = 0;
virtual double get(size_t index,
double fallback = 0.0) const = 0;
virtual int64_t get(size_t index,
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;
+}