Organized test files in subfolders

This commit is contained in:
Benoit Blanchon
2017-04-10 14:32:45 +02:00
parent e664c1ab05
commit ac89d91db5
65 changed files with 4 additions and 141 deletions

View File

@ -0,0 +1,25 @@
// 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>
#define TEST_(name) TEST(JsonObject_Basic_Tests, name)
TEST_(InitialSizeIsZero) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
EXPECT_EQ(0, _object.size());
}
TEST_(SuccessIsTrue) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
EXPECT_TRUE(_object.success());
}

View File

@ -0,0 +1,39 @@
// 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>
#define TEST_(name) TEST(JsonObject_Basic_Tests, name)
TEST_(ContainsKeyReturnsFalseForNonExistingKey) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object.set("hello", 42);
EXPECT_FALSE(_object.containsKey("world"));
}
TEST_(ContainsKeyReturnsTrueForDefinedValue) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object.set("hello", 42);
EXPECT_TRUE(_object.containsKey("hello"));
}
TEST_(ContainsKeyReturnsFalseAfterRemove) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object.set("hello", 42);
_object.remove("hello");
EXPECT_FALSE(_object.containsKey("hello"));
}

26
test/JsonObject/get.cpp Normal file
View File

@ -0,0 +1,26 @@
// 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 JsonObject_Get_Tests : public ::testing::Test {
public:
JsonObject_Get_Tests() : _object(_jsonBuffer.createObject()) {}
protected:
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object;
};
#define TEST_(name) TEST_F(JsonObject_Get_Tests, name)
TEST_(GetConstCharPointer_GivenStringLiteral) {
_object.set("hello", "world");
const char* value = _object.get<const char*>("hello");
EXPECT_STREQ("world", value);
}

View File

@ -0,0 +1,33 @@
// 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(JsonObject_Invalid_Tests, SubscriptFails) {
ASSERT_FALSE(JsonObject::invalid()["key"].success());
}
TEST(JsonObject_Invalid_Tests, AddFails) {
JsonObject& object = JsonObject::invalid();
object.set("hello", "world");
ASSERT_EQ(0, object.size());
}
TEST(JsonObject_Invalid_Tests, CreateNestedArrayFails) {
ASSERT_FALSE(JsonObject::invalid().createNestedArray("hello").success());
}
TEST(JsonObject_Invalid_Tests, CreateNestedObjectFails) {
ASSERT_FALSE(JsonObject::invalid().createNestedObject("world").success());
}
TEST(JsonObject_Invalid_Tests, PrintToWritesBraces) {
char buffer[32];
JsonObject::invalid().printTo(buffer, sizeof(buffer));
ASSERT_STREQ("{}", buffer);
}

View File

@ -0,0 +1,57 @@
// 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 JsonObject_Iterator_Test : public testing::Test {
public:
JsonObject_Iterator_Test() : _object(_buffer.createObject()) {
_object["ab"] = 12;
_object["cd"] = 34;
}
protected:
StaticJsonBuffer<JSON_OBJECT_SIZE(2)> _buffer;
JsonObject& _object;
};
TEST_F(JsonObject_Iterator_Test, NonConstIterator) {
JsonObject::iterator it = _object.begin();
ASSERT_NE(_object.end(), it);
EXPECT_STREQ("ab", it->key);
EXPECT_EQ(12, it->value);
it->key = "a.b";
it->value = 1.2;
++it;
ASSERT_NE(_object.end(), it);
EXPECT_STREQ("cd", it->key);
EXPECT_EQ(34, it->value);
it->key = "c.d";
it->value = 3.4;
++it;
ASSERT_EQ(_object.end(), it);
ASSERT_EQ(2, _object.size());
EXPECT_EQ(1.2, _object["a.b"]);
EXPECT_EQ(3.4, _object["c.d"]);
}
TEST_F(JsonObject_Iterator_Test, ConstIterator) {
const JsonObject& const_object = _object;
JsonObject::const_iterator it = const_object.begin();
ASSERT_NE(const_object.end(), it);
EXPECT_STREQ("ab", it->key);
EXPECT_EQ(12, it->value);
++it;
ASSERT_NE(const_object.end(), it);
EXPECT_STREQ("cd", it->key);
EXPECT_EQ(34, it->value);
++it;
ASSERT_EQ(const_object.end(), it);
}

View File

