json_binder_test_bool.t.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // json_binder_test_bool.t.h
  3. // json
  4. //
  5. // Created by Sam Jaffe on 2/27/17.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "json/json_binder.hpp"
  10. using namespace json::binder;
  11. using namespace json::parser;
  12. class json_binder_value_bool_TestSuite : public CxxTest::TestSuite {
  13. public:
  14. void test_parse_bool_true() {
  15. char data[] = "true";
  16. bool out = false;
  17. value_binder<bool> binder{};
  18. parse(bind(out, binder), data, allow_all);
  19. TS_ASSERT_EQUALS( out, true );
  20. }
  21. void test_parse_bool_false() {
  22. char data[] = "false";
  23. bool out = true;
  24. value_binder<bool> binder{};
  25. parse(bind(out, binder), data, allow_all);
  26. TS_ASSERT_EQUALS( out, false );
  27. }
  28. void test_parse_nonbool_throws() {
  29. char data[] = "YES"; // Obj-C
  30. bool out = false;
  31. value_binder<bool> binder{};
  32. TS_ASSERT_THROWS(parse(bind(out, binder), data, allow_all),
  33. json::malformed_json_exception);
  34. }
  35. void test_write_bool_true() {
  36. std::string const expected = "true";
  37. std::stringstream ss;
  38. bool const in = true;
  39. value_binder<bool> binder{};
  40. write(bind(in, binder), ss);
  41. TS_ASSERT_EQUALS(ss.str(), expected);
  42. }
  43. void test_write_bool_false() {
  44. std::string const expected = "false";
  45. std::stringstream ss;
  46. bool const in = false;
  47. value_binder<bool> binder{};
  48. write(bind(in, binder), ss);
  49. TS_ASSERT_EQUALS(ss.str(), expected);
  50. }
  51. };