#include "unittest/tests/utilities/JSONParserTests.h" #include #include #include #include #include "utilities/JSONParser.h" #include "unittest/TestSuite.h" static void JSONParserTests_test_loadSimpleDocument() { JSONNode * node; char * jsonString; jsonString = "{}"; node = JSONParser_parse(jsonString, strlen(jsonString)); TestCase_assert(node != NULL, "JSONParser_parse returned NULL for valid string"); TestCase_assert(node->type == JSON_TYPE_OBJECT, "Expected JSON_TYPE_OBJECT (%d), but got %d", JSON_TYPE_OBJECT, node->type); TestCase_assert(node->key == NULL, "Expected key == NULL, but got %p", node->key); TestCase_assert(node->numberOfChildren == 0, "Expected numberOfChildren == 0, but got %d", node->numberOfChildren); JSONParser_freeNodeContents(node); free(node); jsonString = "[]"; node = JSONParser_parse(jsonString, strlen(jsonString)); TestCase_assert(node != NULL, "JSONParser_parse returned NULL for valid string"); TestCase_assert(node->type == JSON_TYPE_ARRAY, "Expected JSON_TYPE_ARRAY (%d), but got %d", JSON_TYPE_ARRAY, node->type); TestCase_assert(node->key == NULL, "Expected key == NULL, but got %p", node->key); TestCase_assert(node->numberOfChildren == 0, "Expected numberOfChildren == 0, but got %d", node->numberOfChildren); JSONParser_freeNodeContents(node); free(node); jsonString = "{\"foo\": \"bar\"}"; node = JSONParser_parse(jsonString, strlen(jsonString)); TestCase_assert(node != NULL, "JSONParser_parse returned NULL for valid string"); TestCase_assert(node->type == JSON_TYPE_OBJECT, "Expected type == JSON_TYPE_OBJECT (%d), but got %d", JSON_TYPE_OBJECT, node->type); TestCase_assert(node->key == NULL, "Expected key == NULL, but got %p", node->key); TestCase_assert(node->numberOfChildren == 1, "Expected numberOfChildren == 1, but got %d", node->numberOfChildren); TestCase_assert(node->children != NULL, "Expected children != NULL, but got NULL"); TestCase_assert(node->children[0].type == JSON_TYPE_STRING, "Expected type == JSON_TYPE_STRING (%d), but got %d", JSON_TYPE_STRING, node->children[0].type); TestCase_assert(node->children[0].key != NULL, "Expected key != NULL, but got NULL"); TestCase_assert(!strcmp(node->children[0].key, "foo"), "Expected key == \"foo\", but got %s", node->children[0].key); TestCase_assert(!strcmp(node->children[0].value.string, "bar"), "Expected value == \"bar\", but got %s", node->children[0].value.string); JSONParser_freeNodeContents(node); free(node); jsonString = "{\"number\": -6.245}"; node = JSONParser_parse(jsonString, strlen(jsonString)); TestCase_assert(node != NULL, "JSONParser_parse returned NULL for valid string"); TestCase_assert(node->type == JSON_TYPE_OBJECT, "Expected type == JSON_TYPE_OBJECT (%d), but got %d", JSON_TYPE_OBJECT, node->type); TestCase_assert(node->key == NULL, "Expected key == NULL, but got %p", node->key); TestCase_assert(node->numberOfChildren == 1, "Expected numberOfChildren == 1, but got %d", node->numberOfChildren); TestCase_assert(node->children != NULL, "Expected children != NULL, but got NULL"); TestCase_assert(node->children[0].type == JSON_TYPE_NUMBER, "Expected type == JSON_TYPE_NUMBER (%d), but got %d", JSON_TYPE_NUMBER, node->children[0].type); TestCase_assert(node->children[0].key != NULL, "Expected key != NULL, but got NULL"); TestCase_assert(!strcmp(node->children[0].key, "number"), "Expected key == \"number\", but got %s", node->children[0].key); TestCase_assert(fabs(node->children[0].value.number - -6.245) < 0.0001, "Expected value ~= -6.245, but got %f", node->children[0].value.number); JSONParser_freeNodeContents(node); free(node); jsonString = "{\"o rly?\": false, \"ya rly!\": true}"; node = JSONParser_parse(jsonString, strlen(jsonString)); TestCase_assert(node != NULL, "JSONParser_parse returned NULL for valid string"); TestCase_assert(node->type == JSON_TYPE_OBJECT, "Expected type == JSON_TYPE_OBJECT (%d), but got %d", JSON_TYPE_OBJECT, node->type); TestCase_assert(node->key == NULL, "Expected key == NULL, but got %p", node->key); TestCase_assert(node->numberOfChildren == 2, "Expected numberOfChildren == 2, but got %d", node->numberOfChildren); TestCase_assert(node->children != NULL, "Expected children != NULL, but got NULL"); TestCase_assert(node->children[0].type == JSON_TYPE_BOOLEAN, "Expected type == JSON_TYPE_BOOLEAN (%d), but got %d", JSON_TYPE_BOOLEAN, node->children[0].type); TestCase_assert(node->children[0].key != NULL, "Expected key != NULL, but got NULL"); TestCase_assert(!strcmp(node->children[0].key, "o rly?"), "Expected key == \"o rly?\", but got %s", node->children[0].key); TestCase_assert(!node->children[0].value.boolean, "Expected !value, but was true"); TestCase_assert(node->children[1].type == JSON_TYPE_BOOLEAN, "Expected type == JSON_TYPE_BOOLEAN (%d), but got %d", JSON_TYPE_BOOLEAN, node->children[1].type); TestCase_assert(node->children[1].key != NULL, "Expected key != NULL, but got NULL"); TestCase_assert(!strcmp(node->children[1].key, "ya rly!"), "Expected key == \"ya rly!\", but got %s", node->children[1].key); TestCase_assert(node->children[1].value.boolean, "Expected !!value, but was false"); JSONParser_freeNodeContents(node); free(node); jsonString = "[null]"; node = JSONParser_parse(jsonString, strlen(jsonString)); TestCase_assert(node != NULL, "JSONParser_parse returned NULL for valid string"); TestCase_assert(node->type == JSON_TYPE_ARRAY, "Expected type == JSON_TYPE_ARRAY (%d), but got %d", JSON_TYPE_ARRAY, node->type); TestCase_assert(node->key == NULL, "Expected key == NULL, but got %p", node->key); TestCase_assert(node->numberOfChildren == 1, "Expected numberOfChildren == 1, but got %d", node->numberOfChildren); TestCase_assert(node->children != NULL, "Expected children != NULL, but got NULL"); TestCase_assert(node->children[0].type == JSON_TYPE_NULL, "Expected type == JSON_TYPE_NULL (%d), but got %d", JSON_TYPE_NULL, node->children[0].type); TestCase_assert(node->children[0].key == NULL, "Expected key == NULL, but got %p", node->children[0].key); JSONParser_freeNodeContents(node); free(node); } static void JSONParserTests_test_loadComplexDocument() { JSONNode * node; char * jsonString; jsonString = " \t \n \t \r{\n\t\"foo\":{\t\t\"abcd\":null },\"bar\" :[ true, false, 123, [null, \"abcd\"], {\"efgh\": null}], \"baz\": \"\\\\\\\"\\n\\r\\t\\b\\f\"\n} \t\r"; node = JSONParser_parse(jsonString, strlen(jsonString)); TestCase_assert(node != NULL, "JSONParser_parse returned NULL for valid string"); TestCase_assert(node->type == JSON_TYPE_OBJECT, "Expected type == JSON_TYPE_OBJECT (%d), but got %d", JSON_TYPE_OBJECT, node->type); TestCase_assert(node->key == NULL, "Expected key == NULL, but got %p", node->key); TestCase_assert(node->numberOfChildren == 3, "Expected numberOfChildren == 3, but got %d", node->numberOfChildren); TestCase_assert(node->children != NULL, "Expected children != NULL, but got NULL"); TestCase_assert(node->children[0].type == JSON_TYPE_OBJECT, "Expected type == JSON_TYPE_OBJECT (%d), but got %d", JSON_TYPE_OBJECT, node->children[0].type); TestCase_assert(node->children[0].key != NULL, "Expected key != NULL, but got NULL"); TestCase_assert(!strcmp(node->children[0].key, "foo"), "Expected key == \"foo\", but got %s", node->children[0].key); TestCase_assert(node->children[0].numberOfChildren == 1, "Expected numberOfChildren == 1, but got %d", node->children[0].numberOfChildren); TestCase_assert(node->children[0].children != NULL, "Expected children != NULL, but got NULL"); TestCase_assert(node->children[0].children[0].type == JSON_TYPE_NULL, "Expected type == JSON_TYPE_NULL (%d), but got %d", JSON_TYPE_NULL, node->children[0].children[0].type); TestCase_assert(node->children[0].children[0].key != NULL, "Expected key != NULL, but got NULL"); TestCase_assert(!strcmp(node->children[0].children[0].key, "abcd"), "Expected key == \"abcd\", but got %s", node->children[0].children[0].key); TestCase_assert(node->children[1].type == JSON_TYPE_ARRAY, "Expected type == JSON_TYPE_ARRAY (%d), but got %d", JSON_TYPE_ARRAY, node->children[1].type); TestCase_assert(node->children[1].key != NULL, "Expected key != NULL, but got NULL"); TestCase_assert(!strcmp(node->children[1].key, "bar"), "Expected key == \"bar\", but got %s", node->children[1].key); TestCase_assert(node->children[1].numberOfChildren == 5, "Expected numberOfChildren == 5, but got %d", node->children[1].numberOfChildren); TestCase_assert(node->children[1].children != NULL, "Expected children != NULL, but got NULL"); TestCase_assert(node->children[1].children[0].type == JSON_TYPE_BOOLEAN, "Expected type == JSON_TYPE_BOOLEAN (%d), but got %d", JSON_TYPE_BOOLEAN, node->children[1].children[0].type); TestCase_assert(node->children[1].children[0].key == NULL, "Expected key == NULL, but got %p", node->children[1].children[0].key); TestCase_assert(node->children[1].children[0].value.boolean, "Expected !!value, but was false"); TestCase_assert(node->children[1].children[1].type == JSON_TYPE_BOOLEAN, "Expected type == JSON_TYPE_BOOLEAN (%d), but got %d", JSON_TYPE_BOOLEAN, node->children[1].children[1].type); TestCase_assert(node->children[1].children[1].key == NULL, "Expected key == NULL, but got %p", node->children[1].children[1].key); TestCase_assert(!node->children[1].children[1].value.boolean, "Expected !value, but was true"); TestCase_assert(node->children[1].children[2].type == JSON_TYPE_NUMBER, "Expected type == JSON_TYPE_NUMBER (%d), but got %d", JSON_TYPE_NUMBER, node->children[1].children[2].type); TestCase_assert(node->children[1].children[2].key == NULL, "Expected key == NULL, but got %p", node->children[1].children[2].key); TestCase_assert(fabs(node->children[1].children[2].value.number - 123) < 0.0001, "Expected value ~= 123, but got %f", node->children[1].children[2].value.number); TestCase_assert(node->children[1].children[3].type == JSON_TYPE_ARRAY, "Expected type == JSON_TYPE_ARRAY (%d), but got %d", JSON_TYPE_ARRAY, node->children[1].children[3].type); TestCase_assert(node->children[1].children[3].key == NULL, "Expected key == NULL, but got %p", node->children[1].children[3].key); TestCase_assert(node->children[1].children[3].numberOfChildren == 2, "Expected numberOfChildren == 2, but got %d", node->children[1].children[3].numberOfChildren); TestCase_assert(node->children[1].children[3].children != NULL, "Expected children != NULL, but got NULL"); TestCase_assert(node->children[1].children[3].children[0].type == JSON_TYPE_NULL, "Expected type == JSON_TYPE_NULL (%d), but got %d", JSON_TYPE_NULL, node->children[1].children[3].children[0].type); TestCase_assert(node->children[1].children[3].children[0].key == NULL, "Expected key == NULL, but got %p", node->children[1].children[3].children[0].key); TestCase_assert(node->children[1].children[3].children[1].type == JSON_TYPE_STRING, "Expected type == JSON_TYPE_STRING (%d), but got %d", JSON_TYPE_STRING, node->children[1].children[3].children[2].type); TestCase_assert(node->children[1].children[3].children[1].key == NULL, "Expected key == NULL, but got %p", node->children[1].children[3].children[1].key); TestCase_assert(!strcmp(node->children[1].children[3].children[1].value.string, "abcd"), "Expected value == \"abcd\", but got %s", node->children[1].children[3].children[1].value.string); TestCase_assert(node->children[1].children[4].type == JSON_TYPE_OBJECT, "Expected type == JSON_TYPE_OBJECT (%d), but got %d", JSON_TYPE_OBJECT, node->children[1].children[4].type); TestCase_assert(node->children[1].children[4].key == NULL, "Expected key == NULL, but got %p", node->children[1].children[4].children[1].key); TestCase_assert(node->children[1].children[4].numberOfChildren == 1, "Expected numberOfChildren == 1, but got %d", node->children[1].children[4].numberOfChildren); TestCase_assert(node->children[1].children[4].children != NULL, "Expected children != NULL, but got NULL"); TestCase_assert(node->children[1].children[4].children[0].type == JSON_TYPE_NULL, "Expected type == JSON_TYPE_NULL (%d), but got %d", JSON_TYPE_NULL, node->children[1].children[4].children[0].type); TestCase_assert(node->children[1].children[4].children[0].key != NULL, "Expected key != NULL, but got NULL"); TestCase_assert(!strcmp(node->children[1].children[4].children[0].key, "efgh"), "Expected key == \"efgh\", but got %s", node->children[1].children[4].children[0].key); TestCase_assert(node->children[2].type == JSON_TYPE_STRING, "Expected type == JSON_TYPE_STRING (%d), but got %d", JSON_TYPE_STRING, node->children[2].type); TestCase_assert(node->children[2].key != NULL, "Expected key != NULL, but got NULL"); TestCase_assert(!strcmp(node->children[2].key, "baz"), "Expected key == \"baz\", but got %s", node->children[2].key); TestCase_assert(!strcmp(node->children[2].value.string, "\\\"\n\r\t\b\f"), "Expected value == \"\\\"\n\r\t\b\f\", but got %s", node->children[2].value.string); JSONParser_freeNodeContents(node); free(node); } static void JSONParserTests_test_unicodeSupport() { JSONNode * node; char * jsonString; jsonString = "{\"\\u4E12\\uD834\\uDD10\\u213B\": \"u\\uD834\\uDD1Eu\\uFFFD\"}"; node = JSONParser_parse(jsonString, strlen(jsonString)); TestCase_assert(node != NULL, "JSONParser_parse returned NULL for valid string"); TestCase_assert(node->type == JSON_TYPE_OBJECT, "Expected type == JSON_TYPE_OBJECT (%d), but got %d", JSON_TYPE_OBJECT, node->type); TestCase_assert(node->key == NULL, "Expected key == NULL, but got %p", node->key); TestCase_assert(node->numberOfChildren == 1, "Expected numberOfChildren == 1, but got %d", node->numberOfChildren); TestCase_assert(node->children != NULL, "Expected children != NULL, but got NULL"); TestCase_assert(node->children[0].type == JSON_TYPE_STRING, "Expected type == JSON_TYPE_STRING (%d), but got %d", JSON_TYPE_STRING, node->children[0].type); TestCase_assert(node->children[0].key != NULL, "Expected key != NULL, but got NULL"); TestCase_assert(!strcmp(node->children[0].key, "\xE4\xB8\x92\xF0\x9D\x84\x90\xE2\x84\xBB"), "Expected key == \"\xE4\xB8\x92\xF0\x9D\x84\x90\xE2\x84\xBB\", but got \"%s\"", node->children[0].key); TestCase_assert(!strcmp(node->children[0].value.string, "u\xF0\x9D\x84\x9Eu\xEF\xBF\xBD"), "Expected value == \"u\xF0\x9D\x84\x9Eu\xEF\xBF\xBD\", but got \"%s\"", node->children[0].value.string); JSONParser_freeNodeContents(node); free(node); } static void JSONParserTests_test_invalidInput() { JSONNode * node; static char * invalidJSONStrings[] = { "", "{", "}", "{\"foo\":}", "{\"foo\": \"bar}", "{123: 456}", "{\"ab\\ucd\": \"\\u20\"}", "[\"\\uDFFF\"]" }; unsigned int invalidStringIndex; for (invalidStringIndex = 0; invalidStringIndex < sizeof(invalidJSONStrings) / sizeof(char *); invalidStringIndex++) { node = JSONParser_parse(invalidJSONStrings[invalidStringIndex], strlen(invalidJSONStrings[invalidStringIndex])); TestCase_assert(node == NULL, "JSONParser_parse returned non-NULL (%p) for invalid string %d (\"%s\")", node, invalidStringIndex, invalidJSONStrings[invalidStringIndex]); } } TestSuite * JSONParserTests_getTestSuite() { static TestSuite suite; static void (* testCases[])() = {JSONParserTests_test_loadSimpleDocument, JSONParserTests_test_loadComplexDocument, JSONParserTests_test_unicodeSupport, JSONParserTests_test_invalidInput}; suite.description = "JSONParserTests"; suite.numberOfTestCases = sizeof(testCases) / sizeof(void (*)()); suite.testCases = testCases; return &suite; }