123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931 |
- #include "pch.h"
- #define NO_MAIN
- #include <Json.h>
- #include <main.h>
- #include "CppUnitTest.h"
- using namespace Microsoft::VisualStudio::CppUnitTestFramework;
- namespace FrameworkTests
- {
- TEST_CLASS (JSONParserTests)
- {
- public:
- TEST_METHOD (NullTest)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue("null");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('null') should not return "
- L"0");
- Assert::IsTrue(value->getType() == Framework::JSON::JSONType::NULL_,
- L"Framework::JSON::Parser::getValue('null') should return a "
- L"json null value");
- value->release();
- }
- TEST_METHOD (BooleanTest)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue("false");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('false') should not return "
- L"0");
- Assert::IsTrue(
- value->getType() == Framework::JSON::JSONType::BOOLEAN,
- L"Framework::JSON::Parser::getValue('false') should return a "
- L"boolean");
- Assert::IsTrue(
- ((Framework::JSON::JSONBool*)value)->getBool() == false,
- L"Framework::JSON::Parser::getValue('false') should return a "
- L"boolean with value false");
- value->release();
- value = Framework::JSON::Parser::getValue("true");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('true') should not return "
- L"0");
- Assert::IsTrue(
- value->getType() == Framework::JSON::JSONType::BOOLEAN,
- L"Framework::JSON::Parser::getValue('true') should return a "
- L"boolean");
- Assert::IsTrue(
- ((Framework::JSON::JSONBool*)value)->getBool() == true,
- L"Framework::JSON::Parser::getValue('true') should return a "
- L"boolean with value true");
- value->release();
- }
- TEST_METHOD (StringTest)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue("\"test\"");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('\"test\"') should not "
- L"return 0");
- Assert::IsTrue(
- value->getType() == Framework::JSON::JSONType::STRING,
- L"Framework::JSON::Parser::getValue('\"test\"') should return "
- L"a string");
- Assert::IsTrue(((Framework::JSON::JSONString*)value)
- ->getString()
- .istGleich("test"),
- L"Framework::JSON::Parser::getValue('\"test\"') should return "
- L"a string with value 'test'");
- value->release();
- value = Framework::JSON::Parser::getValue("\"\"");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('\"\"') should not return "
- L"0");
- Assert::IsTrue(
- value->getType() == Framework::JSON::JSONType::STRING,
- L"Framework::JSON::Parser::getValue('\"\"') should return a "
- L"string");
- Assert::IsTrue(((Framework::JSON::JSONString*)value)
- ->getString()
- .istGleich(""),
- L"Framework::JSON::Parser::getValue('\"\"') should return a "
- L"string with value ''");
- value->release();
- }
- TEST_METHOD (NumberTest)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue("0");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('0') should not return 0");
- Assert::IsTrue(
- value->getType() == Framework::JSON::JSONType::NUMBER,
- L"Framework::JSON::Parser::getValue('0') should return a "
- L"number");
- Assert::IsTrue(
- ((Framework::JSON::JSONNumber*)value)->getNumber() == 0.0,
- L"Framework::JSON::Parser::getValue('0') should return a "
- L"number with value '0'");
- value->release();
- value = Framework::JSON::Parser::getValue("1.5");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('1.5') should not return "
- L"0");
- Assert::IsTrue(
- value->getType() == Framework::JSON::JSONType::NUMBER,
- L"Framework::JSON::Parser::getValue('1.5') should return a "
- L"number");
- Assert::IsTrue(
- ((Framework::JSON::JSONNumber*)value)->getNumber() == 1.5,
- L"Framework::JSON::Parser::getValue('1.5') should return a "
- L"number with value '1.5'");
- value->release();
- value = Framework::JSON::Parser::getValue("-1.5");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('-1.5') should not return "
- L"0");
- Assert::IsTrue(
- value->getType() == Framework::JSON::JSONType::NUMBER,
- L"Framework::JSON::Parser::getValue('-1.5') should return a "
- L"number");
- Assert::IsTrue(
- ((Framework::JSON::JSONNumber*)value)->getNumber() == -1.5,
- L"Framework::JSON::Parser::getValue('-1.5') should return a "
- L"number with value '-1.5'");
- value->release();
- value = Framework::JSON::Parser::getValue("-5.0");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('-5.0') should not return "
- L"0");
- Assert::IsTrue(
- value->getType() == Framework::JSON::JSONType::NUMBER,
- L"Framework::JSON::Parser::getValue('-5.0') should return a "
- L"number");
- Assert::IsTrue(
- ((Framework::JSON::JSONNumber*)value)->getNumber() == -5.0,
- L"Framework::JSON::Parser::getValue('-5.0') should return a "
- L"number with value '-5.0'");
- value->release();
- }
- TEST_METHOD (ArrayTest)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue("[]");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('[]') should not return 0");
- Assert::IsTrue(value->getType() == Framework::JSON::JSONType::ARRAY,
- L"Framework::JSON::Parser::getValue('[]') should return an "
- L"array");
- Assert::IsTrue(
- ((Framework::JSON::JSONArray*)value)->getLength() == 0,
- L"Framework::JSON::Parser::getValue('[]') should return an "
- L"array with length 0");
- value->release();
- value = Framework::JSON::Parser::getValue(
- " \t[ \r\n\tnull , \r\n\t 1,true , \"\" ] ");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
- L"should not return 0");
- Assert::IsTrue(value->getType() == Framework::JSON::JSONType::ARRAY,
- L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
- L"should return an array");
- Assert::IsTrue(
- ((Framework::JSON::JSONArray*)value)->getLength() == 4,
- L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
- L"should return an array with length 4");
- Assert::IsTrue(
- ((Framework::JSON::JSONArray*)value)
- ->isValueOfType(0, Framework::JSON::JSONType::NULL_),
- L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
- L"should contain null at index 0");
- Assert::IsTrue(
- ((Framework::JSON::JSONArray*)value)
- ->isValueOfType(1, Framework::JSON::JSONType::NUMBER),
- L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
- L"should contain a number at index 1");
- Assert::IsTrue(
- ((Framework::JSON::JSONArray*)value)
- ->isValueOfType(2, Framework::JSON::JSONType::BOOLEAN),
- L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
- L"should contain a boolean at index 2");
- Assert::IsTrue(
- ((Framework::JSON::JSONArray*)value)
- ->isValueOfType(3, Framework::JSON::JSONType::STRING),
- L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
- L"should contain a boolean at index 3");
- value->release();
- }
- TEST_METHOD (MultipleArrayTest)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue("[[1],2,[[]]]");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should not "
- L"return 0");
- Assert::IsTrue(value->getType() == Framework::JSON::JSONType::ARRAY,
- L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should "
- L"return an array");
- Assert::IsTrue(
- ((Framework::JSON::JSONArray*)value)->getLength() == 3,
- L"Framework::JSON::Parser::getValue('[[1],2,[[]]]') should "
- L"return an array with length 3");
- Assert::IsTrue(
- ((Framework::JSON::JSONArray*)value)
- ->isValueOfType(0, Framework::JSON::JSONType::ARRAY),
- L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
- L"should contain an array at index 0");
- Assert::IsTrue(
- ((Framework::JSON::JSONArray*)value)
- ->isValueOfType(1, Framework::JSON::JSONType::NUMBER),
- L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
- L"should contain a number at index 1");
- Assert::IsTrue(
- ((Framework::JSON::JSONArray*)value)
- ->isValueOfType(2, Framework::JSON::JSONType::ARRAY),
- L"Framework::JSON::Parser::getValue('[null, 1, true, \"\"]') "
- L"should contain an array at index 2");
- value->release();
- }
- TEST_METHOD (ObjectTest)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue("{\" \": []}");
- Assert::IsTrue(value != 0,
- L"Framework::JSON::Parser::getValue('{\"\": []}') should not "
- L"return 0");
- Assert::IsTrue(
- value->getType() == Framework::JSON::JSONType::OBJECT,
- L"Framework::JSON::Parser::getValue('{\" \": []}') should "
- L"return an object");
- Assert::IsTrue(
- ((Framework::JSON::JSONObject*)value)->getFieldCount() == 1,
- L"Framework::JSON::Parser::getValue('{\" \": []}') should "
- L"return an object with one attribute");
- Assert::IsTrue(
- ((Framework::JSON::JSONObject*)value)
- ->isValueOfType(" ", Framework::JSON::JSONType::ARRAY),
- L"Framework::JSON::Parser::getValue('{\" \": []}') should "
- L"contain an array at attribute ' '");
- value->release();
- }
- TEST_METHOD (ToStringTest)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue(
- "{\" \": [1, true, false, 0.0, {}], \"t\": null}");
- Framework::JSON::JSONValue* value2
- = Framework::JSON::Parser::getValue(value->toString());
- Assert::IsTrue(isEqual(value, value2),
- L"Framework::JSON::Parser::getValue(value.toString()) should "
- L"return a json value eqal to value");
- value->release();
- value2->release();
- }
- static bool isEqual(
- Framework::JSON::JSONValue * a, Framework::JSON::JSONValue * b)
- {
- if (a->getType() != b->getType()) return 0;
- switch (a->getType())
- {
- case Framework::JSON::JSONType::NUMBER:
- return ((Framework::JSON::JSONNumber*)a)->getNumber()
- == ((Framework::JSON::JSONNumber*)b)->getNumber();
- case Framework::JSON::JSONType::BOOLEAN:
- return ((Framework::JSON::JSONBool*)a)->getBool()
- == ((Framework::JSON::JSONBool*)b)->getBool();
- case Framework::JSON::JSONType::STRING:
- return ((Framework::JSON::JSONString*)a)
- ->getString()
- .istGleich(((Framework::JSON::JSONString*)b)->getString());
- case Framework::JSON::JSONType::ARRAY:
- {
- Framework::JSON::JSONArray* arrayA
- = (Framework::JSON::JSONArray*)a;
- Framework::JSON::JSONArray* arrayB
- = (Framework::JSON::JSONArray*)b;
- if (arrayA->getLength() != arrayB->getLength()) return 0;
- for (int i = 0; i < arrayA->getLength(); i++)
- {
- Framework::JSON::JSONValue* entryA
- = arrayA->getValue(i);
- Framework::JSON::JSONValue* entryB
- = arrayB->getValue(i);
- bool eq = isEqual(entryA, entryB);
- entryA->release();
- entryB->release();
- if (!eq) return 0;
- }
- return 1;
- }
- case Framework::JSON::JSONType::OBJECT:
- {
- Framework::JSON::JSONObject* objA
- = (Framework::JSON::JSONObject*)a;
- Framework::JSON::JSONObject* objB
- = (Framework::JSON::JSONObject*)b;
- if (objA->getFieldCount() != objB->getFieldCount())
- return 0;
- auto oaf = objA->getFields();
- while (oaf)
- {
- if (!objB->hasValue(oaf)) return 0;
- Framework::JSON::JSONValue* entryA
- = objA->getValue(oaf);
- Framework::JSON::JSONValue* entryB
- = objB->getValue(oaf);
- bool eq = isEqual(entryA, entryB);
- entryA->release();
- entryB->release();
- if (!eq) return 0;
- oaf++;
- }
- return 1;
- }
- }
- return 1;
- }
- TEST_METHOD (ToArrayTest)
- {
- Framework::JSON::JSONArray* jArray
- = Framework::JSON::Parser::getValue("[1,2,3,4,5,6,7,8,9,10]")
- ->asArray();
- Framework::Array<int>* numberArray
- = jArray->toArray<int>([](Framework::JSON::JSONValue& v) {
- return (int)v.asNumber()->getNumber();
- });
- Assert::IsTrue(numberArray->getEintragAnzahl() == 10,
- L"Array hat die falsche Anzahl an elementen");
- Assert::IsTrue(numberArray->get(2) == 3,
- L"Array hat mindestens ein falsches element");
- Assert::IsTrue(numberArray->get(7) == 8,
- L"Array hat mindestens ein falsches element");
- numberArray->release();
- numberArray = jArray->toArray<int>(
- [](Framework::JSON::JSONValue& v) {
- return (int)v.asNumber()->getNumber() % 2 == 0;
- },
- [](Framework::JSON::JSONValue& v) {
- return (int)v.asNumber()->getNumber();
- });
- Assert::IsTrue(numberArray->get(0) == 2,
- L"Array hat mindestens ein falsches element");
- Assert::IsTrue(numberArray->get(3) == 8,
- L"Array hat mindestens ein falsches element");
- jArray->release();
- }
- TEST_METHOD (ToRCArrayTest)
- {
- Framework::JSON::JSONArray* jArray
- = Framework::JSON::Parser::getValue(
- "[\"1\",\"2\",\"3\",\"4\",\"5\"]")
- ->asArray();
- Framework::RCArray<Framework::Text>* numberArray
- = jArray->toRCArray<Framework::Text>(
- [](Framework::JSON::JSONValue& v) {
- return new Framework::Text(v.asString()->getString());
- });
- Assert::IsTrue(numberArray->getEintragAnzahl() == 5,
- L"Array hat die falsche Anzahl an elementen");
- Assert::IsTrue(numberArray->z(1)->istGleich("2"),
- L"Array hat mindestens ein falsches element");
- Assert::IsTrue(numberArray->z(4)->istGleich("5"),
- L"Array hat mindestens ein falsches element");
- numberArray->release();
- numberArray = jArray->toRCArray<Framework::Text>(
- [](Framework::JSON::JSONValue& v) {
- return (int)v.asString()->getString() % 2 == 0;
- },
- [](Framework::JSON::JSONValue& v) {
- return new Framework::Text(v.asString()->getString());
- });
- Assert::IsTrue(numberArray->z(0)->istGleich("2"),
- L"Array hat mindestens ein falsches element");
- Assert::IsTrue(numberArray->z(1)->istGleich("4"),
- L"Array hat mindestens ein falsches element");
- jArray->release();
- }
- class TestObject
- {
- public:
- Framework::Text name;
- Framework::Text value;
- };
- TEST_METHOD (ParseObjectTest)
- {
- Framework::JSON::JSONObject* jObj
- = Framework::JSON::Parser::getValue(
- "{\"name\": \"test\", \"value\": \"1234\"}")
- ->asObject();
- TestObject* obj = jObj->parseTo<TestObject>(new TestObject(),
- [](TestObject* obj,
- Framework::Text attrName,
- Framework::JSON::JSONValue& v) {
- if (attrName.istGleich("name"))
- {
- obj->name = v.asString()->getString();
- }
- else
- {
- obj->value = v.asString()->getString();
- }
- });
- Assert::IsTrue(
- obj->name.istGleich("test"), L"Feld hat falschen wert");
- Assert::IsTrue(
- obj->value.istGleich("1234"), L"Feld hat falschen wert");
- delete obj;
- jObj->release();
- }
- TEST_METHOD (FromArrayTest)
- {
- Framework::Array<int> arr;
- arr.add(1);
- arr.add(2);
- arr.add(3);
- arr.add(4);
- Framework::JSON::JSONArray* jArray
- = Framework::JSON::JSONArray::fromArray<int>(arr,
- [](int v) { return new Framework::JSON::JSONNumber(v); });
- Assert::IsTrue(
- jArray->getLength() == 4, L"Array hat falsche länge");
- Framework::JSON::JSONNumber* n = jArray->getValue(1)->asNumber();
- Assert::IsTrue(n->getNumber() == 2,
- L"Array hat mindestens einen falschen Wert");
- n->release();
- jArray->release();
- Framework::RCArray<Framework::Text> rcArr;
- rcArr.add(new Framework::Text("1"));
- rcArr.add(new Framework::Text("2"));
- rcArr.add(new Framework::Text("3"));
- rcArr.add(new Framework::Text("4"));
- jArray = Framework::JSON::JSONArray::fromRCArray<Framework::Text>(
- rcArr, [](Framework::Text& v) {
- return new Framework::JSON::JSONString(v);
- });
- Assert::IsTrue(
- jArray->getLength() == 4, L"Array hat falsche länge");
- Framework::JSON::JSONString* s = jArray->getValue(2)->asString();
- Assert::IsTrue(s->getString().istGleich("3"),
- L"Array hat mindestens einen falschen Wert");
- s->release();
- jArray->release();
- }
- };
- TEST_CLASS (JSONValidatorTests)
- {
- private:
- static OutputDebugStringBuf<char, std::char_traits<char>>
- charDebugOutput;
- static std::streambuf* buf;
- public:
- TEST_CLASS_INITIALIZE(Init)
- {
- buf = std::cout.rdbuf();
- std::cout.rdbuf(&charDebugOutput);
- }
- TEST_METHOD (ValidTest)
- {
- Framework::JSON::Validator::JSONValidator* validator
- = Framework::JSON::Validator::JSONValidator::buildForArray()
- ->addAcceptedObjectInArray()
- ->withRequiredNumber("x")
- ->whichIsLessThen(5)
- ->finishNumber()
- ->withRequiredBool("bla")
- ->whichIsOptional()
- ->withDefault(true)
- ->finishBool()
- ->finishObject()
- ->finishArray();
- Framework::JSON::JSONArray* jArray
- = Framework::JSON::Parser::getValue(
- "[{\"x\": 4, \"bla\": false}]")
- ->asArray();
- Assert::IsTrue(validator->isValid(jArray),
- L"A valid json Array was marked as invalid by the validator");
- validator->release();
- }
- TEST_METHOD (ComplexTest)
- {
- Framework::JSON::Validator::JSONValidator* validator
- = Framework::JSON::Validator::JSONValidator::buildForArray()
- ->typeSpecifiedByAttribute("type")
- ->addAcceptedObjectInArray()
- ->withRequiredString("type")
- ->withExactMatch("shaped")
- ->finishString()
- ->withRequiredString("group")
- ->finishString()
- ->withRequiredNumber("width")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredNumber("height")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredArray("inputs")
- ->addAcceptedObjectInArray()
- ->withRequiredNumber("x")
- ->whichIsGreaterOrEqual(0)
- ->finishNumber()
- ->withRequiredNumber("y")
- ->whichIsGreaterOrEqual(0)
- ->finishNumber()
- ->withRequiredObject("filter")
- ->withRequiredString("itemType")
- ->finishString()
- ->finishObject()
- ->finishObject()
- ->finishArray()
- ->withRequiredObject("output")
- ->withRequiredString("itemType")
- ->finishString()
- ->finishObject()
- ->withRequiredNumber("outputCount")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->finishObject()
- ->addAcceptedObjectInArray()
- ->withRequiredString("type")
- ->withExactMatch("unordered")
- ->finishString()
- ->withRequiredString("group")
- ->finishString()
- ->withRequiredArray("inputs")
- ->addAcceptedObjectInArray()
- ->withRequiredNumber("count")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredObject("filter")
- ->withRequiredString("itemType")
- ->finishString()
- ->finishObject()
- ->finishObject()
- ->finishArray()
- ->withRequiredArray("output")
- ->addAcceptedObjectInArray()
- ->withRequiredObject("filter")
- ->withRequiredString("itemType")
- ->finishString()
- ->finishObject()
- ->withRequiredNumber("count")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->finishObject()
- ->finishArray()
- ->finishObject()
- ->finishArray();
- std::cout << validator->zConstraints()->toString().getText()
- << "\n";
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue(
- "[{\"type\": \"shaped\",\"group\": "
- "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
- "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
- "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
- "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
- "\"StoneTool\"},\"outputCount\": 1},{\"type\": "
- "\"shaped\",\"group\": \"inventory\",\"width\": "
- "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
- "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
- "0,\"y\": 1,\"filter\": {\"itemType\": "
- "\"Cobble\"}}],\"output\": {\"itemType\": "
- "\"StoneTool\"},\"outputCount\": 1}]");
- std::cout << value->toString().getText() << "\n";
- Framework::JSON::Validator::JSONValidationResult* result
- = validator->validate(value);
- result->printInvalidInfo();
- Assert::IsTrue(
- !result->isValid(), L"Invalid Json was marked as valid");
- Framework::JSON::JSONValue* validValue = result->getValidPart();
- result->release();
- Assert::IsTrue(validValue == 0,
- L"getValidPart of invalid validation result without "
- L"removeInvalidEntries or default values used in validation "
- L"should return 0");
- value->release();
- validator->release();
- }
- TEST_METHOD (ComplexRemoveInvalidTest)
- {
- Framework::JSON::Validator::JSONValidator* validator
- = Framework::JSON::Validator::JSONValidator::buildForArray()
- ->removeInvalidEntries()
- ->typeSpecifiedByAttribute("type")
- ->addAcceptedObjectInArray()
- ->withRequiredString("type")
- ->withExactMatch("shaped")
- ->finishString()
- ->withRequiredString("group")
- ->finishString()
- ->withRequiredNumber("width")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredNumber("height")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredArray("inputs")
- ->addAcceptedObjectInArray()
- ->withRequiredNumber("x")
- ->whichIsGreaterOrEqual(0)
- ->finishNumber()
- ->withRequiredNumber("y")
- ->whichIsGreaterOrEqual(0)
- ->finishNumber()
- ->withRequiredObject("filter")
- ->withRequiredString("itemType")
- ->finishString()
- ->finishObject()
- ->finishObject()
- ->finishArray()
- ->withRequiredObject("output")
- ->withRequiredString("itemType")
- ->finishString()
- ->finishObject()
- ->withRequiredNumber("outputCount")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->finishObject()
- ->addAcceptedObjectInArray()
- ->withRequiredString("type")
- ->withExactMatch("unordered")
- ->finishString()
- ->withRequiredString("group")
- ->finishString()
- ->withRequiredArray("inputs")
- ->addAcceptedObjectInArray()
- ->withRequiredNumber("count")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredObject("filter")
- ->withRequiredString("itemType")
- ->finishString()
- ->finishObject()
- ->finishObject()
- ->finishArray()
- ->withRequiredArray("output")
- ->addAcceptedObjectInArray()
- ->withRequiredObject("filter")
- ->withRequiredString("itemType")
- ->finishString()
- ->finishObject()
- ->withRequiredNumber("count")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->finishObject()
- ->finishArray()
- ->finishObject()
- ->finishArray();
- std::cout << validator->zConstraints()->toString().getText()
- << "\n";
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue(
- "[{\"type\": \"shaped\",\"group\": "
- "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
- "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
- "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
- "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
- "\"StoneTool\"},\"outputCount\": 1},{\"type\": "
- "\"shaped\",\"group\": \"inventory\",\"width\": "
- "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
- "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
- "0,\"y\": 1,\"filter\": {\"itemType\": "
- "\"Cobble\"}}],\"output\": {\"itemType\": "
- "\"StoneTool\"},\"outputCount\": 1}]");
- std::cout << value->toString().getText() << "\n";
- Framework::JSON::Validator::JSONValidationResult* result
- = validator->validate(value);
- result->printInvalidInfo();
- Assert::IsTrue(
- !result->isValid(), L"Invalid Json was marked as valid");
- Framework::JSON::JSONValue* validValue = result->getValidPart();
- result->release();
- Framework::JSON::JSONValue* expected
- = Framework::JSON::Parser::getValue(
- "[{\"type\": \"shaped\",\"group\": "
- "\"inventory\",\"width\": 1,\"height\": 2,\"inputs\": "
- "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
- "\"Cobble\"}},{\"x\": 0,\"y\": 1,\"filter\": "
- "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
- "\"StoneTool\"},\"outputCount\": 1}]");
- Assert::IsTrue(JSONParserTests::isEqual(validValue, expected),
- L"getValidPart of invalid validation result does not match the "
- L"expected valid part");
- result = validator->validate(validValue);
- Assert::IsTrue(result->isValid(),
- L"Re validation of a value returned by getValidPart on a "
- L"validation result should never return an invalid validation "
- L"result");
- value->release();
- value = result->getValidPart();
- Assert::IsTrue(JSONParserTests::isEqual(validValue, value),
- L"getValidPart of a valid validation result should return the "
- L"validated value");
- value->release();
- validValue->release();
- expected->release();
- validator->release();
- }
- TEST_METHOD (DefaultValuesTest)
- {
- Framework::JSON::Validator::JSONValidator* validator
- = Framework::JSON::Validator::JSONValidator::buildForArray()
- ->typeSpecifiedByAttribute("type")
- ->removeInvalidEntries()
- ->addAcceptedTypeInArray(
- Framework::JSON::Validator::JSONValidator::buildForObject()
- ->withRequiredString("type")
- ->withExactMatch("shaped")
- ->finishString()
- ->withRequiredString("group")
- ->withDefault("test")
- ->finishString()
- ->withRequiredNumber("width")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredNumber("height")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredAttribute("inputs",
- Framework::JSON::Validator::JSONValidator::
- buildForArray()
- ->withDefault(
- new Framework::JSON::JSONArray())
- ->addAcceptedTypeInArray(
- Framework::JSON::Validator::
- JSONValidator::buildForObject()
- ->withRequiredNumber("x")
- ->whichIsGreaterOrEqual(0)
- ->finishNumber()
- ->withRequiredNumber("y")
- ->whichIsGreaterOrEqual(0)
- ->finishNumber()
- ->withRequiredObject(
- "filter")
- ->withRequiredString(
- "itemType")
- ->finishString()
- ->finishObject()
- ->finishObject())
- ->finishArray())
- ->withRequiredObject("output")
- ->withRequiredString("itemType")
- ->finishString()
- ->finishObject()
- ->withRequiredNumber("outputCount")
- ->withDefault(1)
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->finishObject())
- ->addAcceptedTypeInArray(
- Framework::JSON::Validator::JSONValidator::buildForObject()
- ->withRequiredString("type")
- ->withExactMatch("unordered")
- ->finishString()
- ->withRequiredString("group")
- ->finishString()
- ->withRequiredAttribute("inputs",
- Framework::JSON::Validator::JSONValidator::
- buildForArray()
- ->withDefault(
- new Framework::JSON::JSONArray())
- ->addAcceptedTypeInArray(
- Framework::JSON::Validator::
- JSONValidator::
- buildForObject()
- ->withRequiredNumber(
- "count")
- ->withDefault(1)
- ->whichIsGreaterThen(
- 0)
- ->finishNumber()
- ->withRequiredObject(
- "filter")
- ->withRequiredString(
- "itemType")
- ->finishString()
- ->finishObject()
- ->finishObject())
- ->finishArray())
- ->withRequiredAttribute("output",
- Framework::JSON::Validator::JSONValidator::
- buildForArray()
- ->addAcceptedTypeInArray(
- Framework::JSON::Validator::
- JSONValidator::
- buildForObject()
- ->withRequiredObject(
- "filter")
- ->withRequiredString(
- "itemType")
- ->finishString()
- ->finishObject()
- ->withRequiredNumber(
- "count")
- ->withDefault(1)
- ->whichIsGreaterThen(
- 0)
- ->finishNumber()
- ->finishObject())
- ->finishArray())
- ->finishObject())
- ->finishArray();
- std::cout << validator->zConstraints()->toString().getText()
- << "\n";
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue(
- "[{\"type\": \"shaped\",\"width\": 1,\"height\": "
- "2,\"inputs\": [{\"x\": 0,\"y\": 0,\"filter\": "
- "{\"itemType\": \"Cobble\"}},{\"x\": 0,\"y\": "
- "1,\"filter\": {\"itemType\": \"Cobble\"}}],\"output\": "
- "{\"itemType\": \"StoneTool\"}},{\"type\": "
- "\"shaped\",\"width\": 1,\"height\": 2,\"inputs\": "
- "[{\"x\": 0,\"y\": 0,\"filter\": {\"itemType\": "
- "\"Cobble\"}},{\"x\": 0,\"y\": -1,\"filter\": "
- "{\"itemType\": \"Cobble\"}}],\"output\": {\"itemType\": "
- "\"StoneTool\"}},{\"type\": \"unordered\",\"group\": "
- "\"bla\", \"inputs\": [{\"filter\": {\"itemType\": "
- "\"Cobble\"}},{\"filter\": {\"itemType\": "
- "\"Cobble\"}}],\"output\": [{\"filter\": {\"itemType\": "
- "\"StoneTool\"}}]}]");
- std::cout << value->toString().getText() << "\n";
- Framework::JSON::Validator::JSONValidationResult* result
- = validator->validate(value);
- result->printInvalidInfo();
- Assert::IsTrue(
- !result->isValid(), L"Invalid Json was marked as valid");
- Framework::JSON::JSONValue* validValue = result->getValidPart();
- result->release();
- Framework::JSON::JSONValue* expected
- = Framework::JSON::Parser::getValue(
- "[{\"type\": \"shaped\", \"group\": \"test\",\"width\": "
- "1,\"height\": 2,\"inputs\": [{\"x\": 0,\"y\": "
- "0,\"filter\": {\"itemType\": \"Cobble\"}},{\"x\": "
- "0,\"y\": 1,\"filter\": {\"itemType\": "
- "\"Cobble\"}}],\"output\": {\"itemType\": \"StoneTool\"}, "
- "\"outputCount\": 1},{\"type\": \"unordered\",\"group\": "
- "\"bla\", \"inputs\": [{\"count\": 1, \"filter\": "
- "{\"itemType\": \"Cobble\"}},{\"count\": 1, \"filter\": "
- "{\"itemType\": \"Cobble\"}}],\"output\": [{\"count\": 1, "
- "\"filter\": {\"itemType\": \"StoneTool\"}}]}]");
- Assert::IsTrue(JSONParserTests::isEqual(validValue, expected),
- L"getValidPart of invalid validation result does not match the "
- L"expected valid part");
- result = validator->validate(validValue);
- Assert::IsTrue(result->isValid(),
- L"Re validation of a value returned by getValidPart on a "
- L"validation result should never return an invalid validation "
- L"result");
- value->release();
- value = result->getValidPart();
- Assert::IsTrue(JSONParserTests::isEqual(validValue, value),
- L"getValidPart of a valid validation result should return the "
- L"validated value");
- value->release();
- validValue->release();
- expected->release();
- validator->release();
- }
- TEST_METHOD (RecursiveValidatorTest)
- {
- Framework::JSON::Validator::JSONValidator* validator
- = Framework::JSON::Validator::JSONValidator::buildForObject()
- ->setObjectReferenceId("TreeNode")
- ->withRequiredAttribute("value",
- Framework::JSON::Validator::JSONValidator::
- buildForString()
- ->whichCanBeNull()
- ->finishString())
- ->withRequiredAttribute("children",
- Framework::JSON::Validator::JSONValidator::
- buildForArray()
- ->whichIsOptional()
- ->addAcceptedTypeInArray(Framework::JSON::
- Validator::JSONValidator::
- buildForObjectReference(
- "TreeNode"))
- ->finishArray())
- ->finishObject();
- Framework::JSON::JSONObject* jArray
- = Framework::JSON::Parser::getValue(
- "{\"value\": \"1\", \"children\": [{\"value\": \"2\"}, "
- "{\"value\": \"3\", \"children\": [{\"value\": \"4\"}]}]}")
- ->asObject();
- Assert::IsTrue(validator->isValid(jArray),
- L"A valid json Object was marked as invalid by the validator");
- validator->release();
- }
- TEST_CLASS_CLEANUP(Cleanup)
- {
- std::cout.rdbuf(buf);
- }
- };
- OutputDebugStringBuf<char, std::char_traits<char>>
- JSONValidatorTests::charDebugOutput;
- std::streambuf* JSONValidatorTests::buf;
- } // namespace FrameworkTests
|