2016-01-07 22:35:12 +01:00
|
|
|
// Copyright Benoit Blanchon 2014-2016
|
2014-10-23 23:39:22 +02:00
|
|
|
// MIT License
|
|
|
|
//
|
|
|
|
// Arduino JSON library
|
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
2016-01-07 22:35:12 +01:00
|
|
|
// If you like this project, please add a star!
|
2014-10-16 16:23:24 +02:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2014-11-11 16:54:46 +01:00
|
|
|
#include <ArduinoJson.h>
|
2014-10-18 23:05:54 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
class JsonArray_PrintTo_Tests : public testing::Test {
|
2014-10-30 10:49:02 +01:00
|
|
|
public:
|
|
|
|
JsonArray_PrintTo_Tests() : array(json.createArray()) {}
|
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
protected:
|
2014-10-30 21:51:59 +01:00
|
|
|
StaticJsonBuffer<JSON_ARRAY_SIZE(2)> json;
|
2014-10-30 10:49:02 +01:00
|
|
|
JsonArray &array;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void outputMustBe(const char *expected) {
|
2015-05-09 16:53:48 +02:00
|
|
|
size_t actualLen = array.printTo(buffer, sizeof(buffer));
|
|
|
|
size_t measuredLen = array.measureLength();
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
EXPECT_STREQ(expected, buffer);
|
2015-05-09 16:53:48 +02:00
|
|
|
EXPECT_EQ(strlen(expected), actualLen);
|
|
|
|
EXPECT_EQ(strlen(expected), measuredLen);
|
2014-10-23 19:54:00 +02:00
|
|
|
}
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
private:
|
2014-10-23 19:54:00 +02:00
|
|
|
char buffer[256];
|
2014-10-16 16:23:24 +02:00
|
|
|
};
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, Empty) { outputMustBe("[]"); }
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, Null) {
|
2014-10-24 00:08:25 +02:00
|
|
|
array.add(static_cast<char *>(0));
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[null]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneString) {
|
|
|
|
array.add("hello");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[\"hello\"]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, TwoStrings) {
|
|
|
|
array.add("hello");
|
|
|
|
array.add("world");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[\"hello\",\"world\"]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneStringOverCapacity) {
|
|
|
|
array.add("hello");
|
|
|
|
array.add("world");
|
|
|
|
array.add("lost");
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[\"hello\",\"world\"]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneDoubleDefaultDigits) {
|
|
|
|
array.add(3.14159265358979323846);
|
|
|
|
outputMustBe("[3.14]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits) {
|
2016-02-14 16:18:13 +01:00
|
|
|
array.add(3.14159265358979323846, 4);
|
|
|
|
outputMustBe("[3.1416]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits_AlternativeSyntax) {
|
2015-05-23 15:32:50 +02:00
|
|
|
array.add(double_with_n_digits(3.14159265358979323846, 4));
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[3.1416]");
|
2016-02-14 16:18:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneFloatDefaultDigits) {
|
|
|
|
array.add(3.14159f);
|
|
|
|
outputMustBe("[3.14]");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneFloatFourDigits) {
|
|
|
|
array.add(3.14159f, 4);
|
|
|
|
outputMustBe("[3.1416]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneInteger) {
|
|
|
|
array.add(1);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[1]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, TwoIntegers) {
|
|
|
|
array.add(1);
|
|
|
|
array.add(2);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[1,2]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneIntegerOverCapacity) {
|
|
|
|
array.add(1);
|
|
|
|
array.add(2);
|
|
|
|
array.add(3);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[1,2]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneTrue) {
|
|
|
|
array.add(true);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[true]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneFalse) {
|
|
|
|
array.add(false);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[false]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, TwoBooleans) {
|
|
|
|
array.add(false);
|
|
|
|
array.add(true);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[false,true]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneBooleanOverCapacity) {
|
|
|
|
array.add(false);
|
|
|
|
array.add(true);
|
|
|
|
array.add(false);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[false,true]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedArray) {
|
|
|
|
array.createNestedArray();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[[]]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrintTo_Tests, OneEmptyNestedHash) {
|
|
|
|
array.createNestedObject();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
outputMustBe("[{}]");
|
2014-10-23 23:45:36 +02:00
|
|
|
}
|