|
@@ -0,0 +1,177 @@
|
|
|
|
+#include "pch.h"
|
|
|
|
+#include "CppUnitTest.h"
|
|
|
|
+#include <Json.h>
|
|
|
|
+
|
|
|
|
+using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
|
|
|
+
|
|
|
|
+namespace FrameworkTests
|
|
|
|
+{
|
|
|
|
+ TEST_CLASS( JSONParserTests )
|
|
|
|
+ {
|
|
|
|
+ public:
|
|
|
|
+ TEST_METHOD( NullTest )
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "null" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('null') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::NULL_, L"Framework::JSON::Parser::getValue('null') should return a json null value" );
|
|
|
|
+ value->release();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ TEST_METHOD( BooleanTest )
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "false" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('false') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::BOOLEAN, L"Framework::JSON::Parser::getValue('false') should return a boolean" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONBool*)value)->getBool() == false, L"Framework::JSON::Parser::getValue('false') should return a boolean with value false" );
|
|
|
|
+ value->release();
|
|
|
|
+
|
|
|
|
+ value = Framework::JSON::Parser::getValue( "true" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('true') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::BOOLEAN, L"Framework::JSON::Parser::getValue('true') should return a boolean" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONBool*)value)->getBool() == true, L"Framework::JSON::Parser::getValue('true') should return a boolean with value true" );
|
|
|
|
+ value->release();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ TEST_METHOD( StringTest )
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "\"test\"" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('\"test\"') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::STRING, L"Framework::JSON::Parser::getValue('\"test\"') should return a string" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONString*)value)->getString().istGleich( "test" ), L"Framework::JSON::Parser::getValue('\"test\"') should return a string with value 'test'" );
|
|
|
|
+ value->release();
|
|
|
|
+
|
|
|
|
+ value = Framework::JSON::Parser::getValue( "\"\"" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('\"\"') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::STRING, L"Framework::JSON::Parser::getValue('\"\"') should return a string" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONString*)value)->getString().istGleich( "" ), L"Framework::JSON::Parser::getValue('\"\"') should return a string with value ''" );
|
|
|
|
+ value->release();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ TEST_METHOD( NumberTest )
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "0" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('0') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::NUMBER, L"Framework::JSON::Parser::getValue('0') should return a number" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONNumber*)value)->getNumber() == 0.0, L"Framework::JSON::Parser::getValue('0') should return a number with value '0'" );
|
|
|
|
+ value->release();
|
|
|
|
+
|
|
|
|
+ value = Framework::JSON::Parser::getValue( "1.5" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('1.5') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::NUMBER, L"Framework::JSON::Parser::getValue('1.5') should return a number" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONNumber*)value)->getNumber() == 1.5, L"Framework::JSON::Parser::getValue('1.5') should return a number with value '1.5'" );
|
|
|
|
+ value->release();
|
|
|
|
+
|
|
|
|
+ value = Framework::JSON::Parser::getValue( "-1.5" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('-1.5') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::NUMBER, L"Framework::JSON::Parser::getValue('-1.5') should return a number" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONNumber*)value)->getNumber() == -1.5, L"Framework::JSON::Parser::getValue('-1.5') should return a number with value '-1.5'" );
|
|
|
|
+ value->release();
|
|
|
|
+
|
|
|
|
+ value = Framework::JSON::Parser::getValue( "-5.0" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('-5.0') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::NUMBER, L"Framework::JSON::Parser::getValue('-5.0') should return a number" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONNumber*)value)->getNumber() == -5.0, L"Framework::JSON::Parser::getValue('-5.0') should return a number with value '-5.0'" );
|
|
|
|
+ value->release();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ TEST_METHOD( ArrayTest )
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "[]" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('[]') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::ARRAY, L"Framework::JSON::Parser::getValue('[]') should return an array" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->getLength() == 0, L"Framework::JSON::Parser::getValue('[]') should return an array with length 0" );
|
|
|
|
+ value->release();
|
|
|
|
+
|
|
|
|
+ value = Framework::JSON::Parser::getValue( " \t[ \r\n\tnull , \r\n\t 1,true , \"\" ] " );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::ARRAY, L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should return an array" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->getLength() == 4, L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should return an array with length 4" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->isValueOfType( 0, Framework::JSON::JSONType::NULL_ ), L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should contain null at index 0" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->isValueOfType( 1, Framework::JSON::JSONType::NUMBER ), L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should contain a number at index 1" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->isValueOfType( 2, Framework::JSON::JSONType::BOOLEAN ), L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should contain a boolean at index 2" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->isValueOfType( 3, Framework::JSON::JSONType::STRING ), L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should contain a boolean at index 3" );
|
|
|
|
+ value->release();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ TEST_METHOD( MultipleArrayTest )
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "[[1],2,[[]]]" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::ARRAY, L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should return an array" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->getLength() == 3, L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should return an array with length 3" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->isValueOfType( 0, Framework::JSON::JSONType::ARRAY ), L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should contain an array at index 0" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->isValueOfType( 1, Framework::JSON::JSONType::NUMBER ), L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should contain a number at index 1" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->isValueOfType( 2, Framework::JSON::JSONType::ARRAY ), L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should contain an array at index 2" );
|
|
|
|
+ value->release();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ TEST_METHOD( ObjectTest )
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "{\" \": []}" );
|
|
|
|
+ Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('{\"\": []}') should not return 0" );
|
|
|
|
+ Assert::IsTrue( value->getType() == Framework::JSON::JSONType::OBJECT, L"Framework::JSON::Parser::getValue('{\" \": []}') should return an object" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONObject*)value)->getFieldCount() == 1, L"Framework::JSON::Parser::getValue('{\" \": []}') should return an object with one attribute" );
|
|
|
|
+ Assert::IsTrue( ((Framework::JSON::JSONObject*)value)->isValueOfType( " ", Framework::JSON::JSONType::ARRAY ), L"Framework::JSON::Parser::getValue('{\" \": []}') should contain an array at attribute ' '" );
|
|
|
|
+ value->release();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ TEST_METHOD( ToStringTest )
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "{\" \": [1, true, false, 0.0, {}], \"t\": null}" );
|
|
|
|
+ Framework::JSON::JSONValue* value2 = Framework::JSON::Parser::getValue( value->toString() );
|
|
|
|
+ Assert::IsTrue( isEqual( value, value2 ), L"Framework::JSON::Parser::getValue(value.toString()) should return a json value eqal to value" );
|
|
|
|
+ value->release();
|
|
|
|
+ value2->release();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ bool isEqual( Framework::JSON::JSONValue* a, Framework::JSON::JSONValue* b )
|
|
|
|
+ {
|
|
|
|
+ if( a->getType() != b->getType() ) return 0;
|
|
|
|
+ switch( a->getType() )
|
|
|
|
+ {
|
|
|
|
+ case Framework::JSON::JSONType::NUMBER:
|
|
|
|
+ return ((Framework::JSON::JSONNumber*)a)->getNumber() == ((Framework::JSON::JSONNumber*)b)->getNumber();
|
|
|
|
+ case Framework::JSON::JSONType::BOOLEAN:
|
|
|
|
+ return ((Framework::JSON::JSONBool*)a)->getBool() == ((Framework::JSON::JSONBool*)b)->getBool();
|
|
|
|
+ case Framework::JSON::JSONType::STRING:
|
|
|
|
+ return ((Framework::JSON::JSONString*)a)->getString().istGleich( ((Framework::JSON::JSONString*)b)->getString() );
|
|
|
|
+ case Framework::JSON::JSONType::ARRAY:
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONArray* arrayA = (Framework::JSON::JSONArray*)a;
|
|
|
|
+ Framework::JSON::JSONArray* arrayB = (Framework::JSON::JSONArray*)b;
|
|
|
|
+ if( arrayA->getLength() != arrayB->getLength() ) return 0;
|
|
|
|
+ for( int i = 0; i < arrayA->getLength(); i++ )
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONValue* entryA = arrayA->getValue( i );
|
|
|
|
+ Framework::JSON::JSONValue* entryB = arrayB->getValue( i );
|
|
|
|
+ bool eq = isEqual( entryA, entryB );
|
|
|
|
+ entryA->release();
|
|
|
|
+ entryB->release();
|
|
|
|
+ if( !eq ) return 0;
|
|
|
|
+ }
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+ case Framework::JSON::JSONType::OBJECT:
|
|
|
|
+ {
|
|
|
|
+ Framework::JSON::JSONObject* objA = (Framework::JSON::JSONObject*)a;
|
|
|
|
+ Framework::JSON::JSONObject* objB = (Framework::JSON::JSONObject*)b;
|
|
|
|
+ if( objA->getFieldCount() != objB->getFieldCount() ) return 0;
|
|
|
|
+ auto oaf = objA->getFields();
|
|
|
|
+ while( oaf )
|
|
|
|
+ {
|
|
|
|
+ if( !objB->hasValue( oaf ) ) return 0;
|
|
|
|
+ Framework::JSON::JSONValue* entryA = objA->getValue( oaf );
|
|
|
|
+ Framework::JSON::JSONValue* entryB = objB->getValue( oaf );
|
|
|
|
+ bool eq = isEqual( entryA, entryB );
|
|
|
|
+ entryA->release();
|
|
|
|
+ entryB->release();
|
|
|
|
+ if( !eq ) return 0;
|
|
|
|
+ oaf++;
|
|
|
|
+ }
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+}
|