Added many tests storing values in JsonValue

This commit is contained in:
Benoit Blanchon
2014-11-03 17:50:01 +01:00
parent 1ce6d663af
commit f9ea82a2af
3 changed files with 71 additions and 42 deletions

View File

@ -25,10 +25,16 @@ class JsonValue {
JsonValue() : _type(Internals::JSON_UNDEFINED) {}
void set(bool value);
void set(const char *value);
void set(double value, int decimals = 2);
void set(int value) { set(static_cast<long>(value)); }
void set(long value);
void set(signed char value) { set(static_cast<long>(value)); }
void set(signed int value) { set(static_cast<long>(value)); }
void set(signed long value);
void set(signed short value) { set(static_cast<long>(value)); }
void set(unsigned char value) { set(static_cast<long>(value)); }
void set(unsigned int value) { set(static_cast<long>(value)); }
void set(unsigned long value) { set(static_cast<long>(value)); }
void set(unsigned short value) { set(static_cast<long>(value)); }
void set(const char *value);
void set(JsonArray &array);
void set(JsonObject &object);
@ -49,10 +55,17 @@ class JsonValue {
}
operator bool() const;
operator const char *() const;
operator double() const;
operator long() const;
operator int() const { return operator long(); }
operator float() const { return as<double>(); }
operator signed char() const { return as<long>(); }
operator signed int() const { return as<long>(); }
operator signed long() const;
operator signed short() const { return as<long>(); }
operator unsigned char() const { return as<long>(); }
operator unsigned int() const { return as<long>(); }
operator unsigned long() const { return as<long>(); }
operator unsigned short() const { return as<long>(); }
operator const char *() const;
operator JsonArray &() const;
operator JsonObject &() const;

View File

@ -19,42 +19,6 @@ class JsonValueTests : public ::testing::Test {
JsonValue jsonValue2;
};
TEST_F(JsonValueTests, CanStoreInteger) {
jsonValue1 = 123;
EXPECT_EQ(123, jsonValue1.as<int>());
}
TEST_F(JsonValueTests, CanStoreDouble) {
jsonValue1 = 123.45;
EXPECT_EQ(123.45, jsonValue1.as<double>());
}
TEST_F(JsonValueTests, CanStoreTrue) {
jsonValue1 = true;
EXPECT_TRUE(jsonValue1.as<bool>());
}
TEST_F(JsonValueTests, CanStoreFalse) {
jsonValue1 = false;
EXPECT_FALSE(jsonValue1.as<bool>());
}
TEST_F(JsonValueTests, CanStoreString) {
jsonValue1 = "hello";
EXPECT_STREQ("hello", jsonValue1.as<const char *>());
}
TEST_F(JsonValueTests, CanStoreObject) {
JsonObject &innerObject1 = json.createObject();
jsonValue1 = innerObject1;
EXPECT_EQ(innerObject1, jsonValue1.asObject());
}
TEST_F(JsonValueTests, IntegersAreCopiedByValue) {
jsonValue1 = 123;
jsonValue2 = jsonValue1;

View File

@ -0,0 +1,52 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include <gtest/gtest.h>
#include <ArduinoJson/StaticJsonBuffer.hpp>
#include <ArduinoJson/JsonArray.hpp>
#include <ArduinoJson/JsonObject.hpp>
#include <ArduinoJson/JsonValue.hpp>
using namespace ArduinoJson;
class JsonValue_Storage_Tests : public ::testing::Test {
protected:
template <typename T>
void testValue(T expected) {
jsonValue.set(expected);
EXPECT_EQ(expected, jsonValue.as<T>());
}
template <typename T>
void testReference(T &expected) {
jsonValue.set(expected);
EXPECT_EQ(expected, jsonValue.as<T &>());
}
JsonValue jsonValue;
};
TEST_F(JsonValue_Storage_Tests, Double) { testValue<double>(123.45); }
TEST_F(JsonValue_Storage_Tests, False) { testValue<bool>(false); }
TEST_F(JsonValue_Storage_Tests, Float) { testValue<float>(123.45f); }
TEST_F(JsonValue_Storage_Tests, Null) { testValue<const char *>(NULL); }
TEST_F(JsonValue_Storage_Tests, SChar) { testValue<signed char>(123); }
TEST_F(JsonValue_Storage_Tests, SInt) { testValue<signed int>(123); }
TEST_F(JsonValue_Storage_Tests, SLong) { testValue<signed long>(123L); }
TEST_F(JsonValue_Storage_Tests, SShort) { testValue<signed short>(123); }
TEST_F(JsonValue_Storage_Tests, String) { testValue<const char *>("hello"); }
TEST_F(JsonValue_Storage_Tests, True) { testValue<bool>(true); }
TEST_F(JsonValue_Storage_Tests, UChar) { testValue<unsigned char>(123); }
TEST_F(JsonValue_Storage_Tests, UInt) { testValue<unsigned int>(123U); }
TEST_F(JsonValue_Storage_Tests, ULong) { testValue<unsigned long>(123UL); }
TEST_F(JsonValue_Storage_Tests, UShort) { testValue<unsigned short>(123); }
TEST_F(JsonValue_Storage_Tests, CanStoreObject) {
StaticJsonBuffer<200> json;
JsonObject &object = json.createObject();
testReference(object);
}