string.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  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. {"\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"", "1\"2\\3/4\b5\f6\n7\r8\t9"},
  16. {"'\\u0041'", "A"},
  17. {"'\\u00e4'", "\xc3\xa4"}, // ä
  18. {"'\\u00E4'", "\xc3\xa4"}, // ä
  19. {"'\\u3042'", "\xe3\x81\x82"}, // あ
  20. {"'\\ud83d\\udda4'", "\xf0\x9f\x96\xa4"}, // 🖤
  21. {"'\\uF053'", "\xef\x81\x93"}, // issue #1173
  22. {"'\\uF015'", "\xef\x80\x95"}, // issue #1173
  23. {"'\\uF054'", "\xef\x81\x94"}, // issue #1173
  24. };
  25. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  26. DynamicJsonDocument doc(4096);
  27. for (size_t i = 0; i < testCount; i++) {
  28. const TestCase& testCase = testCases[i];
  29. CAPTURE(testCase.input);
  30. DeserializationError err = deserializeJson(doc, testCase.input);
  31. REQUIRE(err == DeserializationError::Ok);
  32. REQUIRE(doc.as<std::string>() == testCase.expectedOutput);
  33. }
  34. }
  35. TEST_CASE("Truncated JSON string") {
  36. const char* testCases[] = {"\"hello", "\'hello", "'\\u", "'\\u00", "'\\u000"};
  37. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  38. DynamicJsonDocument doc(4096);
  39. for (size_t i = 0; i < testCount; i++) {
  40. const char* input = testCases[i];
  41. CAPTURE(input);
  42. REQUIRE(deserializeJson(doc, input) ==
  43. DeserializationError::IncompleteInput);
  44. }
  45. }
  46. TEST_CASE("Invalid JSON string") {
  47. const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'",
  48. "'\\u000G'", "'\\u000/'", "\\x1234"};
  49. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  50. DynamicJsonDocument doc(4096);
  51. for (size_t i = 0; i < testCount; i++) {
  52. const char* input = testCases[i];
  53. CAPTURE(input);
  54. REQUIRE(deserializeJson(doc, input) == DeserializationError::InvalidInput);
  55. }
  56. }
  57. TEST_CASE("Not enough room to duplicate the string") {
  58. DynamicJsonDocument doc(4);
  59. REQUIRE(deserializeJson(doc, "\"hello world!\"") ==
  60. DeserializationError::NoMemory);
  61. REQUIRE(doc.isNull() == true);
  62. }