JsonString.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, 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 == JsonString());
  13. CHECK(s != "");
  14. }
  15. SECTION("Null converts to false") {
  16. JsonString s;
  17. CHECK(bool(s) == false);
  18. }
  19. SECTION("Empty string converts to true") {
  20. JsonString s("");
  21. CHECK(bool(s) == true);
  22. }
  23. SECTION("Non-empty string converts to true") {
  24. JsonString s("");
  25. CHECK(bool(s) == true);
  26. }
  27. SECTION("Null strings equals each others") {
  28. JsonString a, b;
  29. CHECK(a == b);
  30. CHECK_FALSE(a != b);
  31. }
  32. SECTION("Null and empty strings differ") {
  33. JsonString a, b("");
  34. CHECK_FALSE(a == b);
  35. CHECK(a != b);
  36. CHECK_FALSE(b == a);
  37. CHECK(b != a);
  38. }
  39. SECTION("Null and non-empty strings differ") {
  40. JsonString a, b("hello");
  41. CHECK_FALSE(a == b);
  42. CHECK(a != b);
  43. CHECK_FALSE(b == a);
  44. CHECK(b != a);
  45. }
  46. SECTION("Compare different strings") {
  47. JsonString a("hello"), b("world");
  48. CHECK_FALSE(a == b);
  49. CHECK(a != b);
  50. }
  51. SECTION("Compare identical by pointer") {
  52. JsonString a("hello"), b("hello");
  53. CHECK(a == b);
  54. CHECK_FALSE(a != b);
  55. }
  56. SECTION("Compare identical by value") {
  57. char s1[] = "hello";
  58. char s2[] = "hello";
  59. JsonString a(s1), b(s2);
  60. CHECK(a == b);
  61. CHECK_FALSE(a != b);
  62. }
  63. SECTION("std::stream") {
  64. std::stringstream ss;
  65. ss << JsonString("hello world!");
  66. CHECK(ss.str() == "hello world!");
  67. }
  68. SECTION("Construct with a size") {
  69. JsonString s("hello world", 5);
  70. CHECK(s.size() == 5);
  71. CHECK(s == "hello");
  72. CHECK(s != "hello world");
  73. }
  74. }