@ -0,0 +1,82 @@
// 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 JsonObject_PrettyPrintTo_Tests : public testing::Test {
public:
JsonObject_PrettyPrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
protected:
DynamicJsonBuffer _jsonBuffer;
JsonObject &_object;
void outputMustBe(const char *expected) {
char buffer[256];
size_t actualLen = _object.prettyPrintTo(buffer);
size_t measuredLen = _object.measurePrettyLength();
EXPECT_STREQ(expected, buffer);
EXPECT_EQ(strlen(expected), actualLen);
EXPECT_EQ(strlen(expected), measuredLen);
}
};
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) {
outputMustBe("{}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
_object["key"] = "value";
outputMustBe(
"{\r\n"
" \"key\": \"value\"\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, TwoMembers) {
_object["key1"] = "value1";
_object["key2"] = "value2";
outputMustBe(
"{\r\n"
" \"key1\": \"value1\",\r\n"
" \"key2\": \"value2\"\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyNestedContainers) {
_object.createNestedObject("key1");
_object.createNestedArray("key2");
outputMustBe(
"{\r\n"
" \"key1\": {},\r\n"
" \"key2\": []\r\n"
"}");
}
TEST_F(JsonObject_PrettyPrintTo_Tests, NestedContainers) {
JsonObject &nested1 = _object.createNestedObject("key1");
nested1["a"] = 1;
JsonArray &nested2 = _object.createNestedArray("key2");
nested2.add(2);
outputMustBe(
"{\r\n"
" \"key1\": {\r\n"
" \"a\": 1\r\n"
" },\r\n"
" \"key2\": [\r\n"
" 2\r\n"
" ]\r\n"
"}");
}

123
test/JsonObject/printTo.cpp Normal file
View File

@ -0,0 +1,123 @@
// 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 JsonObject_PrintTo_Tests : public testing::Test {
public:
JsonObject_PrintTo_Tests() : _object(_jsonBuffer.createObject()) {}
protected:
void outputMustBe(const char *expected) {
char actual[256];
size_t actualLen = _object.printTo(actual);
size_t measuredLen = _object.measureLength();
EXPECT_STREQ(expected, actual);
EXPECT_EQ(strlen(expected), actualLen);
EXPECT_EQ(strlen(expected), measuredLen);
}
DynamicJsonBuffer _jsonBuffer;
JsonObject &_object;
};
TEST_F(JsonObject_PrintTo_Tests, EmptyObject) {
outputMustBe("{}");
}
TEST_F(JsonObject_PrintTo_Tests, TwoStrings) {
_object["key1"] = "value1";
_object.set("key2", "value2");
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
}
TEST_F(JsonObject_PrintTo_Tests, RemoveFirst) {
_object["key1"] = "value1";
_object["key2"] = "value2";
_object.remove("key1");
outputMustBe("{\"key2\":\"value2\"}");
}
TEST_F(JsonObject_PrintTo_Tests, RemoveLast) {
_object["key1"] = "value1";
_object["key2"] = "value2";
_object.remove("key2");
outputMustBe("{\"key1\":\"value1\"}");
}
TEST_F(JsonObject_PrintTo_Tests, RemoveUnexistingKey) {
_object["key1"] = "value1";
_object["key2"] = "value2";
_object.remove("key3");
outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}");
}
TEST_F(JsonObject_PrintTo_Tests, ReplaceExistingKey) {
_object["key"] = "value1";
_object["key"] = "value2";
outputMustBe("{\"key\":\"value2\"}");
}
TEST_F(JsonObject_PrintTo_Tests, TwoIntegers) {
_object["a"] = 1;
_object.set("b", 2);
outputMustBe("{\"a\":1,\"b\":2}");
}
TEST_F(JsonObject_PrintTo_Tests, RawJson) {
_object["a"] = RawJson("[1,2]");
_object.set("b", RawJson("[4,5]"));
outputMustBe("{\"a\":[1,2],\"b\":[4,5]}");
}
TEST_F(JsonObject_PrintTo_Tests, TwoDoublesFourDigits) {
_object["a"] = double_with_n_digits(3.14159265358979323846, 4);
_object.set("b", 2.71828182845904523536, 4);
_object.set("c", double_with_n_digits(3.14159265358979323846, 3));
outputMustBe("{\"a\":3.1416,\"b\":2.7183,\"c\":3.142}");
}
TEST_F(JsonObject_PrintTo_Tests, TwoDoubleDefaultDigits) {
_object["a"] = 3.14159265358979323846;
_object.set("b", 2.71828182845904523536);
outputMustBe("{\"a\":3.14,\"b\":2.72}");
}
TEST_F(JsonObject_PrintTo_Tests, TwoNull) {
_object["a"] = static_cast<char *>(0);
_object.set("b", static_cast<char *>(0));
outputMustBe("{\"a\":null,\"b\":null}");
}
TEST_F(JsonObject_PrintTo_Tests, TwoBooleans) {
_object["a"] = true;
_object.set("b", false);
outputMustBe("{\"a\":true,\"b\":false}");
}
TEST_F(JsonObject_PrintTo_Tests, ThreeNestedArrays) {
_object.createNestedArray("a");
_object["b"] = _jsonBuffer.createArray();
_object.set("c", _jsonBuffer.createArray());
outputMustBe("{\"a\":[],\"b\":[],\"c\":[]}");
}
TEST_F(JsonObject_PrintTo_Tests, ThreeNestedObjects) {
_object.createNestedObject("a");
_object["b"] = _jsonBuffer.createObject();
_object.set("c", _jsonBuffer.createObject());
outputMustBe("{\"a\":{},\"b\":{},\"c\":{}}");
}

View File

@ -0,0 +1,31 @@
// 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>
#define TEST_(name) TEST(JsonObject_Remove_Tests, name)
TEST_(SizeDecreased_WhenValuesAreRemoved) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object["hello"] = 1;
_object.remove("hello");
EXPECT_EQ(0, _object.size());
}
TEST_(SizeUntouched_WhenRemoveIsCalledWithAWrongKey) {
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object = _jsonBuffer.createObject();
_object["hello"] = 1;
_object.remove("world");
EXPECT_EQ(1, _object.size());
}

