1 // 2 // JSONTest.h 3 // 4 // Definition of the JSONTest class. 5 // 6 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. 7 // and Contributors. 8 // 9 // SPDX-License-Identifier: BSL-1.0 10 // 11 12 13 #ifndef JSONTest_INCLUDED 14 #define JSONTest_INCLUDED 15 16 17 #include "Poco/JSON/JSON.h" 18 #include "CppUnit/TestCase.h" 19 #include "Poco/JSON/Object.h" 20 #include "Poco/JSON/Parser.h" 21 #include "Poco/JSON/Query.h" 22 #include "Poco/JSON/JSONException.h" 23 #include "Poco/JSON/Stringifier.h" 24 #include "Poco/JSON/ParseHandler.h" 25 #include "Poco/JSON/PrintHandler.h" 26 #include "Poco/JSON/Template.h" 27 #include <sstream> 28 29 30 class JSONTest: public CppUnit::TestCase 31 { 32 public: 33 JSONTest(const std::string& name); 34 ~JSONTest(); 35 36 void testNullProperty(); 37 void testTrueProperty(); 38 void testFalseProperty(); 39 void testNumberProperty(); 40 void testUnsignedNumberProperty(); 41 #if defined(POCO_HAVE_INT64) 42 void testNumber64Property(); 43 void testUnsignedNumber64Property(); 44 #endif 45 void testStringProperty(); 46 void testEmptyObject(); 47 void testEmptyPropertyName(); 48 void testComplexObject(); 49 void testDoubleProperty(); 50 void testDouble2Property(); 51 void testDouble3Property(); 52 void testObjectProperty(); 53 void testObjectArray(); 54 void testArrayOfObjects(); 55 void testEmptyArray(); 56 void testNestedArray(); 57 void testNullElement(); 58 void testTrueElement(); 59 void testFalseElement(); 60 void testNumberElement(); 61 void testStringElement(); 62 void testEmptyObjectElement(); 63 void testDoubleElement(); 64 void testSetArrayElement(); 65 void testOptValue(); 66 void testQuery(); 67 void testComment(); 68 void testPrintHandler(); 69 void testStringify(); 70 void testStringifyPreserveOrder(); 71 72 void testValidJanssonFiles(); 73 void testInvalidJanssonFiles(); 74 void testTemplate(); 75 void testUnicode(); 76 void testInvalidUnicodeJanssonFiles(); 77 void testSmallBuffer(); 78 void testEscape0(); 79 void testNonEscapeUnicode(); 80 void testEscapeUnicode(); 81 82 void testCopy(); 83 void testMove(); 84 85 void setUp(); 86 void tearDown(); 87 88 static CppUnit::Test* suite(); 89 90 private: 91 std::string getTestFilesPath(const std::string& type); 92 93 template <typename T> testNumber(T number)94 void testNumber(T number) 95 { 96 std::ostringstream os; 97 os << "{ \"test\" : " << number << " }"; 98 std::string json = os.str(); 99 Poco::JSON::Parser parser; 100 Poco::Dynamic::Var result; 101 102 try 103 { 104 result = parser.parse(json); 105 } 106 catch (Poco::JSON::JSONException& jsone) 107 { 108 std::cout << jsone.message() << std::endl; 109 assertTrue (false); 110 } 111 112 assertTrue (result.type() == typeid(Poco::JSON::Object::Ptr)); 113 114 Poco::JSON::Object::Ptr object = result.extract<Poco::JSON::Object::Ptr>(); 115 Poco::Dynamic::Var test = object->get("test"); 116 assertTrue (test.isNumeric()); 117 T value = test; 118 assertTrue (value == number); 119 120 Poco::DynamicStruct ds = *object; 121 assertTrue (!ds["test"].isEmpty()); 122 assertTrue (ds["test"].isNumeric()); 123 assertTrue (ds["test"] == number); 124 125 const Poco::DynamicStruct& rds = *object; 126 assertTrue (!rds["test"].isEmpty()); 127 assertTrue (rds["test"].isNumeric()); 128 assertTrue (rds["test"] == number); 129 } 130 }; 131 132 133 #endif // JSONTest_INCLUDED 134