JsonValueTests.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/JsonValue.hpp>
  9. #include "Printers.hpp"
  10. using namespace ArduinoJson;
  11. class JsonValueTests : public ::testing::Test {
  12. protected:
  13. virtual void SetUp() {
  14. jsonValue1 = json.createValue();
  15. jsonValue2 = json.createValue();
  16. }
  17. StaticJsonBuffer<42> json;
  18. JsonValue jsonValue1;
  19. JsonValue jsonValue2;
  20. };
  21. TEST_F(JsonValueTests, CanStoreInteger) {
  22. jsonValue1 = 123;
  23. EXPECT_EQ(123, jsonValue1.as<int>());
  24. }
  25. TEST_F(JsonValueTests, CanStoreDouble) {
  26. jsonValue1 = 123.45;
  27. EXPECT_EQ(123.45, jsonValue1.as<double>());
  28. }
  29. TEST_F(JsonValueTests, CanStoreTrue) {
  30. jsonValue1 = true;
  31. EXPECT_TRUE(jsonValue1.as<bool>());
  32. }
  33. TEST_F(JsonValueTests, CanStoreFalse) {
  34. jsonValue1 = false;
  35. EXPECT_FALSE(jsonValue1.as<bool>());
  36. }
  37. TEST_F(JsonValueTests, CanStoreString) {
  38. jsonValue1 = "hello";
  39. EXPECT_STREQ("hello", jsonValue1.as<const char *>());
  40. }
  41. TEST_F(JsonValueTests, CanStoreObject) {
  42. JsonObject innerObject1 = json.createObject();
  43. jsonValue1 = innerObject1;
  44. EXPECT_EQ(innerObject1, jsonValue1.as<JsonObject>());
  45. }
  46. TEST_F(JsonValueTests, IntegersAreCopiedByValue) {
  47. jsonValue1 = 123;
  48. jsonValue2 = jsonValue1;
  49. jsonValue1 = 456;
  50. EXPECT_EQ(123, jsonValue2.as<int>());
  51. }
  52. TEST_F(JsonValueTests, DoublesAreCopiedByValue) {
  53. jsonValue1 = 123.45;
  54. jsonValue2 = jsonValue1;
  55. jsonValue1 = 456.78;
  56. EXPECT_EQ(123.45, jsonValue2.as<double>());
  57. }
  58. TEST_F(JsonValueTests, BooleansAreCopiedByValue) {
  59. jsonValue1 = true;
  60. jsonValue2 = jsonValue1;
  61. jsonValue1 = false;
  62. EXPECT_TRUE(jsonValue2.as<bool>());
  63. }
  64. TEST_F(JsonValueTests, StringsAreCopiedByValue) {
  65. jsonValue1 = "hello";
  66. jsonValue2 = jsonValue1;
  67. jsonValue1 = "world";
  68. EXPECT_STREQ("hello", jsonValue2.as<const char *>());
  69. }
  70. TEST_F(JsonValueTests, ObjectsAreCopiedByReference) {
  71. JsonObject object = json.createObject();
  72. jsonValue1 = object;
  73. object["hello"] = "world";
  74. EXPECT_EQ(1, jsonValue1.as<JsonObject>().size());
  75. }
  76. TEST_F(JsonValueTests, ArraysAreCopiedByReference) {
  77. JsonArray array = json.createArray();
  78. jsonValue1 = array;
  79. array.add("world");
  80. EXPECT_EQ(1, jsonValue1.as<JsonArray>().size());
  81. }