string.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2021
  3. // MIT License
  4. #define ARDUINOJSON_DECODE_UNICODE 1
  5. #include <ArduinoJson.h>
  6. #include <catch.hpp>
  7. TEST_CASE("Valid JSON strings value") {
  8. struct TestCase {
  9. const char* input;
  10. const char* expectedOutput;
  11. };
  12. TestCase testCases[] = {
  13. {"\"hello world\"", "hello world"},
  14. {"\'hello world\'", "hello world"},
  15. {"'\"'", "\""},
  16. {"'\\\\'", "\\"},
  17. {"'\\/'", "/"},
  18. {"'\\b'", "\b"},
  19. {"'\\f'", "\f"},
  20. {"'\\n'", "\n"},
  21. {"'\\r'", "\r"},
  22. {"'\\t'", "\t"},
  23. {"\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"", "1\"2\\3/4\b5\f6\n7\r8\t9"},
  24. {"'\\u0041'", "A"},
  25. {"'\\u00e4'", "\xc3\xa4"}, // ä
  26. {"'\\u00E4'", "\xc3\xa4"}, // ä
  27. {"'\\u3042'", "\xe3\x81\x82"}, // あ
  28. {"'\\ud83d\\udda4'", "\xf0\x9f\x96\xa4"}, // 🖤
  29. {"'\\uF053'", "\xef\x81\x93"}, // issue #1173
  30. {"'\\uF015'", "\xef\x80\x95"}, // issue #1173
  31. {"'\\uF054'", "\xef\x81\x94"}, // issue #1173
  32. };
  33. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  34. DynamicJsonDocument doc(4096);
  35. for (size_t i = 0; i < testCount; i++) {
  36. const TestCase& testCase = testCases[i];
  37. CAPTURE(testCase.input);
  38. DeserializationError err = deserializeJson(doc, testCase.input);
  39. CHECK(err == DeserializationError::Ok);
  40. CHECK(doc.as<std::string>() == testCase.expectedOutput);
  41. }
  42. }
  43. TEST_CASE("Truncated JSON string") {
  44. const char* testCases[] = {"\"hello", "\'hello", "'\\u", "'\\u00", "'\\u000"};
  45. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  46. DynamicJsonDocument doc(4096);
  47. for (size_t i = 0; i < testCount; i++) {
  48. const char* input = testCases[i];
  49. CAPTURE(input);
  50. REQUIRE(deserializeJson(doc, input) ==
  51. DeserializationError::IncompleteInput);
  52. }
  53. }
  54. TEST_CASE("Invalid JSON string") {
  55. const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'",
  56. "'\\u000G'", "'\\u000/'", "'\\x1234'"};
  57. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  58. DynamicJsonDocument doc(4096);
  59. for (size_t i = 0; i < testCount; i++) {
  60. const char* input = testCases[i];
  61. CAPTURE(input);
  62. REQUIRE(deserializeJson(doc, input) == DeserializationError::InvalidInput);
  63. }
  64. }
  65. TEST_CASE("Not enough room to save the key") {
  66. DynamicJsonDocument doc(JSON_OBJECT_SIZE(1) + 8);
  67. SECTION("Quoted string") {
  68. REQUIRE(deserializeJson(doc, "{\"accuracy\":1}") ==
  69. DeserializationError::NoMemory);
  70. }
  71. SECTION("Non-quoted string") {
  72. REQUIRE(deserializeJson(doc, "{accuracy:1}") ==
  73. DeserializationError::NoMemory);
  74. }
  75. }