string.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. };
  22. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  23. DynamicJsonDocument doc(4096);
  24. for (size_t i = 0; i < testCount; i++) {
  25. const TestCase& testCase = testCases[i];
  26. CAPTURE(testCase.input);
  27. DeserializationError err = deserializeJson(doc, testCase.input);
  28. REQUIRE(err == DeserializationError::Ok);
  29. REQUIRE(doc.as<std::string>() == testCase.expectedOutput);
  30. }
  31. }
  32. TEST_CASE("Truncated JSON string") {
  33. const char* testCases[] = {"\"hello", "\'hello", "'\\u", "'\\u00", "'\\u000"};
  34. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  35. DynamicJsonDocument doc(4096);
  36. for (size_t i = 0; i < testCount; i++) {
  37. const char* input = testCases[i];
  38. CAPTURE(input);
  39. REQUIRE(deserializeJson(doc, input) ==
  40. DeserializationError::IncompleteInput);
  41. }
  42. }
  43. TEST_CASE("Invalid JSON string") {
  44. const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'",
  45. "'\\u000G'", "'\\u000/'", "\\x1234"};
  46. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  47. DynamicJsonDocument doc(4096);
  48. for (size_t i = 0; i < testCount; i++) {
  49. const char* input = testCases[i];
  50. CAPTURE(input);
  51. REQUIRE(deserializeJson(doc, input) == DeserializationError::InvalidInput);
  52. }
  53. }
  54. TEST_CASE("Not enough room to duplicate the string") {
  55. DynamicJsonDocument doc(4);
  56. REQUIRE(deserializeJson(doc, "\"hello world!\"") ==
  57. DeserializationError::NoMemory);
  58. REQUIRE(doc.isNull() == true);
  59. }