Files
ArduinoJson/test/JsonValueTests.cpp

110 lines
2.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-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>
#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 {
protected:
2014-10-23 19:54:00 +02:00
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) {
2014-10-30 14:03:33 +01:00
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-30 14:03:33 +01:00
EXPECT_EQ(innerObject1, jsonValue1.asObject());
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) {
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
}
2014-10-23 19:54:00 +02:00
TEST_F(JsonValueTests, 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
}