1414using ArduinoJson::detail::sizeofArray;
1515using ArduinoJson::detail::sizeofObject;
1616
17- using MemberProxy =
18- ArduinoJson::detail::MemberProxy<JsonDocument&, const char *>;
19-
2017TEST_CASE (" MemberProxy::add()" ) {
21- JsonDocument doc;
22- MemberProxy mp = doc[" hello" ];
18+ SpyingAllocator spy;
19+ JsonDocument doc (&spy);
20+ auto mp = doc[" hello" ];
2321
24- SECTION (" add(int) " ) {
22+ SECTION (" integer " ) {
2523 mp.add (42 );
2624
2725 REQUIRE (doc.as <std::string>() == " {\" hello\" :[42]}" );
26+ REQUIRE (spy.log () == AllocatorLog{
27+ Allocate (sizeofPool ()),
28+ });
2829 }
2930
30- SECTION (" add(const char*) " ) {
31+ SECTION (" string literal " ) {
3132 mp.add (" world" );
3233
3334 REQUIRE (doc.as <std::string>() == " {\" hello\" :[\" world\" ]}" );
35+ REQUIRE (spy.log () == AllocatorLog{
36+ Allocate (sizeofPool ()),
37+ });
38+ }
39+
40+ SECTION (" const char*" ) {
41+ const char * temp = " world" ;
42+ mp.add (temp);
43+
44+ REQUIRE (doc.as <std::string>() == " {\" hello\" :[\" world\" ]}" );
45+ REQUIRE (spy.log () == AllocatorLog{
46+ Allocate (sizeofPool ()),
47+ Allocate (sizeofString (" world" )),
48+
49+ });
50+ }
51+
52+ SECTION (" char[]" ) {
53+ char temp[] = " world" ;
54+ mp.add (temp);
55+
56+ REQUIRE (doc.as <std::string>() == " {\" hello\" :[\" world\" ]}" );
57+ REQUIRE (spy.log () == AllocatorLog{
58+ Allocate (sizeofPool ()),
59+ Allocate (sizeofString (" world" )),
60+
61+ });
3462 }
3563
3664#ifdef HAS_VARIABLE_LENGTH_ARRAY
37- SECTION (" add(vla) " ) {
65+ SECTION (" VLA " ) {
3866 size_t i = 16 ;
3967 char vla[i];
4068 strcpy (vla, " world" );
4169
4270 mp.add (vla);
4371
4472 REQUIRE (doc.as <std::string>() == " {\" hello\" :[\" world\" ]}" );
73+ REQUIRE (spy.log () == AllocatorLog{
74+ Allocate (sizeofPool ()),
75+ Allocate (sizeofString (" world" )),
76+ });
4577 }
4678#endif
4779}
4880
4981TEST_CASE (" MemberProxy::clear()" ) {
5082 JsonDocument doc;
51- MemberProxy mp = doc[" hello" ];
83+ auto mp = doc[" hello" ];
5284
5385 SECTION (" size goes back to zero" ) {
5486 mp.add (42 );
@@ -139,7 +171,7 @@ TEST_CASE("MemberProxy::operator|()") {
139171
140172TEST_CASE (" MemberProxy::remove()" ) {
141173 JsonDocument doc;
142- MemberProxy mp = doc[" hello" ];
174+ auto mp = doc[" hello" ];
143175
144176 SECTION (" remove(int)" ) {
145177 mp.add (1 );
@@ -186,7 +218,7 @@ TEST_CASE("MemberProxy::remove()") {
186218
187219TEST_CASE (" MemberProxy::set()" ) {
188220 JsonDocument doc;
189- MemberProxy mp = doc[" hello" ];
221+ auto mp = doc[" hello" ];
190222
191223 SECTION (" set(int)" ) {
192224 mp.set (42 );
@@ -223,7 +255,7 @@ TEST_CASE("MemberProxy::set()") {
223255
224256TEST_CASE (" MemberProxy::size()" ) {
225257 JsonDocument doc;
226- MemberProxy mp = doc[" hello" ];
258+ auto mp = doc[" hello" ];
227259
228260 SECTION (" returns 0" ) {
229261 REQUIRE (mp.size () == 0 );
@@ -246,7 +278,7 @@ TEST_CASE("MemberProxy::size()") {
246278
247279TEST_CASE (" MemberProxy::operator[]" ) {
248280 JsonDocument doc;
249- MemberProxy mp = doc[" hello" ];
281+ auto mp = doc[" hello" ];
250282
251283 SECTION (" set member" ) {
252284 mp[" world" ] = 42 ;
@@ -265,7 +297,7 @@ TEST_CASE("MemberProxy cast to JsonVariantConst") {
265297 JsonDocument doc;
266298 doc[" hello" ] = " world" ;
267299
268- const MemberProxy mp = doc[" hello" ];
300+ const auto mp = doc[" hello" ];
269301
270302 JsonVariantConst var = mp;
271303
@@ -276,7 +308,7 @@ TEST_CASE("MemberProxy cast to JsonVariant") {
276308 JsonDocument doc;
277309 doc[" hello" ] = " world" ;
278310
279- MemberProxy mp = doc[" hello" ];
311+ auto mp = doc[" hello" ];
280312
281313 JsonVariant var = mp;
282314
0 commit comments