Files
ArduinoJson/test/JsonParser_Object_Tests.cpp

174 lines
4.2 KiB
C++
Raw Normal View History

2014-10-23 23:39:22 +02:00
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
2014-10-21 21:11:30 +02:00
#include <gtest/gtest.h>
#include <ArduinoJson/StaticJsonBuffer.hpp>
2014-10-21 23:37:17 +02:00
#include <ArduinoJson/JsonValue.hpp>
2014-10-21 21:11:30 +02:00
using namespace ArduinoJson;
2014-10-23 19:54:00 +02:00
class JsonParser_Object_Test : public testing::Test {
protected:
2014-10-23 19:54:00 +02:00
void whenInputIs(const char *jsonString) {
strcpy(_jsonString, jsonString);
_object = _jsonBuffer.parseObject(_jsonString);
}
2014-10-21 21:11:30 +02:00
2014-10-23 19:54:00 +02:00
void parseMustSucceed() { EXPECT_TRUE(_object.success()); }
void parseMustFail() { EXPECT_FALSE(_object.success()); }
void sizeMustBe(int expected) { EXPECT_EQ(expected, _object.size()); }
void keyMustHaveValue(const char *key, const char *expected) {
EXPECT_STREQ(expected, _object[key].as<const char *>());
}
template <typename T>
void keyMustHaveValue(const char *key, T expected) {
2014-10-23 19:54:00 +02:00
EXPECT_EQ(expected, _object[key].as<T>());
}
2014-10-22 15:51:55 +02:00
private:
2014-10-23 19:54:00 +02:00
StaticJsonBuffer<10> _jsonBuffer;
JsonObject _object;
char _jsonString[256];
2014-10-21 21:11:30 +02:00
};
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, EmptyObject) {
whenInputIs("{}");
parseMustSucceed();
sizeMustBe(0);
}
TEST_F(JsonParser_Object_Test, MissingClosingBrace) {
whenInputIs("{");
parseMustFail();
sizeMustBe(0);
2014-10-21 21:50:00 +02:00
}
2014-10-21 21:11:30 +02:00
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, MissingColonAndValue) {
whenInputIs("{\"key\"}");
parseMustFail();
sizeMustBe(0);
2014-10-21 23:37:17 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue) {
whenInputIs("{key}");
parseMustFail();
sizeMustBe(0);
2014-10-22 10:55:36 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, OneString) {
whenInputIs("{\"key\":\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
2014-10-22 10:55:36 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, OneStringSingleQuotes) {
whenInputIs("{'key':'value'}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeKey) {
whenInputIs("{ \"key\":\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
}
2014-10-21 23:42:26 +02:00
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterKey) {
whenInputIs("{\"key\" :\"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
2014-10-22 11:54:33 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeValue) {
whenInputIs("{\"key\": \"value\"}");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
2014-10-21 23:42:26 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterValue) {
whenInputIs("{\"key\":\"value\" }");
parseMustSucceed();
sizeMustBe(1);
keyMustHaveValue("key", "value");
2014-10-21 23:42:26 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, TwoStrings) {
whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", "value1");
keyMustHaveValue("key2", "value2");
2014-10-21 23:42:26 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, TwoStringsSpaceBeforeComma) {
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", "value1");
keyMustHaveValue("key2", "value2");
2014-10-22 11:54:33 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, TwoStringsSpaceAfterComma) {
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", "value1");
keyMustHaveValue("key2", "value2");
2014-10-22 11:56:40 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, EndingWithAComma) {
whenInputIs("{\"key1\":\"value1\",}");
parseMustFail();
sizeMustBe(0);
2014-10-22 11:56:40 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, TwoIntergers) {
whenInputIs("{\"key1\":42,\"key2\":-42}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", 42);
keyMustHaveValue("key2", -42);
2014-10-22 11:56:40 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, TwoDoubles) {
whenInputIs("{\"key1\":12.345,\"key2\":-7.89}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", 12.345);
keyMustHaveValue("key2", -7.89);
}
2014-10-22 15:51:55 +02:00
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, TwoBooleans) {
whenInputIs("{\"key1\":true,\"key2\":false}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", true);
keyMustHaveValue("key2", false);
2014-10-22 15:51:55 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, TwoNulls) {
const char *const nullstr = 0;
whenInputIs("{\"key1\":null,\"key2\":null}");
parseMustSucceed();
sizeMustBe(2);
keyMustHaveValue("key1", nullstr);
keyMustHaveValue("key2", nullstr);
2014-10-22 16:04:29 +02:00
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonParser_Object_Test, NullForKey) {
whenInputIs("null:\"value\"}");
parseMustFail();
2014-10-21 21:11:30 +02:00
}