JsonVariant_Storage_Tests.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. using namespace ArduinoJson;
  11. class JsonVariant_Storage_Tests : public ::testing::Test {
  12. protected:
  13. template <typename T>
  14. void testValue(T expected) {
  15. actual.set(expected);
  16. EXPECT_EQ(expected, actual.as<T>());
  17. }
  18. template <typename T>
  19. void testReference(T &expected) {
  20. actual.set(expected);
  21. EXPECT_EQ(expected, actual.as<T &>());
  22. }
  23. private:
  24. JsonVariant actual;
  25. };
  26. TEST_F(JsonVariant_Storage_Tests, Double) { testValue<double>(123.45); }
  27. TEST_F(JsonVariant_Storage_Tests, False) { testValue<bool>(false); }
  28. TEST_F(JsonVariant_Storage_Tests, Float) { testValue<float>(123.45f); }
  29. TEST_F(JsonVariant_Storage_Tests, Null) { testValue<const char *>(NULL); }
  30. TEST_F(JsonVariant_Storage_Tests, SChar) { testValue<signed char>(123); }
  31. TEST_F(JsonVariant_Storage_Tests, SInt) { testValue<signed int>(123); }
  32. TEST_F(JsonVariant_Storage_Tests, SLong) { testValue<signed long>(123L); }
  33. TEST_F(JsonVariant_Storage_Tests, SShort) { testValue<signed short>(123); }
  34. TEST_F(JsonVariant_Storage_Tests, String) { testValue<const char *>("hello"); }
  35. TEST_F(JsonVariant_Storage_Tests, True) { testValue<bool>(true); }
  36. TEST_F(JsonVariant_Storage_Tests, UChar) { testValue<unsigned char>(123); }
  37. TEST_F(JsonVariant_Storage_Tests, UInt) { testValue<unsigned int>(123U); }
  38. TEST_F(JsonVariant_Storage_Tests, ULong) { testValue<unsigned long>(123UL); }
  39. TEST_F(JsonVariant_Storage_Tests, UShort) { testValue<unsigned short>(123); }
  40. TEST_F(JsonVariant_Storage_Tests, CanStoreObject) {
  41. StaticJsonBuffer<200> json;
  42. JsonObject &object = json.createObject();
  43. testReference(object);
  44. }