2014-10-16 16:23:24 +02:00
|
|
|
/*
|
|
|
|
* Arduino JSON library
|
|
|
|
* Benoit Blanchon 2014 - MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
2014-10-19 15:46:36 +02:00
|
|
|
#include <ArduinoJson/JsonArray.hpp>
|
|
|
|
#include <ArduinoJson/JsonObject.hpp>
|
|
|
|
#include <ArduinoJson/JsonValue.hpp>
|
|
|
|
#include <ArduinoJson/StaticJsonBuffer.hpp>
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-18 23:05:54 +02:00
|
|
|
using namespace ArduinoJson;
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
2014-10-23 23:13:13 +02:00
|
|
|
protected:
|
2014-10-23 19:54:00 +02:00
|
|
|
JsonArray array;
|
|
|
|
StaticJsonBuffer<30> json;
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
virtual void SetUp() { array = json.createArray(); }
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
void outputMustBe(const char *expected) {
|
|
|
|
size_t n = array.prettyPrintTo(buffer, sizeof(buffer));
|
|
|
|
EXPECT_STREQ(expected, buffer);
|
|
|
|
EXPECT_EQ(strlen(expected), n);
|
|
|
|
}
|
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_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
|
|
|
|
array.add(1);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"[\r\n"
|
|
|
|
" 1\r\n"
|
|
|
|
"]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrettyPrintTo_Tests, TwoElements) {
|
|
|
|
array.add(1);
|
|
|
|
array.add(2);
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"[\r\n"
|
|
|
|
" 1,\r\n"
|
|
|
|
" 2\r\n"
|
|
|
|
"]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrettyPrintTo_Tests, EmptyNestedArrays) {
|
|
|
|
array.createNestedArray();
|
|
|
|
array.createNestedArray();
|
2014-10-16 16:23:24 +02:00
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"[\r\n"
|
|
|
|
" [],\r\n"
|
|
|
|
" []\r\n"
|
|
|
|
"]");
|
2014-10-16 16:23:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 19:54:00 +02:00
|
|
|
TEST_F(JsonArray_PrettyPrintTo_Tests, NestedArrays) {
|
|
|
|
JsonArray nested1 = array.createNestedArray();
|
|
|
|
nested1.add(1);
|
|
|
|
nested1.add(2);
|
|
|
|
|
|
|
|
JsonObject nested2 = array.createNestedObject();
|
|
|
|
nested2["key"] = 3;
|
|
|
|
|
2014-10-23 23:13:13 +02:00
|
|
|
outputMustBe(
|
|
|
|
"[\r\n"
|
|
|
|
" [\r\n"
|
|
|
|
" 1,\r\n"
|
|
|
|
" 2\r\n"
|
|
|
|
" ],\r\n"
|
|
|
|
" {\r\n"
|
|
|
|
" \"key\": 3\r\n"
|
|
|
|
" }\r\n"
|
|
|
|
"]");
|
2014-10-07 11:22:10 +02:00
|
|
|
}
|