| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006 |
- #include "pch.h"
- #define NO_MAIN
- #include <DataValidator.h>
- #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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::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::AbstractType::NUMBER:
- return ((Framework::JSON::JSONNumber*)a)->getNumber()
- == ((Framework::JSON::JSONNumber*)b)->getNumber();
- case Framework::AbstractType::BOOLEAN:
- return ((Framework::JSON::JSONBool*)a)->getBool()
- == ((Framework::JSON::JSONBool*)b)->getBool();
- case Framework::AbstractType::STRING:
- return ((Framework::JSON::JSONString*)a)
- ->getString()
- .istGleich(((Framework::JSON::JSONString*)b)->getString());
- case Framework::AbstractType::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::AbstractType::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();
- }
- };
- #ifdef DEBUG
- 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 (ValidGreaterThenTest)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue("{\"test\": 0.001}");
- Framework::Validator::DataValidator* validator
- = Framework::Validator::DataValidator::buildForObject()
- ->withRequiredNumber("test")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->finishObject();
- Framework::RCArray<Framework::Validator::ValidationResult> removed;
- Assert::IsTrue(
- validator->isValid(value), L"Valid value is marked as invalid");
- Framework::JSON::JSONValue* result
- = validator->getValidParts(value, &removed);
- Assert::IsTrue(
- result->asObject()->zValue("test")->asNumber()->getNumber()
- == value->asObject()
- ->zValue("test")
- ->asNumber()
- ->getNumber(),
- L"Get Valid Parts made the valid value invalid");
- value->release();
- validator->release();
- result->release();
- }
- TEST_METHOD (ValidateEndsWith)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue("{\"test\": \"100%\"}");
- Framework::Validator::DataValidator* validator
- = Framework::Validator::DataValidator::buildForObject()
- ->withRequiredString("test")
- ->whichEndsWithMatch("%")
- ->finishString()
- ->finishObject();
- Assert::IsTrue(
- validator->isValid(value), L"Valid value is marked as invalid");
- value->release();
- validator->release();
- }
- TEST_METHOD (ValidateMultipleSimpleTypes)
- {
- Framework::JSON::JSONValue* value
- = Framework::JSON::Parser::getValue("{\"test\": \"100%\"}");
- Framework::Validator::DataValidator* validator
- = Framework::Validator::DataValidator::buildForObject()
- ->withRequiredNumber("test")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredString("test")
- ->whichEndsWithMatch("%")
- ->finishString()
- ->withRequiredString("test")
- ->withExactMatch("auto")
- ->finishString()
- ->finishObject();
- Assert::IsTrue(
- validator->isValid(value), L"Valid value is marked as invalid");
- value->release();
- validator->release();
- }
- TEST_METHOD (ValidTest)
- {
- Framework::Validator::DataValidator* validator
- = Framework::Validator::DataValidator::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::Validator::DataValidator* validator
- = Framework::Validator::DataValidator::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::Validator::ValidationResult* result
- = validator->validate(value);
- std::cout << result->getInvalidInfo();
- Assert::IsTrue(
- !result->isValid(), L"Invalid Json was marked as valid");
- Framework::RCArray<Framework::Validator::ValidationResult>
- invalidParts;
- Framework::JSON::JSONValue* validValue
- = result->getValidPart(&invalidParts);
- Assert::IsTrue(invalidParts.getEintragAnzahl() > 0,
- L"No Invalid parts were returned for an invalid Json");
- for (Framework::Validator::ValidationResult* invalid : invalidParts)
- {
- invalid->getInvalidInfo();
- }
- 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::Validator::DataValidator* validator
- = Framework::Validator::DataValidator::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::Validator::ValidationResult* result
- = validator->validate(value);
- std::cout << result->getInvalidInfo();
- Assert::IsTrue(
- !result->isValid(), L"Invalid Json was marked as valid");
- Framework::JSON::JSONValue* validValue = result->getValidPart(0);
- 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();
- Framework::RCArray<Framework::Validator::ValidationResult>
- invalidParts;
- value = result->getValidPart(&invalidParts);
- Assert::IsTrue(invalidParts.getEintragAnzahl() == 0,
- L"Invalid parts were returned for a valid validation result");
- 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::Validator::DataValidator* validator
- = Framework::Validator::DataValidator::buildForArray()
- ->typeSpecifiedByAttribute("type")
- ->removeInvalidEntries()
- ->addAcceptedTypeInArray(
- Framework::Validator::DataValidator::buildForObject()
- ->withRequiredString("type")
- ->withExactMatch("shaped")
- ->finishString()
- ->withRequiredString("group")
- ->withDefault("test")
- ->finishString()
- ->withRequiredNumber("width")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredNumber("height")
- ->whichIsGreaterThen(0)
- ->finishNumber()
- ->withRequiredAttribute("inputs",
- Framework::Validator::DataValidator::
- buildForArray()
- ->withDefault(
- new Framework::JSON::JSONArray())
- ->addAcceptedTypeInArray(
- Framework::Validator::
- DataValidator::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::Validator::DataValidator::buildForObject()
- ->withRequiredString("type")
- ->withExactMatch("unordered")
- ->finishString()
- ->withRequiredString("group")
- ->finishString()
- ->withRequiredAttribute("inputs",
- Framework::Validator::DataValidator::
- buildForArray()
- ->withDefault(
- new Framework::JSON::JSONArray())
- ->addAcceptedTypeInArray(Framework::
- Validator::DataValidator::
- buildForObject()
- ->withRequiredNumber(
- "count")
- ->withDefault(1)
- ->whichIsGreaterThen(
- 0)
- ->finishNumber()
- ->withRequiredObject(
- "filter")
- ->withRequiredString(
- "itemType")
- ->finishString()
- ->finishObject()
- ->finishObject())
- ->finishArray())
- ->withRequiredAttribute("output",
- Framework::Validator::DataValidator::
- buildForArray()
- ->addAcceptedTypeInArray(Framework::
- Validator::DataValidator::
- 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::Validator::ValidationResult* result
- = validator->validate(value);
- std::cout << result->getInvalidInfo();
- Assert::IsTrue(
- !result->isValid(), L"Invalid Json was marked as valid");
- Framework::RCArray<Framework::Validator::ValidationResult>
- invalidParts;
- Framework::JSON::JSONValue* validValue
- = result->getValidPart(&invalidParts);
- Assert::IsTrue(invalidParts.getEintragAnzahl() > 0,
- L"No Invalid parts were returned for an invalid Json");
- for (Framework::Validator::ValidationResult* invalid : invalidParts)
- {
- std::cout << result->getInvalidInfo();
- }
- 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(0);
- 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::Validator::DataValidator* validator
- = Framework::Validator::DataValidator::buildForObject()
- ->setReferenceId("TreeNode")
- ->withRequiredAttribute("value",
- Framework::Validator::DataValidator::buildForString()
- ->whichCanBeNull()
- ->finishString())
- ->withRequiredAttribute("children",
- Framework::Validator::DataValidator::buildForArray()
- ->whichIsOptional()
- ->addAcceptedTypeInArray(
- Framework::Validator::DataValidator::
- buildForReference("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;
- #endif
- } // namespace FrameworkTests
|