Files
ArduinoJson/test/JsonArray/printTo.cpp

153 lines
3.0 KiB
C++
Raw Normal View History

// Copyright Benoit Blanchon 2014-2017
2014-10-23 23:39:22 +02:00
// MIT License
//
// Arduino JSON library
2017-03-25 22:05:06 +01:00
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!
2014-10-16 16:23:24 +02:00
#include <ArduinoJson.h>
#include <gtest/gtest.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()) {}
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) {
size_t actualLen = array.printTo(buffer);
size_t measuredLen = array.measureLength();
2014-10-23 19:54:00 +02:00
EXPECT_STREQ(expected, buffer);
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
private:
2014-10-23 19:54:00 +02:00
char buffer[256];
2014-10-16 16:23:24 +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) {
array.add(3.14159265358979323846, 4);
outputMustBe("[3.1416]");
}
TEST_F(JsonArray_PrintTo_Tests, OneDoubleFourDigits_AlternativeSyntax) {
array.add(double_with_n_digits(3.14159265358979323846, 4));
2014-10-23 19:54:00 +02:00
outputMustBe("[3.1416]");
}
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
}
TEST_F(JsonArray_PrintTo_Tests, RawJson) {
array.add(RawJson("{\"key\":\"value\"}"));
outputMustBe("[{\"key\":\"value\"}]");
}
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
}