JsonValueTests.cpp 2.2 KB

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