void testArray() { JSONParser *parser; JSONAtom *atom; JSONArray *arrayAtom; tryArrayOk("[]", [](const JSONArray &arrayAtom) { assertIntEqual(arrayAtom.size(), 0); }); tryArrayOk("[0]", [](const JSONArray &arrayAtom) { assertIntEqual(arrayAtom.size(), 1); assertNotNull(arrayAtom[0]); assertIntEqual(arrayAtom[0].asInt(), 0); }); tryArrayOk("[0, true]", [](const JSONArray &arrayAtom) { assertIntEqual(arrayAtom.size(), 2); assertNotNull(arrayAtom[0]); assertIntEqual(arrayAtom[0].asInt(), 0); assertNotNull(arrayAtom[1]); assertTrue(arrayAtom[1].asBool()); }); tryArrayFail("["); tryArrayFail("[,]"); tryArrayFail("[, true]"); tryArrayFail("[null, ]"); tryArrayFail("[null, "); }
void testObject() { JSONParser *parser; JSONAtom *atom; JSONObject *objectAtom; tryObjectOk("{}", [](const JSONObject &objectAtom) { assertIntEqual(objectAtom.size(), 0); }); tryObjectOk("{\"testKey\": 0}", [](const JSONObject &objectAtom) { assertIntEqual(objectAtom.size(), 1); assertTrue(objectAtom.exists("testKey")); assertIntEqual(objectAtom["testKey"].asInt(), 0); }); tryObjectOk("{\"testInt\": 0, \"testBool\": true}", [](const JSONObject &objectAtom) { assertIntEqual(objectAtom.size(), 2); assertTrue(objectAtom.exists("testInt")); assertIntEqual(objectAtom["testInt"].asInt(), 0); assertTrue(objectAtom.exists("testBool")); assertTrue(objectAtom["testBool"].asBool()); }); tryObjectFail("{true}"); tryObjectFail("{true: 0}"); tryObjectFail("{"); tryObjectFail("{\"key\"}"); tryObjectFail("{\"key\": }"); tryObjectFail("{\"key\": ,}"); tryObjectFail("{\"key\": junk}"); tryObjectFail("{\"key\": null, }"); }
void testParseJSON() { JSONAtom *atom; JSONObject *object; JSONArray *array, *innerArray; TRY("{\n\t\"testInt\": 0,\n\t\"testArray\": [\n\t\tnull,\n\t\ttrue,\n\t\tfalse\n\t]\n}", assertNotNull(atom); object = atom->asObject(); assertIntEqual(object->size(), 2); assertTrue(object->exists("testInt")); assertIntEqual((*object)["testInt"].asInt(), 0); assertTrue(object->exists("testArray")); array = (*object)["testArray"].asArray(); assertIntEqual(array->size(), 3); assertNotNull((*array)[0]); assertNotNull((*array)[1]); assertNotNull((*array)[2]); assertNull((*array)[0].asNull()); assertTrue((*array)[1].asBool()); assertFalse((*array)[2].asBool()) ); TRY("[\n\t0,\n\t\[\n\t\tnull,\n\t\ttrue,\n\t\tfalse\n\t]\n]", assertNotNull(atom); array = atom->asArray(); assertIntEqual(array->size(), 2); assertNotNull((*array)[0]); assertIntEqual((*array)[0].asInt(), 0); assertNotNull((*array)[1]); innerArray = (*array)[1].asArray(); assertIntEqual(innerArray->size(), 3); assertNotNull((*innerArray)[0]); assertNotNull((*innerArray)[1]); assertNotNull((*innerArray)[2]); assertNull((*innerArray)[0].asNull()); assertTrue((*innerArray)[1].asBool()); assertFalse((*innerArray)[2].asBool()) ); TRY("[\n\t0,\n\t1\n]", assertNotNull(atom); array = atom->asArray(); assertIntEqual(array->size(), 2); assertIntEqual((*array)[0].asInt(), 0); assertIntEqual((*array)[1].asInt(), 1) ); TRY_SHOULD_FAIL("true"); TRY_SHOULD_FAIL("false"); TRY_SHOULD_FAIL("null"); TRY_SHOULD_FAIL("invalid"); TRY_SHOULD_FAIL("0"); TRY_SHOULD_FAIL("\"true\""); }
void testIntNumber() { tryNumberOk("0 ", [](const JSONAtom &atom) { assertIntEqual(atom.asInt(), 0); }); tryNumberOk("190 ", [](const JSONAtom &atom) { assertIntEqual(atom.asInt(), 190); }); tryNumberOk("-190 ", [](const JSONAtom &atom) { assertIntEqual(atom.asInt(), -190); }); tryNumberOk("-0 ", [](const JSONAtom &atom) { assertIntEqual(atom.asInt(), -0); }); tryNumberOk("19e1 ", [](const JSONAtom &atom) { assertIntEqual(atom.asInt(), 190); }); tryNumberOk("190e-1 ", [](const JSONAtom &atom) { assertIntEqual(atom.asInt(), 19); }); tryNumberFail("00 "); tryNumberFail("0e00 "); }
void testOperatorInt() { assertNotNull(testInt); assertIntEqual(*testInt, 5); assertIntNotEqual(*testInt, 0); }
} assertNotNull(testInt); } void testOperatorInt() { assertNotNull(testInt); assertIntEqual(*testInt, 5); assertIntNotEqual(*testInt, 0); } void testConversions() { UNWANTED_TYPE(testInt, Null) UNWANTED_TYPE(testInt, Bool) WANTED_TYPE(assertIntEqual(testInt->asInt(), 5)) UNWANTED_TYPE(testInt, Float) UNWANTED_TYPE(testInt, String) UNWANTED_TYPE(testInt, Object) UNWANTED_TYPE(testInt, Array) } void testDistruct() { delete testInt; testInt = NULL; } #ifdef __cplusplus extern "C" {
void testPower10() { assertIntEqual(power10(0), 1); assertIntEqual(power10(2), 100); assertIntEqual(power10(4), 10000); }