Json.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. #include "pch.h"
  2. #define NO_MAIN
  3. #include <Json.h>
  4. #include <main.h>
  5. #include "CppUnitTest.h"
  6. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  7. namespace FrameworkTests
  8. {
  9. TEST_CLASS (JSONParserTests)
  10. {
  11. public:
  12. TEST_METHOD (NullTest)
  13. {
  14. Framework::JSON::JSONValue* value
  15. = Framework::JSON::Parser::getValue("null");
  16. Assert::IsTrue(value != 0,
  17. L"Framework::JSON::Parser::getValue('null') should not return "
  18. L"0");
  19. Assert::IsTrue(value->getType() == Framework::JSON::JSONType::NULL_,
  20. L"Framework::JSON::Parser::getValue('null') should return a "
  21. L"json null value");
  22. value->release();
  23. }
  24. TEST_METHOD (BooleanTest)
  25. {
  26. Framework::JSON::JSONValue* value
  27. = Framework::JSON::Parser::getValue("false");
  28. Assert::IsTrue(value != 0,
  29. L"Framework::JSON::Parser::getValue('false') should not return "
  30. L"0");
  31. Assert::IsTrue(
  32. value->getType() == Framework::JSON::JSONType::BOOLEAN,
  33. L"Framework::JSON::Parser::getValue('false') should return a "
  34. L"boolean");
  35. Assert::IsTrue(
  36. ((Framework::JSON::JSONBool*)value)->getBool() == false,
  37. L"Framework::JSON::Parser::getValue('false') should return a "
  38. L"boolean with value false");
  39. value->release();
  40. value = Framework::JSON::Parser::getValue("true");
  41. Assert::IsTrue(value != 0,
  42. L"Framework::JSON::Parser::getValue('true') should not return "
  43. L"0");
  44. Assert::IsTrue(
  45. value->getType() == Framework::JSON::JSONType::BOOLEAN,
  46. L"Framework::JSON::Parser::getValue('true') should return a "
  47. L"boolean");
  48. Assert::IsTrue(
  49. ((Framework::JSON::JSONBool*)value)->getBool() == true,
  50. L"Framework::JSON::Parser::getValue('true') should return a "
  51. L"boolean with value true");
  52. value->release();
  53. }
  54. TEST_METHOD (StringTest)
  55. {
  56. Framework::JSON::JSONValue* value
  57. = Framework::JSON::Parser::getValue("\"test\"");
  58. Assert::IsTrue(value != 0,
  59. L"Framework::JSON::Parser::getValue('\"test\"') should not "
  60. L"return 0");
  61. Assert::IsTrue(
  62. value->getType() == Framework::JSON::JSONType::STRING,
  63. L"Framework::JSON::Parser::getValue('\"test\"') should return "
  64. L"a string");
  65. Assert::IsTrue(((Framework::JSON::JSONString*)value)
  66. ->getString()
  67. .istGleich("test"),
  68. L"Framework::JSON::Parser::getValue('\"test\"') should return "
  69. L"a string with value 'test'");
  70. value->release();
  71. value = Framework::JSON::Parser::getValue("\"\"");
  72. Assert::IsTrue(value != 0,
  73. L"Framework::JSON::Parser::getValue('\"\"') should not return "
  74. L"0");
  75. Assert::IsTrue(
  76. value->getType() == Framework::JSON::JSONType::STRING,
  77. L"Framework::JSON::Parser::getValue('\"\"') should return a "
  78. L"string");
  79. Assert::IsTrue(((Framework::JSON::JSONString*)value)
  80. ->getString()
  81. .istGleich(""),
  82. L"Framework::JSON::Parser::getValue('\"\"') should return a "
  83. L"string with value ''");
  84. value->release();
  85. }
  86. TEST_METHOD (NumberTest)
  87. {
  88. Framework::JSON::JSONValue* value
  89. = Framework::JSON::Parser::getValue("0");
  90. Assert::IsTrue(value != 0,
  91. L"Framework::JSON::Parser::getValue('0') should not return 0");
  92. Assert::IsTrue(
  93. value->getType() == Framework::JSON::JSONType::NUMBER,
  94. L"Framework::JSON::Parser::getValue('0') should return a "
  95. L"number");
  96. Assert::IsTrue(
  97. ((Framework::JSON::JSONNumber*)value)->getNumber() == 0.0,
  98. L"Framework::JSON::Parser::getValue('0') should return a "
  99. L"number with value '0'");
  100. value->release();
  101. value = Framework::JSON::Parser::getValue("1.5");
  102. Assert::IsTrue(value != 0,
  103. L"Framework::JSON::Parser::getValue('1.5') should not return "
  104. L"0");
  105. Assert::IsTrue(
  106. value->getType() == Framework::JSON::JSONType::NUMBER,
  107. L"Framework::JSON::Parser::getValue('1.5') should return a "
  108. L"number");
  109. Assert::IsTrue(
  110. ((Framework::JSON::JSONNumber*)value)->getNumber() == 1.5,
  111. L"Framework::JSON::Parser::getValue('1.5') should return a "
  112. L"number with value '1.5'");
  113. value->release();
  114. value = Framework::JSON::Parser::getValue("-1.5");
  115. Assert::IsTrue(value != 0,
  116. L"Framework::JSON::Parser::getValue('-1.5') should not return "
  117. L"0");
  118. Assert::IsTrue(
  119. value->getType() == Framework::JSON::JSONType::NUMBER,
  120. L"Framework::JSON::Parser::getValue('-1.5') should return a "
  121. L"number");
  122. Assert::IsTrue(
  123. ((Framework::JSON::JSONNumber*)value)->getNumber() == -1.5,
  124. L"Framework::JSON::Parser::getValue('-1.5') should return a "
  125. L"number with value '-1.5'");
  126. value->release();
  127. value = Framework::JSON::Parser::getValue("-5.0");
  128. Assert::IsTrue(value != 0,
  129. L"Framework::JSON::Parser::getValue('-5.0') should not return "
  130. L"0");
  131. Assert::IsTrue(
  132. value->getType() == Framework::JSON::JSONType::NUMBER,
  133. L"Framework::JSON::Parser::getValue('-5.0') should return a "
  134. L"number");
  135. Assert::IsTrue(
  136. ((Framework::JSON::JSONNumber*)value)->getNumber() == -5.0,
  137. L"Framework::JSON::Parser::getValue('-5.0') should return a "
  138. L"number with value '-5.0'");
  139. value->release();
  140. }
  141. TEST_METHOD (ArrayTest)
  142. {
  143. Framework::JSON::JSONValue* value
  144. = Framework::JSON::Parser::getValue("[]");
  145. Assert::IsTrue(value != 0,
  146. L"Framework::JSON::Parser::getValue('[]') should not return 0");
  147. Assert::IsTrue(value->getType() == Framework::JSON::JSONType::ARRAY,
  148. L"Framework::JSON::Parser::getValue('[]') should return an "
  149. L"array");
  150. Assert::IsTrue(
  151. ((Framework::JSON::JSONArray*)value)->getLength() == 0,
  152. L"Framework::JSON::Parser::getValue('[]') should return an "
  153. L"array with length 0");
  154. value->release();
  155. value = Framework::JSON::Parser::getValue(
  156. " \t[ \r\n\tnull , \r\n\t 1,true , \"\" ] ");
  157. Assert::IsTrue(value != 0,
  158. L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
  159. L"should not return 0");
  160. Assert::IsTrue(value->getType() == Framework::JSON::JSONType::ARRAY,
  161. L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
  162. L"should return an array");
  163. Assert::IsTrue(
  164. ((Framework::JSON::JSONArray*)value)->getLength() == 4,
  165. L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
  166. L"should return an array with length 4");
  167. Assert::IsTrue(
  168. ((Framework::JSON::JSONArray*)value)
  169. ->isValueOfType(0, Framework::JSON::JSONType::NULL_),
  170. L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
  171. L"should contain null at index 0");
  172. Assert::IsTrue(
  173. ((Framework::JSON::JSONArray*)value)
  174. ->isValueOfType(1, Framework::JSON::JSONType::NUMBER),
  175. L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
  176. L"should contain a number at index 1");
  177. Assert::IsTrue(
  178. ((Framework::JSON::JSONArray*)value)
  179. ->isValueOfType(2, Framework::JSON::JSONType::BOOLEAN),
  180. L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
  181. L"should contain a boolean at index 2");
  182. Assert::IsTrue(
  183. ((Framework::JSON::JSONArray*)value)
  184. ->isValueOfType(3, Framework::JSON::JSONType::STRING),
  185. L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
  186. L"should contain a boolean at index 3");
  187. value->release();
  188. }
  189. TEST_METHOD (MultipleArrayTest)
  190. {
  191. Framework::JSON::JSONValue* value
  192. = Framework::JSON::Parser::getValue("[[1],2,[[]]]");
  193. Assert::IsTrue(value != 0,
  194. L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should not "
  195. L"return 0");
  196. Assert::IsTrue(value->getType() == Framework::JSON::JSONType::ARRAY,
  197. L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should "
  198. L"return an array");
  199. Assert::IsTrue(
  200. ((Framework::JSON::JSONArray*)value)->getLength() == 3,
  201. L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should "
  202. L"return an array with length 3");
  203. Assert::IsTrue(
  204. ((Framework::JSON::JSONArray*)value)
  205. ->isValueOfType(0, Framework::JSON::JSONType::ARRAY),
  206. L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
  207. L"should contain an array at index 0");
  208. Assert::IsTrue(
  209. ((Framework::JSON::JSONArray*)value)
  210. ->isValueOfType(1, Framework::JSON::JSONType::NUMBER),
  211. L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
  212. L"should contain a number at index 1");
  213. Assert::IsTrue(
  214. ((Framework::JSON::JSONArray*)value)
  215. ->isValueOfType(2, Framework::JSON::JSONType::ARRAY),
  216. L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
  217. L"should contain an array at index 2");
  218. value->release();
  219. }
  220. TEST_METHOD (ObjectTest)
  221. {
  222. Framework::JSON::JSONValue* value
  223. = Framework::JSON::Parser::getValue("{\" \": []}");
  224. Assert::IsTrue(value != 0,
  225. L"Framework::JSON::Parser::getValue('{\"\": []}') should not "
  226. L"return 0");
  227. Assert::IsTrue(
  228. value->getType() == Framework::JSON::JSONType::OBJECT,
  229. L"Framework::JSON::Parser::getValue('{\" \": []}') should "
  230. L"return an object");
  231. Assert::IsTrue(
  232. ((Framework::JSON::JSONObject*)value)->getFieldCount() == 1,
  233. L"Framework::JSON::Parser::getValue('{\" \": []}') should "
  234. L"return an object with one attribute");
  235. Assert::IsTrue(
  236. ((Framework::JSON::JSONObject*)value)
  237. ->isValueOfType(" ", Framework::JSON::JSONType::ARRAY),
  238. L"Framework::JSON::Parser::getValue('{\" \": []}') should "
  239. L"contain an array at attribute ' '");
  240. value->release();
  241. }
  242. TEST_METHOD (ToStringTest)
  243. {
  244. Framework::JSON::JSONValue* value
  245. = Framework::JSON::Parser::getValue(
  246. "{\" \": [1, true, false, 0.0, {}], \"t\": null}");
  247. Framework::JSON::JSONValue* value2
  248. = Framework::JSON::Parser::getValue(value->toString());
  249. Assert::IsTrue(isEqual(value, value2),
  250. L"Framework::JSON::Parser::getValue(value.toString()) should "
  251. L"return a json value eqal to value");
  252. value->release();
  253. value2->release();
  254. }
  255. static bool isEqual(
  256. Framework::JSON::JSONValue * a, Framework::JSON::JSONValue * b)
  257. {
  258. if (a->getType() != b->getType()) return 0;
  259. switch (a->getType())
  260. {
  261. case Framework::JSON::JSONType::NUMBER:
  262. return ((Framework::JSON::JSONNumber*)a)->getNumber()
  263. == ((Framework::JSON::JSONNumber*)b)->getNumber();
  264. case Framework::JSON::JSONType::BOOLEAN:
  265. return ((Framework::JSON::JSONBool*)a)->getBool()
  266. == ((Framework::JSON::JSONBool*)b)->getBool();
  267. case Framework::JSON::JSONType::STRING:
  268. return ((Framework::JSON::JSONString*)a)
  269. ->getString()
  270. .istGleich(((Framework::JSON::JSONString*)b)->getString());
  271. case Framework::JSON::JSONType::ARRAY:
  272. {
  273. Framework::JSON::JSONArray* arrayA
  274. = (Framework::JSON::JSONArray*)a;
  275. Framework::JSON::JSONArray* arrayB
  276. = (Framework::JSON::JSONArray*)b;
  277. if (arrayA->getLength() != arrayB->getLength()) return 0;
  278. for (int i = 0; i < arrayA->getLength(); i++)
  279. {
  280. Framework::JSON::JSONValue* entryA
  281. = arrayA->getValue(i);
  282. Framework::JSON::JSONValue* entryB
  283. = arrayB->getValue(i);
  284. bool eq = isEqual(entryA, entryB);
  285. entryA->release();
  286. entryB->release();
  287. if (!eq) return 0;
  288. }
  289. return 1;
  290. }
  291. case Framework::JSON::JSONType::OBJECT:
  292. {
  293. Framework::JSON::JSONObject* objA
  294. = (Framework::JSON::JSONObject*)a;
  295. Framework::JSON::JSONObject* objB
  296. = (Framework::JSON::JSONObject*)b;
  297. if (objA->getFieldCount() != objB->getFieldCount())
  298. return 0;
  299. auto oaf = objA->getFields();
  300. while (oaf)
  301. {
  302. if (!objB->hasValue(oaf)) return 0;
  303. Framework::JSON::JSONValue* entryA
  304. = objA->getValue(oaf);
  305. Framework::JSON::JSONValue* entryB
  306. = objB->getValue(oaf);
  307. bool eq = isEqual(entryA, entryB);
  308. entryA->release();
  309. entryB->release();
  310. if (!eq) return 0;
  311. oaf++;
  312. }
  313. return 1;
  314. }
  315. }
  316. return 1;
  317. }
  318. TEST_METHOD (ToArrayTest)
  319. {
  320. Framework::JSON::JSONArray* jArray
  321. = Framework::JSON::Parser::getValue("[1,2,3,4,5,6,7,8,9,10]")
  322. ->asArray();
  323. Framework::Array<int>* numberArray
  324. = jArray->toArray<int>([](Framework::JSON::JSONValue& v) {
  325. return (int)v.asNumber()->getNumber();
  326. });
  327. Assert::IsTrue(numberArray->getEintragAnzahl() == 10,
  328. L"Array hat die falsche Anzahl an elementen");
  329. Assert::IsTrue(numberArray->get(2) == 3,
  330. L"Array hat mindestens ein falsches element");
  331. Assert::IsTrue(numberArray->get(7) == 8,
  332. L"Array hat mindestens ein falsches element");
  333. numberArray->release();
  334. numberArray = jArray->toArray<int>(
  335. [](Framework::JSON::JSONValue& v) {
  336. return (int)v.asNumber()->getNumber() % 2 == 0;
  337. },
  338. [](Framework::JSON::JSONValue& v) {
  339. return (int)v.asNumber()->getNumber();
  340. });
  341. Assert::IsTrue(numberArray->get(0) == 2,
  342. L"Array hat mindestens ein falsches element");
  343. Assert::IsTrue(numberArray->get(3) == 8,
  344. L"Array hat mindestens ein falsches element");
  345. jArray->release();
  346. }
  347. TEST_METHOD (ToRCArrayTest)
  348. {
  349. Framework::JSON::JSONArray* jArray
  350. = Framework::JSON::Parser::getValue(
  351. "[\"1\",\"2\",\"3\",\"4\",\"5\"]")
  352. ->asArray();
  353. Framework::RCArray<Framework::Text>* numberArray
  354. = jArray->toRCArray<Framework::Text>(
  355. [](Framework::JSON::JSONValue& v) {
  356. return new Framework::Text(v.asString()->getString());
  357. });
  358. Assert::IsTrue(numberArray->getEintragAnzahl() == 5,
  359. L"Array hat die falsche Anzahl an elementen");
  360. Assert::IsTrue(numberArray->z(1)->istGleich("2"),
  361. L"Array hat mindestens ein falsches element");
  362. Assert::IsTrue(numberArray->z(4)->istGleich("5"),
  363. L"Array hat mindestens ein falsches element");
  364. numberArray->release();
  365. numberArray = jArray->toRCArray<Framework::Text>(
  366. [](Framework::JSON::JSONValue& v) {
  367. return (int)v.asString()->getString() % 2 == 0;
  368. },
  369. [](Framework::JSON::JSONValue& v) {
  370. return new Framework::Text(v.asString()->getString());
  371. });
  372. Assert::IsTrue(numberArray->z(0)->istGleich("2"),
  373. L"Array hat mindestens ein falsches element");
  374. Assert::IsTrue(numberArray->z(1)->istGleich("4"),
  375. L"Array hat mindestens ein falsches element");
  376. jArray->release();
  377. }
  378. class TestObject
  379. {
  380. public:
  381. Framework::Text name;
  382. Framework::Text value;
  383. };
  384. TEST_METHOD (ParseObjectTest)
  385. {
  386. Framework::JSON::JSONObject* jObj
  387. = Framework::JSON::Parser::getValue(
  388. "{\"name\": \"test\", \"value\": \"1234\"}")
  389. ->asObject();
  390. TestObject* obj = jObj->parseTo<TestObject>(new TestObject(),
  391. [](TestObject* obj,
  392. Framework::Text attrName,
  393. Framework::JSON::JSONValue& v) {
  394. if (attrName.istGleich("name"))
  395. {
  396. obj->name = v.asString()->getString();
  397. }
  398. else
  399. {
  400. obj->value = v.asString()->getString();
  401. }
  402. });
  403. Assert::IsTrue(
  404. obj->name.istGleich("test"), L"Feld hat falschen wert");
  405. Assert::IsTrue(
  406. obj->value.istGleich("1234"), L"Feld hat falschen wert");
  407. delete obj;
  408. jObj->release();
  409. }
  410. TEST_METHOD (FromArrayTest)
  411. {
  412. Framework::Array<int> arr;
  413. arr.add(1);
  414. arr.add(2);
  415. arr.add(3);
  416. arr.add(4);
  417. Framework::JSON::JSONArray* jArray
  418. = Framework::JSON::JSONArray::fromArray<int>(arr,
  419. [](int v) { return new Framework::JSON::JSONNumber(v); });
  420. Assert::IsTrue(
  421. jArray->getLength() == 4, L"Array hat falsche länge");
  422. Framework::JSON::JSONNumber* n = jArray->getValue(1)->asNumber();
  423. Assert::IsTrue(n->getNumber() == 2,
  424. L"Array hat mindestens einen falschen Wert");
  425. n->release();
  426. jArray->release();
  427. Framework::RCArray<Framework::Text> rcArr;
  428. rcArr.add(new Framework::Text("1"));
  429. rcArr.add(new Framework::Text("2"));
  430. rcArr.add(new Framework::Text("3"));
  431. rcArr.add(new Framework::Text("4"));
  432. jArray = Framework::JSON::JSONArray::fromRCArray<Framework::Text>(
  433. rcArr, [](Framework::Text& v) {
  434. return new Framework::JSON::JSONString(v);
  435. });
  436. Assert::IsTrue(
  437. jArray->getLength() == 4, L"Array hat falsche länge");
  438. Framework::JSON::JSONString* s = jArray->getValue(2)->asString();
  439. Assert::IsTrue(s->getString().istGleich("3"),
  440. L"Array hat mindestens einen falschen Wert");
  441. s->release();
  442. jArray->release();
  443. }
  444. };
  445. TEST_CLASS (JSONValidatorTests)
  446. {
  447. private:
  448. static OutputDebugStringBuf<char, std::char_traits<char>>
  449. charDebugOutput;
  450. static std::streambuf* buf;
  451. public:
  452. TEST_CLASS_INITIALIZE(Init)
  453. {
  454. buf = std::cout.rdbuf();
  455. std::cout.rdbuf(&charDebugOutput);
  456. }
  457. TEST_METHOD (ValidGreaterThenTest)
  458. {
  459. Framework::JSON::JSONValue* value
  460. = Framework::JSON::Parser::getValue("{\"test\": 0.001}");
  461. Framework::JSON::Validator::JSONValidator* validator
  462. = Framework::JSON::Validator::JSONValidator::buildForObject()
  463. ->withRequiredNumber("test")
  464. ->whichIsGreaterThen(0)
  465. ->finishNumber()
  466. ->finishObject();
  467. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  468. removed;
  469. Assert::IsTrue(
  470. validator->isValid(value), L"Valid value is marked as invalid");
  471. Framework::JSON::JSONValue* result
  472. = validator->getValidParts(value, &removed);
  473. Assert::IsTrue(
  474. result->asObject()->zValue("test")->asNumber()->getNumber()
  475. == value->asObject()
  476. ->zValue("test")
  477. ->asNumber()
  478. ->getNumber(),
  479. L"Get Valid Parts made the valid value invalid");
  480. value->release();
  481. validator->release();
  482. result->release();
  483. }
  484. TEST_METHOD (ValidTest)
  485. {
  486. Framework::JSON::Validator::JSONValidator* validator
  487. = Framework::JSON::Validator::JSONValidator::buildForArray()
  488. ->addAcceptedObjectInArray()
  489. ->withRequiredNumber("x")
  490. ->whichIsLessThen(5)
  491. ->finishNumber()
  492. ->withRequiredBool("bla")
  493. ->whichIsOptional()
  494. ->withDefault(true)
  495. ->finishBool()
  496. ->finishObject()
  497. ->finishArray();
  498. Framework::JSON::JSONArray* jArray
  499. = Framework::JSON::Parser::getValue(
  500. "[{\"x\": 4, \"bla\": false}]")
  501. ->asArray();
  502. Assert::IsTrue(validator->isValid(jArray),
  503. L"A valid json Array was marked as invalid by the validator");
  504. validator->release();
  505. }
  506. TEST_METHOD (ComplexTest)
  507. {
  508. Framework::JSON::Validator::JSONValidator* validator
  509. = Framework::JSON::Validator::JSONValidator::buildForArray()
  510. ->typeSpecifiedByAttribute("type")
  511. ->addAcceptedObjectInArray()
  512. ->withRequiredString("type")
  513. ->withExactMatch("shaped")
  514. ->finishString()
  515. ->withRequiredString("group")
  516. ->finishString()
  517. ->withRequiredNumber("width")
  518. ->whichIsGreaterThen(0)
  519. ->finishNumber()
  520. ->withRequiredNumber("height")
  521. ->whichIsGreaterThen(0)
  522. ->finishNumber()
  523. ->withRequiredArray("inputs")
  524. ->addAcceptedObjectInArray()
  525. ->withRequiredNumber("x")
  526. ->whichIsGreaterOrEqual(0)
  527. ->finishNumber()
  528. ->withRequiredNumber("y")
  529. ->whichIsGreaterOrEqual(0)
  530. ->finishNumber()
  531. ->withRequiredObject("filter")
  532. ->withRequiredString("itemType")
  533. ->finishString()
  534. ->finishObject()
  535. ->finishObject()
  536. ->finishArray()
  537. ->withRequiredObject("output")
  538. ->withRequiredString("itemType")
  539. ->finishString()
  540. ->finishObject()
  541. ->withRequiredNumber("outputCount")
  542. ->whichIsGreaterThen(0)
  543. ->finishNumber()
  544. ->finishObject()
  545. ->addAcceptedObjectInArray()
  546. ->withRequiredString("type")
  547. ->withExactMatch("unordered")
  548. ->finishString()
  549. ->withRequiredString("group")
  550. ->finishString()
  551. ->withRequiredArray("inputs")
  552. ->addAcceptedObjectInArray()
  553. ->withRequiredNumber("count")
  554. ->whichIsGreaterThen(0)
  555. ->finishNumber()
  556. ->withRequiredObject("filter")
  557. ->withRequiredString("itemType")
  558. ->finishString()
  559. ->finishObject()
  560. ->finishObject()
  561. ->finishArray()
  562. ->withRequiredArray("output")
  563. ->addAcceptedObjectInArray()
  564. ->withRequiredObject("filter")
  565. ->withRequiredString("itemType")
  566. ->finishString()
  567. ->finishObject()
  568. ->withRequiredNumber("count")
  569. ->whichIsGreaterThen(0)
  570. ->finishNumber()
  571. ->finishObject()
  572. ->finishArray()
  573. ->finishObject()
  574. ->finishArray();
  575. std::cout << validator->zConstraints()->toString().getText()
  576. << "\n";
  577. Framework::JSON::JSONValue* value
  578. = Framework::JSON::Parser::getValue(
  579. "[{\"type\": \"shaped\",\"group\": "
  580. "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
  581. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  582. "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
  583. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  584. "\"StoneTool\"},\"outputCount\": 1},{\"type\": "
  585. "\"shaped\",\"group\": \"inventory\",\"width\": "
  586. "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
  587. "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
  588. "0,\"y\": 1,\"filter\": {\"itemType\": "
  589. "\"Cobble\"}}],\"output\": {\"itemType\": "
  590. "\"StoneTool\"},\"outputCount\": 1}]");
  591. std::cout << value->toString().getText() << "\n";
  592. Framework::JSON::Validator::JSONValidationResult* result
  593. = validator->validate(value);
  594. std::cout << result->getInvalidInfo();
  595. Assert::IsTrue(
  596. !result->isValid(), L"Invalid Json was marked as valid");
  597. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  598. invalidParts;
  599. Framework::JSON::JSONValue* validValue
  600. = result->getValidPart(&invalidParts);
  601. Assert::IsTrue(invalidParts.getEintragAnzahl() > 0,
  602. L"No Invalid parts were returned for an invalid Json");
  603. for (Framework::JSON::Validator::JSONValidationResult* invalid :
  604. invalidParts)
  605. {
  606. invalid->getInvalidInfo();
  607. }
  608. result->release();
  609. Assert::IsTrue(validValue == 0,
  610. L"getValidPart of invalid validation result without "
  611. L"removeInvalidEntries or default values used in validation "
  612. L"should return 0");
  613. value->release();
  614. validator->release();
  615. }
  616. TEST_METHOD (ComplexRemoveInvalidTest)
  617. {
  618. Framework::JSON::Validator::JSONValidator* validator
  619. = Framework::JSON::Validator::JSONValidator::buildForArray()
  620. ->removeInvalidEntries()
  621. ->typeSpecifiedByAttribute("type")
  622. ->addAcceptedObjectInArray()
  623. ->withRequiredString("type")
  624. ->withExactMatch("shaped")
  625. ->finishString()
  626. ->withRequiredString("group")
  627. ->finishString()
  628. ->withRequiredNumber("width")
  629. ->whichIsGreaterThen(0)
  630. ->finishNumber()
  631. ->withRequiredNumber("height")
  632. ->whichIsGreaterThen(0)
  633. ->finishNumber()
  634. ->withRequiredArray("inputs")
  635. ->addAcceptedObjectInArray()
  636. ->withRequiredNumber("x")
  637. ->whichIsGreaterOrEqual(0)
  638. ->finishNumber()
  639. ->withRequiredNumber("y")
  640. ->whichIsGreaterOrEqual(0)
  641. ->finishNumber()
  642. ->withRequiredObject("filter")
  643. ->withRequiredString("itemType")
  644. ->finishString()
  645. ->finishObject()
  646. ->finishObject()
  647. ->finishArray()
  648. ->withRequiredObject("output")
  649. ->withRequiredString("itemType")
  650. ->finishString()
  651. ->finishObject()
  652. ->withRequiredNumber("outputCount")
  653. ->whichIsGreaterThen(0)
  654. ->finishNumber()
  655. ->finishObject()
  656. ->addAcceptedObjectInArray()
  657. ->withRequiredString("type")
  658. ->withExactMatch("unordered")
  659. ->finishString()
  660. ->withRequiredString("group")
  661. ->finishString()
  662. ->withRequiredArray("inputs")
  663. ->addAcceptedObjectInArray()
  664. ->withRequiredNumber("count")
  665. ->whichIsGreaterThen(0)
  666. ->finishNumber()
  667. ->withRequiredObject("filter")
  668. ->withRequiredString("itemType")
  669. ->finishString()
  670. ->finishObject()
  671. ->finishObject()
  672. ->finishArray()
  673. ->withRequiredArray("output")
  674. ->addAcceptedObjectInArray()
  675. ->withRequiredObject("filter")
  676. ->withRequiredString("itemType")
  677. ->finishString()
  678. ->finishObject()
  679. ->withRequiredNumber("count")
  680. ->whichIsGreaterThen(0)
  681. ->finishNumber()
  682. ->finishObject()
  683. ->finishArray()
  684. ->finishObject()
  685. ->finishArray();
  686. std::cout << validator->zConstraints()->toString().getText()
  687. << "\n";
  688. Framework::JSON::JSONValue* value
  689. = Framework::JSON::Parser::getValue(
  690. "[{\"type\": \"shaped\",\"group\": "
  691. "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
  692. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  693. "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
  694. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  695. "\"StoneTool\"},\"outputCount\": 1},{\"type\": "
  696. "\"shaped\",\"group\": \"inventory\",\"width\": "
  697. "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
  698. "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
  699. "0,\"y\": 1,\"filter\": {\"itemType\": "
  700. "\"Cobble\"}}],\"output\": {\"itemType\": "
  701. "\"StoneTool\"},\"outputCount\": 1}]");
  702. std::cout << value->toString().getText() << "\n";
  703. Framework::JSON::Validator::JSONValidationResult* result
  704. = validator->validate(value);
  705. std::cout << result->getInvalidInfo();
  706. Assert::IsTrue(
  707. !result->isValid(), L"Invalid Json was marked as valid");
  708. Framework::JSON::JSONValue* validValue = result->getValidPart(0);
  709. result->release();
  710. Framework::JSON::JSONValue* expected
  711. = Framework::JSON::Parser::getValue(
  712. "[{\"type\": \"shaped\",\"group\": "
  713. "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
  714. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  715. "\"Cobble\"}},{\"x\": 0,\"y\": 1,\"filter\": "
  716. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  717. "\"StoneTool\"},\"outputCount\": 1}]");
  718. Assert::IsTrue(JSONParserTests::isEqual(validValue, expected),
  719. L"getValidPart of invalid validation result does not match the "
  720. L"expected valid part");
  721. result = validator->validate(validValue);
  722. Assert::IsTrue(result->isValid(),
  723. L"Re validation of a value returned by getValidPart on a "
  724. L"validation result should never return an invalid validation "
  725. L"result");
  726. value->release();
  727. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  728. invalidParts;
  729. value = result->getValidPart(&invalidParts);
  730. Assert::IsTrue(invalidParts.getEintragAnzahl() == 0,
  731. L"Invalid parts were returned for a valid validation result");
  732. Assert::IsTrue(JSONParserTests::isEqual(validValue, value),
  733. L"getValidPart of a valid validation result should return the "
  734. L"validated value");
  735. value->release();
  736. validValue->release();
  737. expected->release();
  738. validator->release();
  739. }
  740. TEST_METHOD (DefaultValuesTest)
  741. {
  742. Framework::JSON::Validator::JSONValidator* validator
  743. = Framework::JSON::Validator::JSONValidator::buildForArray()
  744. ->typeSpecifiedByAttribute("type")
  745. ->removeInvalidEntries()
  746. ->addAcceptedTypeInArray(
  747. Framework::JSON::Validator::JSONValidator::buildForObject()
  748. ->withRequiredString("type")
  749. ->withExactMatch("shaped")
  750. ->finishString()
  751. ->withRequiredString("group")
  752. ->withDefault("test")
  753. ->finishString()
  754. ->withRequiredNumber("width")
  755. ->whichIsGreaterThen(0)
  756. ->finishNumber()
  757. ->withRequiredNumber("height")
  758. ->whichIsGreaterThen(0)
  759. ->finishNumber()
  760. ->withRequiredAttribute("inputs",
  761. Framework::JSON::Validator::JSONValidator::
  762. buildForArray()
  763. ->withDefault(
  764. new Framework::JSON::JSONArray())
  765. ->addAcceptedTypeInArray(
  766. Framework::JSON::Validator::
  767. JSONValidator::buildForObject()
  768. ->withRequiredNumber("x")
  769. ->whichIsGreaterOrEqual(0)
  770. ->finishNumber()
  771. ->withRequiredNumber("y")
  772. ->whichIsGreaterOrEqual(0)
  773. ->finishNumber()
  774. ->withRequiredObject(
  775. "filter")
  776. ->withRequiredString(
  777. "itemType")
  778. ->finishString()
  779. ->finishObject()
  780. ->finishObject())
  781. ->finishArray())
  782. ->withRequiredObject("output")
  783. ->withRequiredString("itemType")
  784. ->finishString()
  785. ->finishObject()
  786. ->withRequiredNumber("outputCount")
  787. ->withDefault(1)
  788. ->whichIsGreaterThen(0)
  789. ->finishNumber()
  790. ->finishObject())
  791. ->addAcceptedTypeInArray(
  792. Framework::JSON::Validator::JSONValidator::buildForObject()
  793. ->withRequiredString("type")
  794. ->withExactMatch("unordered")
  795. ->finishString()
  796. ->withRequiredString("group")
  797. ->finishString()
  798. ->withRequiredAttribute("inputs",
  799. Framework::JSON::Validator::JSONValidator::
  800. buildForArray()
  801. ->withDefault(
  802. new Framework::JSON::JSONArray())
  803. ->addAcceptedTypeInArray(
  804. Framework::JSON::Validator::
  805. JSONValidator::
  806. buildForObject()
  807. ->withRequiredNumber(
  808. "count")
  809. ->withDefault(1)
  810. ->whichIsGreaterThen(
  811. 0)
  812. ->finishNumber()
  813. ->withRequiredObject(
  814. "filter")
  815. ->withRequiredString(
  816. "itemType")
  817. ->finishString()
  818. ->finishObject()
  819. ->finishObject())
  820. ->finishArray())
  821. ->withRequiredAttribute("output",
  822. Framework::JSON::Validator::JSONValidator::
  823. buildForArray()
  824. ->addAcceptedTypeInArray(
  825. Framework::JSON::Validator::
  826. JSONValidator::
  827. buildForObject()
  828. ->withRequiredObject(
  829. "filter")
  830. ->withRequiredString(
  831. "itemType")
  832. ->finishString()
  833. ->finishObject()
  834. ->withRequiredNumber(
  835. "count")
  836. ->withDefault(1)
  837. ->whichIsGreaterThen(
  838. 0)
  839. ->finishNumber()
  840. ->finishObject())
  841. ->finishArray())
  842. ->finishObject())
  843. ->finishArray();
  844. std::cout << validator->zConstraints()->toString().getText()
  845. << "\n";
  846. Framework::JSON::JSONValue* value
  847. = Framework::JSON::Parser::getValue(
  848. "[{\"type\": \"shaped\",\"width\": 1,\"height\": "
  849. "2,\"inputs\": [{\"x\": 0,\"y\": 0,\"filter\": "
  850. "{\"itemType\": \"Cobble\"}},{\"x\": 0,\"y\": "
  851. "1,\"filter\": {\"itemType\": \"Cobble\"}}],\"output\": "
  852. "{\"itemType\": \"StoneTool\"}},{\"type\": "
  853. "\"shaped\",\"width\": 1,\"height\": 2,\"inputs\": "
  854. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  855. "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
  856. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  857. "\"StoneTool\"}},{\"type\": \"unordered\",\"group\": "
  858. "\"bla\", \"inputs\": [{\"filter\": {\"itemType\": "
  859. "\"Cobble\"}},{\"filter\": {\"itemType\": "
  860. "\"Cobble\"}}],\"output\": [{\"filter\": {\"itemType\": "
  861. "\"StoneTool\"}}]}]");
  862. std::cout << value->toString().getText() << "\n";
  863. Framework::JSON::Validator::JSONValidationResult* result
  864. = validator->validate(value);
  865. std::cout << result->getInvalidInfo();
  866. Assert::IsTrue(
  867. !result->isValid(), L"Invalid Json was marked as valid");
  868. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  869. invalidParts;
  870. Framework::JSON::JSONValue* validValue
  871. = result->getValidPart(&invalidParts);
  872. Assert::IsTrue(invalidParts.getEintragAnzahl() > 0,
  873. L"No Invalid parts were returned for an invalid Json");
  874. for (Framework::JSON::Validator::JSONValidationResult* invalid :
  875. invalidParts)
  876. {
  877. std::cout << result->getInvalidInfo();
  878. }
  879. result->release();
  880. Framework::JSON::JSONValue* expected
  881. = Framework::JSON::Parser::getValue(
  882. "[{\"type\": \"shaped\", \"group\": \"test\",\"width\": "
  883. "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
  884. "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
  885. "0,\"y\": 1,\"filter\": {\"itemType\": "
  886. "\"Cobble\"}}],\"output\": {\"itemType\": \"StoneTool\"}, "
  887. "\"outputCount\": 1},{\"type\": \"unordered\",\"group\": "
  888. "\"bla\", \"inputs\": [{\"count\": 1, \"filter\": "
  889. "{\"itemType\": \"Cobble\"}},{\"count\": 1, \"filter\": "
  890. "{\"itemType\": \"Cobble\"}}],\"output\": [{\"count\": 1, "
  891. "\"filter\": {\"itemType\": \"StoneTool\"}}]}]");
  892. Assert::IsTrue(JSONParserTests::isEqual(validValue, expected),
  893. L"getValidPart of invalid validation result does not match the "
  894. L"expected valid part");
  895. result = validator->validate(validValue);
  896. Assert::IsTrue(result->isValid(),
  897. L"Re validation of a value returned by getValidPart on a "
  898. L"validation result should never return an invalid validation "
  899. L"result");
  900. value->release();
  901. value = result->getValidPart(0);
  902. Assert::IsTrue(JSONParserTests::isEqual(validValue, value),
  903. L"getValidPart of a valid validation result should return the "
  904. L"validated value");
  905. value->release();
  906. validValue->release();
  907. expected->release();
  908. validator->release();
  909. }
  910. TEST_METHOD (RecursiveValidatorTest)
  911. {
  912. Framework::JSON::Validator::JSONValidator* validator
  913. = Framework::JSON::Validator::JSONValidator::buildForObject()
  914. ->setObjectReferenceId("TreeNode")
  915. ->withRequiredAttribute("value",
  916. Framework::JSON::Validator::JSONValidator::
  917. buildForString()
  918. ->whichCanBeNull()
  919. ->finishString())
  920. ->withRequiredAttribute("children",
  921. Framework::JSON::Validator::JSONValidator::
  922. buildForArray()
  923. ->whichIsOptional()
  924. ->addAcceptedTypeInArray(Framework::JSON::
  925. Validator::JSONValidator::
  926. buildForObjectReference(
  927. "TreeNode"))
  928. ->finishArray())
  929. ->finishObject();
  930. Framework::JSON::JSONObject* jArray
  931. = Framework::JSON::Parser::getValue(
  932. "{\"value\": \"1\", \"children\": [{\"value\": \"2\"}, "
  933. "{\"value\": \"3\", \"children\": [{\"value\": \"4\"}]}]}")
  934. ->asObject();
  935. Assert::IsTrue(validator->isValid(jArray),
  936. L"A valid json Object was marked as invalid by the validator");
  937. validator->release();
  938. }
  939. TEST_CLASS_CLEANUP(Cleanup)
  940. {
  941. std::cout.rdbuf(buf);
  942. }
  943. };
  944. OutputDebugStringBuf<char, std::char_traits<char>>
  945. JSONValidatorTests::charDebugOutput;
  946. std::streambuf* JSONValidatorTests::buf;
  947. } // namespace FrameworkTests