JsonValue_Cast_Tests.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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((void*) &expected, (void*) &actual);
  62. }
  63. };
  64. }