string.cpp 2.1 KB

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