Json.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include "pch.h"
  2. #include "CppUnitTest.h"
  3. #include <Json.h>
  4. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  5. namespace FrameworkTests
  6. {
  7. TEST_CLASS( JSONParserTests )
  8. {
  9. public:
  10. TEST_METHOD( NullTest )
  11. {
  12. Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "null" );
  13. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('null') should not return 0" );
  14. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::NULL_, L"Framework::JSON::Parser::getValue('null') should return a json null value" );
  15. value->release();
  16. }
  17. TEST_METHOD( BooleanTest )
  18. {
  19. Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "false" );
  20. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('false') should not return 0" );
  21. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::BOOLEAN, L"Framework::JSON::Parser::getValue('false') should return a boolean" );
  22. Assert::IsTrue( ((Framework::JSON::JSONBool*)value)->getBool() == false, L"Framework::JSON::Parser::getValue('false') should return a boolean with value false" );
  23. value->release();
  24. value = Framework::JSON::Parser::getValue( "true" );
  25. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('true') should not return 0" );
  26. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::BOOLEAN, L"Framework::JSON::Parser::getValue('true') should return a boolean" );
  27. Assert::IsTrue( ((Framework::JSON::JSONBool*)value)->getBool() == true, L"Framework::JSON::Parser::getValue('true') should return a boolean with value true" );
  28. value->release();
  29. }
  30. TEST_METHOD( StringTest )
  31. {
  32. Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "\"test\"" );
  33. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('\"test\"') should not return 0" );
  34. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::STRING, L"Framework::JSON::Parser::getValue('\"test\"') should return a string" );
  35. Assert::IsTrue( ((Framework::JSON::JSONString*)value)->getString().istGleich( "test" ), L"Framework::JSON::Parser::getValue('\"test\"') should return a string with value 'test'" );
  36. value->release();
  37. value = Framework::JSON::Parser::getValue( "\"\"" );
  38. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('\"\"') should not return 0" );
  39. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::STRING, L"Framework::JSON::Parser::getValue('\"\"') should return a string" );
  40. Assert::IsTrue( ((Framework::JSON::JSONString*)value)->getString().istGleich( "" ), L"Framework::JSON::Parser::getValue('\"\"') should return a string with value ''" );
  41. value->release();
  42. }
  43. TEST_METHOD( NumberTest )
  44. {
  45. Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "0" );
  46. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('0') should not return 0" );
  47. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::NUMBER, L"Framework::JSON::Parser::getValue('0') should return a number" );
  48. Assert::IsTrue( ((Framework::JSON::JSONNumber*)value)->getNumber() == 0.0, L"Framework::JSON::Parser::getValue('0') should return a number with value '0'" );
  49. value->release();
  50. value = Framework::JSON::Parser::getValue( "1.5" );
  51. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('1.5') should not return 0" );
  52. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::NUMBER, L"Framework::JSON::Parser::getValue('1.5') should return a number" );
  53. Assert::IsTrue( ((Framework::JSON::JSONNumber*)value)->getNumber() == 1.5, L"Framework::JSON::Parser::getValue('1.5') should return a number with value '1.5'" );
  54. value->release();
  55. value = Framework::JSON::Parser::getValue( "-1.5" );
  56. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('-1.5') should not return 0" );
  57. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::NUMBER, L"Framework::JSON::Parser::getValue('-1.5') should return a number" );
  58. Assert::IsTrue( ((Framework::JSON::JSONNumber*)value)->getNumber() == -1.5, L"Framework::JSON::Parser::getValue('-1.5') should return a number with value '-1.5'" );
  59. value->release();
  60. value = Framework::JSON::Parser::getValue( "-5.0" );
  61. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('-5.0') should not return 0" );
  62. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::NUMBER, L"Framework::JSON::Parser::getValue('-5.0') should return a number" );
  63. Assert::IsTrue( ((Framework::JSON::JSONNumber*)value)->getNumber() == -5.0, L"Framework::JSON::Parser::getValue('-5.0') should return a number with value '-5.0'" );
  64. value->release();
  65. }
  66. TEST_METHOD( ArrayTest )
  67. {
  68. Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "[]" );
  69. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('[]') should not return 0" );
  70. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::ARRAY, L"Framework::JSON::Parser::getValue('[]') should return an array" );
  71. Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->getLength() == 0, L"Framework::JSON::Parser::getValue('[]') should return an array with length 0" );
  72. value->release();
  73. value = Framework::JSON::Parser::getValue( " \t[ \r\n\tnull , \r\n\t 1,true , \"\" ] " );
  74. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should not return 0" );
  75. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::ARRAY, L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should return an array" );
  76. Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->getLength() == 4, L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') should return an array with length 4" );
  77. 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" );
  78. 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" );
  79. 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" );
  80. 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" );
  81. value->release();
  82. }
  83. TEST_METHOD( MultipleArrayTest )
  84. {
  85. Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "[[1],2,[[]]]" );
  86. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should not return 0" );
  87. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::ARRAY, L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should return an array" );
  88. Assert::IsTrue( ((Framework::JSON::JSONArray*)value)->getLength() == 3, L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should return an array with length 3" );
  89. 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" );
  90. 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" );
  91. 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" );
  92. value->release();
  93. }
  94. TEST_METHOD( ObjectTest )
  95. {
  96. Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "{\" \": []}" );
  97. Assert::IsTrue( value != 0, L"Framework::JSON::Parser::getValue('{\"\": []}') should not return 0" );
  98. Assert::IsTrue( value->getType() == Framework::JSON::JSONType::OBJECT, L"Framework::JSON::Parser::getValue('{\" \": []}') should return an object" );
  99. Assert::IsTrue( ((Framework::JSON::JSONObject*)value)->getFieldCount() == 1, L"Framework::JSON::Parser::getValue('{\" \": []}') should return an object with one attribute" );
  100. Assert::IsTrue( ((Framework::JSON::JSONObject*)value)->isValueOfType( " ", Framework::JSON::JSONType::ARRAY ), L"Framework::JSON::Parser::getValue('{\" \": []}') should contain an array at attribute ' '" );
  101. value->release();
  102. }
  103. TEST_METHOD( ToStringTest )
  104. {
  105. Framework::JSON::JSONValue* value = Framework::JSON::Parser::getValue( "{\" \": [1, true, false, 0.0, {}], \"t\": null}" );
  106. Framework::JSON::JSONValue* value2 = Framework::JSON::Parser::getValue( value->toString() );
  107. Assert::IsTrue( isEqual( value, value2 ), L"Framework::JSON::Parser::getValue(value.toString()) should return a json value eqal to value" );
  108. value->release();
  109. value2->release();
  110. }
  111. bool isEqual( Framework::JSON::JSONValue* a, Framework::JSON::JSONValue* b )
  112. {
  113. if( a->getType() != b->getType() ) return 0;
  114. switch( a->getType() )
  115. {
  116. case Framework::JSON::JSONType::NUMBER:
  117. return ((Framework::JSON::JSONNumber*)a)->getNumber() == ((Framework::JSON::JSONNumber*)b)->getNumber();
  118. case Framework::JSON::JSONType::BOOLEAN:
  119. return ((Framework::JSON::JSONBool*)a)->getBool() == ((Framework::JSON::JSONBool*)b)->getBool();
  120. case Framework::JSON::JSONType::STRING:
  121. return ((Framework::JSON::JSONString*)a)->getString().istGleich( ((Framework::JSON::JSONString*)b)->getString() );
  122. case Framework::JSON::JSONType::ARRAY:
  123. {
  124. Framework::JSON::JSONArray* arrayA = (Framework::JSON::JSONArray*)a;
  125. Framework::JSON::JSONArray* arrayB = (Framework::JSON::JSONArray*)b;
  126. if( arrayA->getLength() != arrayB->getLength() ) return 0;
  127. for( int i = 0; i < arrayA->getLength(); i++ )
  128. {
  129. Framework::JSON::JSONValue* entryA = arrayA->getValue( i );
  130. Framework::JSON::JSONValue* entryB = arrayB->getValue( i );
  131. bool eq = isEqual( entryA, entryB );
  132. entryA->release();
  133. entryB->release();
  134. if( !eq ) return 0;
  135. }
  136. return 1;
  137. }
  138. case Framework::JSON::JSONType::OBJECT:
  139. {
  140. Framework::JSON::JSONObject* objA = (Framework::JSON::JSONObject*)a;
  141. Framework::JSON::JSONObject* objB = (Framework::JSON::JSONObject*)b;
  142. if( objA->getFieldCount() != objB->getFieldCount() ) return 0;
  143. auto oaf = objA->getFields();
  144. while( oaf )
  145. {
  146. if( !objB->hasValue( oaf ) ) return 0;
  147. Framework::JSON::JSONValue* entryA = objA->getValue( oaf );
  148. Framework::JSON::JSONValue* entryB = objB->getValue( oaf );
  149. bool eq = isEqual( entryA, entryB );
  150. entryA->release();
  151. entryB->release();
  152. if( !eq ) return 0;
  153. oaf++;
  154. }
  155. return 1;
  156. }
  157. }
  158. return 1;
  159. }
  160. };
  161. }