JsonValueTests.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright Benoit Blanchon 2014
  2. // MIT License
  3. //
  4. // Arduino JSON library
  5. // https://github.com/bblanchon/ArduinoJson
  6. #include <gtest/gtest.h>
  7. #include <ArduinoJson/StaticJsonBuffer.hpp>
  8. #include <ArduinoJson/JsonArray.hpp>
  9. #include <ArduinoJson/JsonObject.hpp>
  10. #include <ArduinoJson/JsonValue.hpp>
  11. using namespace ArduinoJson;
  12. class JsonValueTests : public ::testing::Test {
  13. protected:
  14. StaticJsonBuffer<200> json;
  15. JsonValue jsonValue1;
  16. JsonValue jsonValue2;
  17. };
  18. TEST_F(JsonValueTests, CanStoreInteger) {
  19. jsonValue1 = 123;
  20. EXPECT_EQ(123, jsonValue1.as<int>());
  21. }
  22. TEST_F(JsonValueTests, CanStoreDouble) {
  23. jsonValue1 = 123.45;
  24. EXPECT_EQ(123.45, jsonValue1.as<double>());
  25. }
  26. TEST_F(JsonValueTests, CanStoreTrue) {
  27. jsonValue1 = true;
  28. EXPECT_TRUE(jsonValue1.as<bool>());
  29. }
  30. TEST_F(JsonValueTests, CanStoreFalse) {
  31. jsonValue1 = false;
  32. EXPECT_FALSE(jsonValue1.as<bool>());
  33. }
  34. TEST_F(JsonValueTests, CanStoreString) {
  35. jsonValue1 = "hello";
  36. EXPECT_STREQ("hello", jsonValue1.as<const char *>());
  37. }
  38. TEST_F(JsonValueTests, CanStoreObject) {
  39. JsonObject &innerObject1 = json.createObject();
  40. jsonValue1 = innerObject1;
  41. EXPECT_EQ(innerObject1, jsonValue1.asObject());
  42. }
  43. TEST_F(JsonValueTests, IntegersAreCopiedByValue) {
  44. jsonValue1 = 123;
  45. jsonValue2 = jsonValue1;
  46. jsonValue1 = 456;
  47. EXPECT_EQ(123, jsonValue2.as<int>());
  48. }
  49. TEST_F(JsonValueTests, DoublesAreCopiedByValue) {
  50. jsonValue1 = 123.45;
  51. jsonValue2 = jsonValue1;
  52. jsonValue1 = 456.78;
  53. EXPECT_EQ(123.45, jsonValue2.as<double>());
  54. }
  55. TEST_F(JsonValueTests, BooleansAreCopiedByValue) {
  56. jsonValue1 = true;
  57. jsonValue2 = jsonValue1;
  58. jsonValue1 = false;
  59. EXPECT_TRUE(jsonValue2.as<bool>());
  60. }
  61. TEST_F(JsonValueTests, StringsAreCopiedByValue) {
  62. jsonValue1 = "hello";
  63. jsonValue2 = jsonValue1;
  64. jsonValue1 = "world";
  65. EXPECT_STREQ("hello", jsonValue2.as<const char *>());
  66. }
  67. TEST_F(JsonValueTests, ObjectsAreCopiedByReference) {
  68. JsonObject &object = json.createObject();
  69. jsonValue1 = object;
  70. object["hello"] = "world";
  71. EXPECT_EQ(1, jsonValue1.asObject().size());
  72. }
  73. TEST_F(JsonValueTests, ArraysAreCopiedByReference) {
  74. JsonArray &array = json.createArray();
  75. jsonValue1 = array;
  76. array.add("world");
  77. EXPECT_EQ(1, jsonValue1.asArray().size());
  78. }