Json.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. result->printInvalidInfo();
  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, L"No Invalid parts were returned for an invalid Json");
  602. for (Framework::JSON::Validator::JSONValidationResult* invalid :
  603. invalidParts)
  604. {
  605. invalid->printInvalidInfo();
  606. }
  607. result->release();
  608. Assert::IsTrue(validValue == 0,
  609. L"getValidPart of invalid validation result without "
  610. L"removeInvalidEntries or default values used in validation "
  611. L"should return 0");
  612. value->release();
  613. validator->release();
  614. }
  615. TEST_METHOD (ComplexRemoveInvalidTest)
  616. {
  617. Framework::JSON::Validator::JSONValidator* validator
  618. = Framework::JSON::Validator::JSONValidator::buildForArray()
  619. ->removeInvalidEntries()
  620. ->typeSpecifiedByAttribute("type")
  621. ->addAcceptedObjectInArray()
  622. ->withRequiredString("type")
  623. ->withExactMatch("shaped")
  624. ->finishString()
  625. ->withRequiredString("group")
  626. ->finishString()
  627. ->withRequiredNumber("width")
  628. ->whichIsGreaterThen(0)
  629. ->finishNumber()
  630. ->withRequiredNumber("height")
  631. ->whichIsGreaterThen(0)
  632. ->finishNumber()
  633. ->withRequiredArray("inputs")
  634. ->addAcceptedObjectInArray()
  635. ->withRequiredNumber("x")
  636. ->whichIsGreaterOrEqual(0)
  637. ->finishNumber()
  638. ->withRequiredNumber("y")
  639. ->whichIsGreaterOrEqual(0)
  640. ->finishNumber()
  641. ->withRequiredObject("filter")
  642. ->withRequiredString("itemType")
  643. ->finishString()
  644. ->finishObject()
  645. ->finishObject()
  646. ->finishArray()
  647. ->withRequiredObject("output")
  648. ->withRequiredString("itemType")
  649. ->finishString()
  650. ->finishObject()
  651. ->withRequiredNumber("outputCount")
  652. ->whichIsGreaterThen(0)
  653. ->finishNumber()
  654. ->finishObject()
  655. ->addAcceptedObjectInArray()
  656. ->withRequiredString("type")
  657. ->withExactMatch("unordered")
  658. ->finishString()
  659. ->withRequiredString("group")
  660. ->finishString()
  661. ->withRequiredArray("inputs")
  662. ->addAcceptedObjectInArray()
  663. ->withRequiredNumber("count")
  664. ->whichIsGreaterThen(0)
  665. ->finishNumber()
  666. ->withRequiredObject("filter")
  667. ->withRequiredString("itemType")
  668. ->finishString()
  669. ->finishObject()
  670. ->finishObject()
  671. ->finishArray()
  672. ->withRequiredArray("output")
  673. ->addAcceptedObjectInArray()
  674. ->withRequiredObject("filter")
  675. ->withRequiredString("itemType")
  676. ->finishString()
  677. ->finishObject()
  678. ->withRequiredNumber("count")
  679. ->whichIsGreaterThen(0)
  680. ->finishNumber()
  681. ->finishObject()
  682. ->finishArray()
  683. ->finishObject()
  684. ->finishArray();
  685. std::cout << validator->zConstraints()->toString().getText()
  686. << "\n";
  687. Framework::JSON::JSONValue* value
  688. = Framework::JSON::Parser::getValue(
  689. "[{\"type\": \"shaped\",\"group\": "
  690. "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
  691. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  692. "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
  693. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  694. "\"StoneTool\"},\"outputCount\": 1},{\"type\": "
  695. "\"shaped\",\"group\": \"inventory\",\"width\": "
  696. "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
  697. "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
  698. "0,\"y\": 1,\"filter\": {\"itemType\": "
  699. "\"Cobble\"}}],\"output\": {\"itemType\": "
  700. "\"StoneTool\"},\"outputCount\": 1}]");
  701. std::cout << value->toString().getText() << "\n";
  702. Framework::JSON::Validator::JSONValidationResult* result
  703. = validator->validate(value);
  704. result->printInvalidInfo();
  705. Assert::IsTrue(
  706. !result->isValid(), L"Invalid Json was marked as valid");
  707. Framework::JSON::JSONValue* validValue = result->getValidPart(0);
  708. result->release();
  709. Framework::JSON::JSONValue* expected
  710. = Framework::JSON::Parser::getValue(
  711. "[{\"type\": \"shaped\",\"group\": "
  712. "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
  713. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  714. "\"Cobble\"}},{\"x\": 0,\"y\": 1,\"filter\": "
  715. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  716. "\"StoneTool\"},\"outputCount\": 1}]");
  717. Assert::IsTrue(JSONParserTests::isEqual(validValue, expected),
  718. L"getValidPart of invalid validation result does not match the "
  719. L"expected valid part");
  720. result = validator->validate(validValue);
  721. Assert::IsTrue(result->isValid(),
  722. L"Re validation of a value returned by getValidPart on a "
  723. L"validation result should never return an invalid validation "
  724. L"result");
  725. value->release();
  726. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  727. invalidParts;
  728. value = result->getValidPart(&invalidParts);
  729. Assert::IsTrue(invalidParts.getEintragAnzahl() == 0,
  730. L"Invalid parts were returned for a valid validation result");
  731. Assert::IsTrue(JSONParserTests::isEqual(validValue, value),
  732. L"getValidPart of a valid validation result should return the "
  733. L"validated value");
  734. value->release();
  735. validValue->release();
  736. expected->release();
  737. validator->release();
  738. }
  739. TEST_METHOD (DefaultValuesTest)
  740. {
  741. Framework::JSON::Validator::JSONValidator* validator
  742. = Framework::JSON::Validator::JSONValidator::buildForArray()
  743. ->typeSpecifiedByAttribute("type")
  744. ->removeInvalidEntries()
  745. ->addAcceptedTypeInArray(
  746. Framework::JSON::Validator::JSONValidator::buildForObject()
  747. ->withRequiredString("type")
  748. ->withExactMatch("shaped")
  749. ->finishString()
  750. ->withRequiredString("group")
  751. ->withDefault("test")
  752. ->finishString()
  753. ->withRequiredNumber("width")
  754. ->whichIsGreaterThen(0)
  755. ->finishNumber()
  756. ->withRequiredNumber("height")
  757. ->whichIsGreaterThen(0)
  758. ->finishNumber()
  759. ->withRequiredAttribute("inputs",
  760. Framework::JSON::Validator::JSONValidator::
  761. buildForArray()
  762. ->withDefault(
  763. new Framework::JSON::JSONArray())
  764. ->addAcceptedTypeInArray(
  765. Framework::JSON::Validator::
  766. JSONValidator::buildForObject()
  767. ->withRequiredNumber("x")
  768. ->whichIsGreaterOrEqual(0)
  769. ->finishNumber()
  770. ->withRequiredNumber("y")
  771. ->whichIsGreaterOrEqual(0)
  772. ->finishNumber()
  773. ->withRequiredObject(
  774. "filter")
  775. ->withRequiredString(
  776. "itemType")
  777. ->finishString()
  778. ->finishObject()
  779. ->finishObject())
  780. ->finishArray())
  781. ->withRequiredObject("output")
  782. ->withRequiredString("itemType")
  783. ->finishString()
  784. ->finishObject()
  785. ->withRequiredNumber("outputCount")
  786. ->withDefault(1)
  787. ->whichIsGreaterThen(0)
  788. ->finishNumber()
  789. ->finishObject())
  790. ->addAcceptedTypeInArray(
  791. Framework::JSON::Validator::JSONValidator::buildForObject()
  792. ->withRequiredString("type")
  793. ->withExactMatch("unordered")
  794. ->finishString()
  795. ->withRequiredString("group")
  796. ->finishString()
  797. ->withRequiredAttribute("inputs",
  798. Framework::JSON::Validator::JSONValidator::
  799. buildForArray()
  800. ->withDefault(
  801. new Framework::JSON::JSONArray())
  802. ->addAcceptedTypeInArray(
  803. Framework::JSON::Validator::
  804. JSONValidator::
  805. buildForObject()
  806. ->withRequiredNumber(
  807. "count")
  808. ->withDefault(1)
  809. ->whichIsGreaterThen(
  810. 0)
  811. ->finishNumber()
  812. ->withRequiredObject(
  813. "filter")
  814. ->withRequiredString(
  815. "itemType")
  816. ->finishString()
  817. ->finishObject()
  818. ->finishObject())
  819. ->finishArray())
  820. ->withRequiredAttribute("output",
  821. Framework::JSON::Validator::JSONValidator::
  822. buildForArray()
  823. ->addAcceptedTypeInArray(
  824. Framework::JSON::Validator::
  825. JSONValidator::
  826. buildForObject()
  827. ->withRequiredObject(
  828. "filter")
  829. ->withRequiredString(
  830. "itemType")
  831. ->finishString()
  832. ->finishObject()
  833. ->withRequiredNumber(
  834. "count")
  835. ->withDefault(1)
  836. ->whichIsGreaterThen(
  837. 0)
  838. ->finishNumber()
  839. ->finishObject())
  840. ->finishArray())
  841. ->finishObject())
  842. ->finishArray();
  843. std::cout << validator->zConstraints()->toString().getText()
  844. << "\n";
  845. Framework::JSON::JSONValue* value
  846. = Framework::JSON::Parser::getValue(
  847. "[{\"type\": \"shaped\",\"width\": 1,\"height\": "
  848. "2,\"inputs\": [{\"x\": 0,\"y\": 0,\"filter\": "
  849. "{\"itemType\": \"Cobble\"}},{\"x\": 0,\"y\": "
  850. "1,\"filter\": {\"itemType\": \"Cobble\"}}],\"output\": "
  851. "{\"itemType\": \"StoneTool\"}},{\"type\": "
  852. "\"shaped\",\"width\": 1,\"height\": 2,\"inputs\": "
  853. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  854. "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
  855. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  856. "\"StoneTool\"}},{\"type\": \"unordered\",\"group\": "
  857. "\"bla\", \"inputs\": [{\"filter\": {\"itemType\": "
  858. "\"Cobble\"}},{\"filter\": {\"itemType\": "
  859. "\"Cobble\"}}],\"output\": [{\"filter\": {\"itemType\": "
  860. "\"StoneTool\"}}]}]");
  861. std::cout << value->toString().getText() << "\n";
  862. Framework::JSON::Validator::JSONValidationResult* result
  863. = validator->validate(value);
  864. result->printInvalidInfo();
  865. Assert::IsTrue(
  866. !result->isValid(), L"Invalid Json was marked as valid");
  867. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  868. invalidParts;
  869. Framework::JSON::JSONValue* validValue
  870. = result->getValidPart(&invalidParts);
  871. Assert::IsTrue(invalidParts.getEintragAnzahl() > 0,
  872. L"No Invalid parts were returned for an invalid Json");
  873. for (Framework::JSON::Validator::JSONValidationResult* invalid :
  874. invalidParts)
  875. {
  876. invalid->printInvalidInfo();
  877. }
  878. result->release();
  879. Framework::JSON::JSONValue* expected
  880. = Framework::JSON::Parser::getValue(
  881. "[{\"type\": \"shaped\", \"group\": \"test\",\"width\": "
  882. "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
  883. "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
  884. "0,\"y\": 1,\"filter\": {\"itemType\": "
  885. "\"Cobble\"}}],\"output\": {\"itemType\": \"StoneTool\"}, "
  886. "\"outputCount\": 1},{\"type\": \"unordered\",\"group\": "
  887. "\"bla\", \"inputs\": [{\"count\": 1, \"filter\": "
  888. "{\"itemType\": \"Cobble\"}},{\"count\": 1, \"filter\": "
  889. "{\"itemType\": \"Cobble\"}}],\"output\": [{\"count\": 1, "
  890. "\"filter\": {\"itemType\": \"StoneTool\"}}]}]");
  891. Assert::IsTrue(JSONParserTests::isEqual(validValue, expected),
  892. L"getValidPart of invalid validation result does not match the "
  893. L"expected valid part");
  894. result = validator->validate(validValue);
  895. Assert::IsTrue(result->isValid(),
  896. L"Re validation of a value returned by getValidPart on a "
  897. L"validation result should never return an invalid validation "
  898. L"result");
  899. value->release();
  900. value = result->getValidPart(0);
  901. Assert::IsTrue(JSONParserTests::isEqual(validValue, value),
  902. L"getValidPart of a valid validation result should return the "
  903. L"validated value");
  904. value->release();
  905. validValue->release();
  906. expected->release();
  907. validator->release();
  908. }
  909. TEST_METHOD (RecursiveValidatorTest)
  910. {
  911. Framework::JSON::Validator::JSONValidator* validator
  912. = Framework::JSON::Validator::JSONValidator::buildForObject()
  913. ->setObjectReferenceId("TreeNode")
  914. ->withRequiredAttribute("value",
  915. Framework::JSON::Validator::JSONValidator::
  916. buildForString()
  917. ->whichCanBeNull()
  918. ->finishString())
  919. ->withRequiredAttribute("children",
  920. Framework::JSON::Validator::JSONValidator::
  921. buildForArray()
  922. ->whichIsOptional()
  923. ->addAcceptedTypeInArray(Framework::JSON::
  924. Validator::JSONValidator::
  925. buildForObjectReference(
  926. "TreeNode"))
  927. ->finishArray())
  928. ->finishObject();
  929. Framework::JSON::JSONObject* jArray
  930. = Framework::JSON::Parser::getValue(
  931. "{\"value\": \"1\", \"children\": [{\"value\": \"2\"}, "
  932. "{\"value\": \"3\", \"children\": [{\"value\": \"4\"}]}]}")
  933. ->asObject();
  934. Assert::IsTrue(validator->isValid(jArray),
  935. L"A valid json Object was marked as invalid by the validator");
  936. validator->release();
  937. }
  938. TEST_CLASS_CLEANUP(Cleanup)
  939. {
  940. std::cout.rdbuf(buf);
  941. }
  942. };
  943. OutputDebugStringBuf<char, std::char_traits<char>>
  944. JSONValidatorTests::charDebugOutput;
  945. std::streambuf* JSONValidatorTests::buf;
  946. } // namespace FrameworkTests