Browse Source

Adding collection and object writing tests.

Samuel Jaffe 8 years ago
parent
commit
66fcedb6ff

+ 28 - 8
json_binder_collection.t.h

@@ -19,7 +19,7 @@ public:
     char data[] = "[ 0, 1, 2 ]";
     using collect = std::vector<int>;
     collect out;
-    value_binder<collect> binder{value_binder<int>()};
+    value_binder<collect> binder{};
     TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
     TS_ASSERT_EQUALS(out, collect({0, 1, 2}));
   }
@@ -28,7 +28,7 @@ public:
     char data[] = "[ 0, 1";
     using collect = std::vector<int>;
     collect out;
-    value_binder<collect> binder{value_binder<int>()};
+    value_binder<collect> binder{};
     TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
                      json::unterminated_json_exception);
   }
@@ -37,7 +37,7 @@ public:
     char data[] = "0, 1";
     using collect = std::vector<int>;
     collect out;
-    value_binder<collect> binder{value_binder<int>()};
+    value_binder<collect> binder{};
     TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
                      json::malformed_json_exception);
   }
@@ -47,7 +47,7 @@ public:
     char data[] = "{ \"a\" : 1 , \"b\" : 2 }";
     using collect = std::map<std::string, int>;
     collect out;
-    value_binder<collect> binder{value_binder<int>()};
+    value_binder<collect> binder{};
     TS_ASSERT_THROWS_NOTHING(parse(json::binder::bind(out, binder), data, allow_all));
     TS_ASSERT_EQUALS(out, collect({{"a", 1}, {"b", 2}}));
   }
@@ -56,7 +56,7 @@ public:
     char data[] = "{ \"a\" : 1 , \"b\" : 2";
     using collect = std::map<std::string, int>;
     collect out;
-    value_binder<collect> binder{value_binder<int>()};
+    value_binder<collect> binder{};
     TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
                      json::unterminated_json_exception);
   }
@@ -65,7 +65,7 @@ public:
     char data[] = "{ \"a\" : 1 , \"b\" }";
     using collect = std::map<std::string, int>;
     collect out;
-    value_binder<collect> binder{value_binder<int>()};
+    value_binder<collect> binder{};
     TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
                      json::malformed_json_exception);
   }
@@ -74,8 +74,28 @@ public:
     char data[] = "\"a\" : 1 , \"b\" : 2";
     using collect = std::map<std::string, int>;
     collect out;
-    value_binder<collect> binder{value_binder<int>()};
+    value_binder<collect> binder{};
     TS_ASSERT_THROWS(parse(json::binder::bind(out, binder), data, allow_all),
                      json::malformed_json_exception);
   }
-};
+  
+  void test_write_vector() {
+    std::string const expected = "[5,4,7]";
+    std::stringstream ss;
+    using collect = std::vector<int>;
+    collect const in{ 5, 4, 7 };
+    value_binder<collect> binder{};
+    TS_ASSERT_THROWS_NOTHING(write(json::binder::bind(in, binder), ss));
+    TS_ASSERT_EQUALS(ss.str(), expected);
+  }
+  
+  void test_write_map() {
+    std::string const expected = "{\"a\":1,\"b\":2}";
+    std::stringstream ss;
+    using collect = std::map<std::string, int>;
+    collect const in{ { "a", 1 }, { "b", 2 } };
+    value_binder<collect> binder{};
+    TS_ASSERT_THROWS_NOTHING(write(json::binder::bind(in, binder), ss));
+    TS_ASSERT_EQUALS(ss.str(), expected);
+  }
+};

+ 9 - 0
json_binder_object.t.h

@@ -119,4 +119,13 @@ public:
     TS_ASSERT_THROWS(parse(json::binder::bind(out, Get()), data, allow_all),
                      json::unterminated_json_exception);
   }
+  
+  void test_write_object_is_composed_of_correct_data() {
+    std::string const dbl = (std::stringstream{} << std::fixed << 1.0).str();
+    std::string const expected = "{\"average\":"+dbl+",\"count\":10,\"data\":{\"key1\":[1,2],\"key2\":[3,4]}}";
+    std::stringstream ss;
+    TestObject const in = { 10, 1.0, { { "key1", {1, 2} }, { "key2", {3, 4} } } };
+    TS_ASSERT_THROWS_NOTHING(write(json::binder::bind(in, Get()), ss));
+    TS_ASSERT_EQUALS(ss.str(), expected);
+  }
 };

+ 2 - 2
json_binder_test_bool.t.h

@@ -40,7 +40,7 @@ public:
   }
   
   void test_write_bool_true() {
-    std::string expected = "true";
+    std::string const expected = "true";
     std::stringstream ss;
     bool const in = true;
     value_binder<bool> binder{};
@@ -49,7 +49,7 @@ public:
   }
   
   void test_write_bool_false() {
-    std::string expected = "false";
+    std::string const expected = "false";
     std::stringstream ss;
     bool const in = false;
     value_binder<bool> binder{};

+ 1 - 1
json_binder_tuple.t.h

@@ -93,7 +93,7 @@ public:
   }
   
   void test_write_struct_tuple_binding() {
-    std::string expected = "[10,5]";
+    std::string const expected = "[10,5]";
     std::stringstream ss;
     point const in{10, 5};
     auto binder = tuple_binder<point>()(&point::x)(&point::y);

+ 3 - 3
json_binder_value_int.t.h

@@ -147,7 +147,7 @@ public:
   }
 
   void test_output_from_integer_type() {
-    std::string expected = "100";
+    std::string const expected = "100";
     std::stringstream ss;
     int const in = 100;
     value_binder<int> binder{};
@@ -156,7 +156,7 @@ public:
   }
   
   void test_unsigned_integer_output() {
-    std::string expected = "2147483648";
+    std::string const expected = "2147483648";
     std::stringstream ss;
     unsigned int const in = 2147483648;
     value_binder<unsigned int> binder{};
@@ -165,7 +165,7 @@ public:
   }
   
   void test_signed_integer_output() {
-    std::string expected = "-2147483648";
+    std::string const expected = "-2147483648";
     std::stringstream ss;
     int const in = -2147483648;
     value_binder<int> binder{};

+ 2 - 2
json_binder_value_string.t.h

@@ -50,7 +50,7 @@ public:
   }
   
   void test_write_string() {
-    std::string expected = "\"This is a string\"";
+    std::string const expected = "\"This is a string\"";
     std::stringstream ss;
     std::string const in = "This is a string";
     value_binder<std::string> binder{};
@@ -59,7 +59,7 @@ public:
   }
   
   void test_write_string_with_quotes() {
-    std::string expected = "\"This is a \\\"string\\\"\"";
+    std::string const expected = "\"This is a \\\"string\\\"\"";
     std::stringstream ss;
     std::string const in = "This is a \"string\"";
     value_binder<std::string> binder{};