From 4f4b35bd417f592d8b1c9171689cbf044e177c3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Fri, 27 Jun 2014 13:09:52 +0200 Subject: [PATCH] Small refactoring of the tests --- JsonGeneratorTests/JsonArrayTests.cpp | 84 ++++++++++++----------- JsonGeneratorTests/JsonHashTableTests.cpp | 6 +- 2 files changed, 48 insertions(+), 42 deletions(-) diff --git a/JsonGeneratorTests/JsonArrayTests.cpp b/JsonGeneratorTests/JsonArrayTests.cpp index 66bb6b32..0ab54175 100644 --- a/JsonGeneratorTests/JsonArrayTests.cpp +++ b/JsonGeneratorTests/JsonArrayTests.cpp @@ -7,108 +7,106 @@ namespace JsonGeneratorTests { TEST_CLASS(JsonArrayTests) { - JsonArray<2> arr; - public: TEST_METHOD(Empty) { - assertJsonIs("[]"); + jsonIs("[]"); } TEST_METHOD(AddNull) { - arr.add((char*)0); + add((char*)0); - assertJsonIs("[null]"); + jsonIs("[null]"); } TEST_METHOD(AddOneString) { - arr.add("hello"); + add("hello"); - assertJsonIs("[\"hello\"]"); + jsonIs("[\"hello\"]"); } TEST_METHOD(AddTwoStrings) { - arr.add("hello"); - arr.add("world"); + add("hello"); + add("world"); - assertJsonIs("[\"hello\",\"world\"]"); + jsonIs("[\"hello\",\"world\"]"); } TEST_METHOD(AddOneStringOverCapacity) { - arr.add("hello"); - arr.add("world"); - arr.add("lost"); + add("hello"); + add("world"); + add("lost"); - assertJsonIs("[\"hello\",\"world\"]"); + jsonIs("[\"hello\",\"world\"]"); } TEST_METHOD(AddOneNumber) { - arr.add(3.14); + add(3.14); - assertJsonIs("[3.14]"); + jsonIs("[3.14]"); } TEST_METHOD(AddTwoNumbers) { - arr.add(3.14); - arr.add(2.72); + add(3.14); + add(2.72); - assertJsonIs("[3.14,2.72]"); + jsonIs("[3.14,2.72]"); } TEST_METHOD(AddOneNumberOverCapacity) { - arr.add(3.14); - arr.add(2.72); - arr.add(1.41); + add(3.14); + add(2.72); + add(1.41); - assertJsonIs("[3.14,2.72]"); + jsonIs("[3.14,2.72]"); } TEST_METHOD(AddTrue) { - arr.add(true); + add(true); - assertJsonIs("[true]"); + jsonIs("[true]"); } TEST_METHOD(AddFalse) { - arr.add(false); + add(false); - assertJsonIs("[false]"); + jsonIs("[false]"); } TEST_METHOD(AddTwoBooleans) { - arr.add(false); - arr.add(true); + add(false); + add(true); - assertJsonIs("[false,true]"); + jsonIs("[false,true]"); } TEST_METHOD(AddOneBooleanOverCapacity) { - arr.add(false); - arr.add(true); - arr.add(false); + add(false); + add(true); + add(false); - assertJsonIs("[false,true]"); + jsonIs("[false,true]"); } TEST_METHOD(AddOneEmptyNestedArray) { JsonArray<1> nestedArray; - arr.add(nestedArray); + add(nestedArray); - assertJsonIs("[[]]"); + jsonIs("[[]]"); } TEST_METHOD(AddOneNestedArrayWithOneItem) @@ -116,14 +114,22 @@ namespace JsonGeneratorTests JsonArray<1> nestedArray; nestedArray.add(3.14); - arr.add(nestedArray); + add(nestedArray); - assertJsonIs("[[3.14]]"); + jsonIs("[[3.14]]"); } private: - void assertJsonIs(const char* expected) + JsonArray<2> arr; + + template + void add(T value) + { + arr.add(value); + } + + void jsonIs(const char* expected) { char buffer[256]; diff --git a/JsonGeneratorTests/JsonHashTableTests.cpp b/JsonGeneratorTests/JsonHashTableTests.cpp index a0bdcd3b..feee8c06 100644 --- a/JsonGeneratorTests/JsonHashTableTests.cpp +++ b/JsonGeneratorTests/JsonHashTableTests.cpp @@ -13,13 +13,13 @@ namespace JsonGeneratorTests TEST_METHOD(Empty) { - assertJsonIs("{}"); + jsonIs("{}"); } TEST_METHOD(OneString) { add("key", "value"); - assertJsonIs("{\"key\":\"value\"}"); + jsonIs("{\"key\":\"value\"}"); } private: @@ -30,7 +30,7 @@ namespace JsonGeneratorTests hash.add(key, value); } - void assertJsonIs(const char* expected) + void jsonIs(const char* expected) { char buffer[256];