From 39601c183a79686b7940dd7927e416268d2e8770 Mon Sep 17 00:00:00 2001 From: Sean Middleditch Date: Tue, 12 Jan 2010 01:10:20 -0800 Subject: [PATCH] add tests --- Makefile | 13 ++++++++ test.cc | 66 +++++++++++++++++++++++++++++++++++++++++ test.json | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 Makefile create mode 100644 test.cc create mode 100644 test.json diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..21f68e6 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +JANSSON_CFLAGS := $(shell pkg-config --cflags jansson) +JANSSON_LIBS := $(shell pkg-config --libs jansson) + +all: test + +test-bin: test.cc janssonxx.h Makefile + $(CXX) -o $@ -g -O0 -Wall $(JANSSON_CFLAGS) $< $(JANSSON_LIBS) + +test: test-bin + ./test-bin + +clean: + rm -f test-bin diff --git a/test.cc b/test.cc new file mode 100644 index 0000000..7b44bb0 --- /dev/null +++ b/test.cc @@ -0,0 +1,66 @@ +#include +#include + +#include "janssonxx.h" + +using namespace std; + +#define ASSERT_OP(lhs, rhs, op, m) \ + do { \ + if(!((lhs) op (rhs))) { \ + std::cerr << std::boolalpha; \ + std::cerr << __FILE__ << '[' << __LINE__ << "]: ERROR: " << (m) << std::endl; \ + std::cerr << "\ttest: " << #lhs << ' ' << #op << ' ' << #rhs << std::endl; \ + std::cerr << "\tresult: " << (lhs) << ' ' << #op << ' ' << (rhs) << std::endl; \ + return 1; \ + } \ + } while(0) +#define ASSERT_EQ(lhs, rhs, m) ASSERT_OP(lhs, rhs, ==, m) +#define ASSERT_NE(lhs, rhs, m) ASSERT_OP(lhs, rhs, !=, m) +#define ASSERT_TRUE(p, m) ASSERT_OP(p, true, ==, m) +#define ASSERT_FALSE(p, m) ASSERT_OP(p, true, !=, m) + +int main() { + jansson::Value e1(jansson::Value::load_file("test.json")); + jansson::Value e2(e1); + jansson::Value e3; + jansson::Value e4(jansson::Value::load_string("{\"foo\": true, \"bar\": \"test\"}")); + + ASSERT_TRUE(e1.is_object(), "e1 is not an object"); + ASSERT_TRUE(e2.is_object(), "e2 is not an object"); + ASSERT_TRUE(e3.is_undefined(), "e3 has a defined value"); + ASSERT_TRUE(e4.is_object(), "e4 is not an object"); + + ASSERT_EQ(e1.size(), 1, "e1 has too many properties"); + ASSERT_EQ(e2.size(), 1, "e2 has too many properties"); + ASSERT_EQ(e4.size(), 2, "e4 does not have 2 elements"); + + ASSERT_TRUE(e1.get("web-app").is_object(), "e1[0].web-app is not an object"); + ASSERT_EQ(e1.get("web-app").get("servlet").at(0).get("servlet-class").as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value"); + ASSERT_EQ(e1["web-app"]["servlet"][0]["servlet-class"].as_string(), "org.cofax.cds.CDSServlet", "property has incorrect value"); + + ASSERT_EQ(e4["foo"].as_boolean(), true, "property has incorrect value"); + + jansson::Iterator i(e1.get("web-app")); + ASSERT_EQ(i.key(), "taglib", "first iterator result has incorrect key"); + i.next(); + ASSERT_EQ(i.key(), "servlet", "first iterator result has incorrect key"); + i.next(); + ASSERT_EQ(i.key(), "servlet-mapping", "first iterator result has incorrect key"); + i.next(); + ASSERT_FALSE(i.valid(), "iterator has more values than expected"); + + e3 = 12.34; + ASSERT_TRUE(e3.is_number(), "e3 is not a number after assignment"); + ASSERT_EQ(e3.as_real(), 12.34, "e3 has incorrect value after assignment"); + + e3 = true; + ASSERT_TRUE(e3.is_boolean(), "e3 is not a boolean after assignment"); + ASSERT_EQ(e3.as_boolean(), true, "e3 has incorrect value after assignment"); + + e3 = "foobar"; + ASSERT_TRUE(e3.is_string(), "e3 is not a string after assignment"); + ASSERT_EQ(e3.as_string(), "foobar", "e3 has incorrect value after assignment"); + + return 0; +} diff --git a/test.json b/test.json new file mode 100644 index 0000000..9eea6ad --- /dev/null +++ b/test.json @@ -0,0 +1,88 @@ +{"web-app": { + "servlet": [ + { + "servlet-name": "cofaxCDS", + "servlet-class": "org.cofax.cds.CDSServlet", + "init-param": { + "configGlossary:installationAt": "Philadelphia, PA", + "configGlossary:adminEmail": "ksm@pobox.com", + "configGlossary:poweredBy": "Cofax", + "configGlossary:poweredByIcon": "/images/cofax.gif", + "configGlossary:staticPath": "/content/static", + "templateProcessorClass": "org.cofax.WysiwygTemplate", + "templateLoaderClass": "org.cofax.FilesTemplateLoader", + "templatePath": "templates", + "templateOverridePath": "", + "defaultListTemplate": "listTemplate.htm", + "defaultFileTemplate": "articleTemplate.htm", + "useJSP": false, + "jspListTemplate": "listTemplate.jsp", + "jspFileTemplate": "articleTemplate.jsp", + "cachePackageTagsTrack": 200, + "cachePackageTagsStore": 200, + "cachePackageTagsRefresh": 60, + "cacheTemplatesTrack": 100, + "cacheTemplatesStore": 50, + "cacheTemplatesRefresh": 15, + "cachePagesTrack": 200, + "cachePagesStore": 100, + "cachePagesRefresh": 10, + "cachePagesDirtyRead": 10, + "searchEngineListTemplate": "forSearchEnginesList.htm", + "searchEngineFileTemplate": "forSearchEngines.htm", + "searchEngineRobotsDb": "WEB-INF/robots.db", + "useDataStore": true, + "dataStoreClass": "org.cofax.SqlDataStore", + "redirectionClass": "org.cofax.SqlRedirection", + "dataStoreName": "cofax", + "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver", + "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon", + "dataStoreUser": "sa", + "dataStorePassword": "dataStoreTestQuery", + "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';", + "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log", + "dataStoreInitConns": 10, + "dataStoreMaxConns": 100, + "dataStoreConnUsageLimit": 100, + "dataStoreLogLevel": "debug", + "maxUrlLength": 500}}, + { + "servlet-name": "cofaxEmail", + "servlet-class": "org.cofax.cds.EmailServlet", + "init-param": { + "mailHost": "mail1", + "mailHostOverride": "mail2"}}, + { + "servlet-name": "cofaxAdmin", + "servlet-class": "org.cofax.cds.AdminServlet"}, + + { + "servlet-name": "fileServlet", + "servlet-class": "org.cofax.cds.FileServlet"}, + { + "servlet-name": "cofaxTools", + "servlet-class": "org.cofax.cms.CofaxToolsServlet", + "init-param": { + "templatePath": "toolstemplates/", + "log": 1, + "logLocation": "/usr/local/tomcat/logs/CofaxTools.log", + "logMaxSize": "", + "dataLog": 1, + "dataLogLocation": "/usr/local/tomcat/logs/dataLog.log", + "dataLogMaxSize": "", + "removePageCache": "/content/admin/remove?cache=pages&id=", + "removeTemplateCache": "/content/admin/remove?cache=templates&id=", + "fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder", + "lookInContext": 1, + "adminGroupID": 4, + "betaServer": true}}], + "servlet-mapping": { + "cofaxCDS": "/", + "cofaxEmail": "/cofaxutil/aemail/*", + "cofaxAdmin": "/admin/*", + "fileServlet": "/static/*", + "cofaxTools": "/tools/*"}, + + "taglib": { + "taglib-uri": "cofax.tld", + "taglib-location": "/WEB-INF/tlds/cofax.tld"}}}