Json.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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 (ValidTest)
  458. {
  459. Framework::JSON::Validator::JSONValidator* validator
  460. = Framework::JSON::Validator::JSONValidator::buildForArray()
  461. ->addAcceptedObjectInArray()
  462. ->withRequiredNumber("x")
  463. ->whichIsLessThen(5)
  464. ->finishNumber()
  465. ->withRequiredBool("bla")
  466. ->whichIsOptional()
  467. ->withDefault(true)
  468. ->finishBool()
  469. ->finishObject()
  470. ->finishArray();
  471. Framework::JSON::JSONArray* jArray
  472. = Framework::JSON::Parser::getValue(
  473. "[{\"x\": 4, \"bla\": false}]")
  474. ->asArray();
  475. Assert::IsTrue(validator->isValid(jArray),
  476. L"A valid json Array was marked as invalid by the validator");
  477. validator->release();
  478. }
  479. TEST_METHOD (ComplexTest)
  480. {
  481. Framework::JSON::Validator::JSONValidator* validator
  482. = Framework::JSON::Validator::JSONValidator::buildForArray()
  483. ->typeSpecifiedByAttribute("type")
  484. ->addAcceptedObjectInArray()
  485. ->withRequiredString("type")
  486. ->withExactMatch("shaped")
  487. ->finishString()
  488. ->withRequiredString("group")
  489. ->finishString()
  490. ->withRequiredNumber("width")
  491. ->whichIsGreaterThen(0)
  492. ->finishNumber()
  493. ->withRequiredNumber("height")
  494. ->whichIsGreaterThen(0)
  495. ->finishNumber()
  496. ->withRequiredArray("inputs")
  497. ->addAcceptedObjectInArray()
  498. ->withRequiredNumber("x")
  499. ->whichIsGreaterOrEqual(0)
  500. ->finishNumber()
  501. ->withRequiredNumber("y")
  502. ->whichIsGreaterOrEqual(0)
  503. ->finishNumber()
  504. ->withRequiredObject("filter")
  505. ->withRequiredString("itemType")
  506. ->finishString()
  507. ->finishObject()
  508. ->finishObject()
  509. ->finishArray()
  510. ->withRequiredObject("output")
  511. ->withRequiredString("itemType")
  512. ->finishString()
  513. ->finishObject()
  514. ->withRequiredNumber("outputCount")
  515. ->whichIsGreaterThen(0)
  516. ->finishNumber()
  517. ->finishObject()
  518. ->addAcceptedObjectInArray()
  519. ->withRequiredString("type")
  520. ->withExactMatch("unordered")
  521. ->finishString()
  522. ->withRequiredString("group")
  523. ->finishString()
  524. ->withRequiredArray("inputs")
  525. ->addAcceptedObjectInArray()
  526. ->withRequiredNumber("count")
  527. ->whichIsGreaterThen(0)
  528. ->finishNumber()
  529. ->withRequiredObject("filter")
  530. ->withRequiredString("itemType")
  531. ->finishString()
  532. ->finishObject()
  533. ->finishObject()
  534. ->finishArray()
  535. ->withRequiredArray("output")
  536. ->addAcceptedObjectInArray()
  537. ->withRequiredObject("filter")
  538. ->withRequiredString("itemType")
  539. ->finishString()
  540. ->finishObject()
  541. ->withRequiredNumber("count")
  542. ->whichIsGreaterThen(0)
  543. ->finishNumber()
  544. ->finishObject()
  545. ->finishArray()
  546. ->finishObject()
  547. ->finishArray();
  548. std::cout << validator->zConstraints()->toString().getText()
  549. << "\n";
  550. Framework::JSON::JSONValue* value
  551. = Framework::JSON::Parser::getValue(
  552. "[{\"type\": \"shaped\",\"group\": "
  553. "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
  554. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  555. "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
  556. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  557. "\"StoneTool\"},\"outputCount\": 1},{\"type\": "
  558. "\"shaped\",\"group\": \"inventory\",\"width\": "
  559. "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
  560. "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
  561. "0,\"y\": 1,\"filter\": {\"itemType\": "
  562. "\"Cobble\"}}],\"output\": {\"itemType\": "
  563. "\"StoneTool\"},\"outputCount\": 1}]");
  564. std::cout << value->toString().getText() << "\n";
  565. Framework::JSON::Validator::JSONValidationResult* result
  566. = validator->validate(value);
  567. result->printInvalidInfo();
  568. Assert::IsTrue(
  569. !result->isValid(), L"Invalid Json was marked as valid");
  570. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  571. invalidParts;
  572. Framework::JSON::JSONValue* validValue
  573. = result->getValidPart(&invalidParts);
  574. Assert::IsTrue(invalidParts.getEintragAnzahl() > 0, L"No Invalid parts were returned for an invalid Json");
  575. for (Framework::JSON::Validator::JSONValidationResult* invalid :
  576. invalidParts)
  577. {
  578. invalid->printInvalidInfo();
  579. }
  580. result->release();
  581. Assert::IsTrue(validValue == 0,
  582. L"getValidPart of invalid validation result without "
  583. L"removeInvalidEntries or default values used in validation "
  584. L"should return 0");
  585. value->release();
  586. validator->release();
  587. }
  588. TEST_METHOD (ComplexRemoveInvalidTest)
  589. {
  590. Framework::JSON::Validator::JSONValidator* validator
  591. = Framework::JSON::Validator::JSONValidator::buildForArray()
  592. ->removeInvalidEntries()
  593. ->typeSpecifiedByAttribute("type")
  594. ->addAcceptedObjectInArray()
  595. ->withRequiredString("type")
  596. ->withExactMatch("shaped")
  597. ->finishString()
  598. ->withRequiredString("group")
  599. ->finishString()
  600. ->withRequiredNumber("width")
  601. ->whichIsGreaterThen(0)
  602. ->finishNumber()
  603. ->withRequiredNumber("height")
  604. ->whichIsGreaterThen(0)
  605. ->finishNumber()
  606. ->withRequiredArray("inputs")
  607. ->addAcceptedObjectInArray()
  608. ->withRequiredNumber("x")
  609. ->whichIsGreaterOrEqual(0)
  610. ->finishNumber()
  611. ->withRequiredNumber("y")
  612. ->whichIsGreaterOrEqual(0)
  613. ->finishNumber()
  614. ->withRequiredObject("filter")
  615. ->withRequiredString("itemType")
  616. ->finishString()
  617. ->finishObject()
  618. ->finishObject()
  619. ->finishArray()
  620. ->withRequiredObject("output")
  621. ->withRequiredString("itemType")
  622. ->finishString()
  623. ->finishObject()
  624. ->withRequiredNumber("outputCount")
  625. ->whichIsGreaterThen(0)
  626. ->finishNumber()
  627. ->finishObject()
  628. ->addAcceptedObjectInArray()
  629. ->withRequiredString("type")
  630. ->withExactMatch("unordered")
  631. ->finishString()
  632. ->withRequiredString("group")
  633. ->finishString()
  634. ->withRequiredArray("inputs")
  635. ->addAcceptedObjectInArray()
  636. ->withRequiredNumber("count")
  637. ->whichIsGreaterThen(0)
  638. ->finishNumber()
  639. ->withRequiredObject("filter")
  640. ->withRequiredString("itemType")
  641. ->finishString()
  642. ->finishObject()
  643. ->finishObject()
  644. ->finishArray()
  645. ->withRequiredArray("output")
  646. ->addAcceptedObjectInArray()
  647. ->withRequiredObject("filter")
  648. ->withRequiredString("itemType")
  649. ->finishString()
  650. ->finishObject()
  651. ->withRequiredNumber("count")
  652. ->whichIsGreaterThen(0)
  653. ->finishNumber()
  654. ->finishObject()
  655. ->finishArray()
  656. ->finishObject()
  657. ->finishArray();
  658. std::cout << validator->zConstraints()->toString().getText()
  659. << "\n";
  660. Framework::JSON::JSONValue* value
  661. = Framework::JSON::Parser::getValue(
  662. "[{\"type\": \"shaped\",\"group\": "
  663. "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
  664. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  665. "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
  666. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  667. "\"StoneTool\"},\"outputCount\": 1},{\"type\": "
  668. "\"shaped\",\"group\": \"inventory\",\"width\": "
  669. "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
  670. "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
  671. "0,\"y\": 1,\"filter\": {\"itemType\": "
  672. "\"Cobble\"}}],\"output\": {\"itemType\": "
  673. "\"StoneTool\"},\"outputCount\": 1}]");
  674. std::cout << value->toString().getText() << "\n";
  675. Framework::JSON::Validator::JSONValidationResult* result
  676. = validator->validate(value);
  677. result->printInvalidInfo();
  678. Assert::IsTrue(
  679. !result->isValid(), L"Invalid Json was marked as valid");
  680. Framework::JSON::JSONValue* validValue = result->getValidPart(0);
  681. result->release();
  682. Framework::JSON::JSONValue* expected
  683. = Framework::JSON::Parser::getValue(
  684. "[{\"type\": \"shaped\",\"group\": "
  685. "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
  686. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  687. "\"Cobble\"}},{\"x\": 0,\"y\": 1,\"filter\": "
  688. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  689. "\"StoneTool\"},\"outputCount\": 1}]");
  690. Assert::IsTrue(JSONParserTests::isEqual(validValue, expected),
  691. L"getValidPart of invalid validation result does not match the "
  692. L"expected valid part");
  693. result = validator->validate(validValue);
  694. Assert::IsTrue(result->isValid(),
  695. L"Re validation of a value returned by getValidPart on a "
  696. L"validation result should never return an invalid validation "
  697. L"result");
  698. value->release();
  699. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  700. invalidParts;
  701. value = result->getValidPart(&invalidParts);
  702. Assert::IsTrue(invalidParts.getEintragAnzahl() == 0,
  703. L"Invalid parts were returned for a valid validation result");
  704. Assert::IsTrue(JSONParserTests::isEqual(validValue, value),
  705. L"getValidPart of a valid validation result should return the "
  706. L"validated value");
  707. value->release();
  708. validValue->release();
  709. expected->release();
  710. validator->release();
  711. }
  712. TEST_METHOD (DefaultValuesTest)
  713. {
  714. Framework::JSON::Validator::JSONValidator* validator
  715. = Framework::JSON::Validator::JSONValidator::buildForArray()
  716. ->typeSpecifiedByAttribute("type")
  717. ->removeInvalidEntries()
  718. ->addAcceptedTypeInArray(
  719. Framework::JSON::Validator::JSONValidator::buildForObject()
  720. ->withRequiredString("type")
  721. ->withExactMatch("shaped")
  722. ->finishString()
  723. ->withRequiredString("group")
  724. ->withDefault("test")
  725. ->finishString()
  726. ->withRequiredNumber("width")
  727. ->whichIsGreaterThen(0)
  728. ->finishNumber()
  729. ->withRequiredNumber("height")
  730. ->whichIsGreaterThen(0)
  731. ->finishNumber()
  732. ->withRequiredAttribute("inputs",
  733. Framework::JSON::Validator::JSONValidator::
  734. buildForArray()
  735. ->withDefault(
  736. new Framework::JSON::JSONArray())
  737. ->addAcceptedTypeInArray(
  738. Framework::JSON::Validator::
  739. JSONValidator::buildForObject()
  740. ->withRequiredNumber("x")
  741. ->whichIsGreaterOrEqual(0)
  742. ->finishNumber()
  743. ->withRequiredNumber("y")
  744. ->whichIsGreaterOrEqual(0)
  745. ->finishNumber()
  746. ->withRequiredObject(
  747. "filter")
  748. ->withRequiredString(
  749. "itemType")
  750. ->finishString()
  751. ->finishObject()
  752. ->finishObject())
  753. ->finishArray())
  754. ->withRequiredObject("output")
  755. ->withRequiredString("itemType")
  756. ->finishString()
  757. ->finishObject()
  758. ->withRequiredNumber("outputCount")
  759. ->withDefault(1)
  760. ->whichIsGreaterThen(0)
  761. ->finishNumber()
  762. ->finishObject())
  763. ->addAcceptedTypeInArray(
  764. Framework::JSON::Validator::JSONValidator::buildForObject()
  765. ->withRequiredString("type")
  766. ->withExactMatch("unordered")
  767. ->finishString()
  768. ->withRequiredString("group")
  769. ->finishString()
  770. ->withRequiredAttribute("inputs",
  771. Framework::JSON::Validator::JSONValidator::
  772. buildForArray()
  773. ->withDefault(
  774. new Framework::JSON::JSONArray())
  775. ->addAcceptedTypeInArray(
  776. Framework::JSON::Validator::
  777. JSONValidator::
  778. buildForObject()
  779. ->withRequiredNumber(
  780. "count")
  781. ->withDefault(1)
  782. ->whichIsGreaterThen(
  783. 0)
  784. ->finishNumber()
  785. ->withRequiredObject(
  786. "filter")
  787. ->withRequiredString(
  788. "itemType")
  789. ->finishString()
  790. ->finishObject()
  791. ->finishObject())
  792. ->finishArray())
  793. ->withRequiredAttribute("output",
  794. Framework::JSON::Validator::JSONValidator::
  795. buildForArray()
  796. ->addAcceptedTypeInArray(
  797. Framework::JSON::Validator::
  798. JSONValidator::
  799. buildForObject()
  800. ->withRequiredObject(
  801. "filter")
  802. ->withRequiredString(
  803. "itemType")
  804. ->finishString()
  805. ->finishObject()
  806. ->withRequiredNumber(
  807. "count")
  808. ->withDefault(1)
  809. ->whichIsGreaterThen(
  810. 0)
  811. ->finishNumber()
  812. ->finishObject())
  813. ->finishArray())
  814. ->finishObject())
  815. ->finishArray();
  816. std::cout << validator->zConstraints()->toString().getText()
  817. << "\n";
  818. Framework::JSON::JSONValue* value
  819. = Framework::JSON::Parser::getValue(
  820. "[{\"type\": \"shaped\",\"width\": 1,\"height\": "
  821. "2,\"inputs\": [{\"x\": 0,\"y\": 0,\"filter\": "
  822. "{\"itemType\": \"Cobble\"}},{\"x\": 0,\"y\": "
  823. "1,\"filter\": {\"itemType\": \"Cobble\"}}],\"output\": "
  824. "{\"itemType\": \"StoneTool\"}},{\"type\": "
  825. "\"shaped\",\"width\": 1,\"height\": 2,\"inputs\": "
  826. "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
  827. "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
  828. "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
  829. "\"StoneTool\"}},{\"type\": \"unordered\",\"group\": "
  830. "\"bla\", \"inputs\": [{\"filter\": {\"itemType\": "
  831. "\"Cobble\"}},{\"filter\": {\"itemType\": "
  832. "\"Cobble\"}}],\"output\": [{\"filter\": {\"itemType\": "
  833. "\"StoneTool\"}}]}]");
  834. std::cout << value->toString().getText() << "\n";
  835. Framework::JSON::Validator::JSONValidationResult* result
  836. = validator->validate(value);
  837. result->printInvalidInfo();
  838. Assert::IsTrue(
  839. !result->isValid(), L"Invalid Json was marked as valid");
  840. Framework::RCArray<Framework::JSON::Validator::JSONValidationResult>
  841. invalidParts;
  842. Framework::JSON::JSONValue* validValue
  843. = result->getValidPart(&invalidParts);
  844. Assert::IsTrue(invalidParts.getEintragAnzahl() > 0,
  845. L"No Invalid parts were returned for an invalid Json");
  846. for (Framework::JSON::Validator::JSONValidationResult* invalid :
  847. invalidParts)
  848. {
  849. invalid->printInvalidInfo();
  850. }
  851. result->release();
  852. Framework::JSON::JSONValue* expected
  853. = Framework::JSON::Parser::getValue(
  854. "[{\"type\": \"shaped\", \"group\": \"test\",\"width\": "
  855. "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
  856. "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
  857. "0,\"y\": 1,\"filter\": {\"itemType\": "
  858. "\"Cobble\"}}],\"output\": {\"itemType\": \"StoneTool\"}, "
  859. "\"outputCount\": 1},{\"type\": \"unordered\",\"group\": "
  860. "\"bla\", \"inputs\": [{\"count\": 1, \"filter\": "
  861. "{\"itemType\": \"Cobble\"}},{\"count\": 1, \"filter\": "
  862. "{\"itemType\": \"Cobble\"}}],\"output\": [{\"count\": 1, "
  863. "\"filter\": {\"itemType\": \"StoneTool\"}}]}]");
  864. Assert::IsTrue(JSONParserTests::isEqual(validValue, expected),
  865. L"getValidPart of invalid validation result does not match the "
  866. L"expected valid part");
  867. result = validator->validate(validValue);
  868. Assert::IsTrue(result->isValid(),
  869. L"Re validation of a value returned by getValidPart on a "
  870. L"validation result should never return an invalid validation "
  871. L"result");
  872. value->release();
  873. value = result->getValidPart(0);
  874. Assert::IsTrue(JSONParserTests::isEqual(validValue, value),
  875. L"getValidPart of a valid validation result should return the "
  876. L"validated value");
  877. value->release();
  878. validValue->release();
  879. expected->release();
  880. validator->release();
  881. }
  882. TEST_METHOD (RecursiveValidatorTest)
  883. {
  884. Framework::JSON::Validator::JSONValidator* validator
  885. = Framework::JSON::Validator::JSONValidator::buildForObject()
  886. ->setObjectReferenceId("TreeNode")
  887. ->withRequiredAttribute("value",
  888. Framework::JSON::Validator::JSONValidator::
  889. buildForString()
  890. ->whichCanBeNull()
  891. ->finishString())
  892. ->withRequiredAttribute("children",
  893. Framework::JSON::Validator::JSONValidator::
  894. buildForArray()
  895. ->whichIsOptional()
  896. ->addAcceptedTypeInArray(Framework::JSON::
  897. Validator::JSONValidator::
  898. buildForObjectReference(
  899. "TreeNode"))
  900. ->finishArray())
  901. ->finishObject();
  902. Framework::JSON::JSONObject* jArray
  903. = Framework::JSON::Parser::getValue(
  904. "{\"value\": \"1\", \"children\": [{\"value\": \"2\"}, "
  905. "{\"value\": \"3\", \"children\": [{\"value\": \"4\"}]}]}")
  906. ->asObject();
  907. Assert::IsTrue(validator->isValid(jArray),
  908. L"A valid json Object was marked as invalid by the validator");
  909. validator->release();
  910. }
  911. TEST_CLASS_CLEANUP(Cleanup)
  912. {
  913. std::cout.rdbuf(buf);
  914. }
  915. };
  916. OutputDebugStringBuf<char, std::char_traits<char>>
  917. JSONValidatorTests::charDebugOutput;
  918. std::streambuf* JSONValidatorTests::buf;
  919. } // namespace FrameworkTests