JsonValueTests.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  9. using namespace ArduinoJson::Internals;
  10. namespace JsonGeneratorTests
  11. {
  12. TEST_CLASS(JsonValueTests)
  13. {
  14. char buffer[1024];
  15. size_t returnValue;
  16. public:
  17. TEST_METHOD(String)
  18. {
  19. whenInputIs("hello");
  20. outputMustBe("\"hello\"");
  21. }
  22. TEST_METHOD(Float)
  23. {
  24. whenInputIs(3.1415f);
  25. outputMustBe("3.14");
  26. }
  27. TEST_METHOD(DoubleZeroDigits)
  28. {
  29. whenInputIs<0>(3.14159265358979323846);
  30. outputMustBe("3");
  31. }
  32. TEST_METHOD(DoubleOneDigit)
  33. {
  34. whenInputIs<1>(3.14159265358979323846);
  35. outputMustBe("3.1");
  36. }
  37. TEST_METHOD(DoubleTwoDigits)
  38. {
  39. whenInputIs<2>(3.14159265358979323846);
  40. outputMustBe("3.14");
  41. }
  42. TEST_METHOD(Integer)
  43. {
  44. whenInputIs(314);
  45. outputMustBe("314");
  46. }
  47. TEST_METHOD(Char)
  48. {
  49. whenInputIs('A');
  50. outputMustBe("65");
  51. }
  52. TEST_METHOD(Short)
  53. {
  54. whenInputIs((short)314);
  55. outputMustBe("314");
  56. }
  57. TEST_METHOD(Long)
  58. {
  59. whenInputIs(314159265L);
  60. outputMustBe("314159265");
  61. }
  62. private:
  63. template<int DIGITS>
  64. void whenInputIs(double value)
  65. {
  66. StringBuilder sb(buffer, sizeof(buffer));
  67. JsonValue jsonValue;
  68. jsonValue.set<DIGITS>(value);
  69. returnValue = jsonValue.printTo(sb);
  70. }
  71. template<typename T>
  72. void whenInputIs(T value)
  73. {
  74. StringBuilder sb(buffer, sizeof(buffer));
  75. JsonValue jsonValue;
  76. jsonValue.set(value);
  77. returnValue = jsonValue.printTo(sb);
  78. }
  79. void outputMustBe(const char* expected)
  80. {
  81. Assert::AreEqual(expected, buffer);
  82. Assert::AreEqual(strlen(expected), returnValue);
  83. }
  84. };
  85. }