2014-10-23 23:39:22 +02:00
|
|
|
// Copyright Benoit Blanchon 2014
|
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
|
|
|
|
2014-10-16 16:23:24 +02:00
|
|
|
#include <gtest/gtest.h>
|
2014-10-19 15:46:36 +02:00
|
|
|
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
2014-10-27 22:50:50 +01:00
|
|
|
#include <ArduinoJson/JsonArray.hpp>
|
|
|
|
#include <ArduinoJson/JsonObject.hpp>
|
2014-10-19 15:46:36 +02:00
|
|
|
#include <ArduinoJson/JsonValue.hpp>
|
2014-10-24 18:53:03 +02:00
|
|
|
#include "Printers.hpp"
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-18 23:05:54 +02:00
|
|
|
using namespace ArduinoJson;
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
class JsonValueTests : public ::testing::Test {
|
2014-10-23 23:13:13 +02:00
|
|
|
protected:
|
2014-10-23 19:54:00 +02:00
|
|
|
virtual void SetUp() {
|
|
|
|
jsonValue1 = json.createValue();
|
|
|
|
jsonValue2 = json.createValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticJsonBuffer<42> json;
|
|
|
|
JsonValue jsonValue1;
|
|
|
|
JsonValue jsonValue2;
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, CanStoreInteger) {
|
|
|
|
jsonValue1 = 123;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(123, jsonValue1.as<int>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, CanStoreDouble) {
|
|
|
|
jsonValue1 = 123.45;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(123.45, jsonValue1.as<double>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, CanStoreTrue) {
|
|
|
|
jsonValue1 = true;
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_TRUE(jsonValue1.as<bool>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, CanStoreFalse) {
|
|
|
|
jsonValue1 = false;
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_FALSE(jsonValue1.as<bool>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, CanStoreString) {
|
|
|
|
jsonValue1 = "hello";
|
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_STREQ("hello", jsonValue1.as<const char *>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, CanStoreObject) {
|
|
|
|
JsonObject innerObject1 = json.createObject();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
jsonValue1 = innerObject1;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(innerObject1, jsonValue1.as<JsonObject>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, IntegersAreCopiedByValue) {
|
|
|
|
jsonValue1 = 123;
|
|
|
|
jsonValue2 = jsonValue1;
|
|
|
|
jsonValue1 = 456;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(123, jsonValue2.as<int>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, DoublesAreCopiedByValue) {
|
|
|
|
jsonValue1 = 123.45;
|
|
|
|
jsonValue2 = jsonValue1;
|
|
|
|
jsonValue1 = 456.78;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(123.45, jsonValue2.as<double>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, BooleansAreCopiedByValue) {
|
|
|
|
jsonValue1 = true;
|
|
|
|
jsonValue2 = jsonValue1;
|
|
|
|
jsonValue1 = false;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_TRUE(jsonValue2.as<bool>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, StringsAreCopiedByValue) {
|
|
|
|
jsonValue1 = "hello";
|
|
|
|
jsonValue2 = jsonValue1;
|
|
|
|
jsonValue1 = "world";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_STREQ("hello", jsonValue2.as<const char *>());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, ObjectsAreCopiedByReference) {
|
|
|
|
JsonObject object = json.createObject();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
jsonValue1 = object;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
object["hello"] = "world";
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(1, jsonValue1.as<JsonObject>().size());
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonValueTests, ArraysAreCopiedByReference) {
|
|
|
|
JsonArray array = json.createArray();
|
|
|
|
|
|
|
|
jsonValue1 = array;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
array.add("world");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-24 00:08:25 +02:00
|
|
|
EXPECT_EQ(1, jsonValue1.as<JsonArray>().size());
|
2014-10-23 23:45:36 +02:00
|
|
|
}
|