JsonValue_PrintTo_Tests.cpp 2.3 KB

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