JsonValue_Cast_Tests.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Arduino JSON library
  3. * Benoit Blanchon 2014 - MIT License
  4. */
  5. #include "CppUnitTest.h"
  6. #include "StringBuilder.h"
  7. #include "JsonValue.h"
  8. #include "JsonArray.h"
  9. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  10. using namespace ArduinoJson::Generator;
  11. using namespace ArduinoJson::Internals;
  12. namespace JsonGeneratorTests
  13. {
  14. TEST_CLASS(JsonValue_Cast_Tests)
  15. {
  16. JsonValue value;
  17. public:
  18. TEST_METHOD(Bool)
  19. {
  20. setValueAndCheckCast(true);
  21. setValueAndCheckCast(false);
  22. }
  23. TEST_METHOD(Double)
  24. {
  25. setValueAndCheckCast(3.14156);
  26. }
  27. TEST_METHOD(Float)
  28. {
  29. setValueAndCheckCast(3.14f);
  30. }
  31. TEST_METHOD(Integer)
  32. {
  33. setValueAndCheckCast(42);
  34. }
  35. TEST_METHOD(Long)
  36. {
  37. setValueAndCheckCast(42L);
  38. }
  39. TEST_METHOD(Array)
  40. {
  41. JsonArray<2> array;
  42. setValueAndCheckCast(array);
  43. }
  44. TEST_METHOD(String)
  45. {
  46. setValueAndCheckCast("hello");
  47. }
  48. private:
  49. template<typename T>
  50. void setValueAndCheckCast(T expected)
  51. {
  52. value = expected;
  53. T actual = value;
  54. Assert::AreEqual(expected, actual);
  55. }
  56. template<int N>
  57. void setValueAndCheckCast(JsonArray<N>& expected)
  58. {
  59. value = expected;
  60. const Printable& actual = value;
  61. Assert::AreEqual(
  62. reinterpret_cast<const void*>(&expected),
  63. reinterpret_cast<const void*>(&actual));
  64. }
  65. };
  66. }