From f9ea82a2aff268ff2b797b393988707be30013fe Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 3 Nov 2014 17:50:01 +0100 Subject: [PATCH] Added many tests storing values in JsonValue --- include/ArduinoJson/JsonValue.hpp | 25 +++++++++++---- test/JsonValueTests.cpp | 36 --------------------- test/JsonValue_Storage_Tests.cpp | 52 +++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 42 deletions(-) create mode 100644 test/JsonValue_Storage_Tests.cpp diff --git a/include/ArduinoJson/JsonValue.hpp b/include/ArduinoJson/JsonValue.hpp index 015c105c..0a6d00b8 100644 --- a/include/ArduinoJson/JsonValue.hpp +++ b/include/ArduinoJson/JsonValue.hpp @@ -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(value)); } - void set(long value); + void set(signed char value) { set(static_cast(value)); } + void set(signed int value) { set(static_cast(value)); } + void set(signed long value); + void set(signed short value) { set(static_cast(value)); } + void set(unsigned char value) { set(static_cast(value)); } + void set(unsigned int value) { set(static_cast(value)); } + void set(unsigned long value) { set(static_cast(value)); } + void set(unsigned short value) { set(static_cast(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(); } + operator signed char() const { return as(); } + operator signed int() const { return as(); } + operator signed long() const; + operator signed short() const { return as(); } + operator unsigned char() const { return as(); } + operator unsigned int() const { return as(); } + operator unsigned long() const { return as(); } + operator unsigned short() const { return as(); } + operator const char *() const; operator JsonArray &() const; operator JsonObject &() const; diff --git a/test/JsonValueTests.cpp b/test/JsonValueTests.cpp index 9882a3fc..92093afc 100644 --- a/test/JsonValueTests.cpp +++ b/test/JsonValueTests.cpp @@ -19,42 +19,6 @@ class JsonValueTests : public ::testing::Test { JsonValue jsonValue2; }; -TEST_F(JsonValueTests, CanStoreInteger) { - jsonValue1 = 123; - - EXPECT_EQ(123, jsonValue1.as()); -} - -TEST_F(JsonValueTests, CanStoreDouble) { - jsonValue1 = 123.45; - - EXPECT_EQ(123.45, jsonValue1.as()); -} - -TEST_F(JsonValueTests, CanStoreTrue) { - jsonValue1 = true; - EXPECT_TRUE(jsonValue1.as()); -} - -TEST_F(JsonValueTests, CanStoreFalse) { - jsonValue1 = false; - EXPECT_FALSE(jsonValue1.as()); -} - -TEST_F(JsonValueTests, CanStoreString) { - jsonValue1 = "hello"; - - EXPECT_STREQ("hello", jsonValue1.as()); -} - -TEST_F(JsonValueTests, CanStoreObject) { - JsonObject &innerObject1 = json.createObject(); - - jsonValue1 = innerObject1; - - EXPECT_EQ(innerObject1, jsonValue1.asObject()); -} - TEST_F(JsonValueTests, IntegersAreCopiedByValue) { jsonValue1 = 123; jsonValue2 = jsonValue1; diff --git a/test/JsonValue_Storage_Tests.cpp b/test/JsonValue_Storage_Tests.cpp new file mode 100644 index 00000000..29af9ab0 --- /dev/null +++ b/test/JsonValue_Storage_Tests.cpp @@ -0,0 +1,52 @@ +// Copyright Benoit Blanchon 2014 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson + +#include +#include +#include +#include +#include + +using namespace ArduinoJson; + +class JsonValue_Storage_Tests : public ::testing::Test { + protected: + template + void testValue(T expected) { + jsonValue.set(expected); + EXPECT_EQ(expected, jsonValue.as()); + } + + template + void testReference(T &expected) { + jsonValue.set(expected); + EXPECT_EQ(expected, jsonValue.as()); + } + + JsonValue jsonValue; +}; + +TEST_F(JsonValue_Storage_Tests, Double) { testValue(123.45); } +TEST_F(JsonValue_Storage_Tests, False) { testValue(false); } +TEST_F(JsonValue_Storage_Tests, Float) { testValue(123.45f); } +TEST_F(JsonValue_Storage_Tests, Null) { testValue(NULL); } +TEST_F(JsonValue_Storage_Tests, SChar) { testValue(123); } +TEST_F(JsonValue_Storage_Tests, SInt) { testValue(123); } +TEST_F(JsonValue_Storage_Tests, SLong) { testValue(123L); } +TEST_F(JsonValue_Storage_Tests, SShort) { testValue(123); } +TEST_F(JsonValue_Storage_Tests, String) { testValue("hello"); } +TEST_F(JsonValue_Storage_Tests, True) { testValue(true); } +TEST_F(JsonValue_Storage_Tests, UChar) { testValue(123); } +TEST_F(JsonValue_Storage_Tests, UInt) { testValue(123U); } +TEST_F(JsonValue_Storage_Tests, ULong) { testValue(123UL); } +TEST_F(JsonValue_Storage_Tests, UShort) { testValue(123); } + +TEST_F(JsonValue_Storage_Tests, CanStoreObject) { + StaticJsonBuffer<200> json; + JsonObject &object = json.createObject(); + + testReference(object); +}