127
test/JsonObject/set.cpp Normal file
View File

@ -0,0 +1,127 @@
// 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 JsonObject_Set_Tests : public ::testing::Test {
public:
JsonObject_Set_Tests() : _object(_jsonBuffer.createObject()) {}
protected:
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object;
};
#define TEST_(name) TEST_F(JsonObject_Set_Tests, name)
TEST_(SizeIncreased_WhenValuesAreAdded) {
_object.set("hello", 42);
EXPECT_EQ(1, _object.size());
}
TEST_(SizeUntouched_WhenSameValueIsAdded) {
_object["hello"] = 1;
_object["hello"] = 2;
EXPECT_EQ(1, _object.size());
}
TEST_(StoreInteger) {
_object.set("hello", 123);
EXPECT_EQ(123, _object["hello"].as<int>());
EXPECT_TRUE(_object["hello"].is<int>());
EXPECT_FALSE(_object["hello"].is<double>());
}
TEST_(StoreDouble) {
_object.set("hello", 123.45);
EXPECT_EQ(123.45, _object["hello"].as<double>());
EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
}
TEST_(StoreDoubleWithDigits) {
_object.set("hello", 123.45, 2);
EXPECT_EQ(123.45, _object["hello"].as<double>());
EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
}
TEST_(StoreBoolean) {
_object.set("hello", true);
EXPECT_TRUE(_object["hello"].as<bool>());
EXPECT_TRUE(_object["hello"].is<bool>());
EXPECT_FALSE(_object["hello"].is<long>());
}
TEST_(StoreString) {
_object.set("hello", "h3110");
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
EXPECT_TRUE(_object["hello"].is<const char*>());
EXPECT_FALSE(_object["hello"].is<long>());
}
TEST_(StoreArray) {
JsonArray& arr = _jsonBuffer.createArray();
_object.set("hello", arr);
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray>());
EXPECT_TRUE(_object["hello"].is<JsonArray&>());
EXPECT_FALSE(_object["hello"].is<JsonObject&>());
}
TEST_(StoreObject) {
JsonObject& obj = _jsonBuffer.createObject();
_object.set("hello", obj);
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject>());
EXPECT_TRUE(_object["hello"].is<JsonObject&>());
EXPECT_FALSE(_object["hello"].is<JsonArray&>());
}
TEST_(StoreArraySubscript) {
JsonArray& arr = _jsonBuffer.createArray();
arr.add(42);
_object.set("a", arr[0]);
EXPECT_EQ(42, _object["a"]);
}
TEST_(StoreObjectSubscript) {
JsonObject& obj = _jsonBuffer.createObject();
obj.set("x", 42);
_object.set("a", obj["x"]);
EXPECT_EQ(42, _object["a"]);
}
TEST_(ShouldReturnTrue_WhenAllocationSucceeds) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
JsonObject& obj = jsonBuffer.createObject();
bool result = obj.set(std::string("hello"), std::string("world"));
ASSERT_TRUE(result);
}
TEST_(ShouldReturnFalse_WhenAllocationFails) {
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
JsonObject& obj = jsonBuffer.createObject();
bool result = obj.set(std::string("hello"), std::string("world"));
ASSERT_FALSE(result);
}

