Permalink
Browse files

Added a bunch of tests, probably need more.

  • Loading branch information...
1 parent 2a94744 commit 86379deaa7716dce600eb56330f02493b2f16d4f @pibara committed Aug 13, 2011
Showing with 160 additions and 56 deletions.
  1. +1 −0 .gitignore
  2. +27 −3 CMakeLists.txt
  3. +0 −1 README
  4. +0 −52 main.cpp
  5. +11 −0 tests/bogusfile.cpp
  6. +20 −0 tests/bogusnode.cpp
  7. +11 −0 tests/bogusstring.cpp
  8. +16 −0 tests/keys.cpp
  9. +16 −0 tests/nodecount.cpp
  10. +11 −0 tests/parsejsonfile.cpp
  11. +11 −0 tests/parsejsonstring.cpp
  12. +19 −0 tests/validnode.cpp
  13. +17 −0 tests/value.cpp
View
@@ -9,3 +9,4 @@ apitest
cmake_install.cmake
*.so
*.so.*
+build/test_*
View
@@ -13,6 +13,30 @@ SET_TARGET_PROPERTIES(jsonme PROPERTIES VERSION 1.0.0 SOVERSION 1)
target_link_libraries(jsonme gobject-2.0 json-glib-1.0)
install_targets(/lib jsonme)
install(FILES json-me.hpp DESTINATION include)
-add_executable(apitest main.cpp)
-target_link_libraries(apitest jsonme)
-add_test(tests.api apitest)
+add_executable(test_bogusstring tests/bogusstring.cpp)
+target_link_libraries(test_bogusstring jsonme)
+add_test(tests.bogusstring test_bogusstring)
+add_executable(test_bogusfile tests/bogusfile.cpp)
+target_link_libraries(test_bogusfile jsonme)
+add_test(tests.bogusfile test_bogusfile)
+add_executable(test_parsejsonstring tests/parsejsonstring.cpp)
+target_link_libraries(test_parsejsonstring jsonme)
+add_test(tests.parsejsonstring test_parsejsonstring)
+add_executable(test_parsejsonfile tests/parsejsonfile.cpp)
+target_link_libraries(test_parsejsonfile jsonme)
+add_test(tests.parsejsonfile test_parsejsonfile)
+add_executable(test_bogusnode tests/bogusnode.cpp)
+target_link_libraries(test_bogusnode jsonme)
+add_test(tests.bogusnode test_bogusnode)
+add_executable(test_validnode tests/validnode.cpp)
+target_link_libraries(test_validnode jsonme)
+add_test(tests.validnode test_validnode)
+add_executable(test_nodecount tests/nodecount.cpp)
+target_link_libraries(test_nodecount jsonme)
+add_test(tests.nodecount test_nodecount)
+add_executable(test_value tests/value.cpp)
+target_link_libraries(test_value jsonme)
+add_test(tests.value test_value)
+add_executable(test_keys tests/keys.cpp)
+target_link_libraries(test_keys jsonme)
+add_test(tests.keys test_keys)
View
1 README
@@ -8,7 +8,6 @@ it as a wishlist that if you like this library would make me very happy if you c
Before we release a 1.0 version:
-* Write some decent tests.
* Create an ubuntu .deb package.
After the 1.0 we may look at the following:
View
@@ -1,52 +0,0 @@
-#include "json-me.hpp"
-#include <iostream>
-int main(int argc,char **argv) {
- jsonme::JsonMeLib jsonlib;
- jsonme::Node rootnode=jsonlib.parseFile("example.json");
- std::cerr << "Root node acquired, starting." << std::endl;
- bool disabled=rootnode["disabled"];
- std::cerr << "disabled:" << disabled << std::endl;
- size_t numberofclients=rootnode["devices"]["clients"].size();
- std::cerr << "Client count: " << numberofclients << std::endl;
- for (size_t index=0; index < numberofclients; index++) {
- jsonme::Node client=rootnode["devices"]["clients"][index];
- std::string device=client["device"];
- std::string group= client["groupname"];
- std::string ip=client["ip"];
- std::string net=client["net"];
- std::cerr << "client net: " << device << std::endl << "\tgroup\t:\t" << group << std::endl <<"\tip\t:\t" << ip << std::endl << "\tnet\t:\t" << net << std::endl;
- }
- jsonme::Node router=rootnode["devices"]["routers"];
- if (router.keys().size() < 4) {
- std::cerr << "No keys returned from routers." << std::endl;;
- return 1;
- }
- for (size_t keyindex=0;keyindex < router.keys().size() ; keyindex++) {
- std::cerr << "keys[" << keyindex << "] : " << std::endl;
- jsonme::AbstractKeys &keys=router.keys();
- std::string key=keys[keyindex];
- std::cerr << " key=" << key << std::endl;
- std::string val=router[key];
- std::cerr << " val=" << val << std::endl;
- }
- std::string device=router["device"];
- std::string group=router["groupname"];
- std::string ip= router["ip"];
- std::string net=router["net"];
- std::cerr << "router net: " << device << std::endl << "\tgroup\t:\t" << group << std::endl << "\tip\t:\t" << ip << std::endl << "\tnet\t:\t" << net << std::endl;
- size_t numberofgateways=rootnode["gateways"].size();
- for (size_t index=0; index < numberofgateways; index++) {
- jsonme::Node gateway=rootnode["gateways"][index];
- std::string name=gateway["name"];
- std::string gip=gateway["ip"];
- long long table = gateway["tableno"];
- jsonme::Node tn=gateway["tableno"];
- std::cerr << "gateway: " << name << std::endl << "\tip\t:\t" << gip << std::endl << "\ttable\t:\t" << table << std::endl;
- size_t groupcount=gateway["allowedgroups"].size();
- for (size_t index2=0; index2 < groupcount; index2++) {
- std::string group2=gateway["allowedgroups"][index2];
- std::cerr << "\tgroup\t:\t" << group2 << std::endl;
- }
- }
- return 0;
-};
View
@@ -0,0 +1,11 @@
+#include "../json-me.hpp"
+#include <iostream>
+int main(int argc,char **argv) {
+ jsonme::JsonMeLib jsonlib;
+ try {
+ jsonme::Node rootnode=jsonlib.parseFile("Makefile");
+ } catch (jsonme::ParseError &e) {
+ return 0;
+ }
+ return 1;
+};
View
@@ -0,0 +1,20 @@
+#include "../json-me.hpp"
+#include <iostream>
+int main(int argc,char **argv) {
+ jsonme::JsonMeLib jsonlib;
+ try {
+ jsonme::Node rootnode=jsonlib.parseFile("example.json");
+ jsonme::Node bogusnode=rootnode["bogusnode"];
+ if (bogusnode.nodetype() != jsonme::INVALID) {
+ return 2;
+ }
+ bool asbool=bogusnode;
+ if (asbool) {
+ return 3;
+ }
+
+ } catch (jsonme::ParseError &e) {
+ return 1;
+ }
+ return 0;
+};
View
@@ -0,0 +1,11 @@
+#include "../json-me.hpp"
+#include <iostream>
+int main(int argc,char **argv) {
+ jsonme::JsonMeLib jsonlib;
+ try {
+ jsonme::Node rootnode=jsonlib.parse("This string isn't valid JSON");
+ } catch (jsonme::ParseError &e) {
+ return 0;
+ }
+ return 1;
+};
View
@@ -0,0 +1,16 @@
+#include "../json-me.hpp"
+#include <iostream>
+int main(int argc,char **argv) {
+ jsonme::JsonMeLib jsonlib;
+ try {
+ jsonme::Node rootnode=jsonlib.parseFile("example.json");
+ jsonme::Node router=rootnode["devices"]["routers"];
+ if (router.keys().size() != 4) {
+ std::cerr << "Bad number of keys (" << router.keys().size() << ") returned from routers." << std::endl;;
+ return 2;
+ }
+ return 0;
+ } catch (jsonme::ParseError &e) {
+ return 1;
+ }
+};
View
@@ -0,0 +1,16 @@
+#include "../json-me.hpp"
+#include <iostream>
+int main(int argc,char **argv) {
+ jsonme::JsonMeLib jsonlib;
+ try {
+ jsonme::Node rootnode=jsonlib.parseFile("example.json");
+ size_t numberofclients=rootnode["gateways"].size();
+ if (numberofclients == 2) {
+ return 0;
+ }
+ std::cerr << numberofclients << " found while 1 expected" << std::endl;
+ return 1;
+ } catch (jsonme::ParseError &e) {
+ return 1;
+ }
+};
View
@@ -0,0 +1,11 @@
+#include "../json-me.hpp"
+#include <iostream>
+int main(int argc,char **argv) {
+ jsonme::JsonMeLib jsonlib;
+ try {
+ jsonme::Node rootnode=jsonlib.parseFile("example.json");
+ } catch (jsonme::ParseError &e) {
+ return 1;
+ }
+ return 0;
+};
View
@@ -0,0 +1,11 @@
+#include "../json-me.hpp"
+#include <iostream>
+int main(int argc,char **argv) {
+ jsonme::JsonMeLib jsonlib;
+ try {
+ jsonme::Node rootnode=jsonlib.parse("{\n\t\"foo\": 1,\n\t\"bar\": \"Blue\"\n}");
+ } catch (jsonme::ParseError &e) {
+ return 1;
+ }
+ return 0;
+};
View
@@ -0,0 +1,19 @@
+#include "../json-me.hpp"
+#include <iostream>
+int main(int argc,char **argv) {
+ jsonme::JsonMeLib jsonlib;
+ try {
+ jsonme::Node rootnode=jsonlib.parseFile("example.json");
+ jsonme::Node validnode=rootnode["devices"];
+ if (validnode.nodetype() != jsonme::OBJECT) {
+ return 2;
+ }
+ bool asbool=validnode;
+ if (asbool) {
+ return 3;
+ }
+ } catch (jsonme::ParseError &e) {
+ return 1;
+ }
+ return 0;
+};
View
@@ -0,0 +1,17 @@
+#include "../json-me.hpp"
+#include <iostream>
+int main(int argc,char **argv) {
+ jsonme::JsonMeLib jsonlib;
+ try {
+ jsonme::Node rootnode=jsonlib.parseFile("example.json");
+ size_t index=0;
+ std::string val=rootnode["gateways"][index]["name"];
+ if (val == "gateway1"){
+ return 0;
+ }
+ std::cerr << "'" << val << " found while 'gateway1' expected" << std::endl;
+ return 1;
+ } catch (jsonme::ParseError &e) {
+ return 1;
+ }
+};

0 comments on commit 86379de

Please sign in to comment.