Permalink
Browse files

Fixed the truthyness of objects and extended the trutyhess test

  • Loading branch information...
1 parent 86379de commit b86da084626f7fb92637e47355f262f0741d502a @pibara committed Aug 13, 2011
Showing with 52 additions and 4 deletions.
  1. +12 −2 Node.cpp
  2. +1 −1 build/example.json
  3. +39 −1 tests/validnode.cpp
View
@@ -52,8 +52,18 @@ namespace jsonme {
return s;
}
Node::operator bool() const {
- Scalar s(*mNode);
- return s;
+ switch (mNode->nodetype()) {
+ case INVALID:
+ return false;
+ case OBJECT:
+ return true;
+ case ARRAY:
+ return (mNode->size() != 0);
+ case SCALAR:
+ Scalar s(*mNode);
+ return s;
+ }
+ return false;
}
bool Node::isNull() const {
Scalar s(*mNode);
View
@@ -22,4 +22,4 @@
"tableno": 2
}
]
-}
+}
View
@@ -4,14 +4,52 @@ int main(int argc,char **argv) {
jsonme::JsonMeLib jsonlib;
try {
jsonme::Node rootnode=jsonlib.parseFile("example.json");
+ //An object is always true
jsonme::Node validnode=rootnode["devices"];
if (validnode.nodetype() != jsonme::OBJECT) {
+ std::cerr << "Expected devices node to be an object." << std::endl;
return 2;
}
bool asbool=validnode;
- if (asbool) {
+ if (!asbool) {
+ std::cerr << "Expected object node to be an TRUE" << std::endl;
return 3;
}
+ //An empty array is false
+ jsonme::Node emptyarray=rootnode["devices"]["clients"];
+ if (emptyarray.nodetype() != jsonme::ARRAY) {
+ std::cerr << "Expected devices::clients node to be an (empty) array." << std::endl;
+ if (emptyarray.nodetype() != jsonme::INVALID) {
+ std::cerr << "Its invalid." << std::endl;
+ } else if (emptyarray.nodetype() != jsonme::SCALAR) {
+ std::cerr << "Its a scalar." << std::endl;
+ } else if (emptyarray.nodetype() != jsonme::OBJECT) {
+ std::cerr << "Its an object" << std::endl;
+ }
+ return 4;
+ }
+ asbool = emptyarray;
+ if (asbool) {
+ std::cerr << "Expected empty array node to be an FALSE" << std::endl;
+ return 5;
+ }
+ //An non empty array is true.
+ jsonme::Node fullarray=rootnode["gateways"];
+ if (fullarray.nodetype() != jsonme::ARRAY) {
+ std::cerr << "Expected gateways node to be an array." << std::endl;
+ return 6;
+ }
+ asbool = fullarray;
+ if (!asbool) {
+ std::cerr << "Expected non empty array node to be a TRUE" << std::endl;
+ return 7;
+ }
+ //A string scalar is true
+ jsonme::Node scal=rootnode["devices"]["routers"]["device"];
+ if (!asbool) {
+ std::cerr << "Expected string scalar node to be a TRUE" << std::endl;
+ return 7;
+ }
} catch (jsonme::ParseError &e) {
return 1;
}

0 comments on commit b86da08

Please sign in to comment.