forked from bblanchon/ArduinoJson
Organized test files in subfolders
This commit is contained in:
64
test/JsonBuffer/nested.cpp
Normal file
64
test/JsonBuffer/nested.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
char jsonString[] = " { \"ab\" : [ 1 , 2 ] , \"cd\" : [ 3 , 4 ] } ";
|
||||
|
||||
JsonObject &object = jsonBuffer.parseObject(jsonString);
|
||||
JsonArray &array1 = object["ab"];
|
||||
const JsonArray &array2 = object["cd"];
|
||||
JsonArray &array3 = object["ef"];
|
||||
|
||||
ASSERT_TRUE(object.success());
|
||||
|
||||
ASSERT_TRUE(array1.success());
|
||||
ASSERT_TRUE(array2.success());
|
||||
ASSERT_FALSE(array3.success());
|
||||
|
||||
ASSERT_EQ(2, array1.size());
|
||||
ASSERT_EQ(2, array2.size());
|
||||
ASSERT_EQ(0, array3.size());
|
||||
|
||||
EXPECT_EQ(1, array1[0].as<int>());
|
||||
EXPECT_EQ(2, array1[1].as<int>());
|
||||
|
||||
EXPECT_EQ(3, array2[0].as<int>());
|
||||
EXPECT_EQ(4, array2[1].as<int>());
|
||||
|
||||
EXPECT_EQ(0, array3[0].as<int>());
|
||||
}
|
||||
|
||||
TEST(JsonParser_Nested_Tests, ObjectNestedInArray) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
char jsonString[] =
|
||||
" [ { \"a\" : 1 , \"b\" : 2 } , { \"c\" : 3 , \"d\" : 4 } ] ";
|
||||
|
||||
JsonArray &array = jsonBuffer.parseArray(jsonString);
|
||||
JsonObject &object1 = array[0];
|
||||
const JsonObject &object2 = array[1];
|
||||
JsonObject &object3 = array[2];
|
||||
|
||||
ASSERT_TRUE(array.success());
|
||||
|
||||
ASSERT_TRUE(object1.success());
|
||||
ASSERT_TRUE(object2.success());
|
||||
ASSERT_FALSE(object3.success());
|
||||
|
||||
ASSERT_EQ(2, object1.size());
|
||||
ASSERT_EQ(2, object2.size());
|
||||
ASSERT_EQ(0, object3.size());
|
||||
|
||||
EXPECT_EQ(1, object1["a"].as<int>());
|
||||
EXPECT_EQ(2, object1["b"].as<int>());
|
||||
EXPECT_EQ(3, object2["c"].as<int>());
|
||||
EXPECT_EQ(4, object2["d"].as<int>());
|
||||
EXPECT_EQ(0, object3["e"].as<int>());
|
||||
}
|
85
test/JsonBuffer/nestingLimit.cpp
Normal file
85
test/JsonBuffer/nestingLimit.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonParser_NestingLimit_Tests : public testing::Test {
|
||||
protected:
|
||||
void whenNestingLimitIs(uint8_t nestingLimit) {
|
||||
_nestingLimit = nestingLimit;
|
||||
}
|
||||
|
||||
void parseArrayMustFail(const char *json) {
|
||||
ASSERT_FALSE(tryParseArray(json));
|
||||
}
|
||||
|
||||
void parseArrayMustSucceed(const char *json) {
|
||||
ASSERT_TRUE(tryParseArray(json));
|
||||
}
|
||||
|
||||
void parseObjectMustFail(const char *json) {
|
||||
ASSERT_FALSE(tryParseObject(json));
|
||||
}
|
||||
|
||||
void parseObjectMustSucceed(const char *json) {
|
||||
ASSERT_TRUE(tryParseObject(json));
|
||||
}
|
||||
|
||||
private:
|
||||
bool tryParseArray(const char *json) {
|
||||
DynamicJsonBuffer buffer;
|
||||
char s[256];
|
||||
strcpy(s, json);
|
||||
return buffer.parseArray(s, _nestingLimit).success();
|
||||
}
|
||||
|
||||
bool tryParseObject(const char *json) {
|
||||
DynamicJsonBuffer buffer;
|
||||
char s[256];
|
||||
strcpy(s, json);
|
||||
return buffer.parseObject(s, _nestingLimit).success();
|
||||
}
|
||||
|
||||
uint8_t _nestingLimit;
|
||||
};
|
||||
|
||||
TEST_F(JsonParser_NestingLimit_Tests, ParseArrayWithNestingLimit0) {
|
||||
whenNestingLimitIs(0);
|
||||
parseArrayMustSucceed("[]");
|
||||
parseArrayMustFail("[[]]");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_NestingLimit_Tests, ParseArrayWithNestingLimit1) {
|
||||
whenNestingLimitIs(1);
|
||||
parseArrayMustSucceed("[[]]");
|
||||
parseArrayMustFail("[[[]]]");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_NestingLimit_Tests, ParseArrayWithNestingLimit2) {
|
||||
whenNestingLimitIs(2);
|
||||
parseArrayMustSucceed("[[[]]]");
|
||||
parseArrayMustFail("[[[[]]]]");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_NestingLimit_Tests, ParseObjectWithNestingLimit0) {
|
||||
whenNestingLimitIs(0);
|
||||
parseObjectMustSucceed("{}");
|
||||
parseObjectMustFail("{\"key\":{}}");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_NestingLimit_Tests, ParseObjectWithNestingLimit1) {
|
||||
whenNestingLimitIs(1);
|
||||
parseObjectMustSucceed("{\"key\":{}}");
|
||||
parseObjectMustFail("{\"key\":{\"key\":{}}}");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_NestingLimit_Tests, ParseObjectWithNestingLimit2) {
|
||||
whenNestingLimitIs(2);
|
||||
parseObjectMustSucceed("{\"key\":{\"key\":{}}}");
|
||||
parseObjectMustFail("{\"key\":{\"key\":{\"key\":{}}}}");
|
||||
}
|
105
test/JsonBuffer/parse.cpp
Normal file
105
test/JsonBuffer/parse.cpp
Normal file
@ -0,0 +1,105 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonParser_Variant_Test : public testing::Test {
|
||||
protected:
|
||||
void whenInputIs(const char* jsonString) {
|
||||
strcpy(_jsonString, jsonString);
|
||||
_result = _jsonBuffer.parse(_jsonString);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void resultMustEqual(T expected) {
|
||||
EXPECT_EQ(expected, _result.as<T>());
|
||||
}
|
||||
|
||||
void resultMustEqual(const char* expected) {
|
||||
EXPECT_STREQ(expected, _result.as<char*>());
|
||||
}
|
||||
|
||||
void resultMustEqual(double expected) {
|
||||
EXPECT_DOUBLE_EQ(expected, _result.as<double>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void resultTypeMustBe() {
|
||||
EXPECT_TRUE(_result.is<T>());
|
||||
}
|
||||
|
||||
void resultMustBeInvalid() {
|
||||
EXPECT_FALSE(_result.success());
|
||||
}
|
||||
void resultMustBeValid() {
|
||||
EXPECT_TRUE(_result.success());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void verify(const char* input, T expected) {
|
||||
whenInputIs(input);
|
||||
resultMustBeValid();
|
||||
resultTypeMustBe<T>();
|
||||
resultMustEqual(expected);
|
||||
}
|
||||
|
||||
private:
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonVariant _result;
|
||||
char _jsonString[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, EmptyObject) {
|
||||
whenInputIs("{}");
|
||||
resultMustBeValid();
|
||||
resultTypeMustBe<JsonObject>();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, EmptyArray) {
|
||||
whenInputIs("[]");
|
||||
resultMustBeValid();
|
||||
resultTypeMustBe<JsonArray>();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, Integer) {
|
||||
verify("42", 42);
|
||||
verify("-42", -42);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, Double) {
|
||||
verify("3.14", 3.14);
|
||||
verify("3.14", 3.14);
|
||||
verify("1E+10", 1E+10);
|
||||
verify("-1E+10", -1E+10);
|
||||
verify("1.234E+10", 1.234E+10);
|
||||
verify("1.79769e+308", 1.79769e+308);
|
||||
verify("-1.79769e+308", -1.79769e+308);
|
||||
verify("1.7976931348623157e+308", 1.7976931348623157e+308);
|
||||
verify("0.017976931348623157e+310", 0.017976931348623157e+310);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, String) {
|
||||
verify("\"hello world\"", "hello world");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, True) {
|
||||
verify("true", true);
|
||||
verify("false", false);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, OpenBrace) {
|
||||
whenInputIs("{");
|
||||
resultMustBeInvalid();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, IncompleteStrings) {
|
||||
verify("\"", "");
|
||||
verify("\"hello", "hello");
|
||||
verify("\'", "");
|
||||
verify("\'world", "world");
|
||||
}
|
367
test/JsonBuffer/parseArray.cpp
Normal file
367
test/JsonBuffer/parseArray.cpp
Normal file
@ -0,0 +1,367 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonParser_Array_Tests : public testing::Test {
|
||||
protected:
|
||||
void whenInputIs(const char *json) {
|
||||
strcpy(_jsonString, json);
|
||||
}
|
||||
|
||||
void whenInputIs(const char *json, size_t len) {
|
||||
memcpy(_jsonString, json, len);
|
||||
}
|
||||
|
||||
void parseMustSucceed() {
|
||||
_array = &_jsonBuffer.parseArray(_jsonString);
|
||||
EXPECT_TRUE(_array->success());
|
||||
}
|
||||
|
||||
void parseMustFail() {
|
||||
_array = &_jsonBuffer.parseArray(_jsonString);
|
||||
EXPECT_FALSE(_array->success());
|
||||
EXPECT_EQ(0, _array->size());
|
||||
}
|
||||
|
||||
void sizeMustBe(int expected) {
|
||||
ASSERT_EQ(expected, _array->size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void firstElementMustBe(T expected) {
|
||||
elementAtIndexMustBe(0, expected);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void secondElementMustBe(T expected) {
|
||||
elementAtIndexMustBe(1, expected);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void elementAtIndexMustBe(int index, T expected) {
|
||||
EXPECT_EQ(expected, (*_array)[index].as<T>());
|
||||
}
|
||||
|
||||
void elementAtIndexMustBe(int index, const char *expected) {
|
||||
EXPECT_STREQ(expected, (*_array)[index].as<const char *>());
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray *_array;
|
||||
char _jsonString[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, EmptyArray) {
|
||||
whenInputIs("[]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, MissingOpeningBracket) {
|
||||
whenInputIs("]");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, ArrayWithNoEnd) {
|
||||
whenInputIs("[");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, EmptyArrayWithLeadingSpaces) {
|
||||
whenInputIs(" []");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, Garbage) {
|
||||
whenInputIs("%*$£¤");
|
||||
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, OneInteger) {
|
||||
whenInputIs("[42]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe(42);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpacesBefore) {
|
||||
whenInputIs("[ \t\r\n42]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe(42);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, OneIntegerWithSpaceAfter) {
|
||||
whenInputIs("[42 \t\r\n]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe(42);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoIntegers) {
|
||||
whenInputIs("[42,84]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(42);
|
||||
secondElementMustBe(84);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoDoubles) {
|
||||
whenInputIs("[4.2,1e2]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(4.2);
|
||||
secondElementMustBe(1e2);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, UnsignedLong) {
|
||||
whenInputIs("[4294967295]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe(4294967295UL);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoBooleans) {
|
||||
whenInputIs("[true,false]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(true);
|
||||
secondElementMustBe(false);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoNulls) {
|
||||
const char *const nullCharPtr = 0;
|
||||
|
||||
whenInputIs("[null,null]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe(nullCharPtr);
|
||||
secondElementMustBe(nullCharPtr);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoStringsDoubleQuotes) {
|
||||
whenInputIs("[ \"hello\" , \"world\" ]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("hello");
|
||||
secondElementMustBe("world");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoStringsSingleQuotes) {
|
||||
whenInputIs("[ 'hello' , 'world' ]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("hello");
|
||||
secondElementMustBe("world");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, TwoStringsNoQuotes) {
|
||||
whenInputIs("[ hello , world ]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("hello");
|
||||
secondElementMustBe("world");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, EmptyStringsDoubleQuotes) {
|
||||
whenInputIs("[\"\",\"\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("");
|
||||
secondElementMustBe("");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, EmptyStringSingleQuotes) {
|
||||
whenInputIs("[\'\',\'\']");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("");
|
||||
secondElementMustBe("");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, EmptyStringNoQuotes) {
|
||||
whenInputIs("[,]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("");
|
||||
secondElementMustBe("");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, ClosingDoubleQuoteMissing) {
|
||||
whenInputIs("[\"]");
|
||||
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, ClosingSignleQuoteMissing) {
|
||||
whenInputIs("[\']");
|
||||
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, StringWithEscapedChars) {
|
||||
whenInputIs("[\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe("1\"2\\3/4\b5\f6\n7\r8\t9");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, StringWithUnterminatedEscapeSequence) {
|
||||
whenInputIs("\"\\\0\"", 4);
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CCommentBeforeOpeningBracket) {
|
||||
whenInputIs("/*COMMENT*/ [\"hello\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe("hello");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CCommentAfterOpeningBracket) {
|
||||
whenInputIs("[/*COMMENT*/ \"hello\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe("hello");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CCommentBeforeClosingBracket) {
|
||||
whenInputIs("[\"hello\"/*COMMENT*/]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe("hello");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CCommentAfterClosingBracket) {
|
||||
whenInputIs("[\"hello\"]/*COMMENT*/");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe("hello");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CCommentBeforeComma) {
|
||||
whenInputIs("[\"hello\"/*COMMENT*/,\"world\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("hello");
|
||||
secondElementMustBe("world");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CCommentAfterComma) {
|
||||
whenInputIs("[\"hello\",/*COMMENT*/ \"world\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("hello");
|
||||
secondElementMustBe("world");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CppCommentBeforeOpeningBracket) {
|
||||
whenInputIs("//COMMENT\n\t[\"hello\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe("hello");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CppCommentAfterOpeningBracket) {
|
||||
whenInputIs("[//COMMENT\n\"hello\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe("hello");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CppCommentBeforeClosingBracket) {
|
||||
whenInputIs("[\"hello\"//COMMENT\r\n]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe("hello");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CppCommentAfterClosingBracket) {
|
||||
whenInputIs("[\"hello\"]//COMMENT\n");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
firstElementMustBe("hello");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CppCommentBeforeComma) {
|
||||
whenInputIs("[\"hello\"//COMMENT\n,\"world\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("hello");
|
||||
secondElementMustBe("world");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, CppCommentAfterComma) {
|
||||
whenInputIs("[\"hello\",//COMMENT\n\"world\"]");
|
||||
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
firstElementMustBe("hello");
|
||||
secondElementMustBe("world");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, InvalidCppComment) {
|
||||
whenInputIs("[/COMMENT\n]");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, InvalidComment) {
|
||||
whenInputIs("[/*/\n]");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, UnfinishedCComment) {
|
||||
whenInputIs("[/*COMMENT]");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, EndsInCppComment) {
|
||||
whenInputIs("[//COMMENT");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, AfterClosingStar) {
|
||||
whenInputIs("[/*COMMENT*");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, DeeplyNested) {
|
||||
whenInputIs("[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]");
|
||||
parseMustSucceed();
|
||||
}
|
187
test/JsonBuffer/parseObject.cpp
Normal file
187
test/JsonBuffer/parseObject.cpp
Normal file
@ -0,0 +1,187 @@
|
||||
// Copyright Benoit Blanchon 2014-2017
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://bblanchon.github.io/ArduinoJson/
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonParser_Object_Test : public testing::Test {
|
||||
protected:
|
||||
void whenInputIs(const char *jsonString) {
|
||||
strcpy(_jsonString, jsonString);
|
||||
_object = &_jsonBuffer.parseObject(_jsonString);
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void keyMustHaveValue(const char *key, T expected) {
|
||||
EXPECT_EQ(expected, (*_object)[key].as<T>());
|
||||
}
|
||||
|
||||
private:
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject *_object;
|
||||
char _jsonString[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonParser_Object_Test, EmptyObject) {
|
||||
whenInputIs("{}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, MissingOpeningBrace) {
|
||||
whenInputIs("}");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, MissingClosingBrace) {
|
||||
whenInputIs("{");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, MissingColonAndValue) {
|
||||
whenInputIs("{\"key\"}");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, MissingQuotesAndColonAndValue) {
|
||||
whenInputIs("{key}");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneString) {
|
||||
whenInputIs("{\"key\":\"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringSingleQuotes) {
|
||||
whenInputIs("{'key':'value'}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringNoQuotes) {
|
||||
whenInputIs("{key:value}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeKey) {
|
||||
whenInputIs("{ \"key\":\"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterKey) {
|
||||
whenInputIs("{\"key\" :\"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceBeforeValue) {
|
||||
whenInputIs("{\"key\": \"value\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, OneStringSpaceAfterValue) {
|
||||
whenInputIs("{\"key\":\"value\" }");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(1);
|
||||
keyMustHaveValue("key", "value");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoStrings) {
|
||||
whenInputIs("{\"key1\":\"value1\",\"key2\":\"value2\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", "value1");
|
||||
keyMustHaveValue("key2", "value2");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoStringsSpaceBeforeComma) {
|
||||
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", "value1");
|
||||
keyMustHaveValue("key2", "value2");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoStringsSpaceAfterComma) {
|
||||
whenInputIs("{\"key1\":\"value1\" ,\"key2\":\"value2\"}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", "value1");
|
||||
keyMustHaveValue("key2", "value2");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, EndingWithAComma) {
|
||||
whenInputIs("{\"key1\":\"value1\",}");
|
||||
parseMustFail();
|
||||
sizeMustBe(0);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoIntergers) {
|
||||
whenInputIs("{\"key1\":42,\"key2\":-42}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", 42);
|
||||
keyMustHaveValue("key2", -42);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoDoubles) {
|
||||
whenInputIs("{\"key1\":12.345,\"key2\":-7E89}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", 12.345);
|
||||
keyMustHaveValue("key2", -7E89);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, TwoBooleans) {
|
||||
whenInputIs("{\"key1\":true,\"key2\":false}");
|
||||
parseMustSucceed();
|
||||
sizeMustBe(2);
|
||||
keyMustHaveValue("key1", true);
|
||||
keyMustHaveValue("key2", false);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Object_Test, NullForKey) {
|
||||
whenInputIs("null:\"value\"}");
|
||||
parseMustFail();
|
||||
}
|
Reference in New Issue
Block a user