11
11
using namespace ARDUINOJSON_NAMESPACE ;
12
12
13
13
template <typename StringWriter>
14
- static size_t print (StringWriter& sb , const char * s) {
15
- return sb .write (reinterpret_cast <const uint8_t *>(s), strlen (s));
14
+ static size_t print (StringWriter& writer , const char * s) {
15
+ return writer .write (reinterpret_cast <const uint8_t *>(s), strlen (s));
16
16
}
17
17
18
18
template <typename StringWriter, typename String>
19
- void common_tests (StringWriter& sb , const String& output) {
19
+ void common_tests (StringWriter& writer , const String& output) {
20
20
SECTION (" InitialState" ) {
21
21
REQUIRE (std::string (" " ) == output);
22
22
}
23
23
24
24
SECTION (" EmptyString" ) {
25
- REQUIRE (0 == print (sb , " " ));
25
+ REQUIRE (0 == print (writer , " " ));
26
26
REQUIRE (std::string (" " ) == output);
27
27
}
28
28
29
29
SECTION (" OneString" ) {
30
- REQUIRE (4 == print (sb , " ABCD" ));
30
+ REQUIRE (4 == print (writer , " ABCD" ));
31
31
REQUIRE (std::string (" ABCD" ) == output);
32
32
}
33
33
34
34
SECTION (" TwoStrings" ) {
35
- REQUIRE (4 == print (sb , " ABCD" ));
36
- REQUIRE (4 == print (sb , " EFGH" ));
35
+ REQUIRE (4 == print (writer , " ABCD" ));
36
+ REQUIRE (4 == print (writer , " EFGH" ));
37
37
REQUIRE (std::string (" ABCDEFGH" ) == output);
38
38
}
39
39
}
40
40
41
41
TEST_CASE (" StaticStringWriter" ) {
42
42
char output[20 ] = {0 };
43
- StaticStringWriter sb (output, sizeof (output));
43
+ StaticStringWriter writer (output, sizeof (output));
44
44
45
- common_tests (sb , static_cast <const char *>(output));
45
+ common_tests (writer , static_cast <const char *>(output));
46
46
47
47
SECTION (" OverCapacity" ) {
48
- REQUIRE (20 == print (sb , " ABCDEFGHIJKLMNOPQRSTUVWXYZ" ));
49
- REQUIRE (0 == print (sb , " ABC" ));
48
+ REQUIRE (20 == print (writer , " ABCDEFGHIJKLMNOPQRSTUVWXYZ" ));
49
+ REQUIRE (0 == print (writer , " ABC" ));
50
50
REQUIRE (" ABCDEFGHIJKLMNOPQRST" == std::string (output, 20 ));
51
51
}
52
52
}
53
53
54
54
TEST_CASE (" Writer<std::string>" ) {
55
55
std::string output;
56
- Writer<std::string> sb (output);
57
- common_tests (sb , output);
56
+ Writer<std::string> writer (output);
57
+ common_tests (writer , output);
58
58
}
59
59
60
60
TEST_CASE (" Writer<String>" ) {
61
61
::String output;
62
- Writer< ::String> sb (output);
63
-
64
- common_tests (sb, output);
65
-
66
- SECTION (" Writes characters to temporary buffer" ) {
67
- // accumulate in buffer
68
- sb.write (' a' );
69
- sb.write (' b' );
70
- sb.write (' c' );
71
- REQUIRE (output == " " );
72
-
73
- // flush when full
74
- sb.write (' d' );
75
- REQUIRE (output == " abcd" );
76
-
77
- // flush on destruction
78
- sb.write (' e' );
79
- sb.~Writer ();
80
- REQUIRE (output == " abcde" );
62
+ Writer< ::String> writer (output);
63
+
64
+ SECTION (" write(char)" ) {
65
+ SECTION (" writes to temporary buffer" ) {
66
+ // accumulate in buffer
67
+ writer.write (' a' );
68
+ writer.write (' b' );
69
+ writer.write (' c' );
70
+ writer.write (' d' );
71
+ REQUIRE (output == " " );
72
+
73
+ // flush when full
74
+ writer.write (' e' );
75
+ REQUIRE (output == " abcd" );
76
+
77
+ // flush on destruction
78
+ writer.write (' f' );
79
+ writer.~Writer ();
80
+ REQUIRE (output == " abcdef" );
81
+ }
82
+
83
+ SECTION (" returns 1 on success" ) {
84
+ for (int i = 0 ; i < ARDUINOJSON_STRING_BUFFER_SIZE; i++) {
85
+ REQUIRE (writer.write (' x' ) == 1 );
86
+ }
87
+ }
88
+
89
+ SECTION (" returns 0 on error" ) {
90
+ output.limitCapacityTo (1 );
91
+
92
+ REQUIRE (writer.write (' a' ) == 1 );
93
+ REQUIRE (writer.write (' b' ) == 1 );
94
+ REQUIRE (writer.write (' c' ) == 1 );
95
+ REQUIRE (writer.write (' d' ) == 1 );
96
+ REQUIRE (writer.write (' e' ) == 0 );
97
+ REQUIRE (writer.write (' f' ) == 0 );
98
+ }
81
99
}
82
100
83
- SECTION (" Writes strings to temporary buffer" ) {
84
- // accumulate in buffer
85
- print (sb, " abc" );
86
- REQUIRE (output == " " );
87
-
88
- // flush when full, and continue to accumulate
89
- print (sb, " de" );
90
- REQUIRE (output == " abcd" );
91
-
92
- // flush on destruction
93
- sb.~Writer ();
94
- REQUIRE (output == " abcde" );
101
+ SECTION (" write(char*, size_t)" ) {
102
+ SECTION (" empty string" ) {
103
+ REQUIRE (0 == print (writer, " " ));
104
+ writer.flush ();
105
+ REQUIRE (output == " " );
106
+ }
107
+
108
+ SECTION (" writes to temporary buffer" ) {
109
+ // accumulate in buffer
110
+ print (writer, " abc" );
111
+ REQUIRE (output == " " );
112
+
113
+ // flush when full, and continue to accumulate
114
+ print (writer, " de" );
115
+ REQUIRE (output == " abcd" );
116
+
117
+ // flush on destruction
118
+ writer.~Writer ();
119
+ REQUIRE (output == " abcde" );
120
+ }
95
121
}
96
122
}
97
123
98
124
TEST_CASE (" Writer<custom_string>" ) {
99
125
custom_string output;
100
- Writer<custom_string> sb (output);
126
+ Writer<custom_string> writer (output);
101
127
102
- REQUIRE (4 == print (sb , " ABCD" ));
128
+ REQUIRE (4 == print (writer , " ABCD" ));
103
129
REQUIRE (" ABCD" == output);
104
130
}
105
131
@@ -116,3 +142,20 @@ TEST_CASE("IsWriteableString") {
116
142
REQUIRE (IsWriteableString<std::basic_string<wchar_t > >::value == false );
117
143
}
118
144
}
145
+
146
+ TEST_CASE (" serializeJson(doc, String)" ) {
147
+ StaticJsonDocument<1024 > doc;
148
+ doc[" hello" ] = " world" ;
149
+ ::String output;
150
+
151
+ SECTION (" sufficient capacity" ) {
152
+ serializeJson (doc, output);
153
+ REQUIRE (output == " {\" hello\" :\" world\" }" );
154
+ }
155
+
156
+ SECTION (" unsufficient capacity" ) { // issue #1561
157
+ output.limitCapacityTo (10 );
158
+ serializeJson (doc, output);
159
+ REQUIRE (output == " {\" hello\" " );
160
+ }
161
+ }
0 commit comments