Files
ArduinoJson/test/JsonValue_Copy_Tests.cpp

73 lines
1.6 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-16 16:23:24 +02:00
#include <gtest/gtest.h>
#include <ArduinoJson/StaticJsonBuffer.hpp>
2014-10-27 22:50:50 +01:00
#include <ArduinoJson/JsonArray.hpp>
#include <ArduinoJson/JsonObject.hpp>
#include <ArduinoJson/JsonValue.hpp>
2014-10-16 16:23:24 +02:00
2014-10-18 23:05:54 +02:00
using namespace ArduinoJson;
class JsonValue_Copy_Tests : public ::testing::Test {
protected:
2014-10-30 21:51:59 +01:00
StaticJsonBuffer<200> json;
2014-10-23 19:54:00 +02:00
JsonValue jsonValue1;
JsonValue jsonValue2;
2014-10-16 16:23:24 +02:00
};
TEST_F(JsonValue_Copy_Tests, IntegersAreCopiedByValue) {
2014-10-23 19:54:00 +02:00
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
}
TEST_F(JsonValue_Copy_Tests, DoublesAreCopiedByValue) {
2014-10-23 19:54:00 +02:00
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
}
TEST_F(JsonValue_Copy_Tests, BooleansAreCopiedByValue) {
2014-10-23 19:54:00 +02:00
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
}
TEST_F(JsonValue_Copy_Tests, StringsAreCopiedByValue) {
2014-10-23 19:54:00 +02:00
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
}
TEST_F(JsonValue_Copy_Tests, ObjectsAreCopiedByReference) {
2014-10-30 14:03:33 +01:00
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-30 14:03:33 +01:00
EXPECT_EQ(1, jsonValue1.asObject().size());
2014-10-16 16:23:24 +02:00
}
TEST_F(JsonValue_Copy_Tests, ArraysAreCopiedByReference) {
2014-10-30 14:03:33 +01:00
JsonArray &array = json.createArray();
2014-10-23 19:54:00 +02:00
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-30 14:03:33 +01:00
EXPECT_EQ(1, jsonValue1.asArray().size());
2014-10-23 23:45:36 +02:00
}