View File

@ -0,0 +1,137 @@
// 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 JsonObject_Subscript_Tests : public ::testing::Test {
public:
JsonObject_Subscript_Tests() : _object(_jsonBuffer.createObject()) {}
protected:
DynamicJsonBuffer _jsonBuffer;
JsonObject& _object;
};
#define TEST_(name) TEST_F(JsonObject_Subscript_Tests, name)
TEST_(SizeIncreased_WhenValuesAreAdded) {
_object["hello"] = 1;
EXPECT_EQ(1, _object.size());
}
TEST_(SizeUntouched_WhenSameValueIsAdded) {
_object["hello"] = 1;
_object["hello"] = 2;
EXPECT_EQ(1, _object.size());
}
TEST_(StoreInteger) {
_object["hello"] = 123;
EXPECT_EQ(123, _object["hello"].as<int>());
EXPECT_TRUE(_object["hello"].is<int>());
EXPECT_FALSE(_object["hello"].is<double>());
}
TEST_(StoreVolatileInteger) { // issue #415
volatile int i = 123;
_object["hello"] = i;
EXPECT_EQ(123, _object["hello"].as<int>());
EXPECT_TRUE(_object["hello"].is<int>());
EXPECT_FALSE(_object["hello"].is<double>());
}
TEST_(StoreDouble) {
_object["hello"] = 123.45;
EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_EQ(123.45, _object["hello"].as<double>());
}
TEST_(StoreDoubleWithDigits) {
_object["hello"].set(123.45, 2);
EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_EQ(123.45, _object["hello"].as<double>());
}
TEST_(StoreBoolean) {
_object["hello"] = true;
EXPECT_TRUE(_object["hello"].is<bool>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_TRUE(_object["hello"].as<bool>());
}
TEST_(StoreString) {
_object["hello"] = "h3110";
EXPECT_TRUE(_object["hello"].is<const char*>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
EXPECT_STREQ("h3110", _object["hello"].as<char*>()); // <- short hand
}
TEST_(StoreArray) {
JsonArray& arr = _jsonBuffer.createArray();
_object["hello"] = arr;
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray&>());
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray>()); // <- short hand
EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray&>());
EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray>()); // <- short hand
EXPECT_TRUE(_object["hello"].is<JsonArray&>());
EXPECT_TRUE(_object["hello"].is<JsonArray>());
EXPECT_TRUE(_object["hello"].is<const JsonArray&>());
EXPECT_TRUE(_object["hello"].is<const JsonArray>());
EXPECT_FALSE(_object["hello"].is<JsonObject&>());
}
TEST_(StoreObject) {
JsonObject& obj = _jsonBuffer.createObject();
_object["hello"] = obj;
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject&>());
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject>()); // <- short hand
EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject&>());
EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject>()); // <- short hand
EXPECT_TRUE(_object["hello"].is<JsonObject&>());
EXPECT_TRUE(_object["hello"].is<JsonObject>());
EXPECT_TRUE(_object["hello"].is<const JsonObject&>());
EXPECT_TRUE(_object["hello"].is<const JsonObject>());
EXPECT_FALSE(_object["hello"].is<JsonArray&>());
}
TEST_(StoreArraySubscript) {
JsonArray& arr = _jsonBuffer.createArray();
arr.add(42);
_object["a"] = arr[0];
EXPECT_EQ(42, _object["a"]);
}
TEST_(StoreObjectSubscript) {
JsonObject& obj = _jsonBuffer.createObject();
obj.set("x", 42);
_object["a"] = obj["x"];
EXPECT_EQ(42, _object["a"]);
}
TEST_(KeyAsCharArray) { // issue #423
char key[] = "hello";
_object[key] = 42;
EXPECT_EQ(42, _object[key]);
}