|
|
@@ -15,85 +15,130 @@ namespace ArduinoJsonParserTests
|
|
|
{
|
|
|
TEST_CLASS(JsonHashTableTests)
|
|
|
{
|
|
|
- char json[128];
|
|
|
JsonParser<32> parser;
|
|
|
JsonHashTable hashTable;
|
|
|
+ JsonArray nestedArray;
|
|
|
+ char json[256];
|
|
|
|
|
|
public:
|
|
|
|
|
|
- TEST_METHOD_INITIALIZE(Initialize)
|
|
|
- {
|
|
|
- strcpy(json, "{\"Name\":\"Blanchon\",\"Skills\":[\"C\",\"C++\",\"C#\"],\"Age\":32,\"Online\":true}");
|
|
|
- hashTable = parser.parseHashTable(json);
|
|
|
- }
|
|
|
-
|
|
|
- TEST_METHOD(HashTable_Success_ReturnsTrue)
|
|
|
- {
|
|
|
- Assert::IsTrue(hashTable.success());
|
|
|
- }
|
|
|
-
|
|
|
- TEST_METHOD(HashTable_GetString_ReturnsExpectedValue)
|
|
|
- {
|
|
|
- string name = hashTable.getString("Name");
|
|
|
- Assert::AreEqual(name, string("Blanchon"));
|
|
|
- }
|
|
|
-
|
|
|
- TEST_METHOD(HashTable_GetArray_ReturnsExpectedValue)
|
|
|
- {
|
|
|
- JsonArray skills = hashTable.getArray("Skills");
|
|
|
-
|
|
|
- string skill0 = skills.getString(0);
|
|
|
- Assert::AreEqual(skill0, string("C"));
|
|
|
-
|
|
|
- string skill1 = skills.getString(1);
|
|
|
- Assert::AreEqual(skill1, string("C++"));
|
|
|
-
|
|
|
- string skill2 = skills.getString(2);
|
|
|
- Assert::AreEqual(skill2, string("C#"));
|
|
|
- }
|
|
|
-
|
|
|
- TEST_METHOD(HashTable_GetLong_ReturnsExpectedValue)
|
|
|
- {
|
|
|
- int age = hashTable.getLong("Age");
|
|
|
- Assert::AreEqual(32, age);
|
|
|
- }
|
|
|
-
|
|
|
- TEST_METHOD(HashTable_GetBool_ReturnsExpectedValue)
|
|
|
- {
|
|
|
- bool online = hashTable.getBool("Online");
|
|
|
- Assert::AreEqual(true, online);
|
|
|
- }
|
|
|
-
|
|
|
- TEST_METHOD(OneDimensionArrayInHashTable)
|
|
|
+ TEST_METHOD(EmptyString)
|
|
|
{
|
|
|
- char json[] = "{a:[0,0],b:[0,0]}";
|
|
|
+ whenInputIs("");
|
|
|
+ parseMustFail();
|
|
|
+ }
|
|
|
+
|
|
|
+ TEST_METHOD(EmptyHashTable)
|
|
|
+ {
|
|
|
+ whenInputIs("{}");
|
|
|
+ parseMustSucceed();
|
|
|
+ }
|
|
|
+
|
|
|
+ TEST_METHOD(TwoIntegers)
|
|
|
+ {
|
|
|
+ whenInputIs("{\"key1\":1,\"key2\":2}");
|
|
|
+ parseMustSucceed();
|
|
|
+ itemMustBe("key1", 1L);
|
|
|
+ itemMustBe("key2", 2L);
|
|
|
+ itemMustNotExists("key3");
|
|
|
+ }
|
|
|
+
|
|
|
+ TEST_METHOD(TwoBooleans)
|
|
|
+ {
|
|
|
+ whenInputIs("{\"key1\":true,\"key2\":false}");
|
|
|
+ parseMustSucceed();
|
|
|
+ itemMustBe("key1", true);
|
|
|
+ itemMustBe("key2", false);
|
|
|
+ itemMustNotExists("key3");
|
|
|
+ }
|
|
|
+
|
|
|
+ TEST_METHOD(TwoStrings)
|
|
|
+ {
|
|
|
+ whenInputIs("{\"key1\":\"hello\",\"key2\":\"world\"}");
|
|
|
+ parseMustSucceed();
|
|
|
+ itemMustBe("key1", "hello");
|
|
|
+ itemMustBe("key2", "world");
|
|
|
+ itemMustNotExists("key3");
|
|
|
+ }
|
|
|
+
|
|
|
+ TEST_METHOD(TwoNestedArrays)
|
|
|
+ {
|
|
|
+ whenInputIs("{\"key1\":[1,2],\"key2\":[3,4]}");
|
|
|
+ parseMustSucceed();
|
|
|
|
|
|
- JsonHashTable root = parser.parseHashTable(json);
|
|
|
- Assert::IsTrue(root.success());
|
|
|
+ itemMustBeAnArray("key1");
|
|
|
+ arrayLengthMustBe(2);
|
|
|
+ arrayItemMustBe(0, 1L);
|
|
|
+ arrayItemMustBe(1, 2L);
|
|
|
+ arrayItemMustBe(2, 0L);
|
|
|
|
|
|
- JsonArray arrayA = root.getArray("a");
|
|
|
- Assert::IsTrue(arrayA.success());
|
|
|
- Assert::AreEqual(2, arrayA.getLength());
|
|
|
+ itemMustBeAnArray("key2");
|
|
|
+ arrayLengthMustBe(2);
|
|
|
+ arrayItemMustBe(0, 3L);
|
|
|
+ arrayItemMustBe(1, 4L);
|
|
|
+ arrayItemMustBe(2, 0L);
|
|
|
|
|
|
- JsonArray arrayB = root.getArray("b");
|
|
|
- Assert::IsTrue(arrayB.success());
|
|
|
- Assert::AreEqual(2, arrayB.getLength());
|
|
|
+ itemMustNotExists("key3");
|
|
|
+ }
|
|
|
+
|
|
|
+ private:
|
|
|
+
|
|
|
+ void whenInputIs(const char* input)
|
|
|
+ {
|
|
|
+ strcpy(json, input);
|
|
|
+ hashTable = parser.parseHashTable(json);
|
|
|
}
|
|
|
|
|
|
- TEST_METHOD(TwoDimensionsArrayInHashTable)
|
|
|
+ void parseMustFail()
|
|
|
{
|
|
|
- char json[] = "{a:[[0],[0]],b:[[0],[0]]}";
|
|
|
-
|
|
|
- JsonHashTable root = parser.parseHashTable(json);
|
|
|
- Assert::IsTrue(root.success());
|
|
|
-
|
|
|
- JsonArray arrayA = root.getArray("a");
|
|
|
- Assert::IsTrue(arrayA.success());
|
|
|
- Assert::AreEqual(2, arrayA.getLength());
|
|
|
-
|
|
|
- JsonArray arrayB = root.getArray("b");
|
|
|
- Assert::IsTrue(arrayB.success());
|
|
|
- Assert::AreEqual(2, arrayB.getLength());
|
|
|
+ Assert::IsFalse(hashTable.success());
|
|
|
+ }
|
|
|
+
|
|
|
+ void parseMustSucceed()
|
|
|
+ {
|
|
|
+ Assert::IsTrue(hashTable.success());
|
|
|
+ }
|
|
|
+
|
|
|
+ void itemMustBe(const char* key, long expected)
|
|
|
+ {
|
|
|
+ Assert::AreEqual(expected, hashTable.getLong(key));
|
|
|
+ }
|
|
|
+
|
|
|
+ void itemMustBe(const char* key, bool expected)
|
|
|
+ {
|
|
|
+ Assert::AreEqual(expected, hashTable.getBool(key));
|
|
|
+ }
|
|
|
+
|
|
|
+ void itemMustBe(const char* key, const char* expected)
|
|
|
+ {
|
|
|
+ Assert::AreEqual(expected, hashTable.getString(key));
|
|
|
+ }
|
|
|
+
|
|
|
+ void itemMustNotExists(const char* key)
|
|
|
+ {
|
|
|
+ Assert::IsFalse(hashTable.containsKey(key));
|
|
|
+ Assert::IsFalse(hashTable.getHashTable(key).success());
|
|
|
+ Assert::IsFalse(hashTable.getArray(key).success());
|
|
|
+ Assert::IsFalse(hashTable.getBool(key));
|
|
|
+ Assert::AreEqual(0.0, hashTable.getDouble(key));
|
|
|
+ Assert::AreEqual(0L, hashTable.getLong(key));
|
|
|
+ Assert::IsNull(hashTable.getString(key));
|
|
|
+ }
|
|
|
+
|
|
|
+ void itemMustBeAnArray(const char* key)
|
|
|
+ {
|
|
|
+ nestedArray = hashTable.getArray(key);
|
|
|
+ Assert::IsTrue(nestedArray.success());
|
|
|
+ }
|
|
|
+
|
|
|
+ void arrayLengthMustBe(int expected)
|
|
|
+ {
|
|
|
+ Assert::AreEqual(expected, nestedArray.getLength());
|
|
|
+ }
|
|
|
+
|
|
|
+ void arrayItemMustBe(int index, long expected)
|
|
|
+ {
|
|
|
+ Assert::AreEqual(expected, nestedArray.getLong(index));
|
|
|
}
|
|
|
};
|
|
|
}
|