Renamed function RawJson() to serialized()

This commit is contained in:
Benoit Blanchon
2018-07-12 09:08:20 +02:00
parent 765752261c
commit 87fa87d87b
59 changed files with 842 additions and 550 deletions

View File

@ -4,16 +4,17 @@
add_executable(JsonArrayTests
add.cpp
basics.cpp
copyFrom.cpp
copyTo.cpp
invalid.cpp
createNested.cpp
isNull.cpp
iterator.cpp
remove.cpp
set.cpp
size.cpp
std_string.cpp
subscript.cpp
undefined.cpp
)
target_link_libraries(JsonArrayTests catch)

View File

@ -100,14 +100,26 @@ TEST_CASE("JsonArray::add()") {
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should not duplicate RawJson(const char*)") {
_array.add(RawJson("{}"));
SECTION("should not duplicate serialized(const char*)") {
_array.add(serialized("{}"));
const size_t expectedSize = JSON_ARRAY_SIZE(1);
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate RawJson(char*)") {
_array.add(RawJson(const_cast<char*>("{}")));
SECTION("should duplicate serialized(char*)") {
_array.add(serialized(const_cast<char*>("{}")));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 2;
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate serialized(std::string)") {
_array.add(serialized(std::string("{}")));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 2;
REQUIRE(expectedSize == doc.memoryUsage());
}
SECTION("should duplicate serialized(std::string)") {
_array.add(serialized(std::string("\0XX", 3)));
const size_t expectedSize = JSON_ARRAY_SIZE(1) + 3;
REQUIRE(expectedSize == doc.memoryUsage());
}

View File

@ -9,14 +9,6 @@ TEST_CASE("JsonArray basics") {
DynamicJsonDocument doc;
JsonArray array = doc.to<JsonArray>();
SECTION("isNull()") {
REQUIRE(array.isNull() == false);
}
SECTION("InitialSizeIsZero") {
REQUIRE(0U == array.size());
}
SECTION("CreateNestedArray") {
JsonArray arr = array.createNestedArray();
REQUIRE(arr == array[0].as<JsonArray>());

25
test/JsonArray/isNull.cpp Normal file
View File

@ -0,0 +1,25 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonArray::isNull()") {
SECTION("returns true for undefined JsonArray") {
JsonArray array;
REQUIRE(array.isNull() == true);
}
SECTION("returns false when allocation succeeds") {
StaticJsonDocument<JSON_ARRAY_SIZE(0)> doc;
JsonArray array = doc.to<JsonArray>();
REQUIRE(array.isNull() == false);
}
SECTION("returns true when allocation fails") {
StaticJsonDocument<1> doc;
JsonArray array = doc.to<JsonArray>();
REQUIRE(array.isNull() == true);
}
}

View File

@ -7,29 +7,33 @@
TEST_CASE("JsonArray::size()") {
DynamicJsonDocument doc;
JsonArray _array = doc.to<JsonArray>();
JsonArray array = doc.to<JsonArray>();
SECTION("InitialSizeIsZero") {
REQUIRE(0U == array.size());
}
SECTION("increases after add()") {
_array.add("hello");
REQUIRE(1U == _array.size());
array.add("hello");
REQUIRE(1U == array.size());
_array.add("world");
REQUIRE(2U == _array.size());
array.add("world");
REQUIRE(2U == array.size());
}
SECTION("remains the same after set()") {
_array.add("hello");
REQUIRE(1U == _array.size());
array.add("hello");
REQUIRE(1U == array.size());
_array.set(0, "hello");
REQUIRE(1U == _array.size());
array.set(0, "hello");
REQUIRE(1U == array.size());
}
SECTION("remains the same after assigment") {
_array.add("hello");
REQUIRE(1U == _array.size());
array.add("hello");
REQUIRE(1U == array.size());
_array[0] = "hello";
REQUIRE(1U == _array.size());
array[0] = "hello";
REQUIRE(1U == array.size());
}
}

View File

@ -3,10 +3,10 @@
# MIT License
add_executable(JsonObjectTests
basics.cpp
containsKey.cpp
get.cpp
invalid.cpp
isNull.cpp
iterator.cpp
remove.cpp
set.cpp

View File

@ -1,15 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject basics") {
DynamicJsonDocument doc;
JsonObject obj = doc.to<JsonObject>();
SECTION("isNull()") {
REQUIRE(obj.isNull() == false);
}
}

View File

@ -0,0 +1,25 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("JsonObject::isNull()") {
SECTION("returns true for undefined JsonObject") {
JsonObject array;
REQUIRE(array.isNull() == true);
}
SECTION("returns false when allocation succeeds") {
StaticJsonDocument<JSON_OBJECT_SIZE(0)> doc;
JsonObject array = doc.to<JsonObject>();
REQUIRE(array.isNull() == false);
}
SECTION("returns true when allocation fails") {
StaticJsonDocument<1> doc;
JsonObject array = doc.to<JsonObject>();
REQUIRE(array.isNull() == true);
}
}

View File

@ -67,15 +67,15 @@ TEST_CASE("serializeJson(JsonArray)") {
check(array, "[1,2]");
}
SECTION("RawJson(const char*)") {
array.add(RawJson("{\"key\":\"value\"}"));
SECTION("serialized(const char*)") {
array.add(serialized("{\"key\":\"value\"}"));
check(array, "[{\"key\":\"value\"}]");
}
SECTION("RawJson(char*)") {
SECTION("serialized(char*)") {
char tmp[] = "{\"key\":\"value\"}";
array.add(RawJson(tmp));
array.add(serialized(tmp));
check(array, "[{\"key\":\"value\"}]");
}

View File

@ -68,9 +68,9 @@ TEST_CASE("serializeJson(JsonObject)") {
check(obj, "{\"a\":1,\"b\":2}");
}
SECTION("RawJson") {
obj["a"] = RawJson("[1,2]");
obj.set("b", RawJson("[4,5]"));
SECTION("serialized(const char*)") {
obj["a"] = serialized("[1,2]");
obj.set("b", serialized("[4,5]"));
check(obj, "{\"a\":[1,2],\"b\":[4,5]}");
}

View File

@ -95,21 +95,4 @@ TEST_CASE("JsonVariant::is()") {
SECTION("string") {
checkIsString("42");
}
SECTION("unparsed bool") {
checkIsBool(RawJson("true"));
checkIsBool(RawJson("false"));
}
SECTION("unparsed int") {
checkIsInteger(RawJson("42"));
}
SECTION("unparsed float") {
checkIsFloat(RawJson("4.2e-10"));
}
SECTION("unparsed null") {
checkIsString(RawJson("null"));
}
}

View File

@ -7,15 +7,15 @@
#include <string>
#include <ArduinoJson/Json/JsonWriter.hpp>
#include <ArduinoJson/Serialization/DynamicStringBuilder.hpp>
#include <ArduinoJson/Serialization/DynamicStringWriter.hpp>
using namespace ArduinoJson::Internals;
template <typename TFloat>
void check(TFloat input, const std::string& expected) {
std::string output;
DynamicStringBuilder<std::string> sb(output);
JsonWriter<DynamicStringBuilder<std::string> > writer(sb);
DynamicStringWriter<std::string> sb(output);
JsonWriter<DynamicStringWriter<std::string> > writer(sb);
writer.writeFloat(input);
REQUIRE(writer.bytesWritten() == output.size());
CHECK(expected == output);

View File

@ -5,14 +5,14 @@
#include <catch.hpp>
#include <ArduinoJson/Json/JsonWriter.hpp>
#include <ArduinoJson/Serialization/StaticStringBuilder.hpp>
#include <ArduinoJson/Serialization/StaticStringWriter.hpp>
using namespace ArduinoJson::Internals;
void check(const char* input, std::string expected) {
char output[1024];
StaticStringBuilder sb(output, sizeof(output));
JsonWriter<StaticStringBuilder> writer(sb);
StaticStringWriter sb(output, sizeof(output));
JsonWriter<StaticStringWriter> writer(sb);
writer.writeString(input);
REQUIRE(expected == output);
REQUIRE(writer.bytesWritten() == expected.size());

View File

@ -4,8 +4,7 @@
add_executable(MiscTests
FloatParts.cpp
StringBuilder.cpp
StringTraits.cpp
StringWriter.cpp
TypeTraits.cpp
unsigned_char.cpp
version.cpp

View File

@ -1,22 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ArduinoJson::Internals;
template <typename String>
bool should_duplicate() {
return StringTraits<String>::should_duplicate;
}
TEST_CASE("StringTraits") {
SECTION("should_duplicate") {
REQUIRE(false == should_duplicate<const char*>());
REQUIRE(true == should_duplicate<char*>());
REQUIRE(true == should_duplicate<RawJsonString<char*> >());
REQUIRE(false == should_duplicate<RawJsonString<const char*> >());
}
}

View File

@ -7,44 +7,49 @@
using namespace ArduinoJson::Internals;
template <typename StringBuilder, typename String>
void common_tests(StringBuilder& sb, const String& output) {
template <typename StringWriter>
static size_t print(StringWriter& sb, const char* s) {
return sb.write(reinterpret_cast<const uint8_t*>(s), strlen(s));
}
template <typename StringWriter, typename String>
void common_tests(StringWriter& sb, const String& output) {
SECTION("InitialState") {
REQUIRE(std::string("") == output);
}
SECTION("EmptyString") {
REQUIRE(0 == sb.print(""));
REQUIRE(0 == print(sb, ""));
REQUIRE(std::string("") == output);
}
SECTION("OneString") {
REQUIRE(4 == sb.print("ABCD"));
REQUIRE(4 == print(sb, "ABCD"));
REQUIRE(std::string("ABCD") == output);
}
SECTION("TwoStrings") {
REQUIRE(4 == sb.print("ABCD"));
REQUIRE(4 == sb.print("EFGH"));
REQUIRE(4 == print(sb, "ABCD"));
REQUIRE(4 == print(sb, "EFGH"));
REQUIRE(std::string("ABCDEFGH") == output);
}
}
TEST_CASE("StaticStringBuilder") {
TEST_CASE("StaticStringWriter") {
char output[20];
StaticStringBuilder sb(output, sizeof(output));
StaticStringWriter sb(output, sizeof(output));
common_tests(sb, static_cast<const char*>(output));
SECTION("OverCapacity") {
REQUIRE(19 == sb.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
REQUIRE(0 == sb.print("ABC"));
REQUIRE(19 == print(sb, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
REQUIRE(0 == print(sb, "ABC"));
REQUIRE(std::string("ABCDEFGHIJKLMNOPQRS") == output);
}
}
TEST_CASE("DynamicStringBuilder") {
TEST_CASE("DynamicStringWriter") {
std::string output;
DynamicStringBuilder<std::string> sb(output);
DynamicStringWriter<std::string> sb(output);
common_tests(sb, output);
}

View File

@ -70,4 +70,14 @@ TEST_CASE("serialize MsgPack object") {
//
// check(object, expected);
// }
SECTION("serialized(const char*)") {
object["hello"] = serialized("\xDB\x00\x01\x00\x00", 5);
check(object, "\x81\xA5hello\xDB\x00\x01\x00\x00");
}
SECTION("serialized(std::string)") {
object["hello"] = serialized(std::string("\xDB\x00\x01\x00\x00", 5));
check(object, "\x81\xA5hello\xDB\x00\x01\x00\x00");
}
}

View File

@ -126,4 +126,9 @@ TEST_CASE("serialize MsgPack value") {
std::string shortest(65536, '?');
check(shortest.c_str(), std::string("\xDB\x00\x01\x00\x00", 5) + shortest);
}
SECTION("serialized(const char*)") {
check(serialized("\xDA\xFF\xFF"), "\xDA\xFF\xFF");
check(serialized("\xDB\x00\x01\x00\x00", 5), "\xDB\x00\x01\x00\x00");
}
}