JsonString.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <sstream>
  7. TEST_CASE("JsonString") {
  8. SECTION("Default constructor creates a null JsonString") {
  9. JsonString s;
  10. CHECK(s.isNull() == true);
  11. CHECK(s.c_str() == 0);
  12. CHECK(s.isLinked() == true);
  13. CHECK(s == JsonString());
  14. CHECK(s != "");
  15. }
  16. SECTION("Null converts to false") {
  17. JsonString s;
  18. CHECK(bool(s) == false);
  19. }
  20. SECTION("Empty string converts to true") {
  21. JsonString s("");
  22. CHECK(bool(s) == true);
  23. }
  24. SECTION("Non-empty string converts to true") {
  25. JsonString s("");
  26. CHECK(bool(s) == true);
  27. }
  28. SECTION("Null strings equals each others") {
  29. JsonString a, b;
  30. CHECK(a == b);
  31. CHECK_FALSE(a != b);
  32. }
  33. SECTION("Null and empty strings differ") {
  34. JsonString a, b("");
  35. CHECK_FALSE(a == b);
  36. CHECK(a != b);
  37. CHECK_FALSE(b == a);
  38. CHECK(b != a);
  39. }
  40. SECTION("Null and non-empty strings differ") {
  41. JsonString a, b("hello");
  42. CHECK_FALSE(a == b);
  43. CHECK(a != b);
  44. CHECK_FALSE(b == a);
  45. CHECK(b != a);
  46. }
  47. SECTION("Compare different strings") {
  48. JsonString a("hello"), b("world");
  49. CHECK_FALSE(a == b);
  50. CHECK(a != b);
  51. }
  52. SECTION("Compare identical by pointer") {
  53. JsonString a("hello"), b("hello");
  54. CHECK(a == b);
  55. CHECK_FALSE(a != b);
  56. }
  57. SECTION("Compare identical by value") {
  58. char s1[] = "hello";
  59. char s2[] = "hello";
  60. JsonString a(s1), b(s2);
  61. CHECK(a == b);
  62. CHECK_FALSE(a != b);
  63. }
  64. SECTION("std::stream") {
  65. std::stringstream ss;
  66. ss << JsonString("hello world!");
  67. CHECK(ss.str() == "hello world!");
  68. }
  69. SECTION("Construct with a size") {
  70. JsonString s("hello world", 5);
  71. CHECK(s.size() == 5);
  72. CHECK(s.isLinked() == true);
  73. CHECK(s == "hello");
  74. CHECK(s != "hello world");
  75. }
  76. }