| 13 | } |
| 14 | |
| 15 | void execute() override |
| 16 | { |
| 17 | DEFINE_FSTR_LOCAL(flashString1, "FlashString-1"); |
| 18 | DEFINE_FSTR_LOCAL(flashString2, "FlashString-2"); |
| 19 | DEFINE_FSTR_LOCAL(formatStrings, "Compact\0Pretty\0MessagePack"); |
| 20 | DEFINE_FSTR_LOCAL(test_json, "test.json"); |
| 21 | DEFINE_FSTR_LOCAL(test_msgpack, "test.msgpack"); |
| 22 | |
| 23 | doc["string1"] = "string value 1"; |
| 24 | auto json = doc.as<JsonObject>(); |
| 25 | DEFINE_FSTR_LOCAL(FS_number2, "number2"); |
| 26 | json[FS_number2] = 12345; |
| 27 | auto arr = doc.createNestedArray("arr"); |
| 28 | arr.add(flashString1); |
| 29 | doc[flashString2] = flashString1; |
| 30 | |
| 31 | TEST_CASE("serialize") |
| 32 | { |
| 33 | DEFINE_FSTR_LOCAL(serialized1, |
| 34 | "{\"string1\":\"string value " |
| 35 | "1\",\"number2\":12345,\"arr\":[\"FlashString-1\"],\"FlashString-2\":\"FlashString-1\"}"); |
| 36 | |
| 37 | String s = Json::serialize(doc); |
| 38 | debug_d("Test doc: %s", s.c_str()); |
| 39 | REQUIRE(s == serialized1); |
| 40 | CString cs; |
| 41 | Json::serialize(doc, cs); |
| 42 | debug_d("Test doc: %s", cs.c_str()); |
| 43 | REQUIRE(cs == serialized1); |
| 44 | } |
| 45 | |
| 46 | TEST_CASE("Json::measure()") |
| 47 | { |
| 48 | CStringArray formats(formatStrings); |
| 49 | const uint8_t sizes[] = {100, 132, 82}; |
| 50 | for(auto fmt = Json::Compact; fmt <= Json::MessagePack; ++fmt) { |
| 51 | auto len = Json::measure(doc, fmt); |
| 52 | debug_d("Measure(doc, %s) = %u", formats[fmt], len); |
| 53 | REQUIRE(len == sizes[fmt]); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | TEST_CASE("Json::getValue(doc[\"number2\"], value))") |
| 58 | { |
| 59 | int value; |
| 60 | if(Json::getValue(doc["number2"], value)) { |
| 61 | debug_d("number2 = %d", value); |
| 62 | REQUIRE(value == 12345); |
| 63 | } else { |
| 64 | debug_e("number2 not found"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | TEST_CASE("Json::getValue(doc[\"string\"], value))") |
| 69 | { |
| 70 | String value; |
| 71 | REQUIRE(Json::getValue(doc["string"], value) == false); |
| 72 | } |
nothing calls this directly
no test coverage detected