From bc86ae800ab3e461e88b6b4fc10b75735ec1ad83 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Mon, 4 Aug 2014 09:22:45 +0200 Subject: [PATCH] Changed JsonArray tests to show the issue --- JsonGenerator/JsonArrayBase.h | 11 ++++++++--- JsonGenerator/JsonValue.h | 14 +++++++------- JsonGeneratorTests/JsonArrayTests.cpp | 17 +++++++++-------- JsonGeneratorTests/JsonValue_Cast_Tests.cpp | 2 +- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/JsonGenerator/JsonArrayBase.h b/JsonGenerator/JsonArrayBase.h index ab1f8aee..1e77bc54 100644 --- a/JsonGenerator/JsonArrayBase.h +++ b/JsonGenerator/JsonArrayBase.h @@ -20,12 +20,17 @@ namespace ArduinoJson } + void add(const Printable& nestedObject) + { + if (count < capacity) + items[count++] = nestedObject; + } + template void add(T value) { - if (count >= capacity) return; - - items[count++] = value; + if (count < capacity) + items[count++] = value; } template diff --git a/JsonGenerator/JsonValue.h b/JsonGenerator/JsonValue.h index ab3283eb..5c080818 100644 --- a/JsonGenerator/JsonValue.h +++ b/JsonGenerator/JsonValue.h @@ -35,7 +35,7 @@ namespace ArduinoJson content.asLong = value; } - void operator=(Printable& value) + void operator=(const Printable& value) { printToImpl = &printPrintableTo; content.asPrintable = &value; @@ -89,7 +89,7 @@ namespace ArduinoJson return content.asLong; } - operator Printable&() + operator const Printable&() { return *content.asPrintable; } @@ -109,11 +109,11 @@ namespace ArduinoJson private: union Content { - bool asBool; - double asDouble; - long asLong; - Printable* asPrintable; - const char* asString; + bool asBool; + double asDouble; + long asLong; + const Printable* asPrintable; + const char* asString; }; Content content; diff --git a/JsonGeneratorTests/JsonArrayTests.cpp b/JsonGeneratorTests/JsonArrayTests.cpp index f26135a2..10e27f73 100644 --- a/JsonGeneratorTests/JsonArrayTests.cpp +++ b/JsonGeneratorTests/JsonArrayTests.cpp @@ -124,13 +124,19 @@ namespace JsonGeneratorTests TEST_METHOD(OneEmptyNestedArray) { - addNested(JsonArray<1>()); + JsonArray<1> nestedArray; + + arr.add(nestedArray); + outputMustBe("[[]]"); } TEST_METHOD(OneEmptyNestedHash) { - addNested(JsonHashTable<1>()); + JsonObject<1> nestedObject; + + arr.add(nestedObject); + outputMustBe("[{}]"); } @@ -139,18 +145,13 @@ namespace JsonGeneratorTests JsonArray<1> nestedArray; nestedArray.add(1); - addNested(nestedArray); + arr.add(nestedArray); outputMustBe("[[1]]"); } private: - void addNested(Printable& value) - { - arr.add(value); - } - template void add(T value) { diff --git a/JsonGeneratorTests/JsonValue_Cast_Tests.cpp b/JsonGeneratorTests/JsonValue_Cast_Tests.cpp index edaa2fe8..a863f9a5 100644 --- a/JsonGeneratorTests/JsonValue_Cast_Tests.cpp +++ b/JsonGeneratorTests/JsonValue_Cast_Tests.cpp @@ -71,7 +71,7 @@ namespace JsonGeneratorTests void setValueAndCheckCast(JsonArray& expected) { value = expected; - Printable& actual = value; + const Printable& actual = value; Assert::AreEqual((void*) &expected, (void*) &actual); } };