From f18f554c2f2a617181b0d392f9dc5e3f0109130c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Tue, 24 Jun 2014 13:34:55 +0200 Subject: [PATCH] Generator: added a test of one string in an array --- JsonGeneratorTests/.gitignore | 1 + JsonGeneratorTests/JsonArray.cpp | 6 ----- JsonGeneratorTests/JsonArray.h | 38 ++++++++++++++++++++++++++- JsonGeneratorTests/JsonArrayTests.cpp | 17 ++++++++++-- JsonParserTests/.gitignore | 1 + 5 files changed, 54 insertions(+), 9 deletions(-) diff --git a/JsonGeneratorTests/.gitignore b/JsonGeneratorTests/.gitignore index abbcaa0a..23550d0d 100644 --- a/JsonGeneratorTests/.gitignore +++ b/JsonGeneratorTests/.gitignore @@ -3,3 +3,4 @@ /*.suo /Debug /ipch +/*.opensdf diff --git a/JsonGeneratorTests/JsonArray.cpp b/JsonGeneratorTests/JsonArray.cpp index 510953c2..62ce93ae 100644 --- a/JsonGeneratorTests/JsonArray.cpp +++ b/JsonGeneratorTests/JsonArray.cpp @@ -6,9 +6,3 @@ #include #include "JsonArray.h" - -void JsonArray::writeTo(char* buffer, size_t bufferSize) -{ - strncpy(buffer, "[]", bufferSize); - -} \ No newline at end of file diff --git a/JsonGeneratorTests/JsonArray.h b/JsonGeneratorTests/JsonArray.h index 02104a4c..1202edc2 100644 --- a/JsonGeneratorTests/JsonArray.h +++ b/JsonGeneratorTests/JsonArray.h @@ -5,9 +5,45 @@ #pragma once +template class JsonArray { public: - void writeTo(char* buffer, size_t bufferSize); + JsonArray() + { + itemCount = 0; + } + + void add(const char* data) + { + if (itemCount <= N) + items[itemCount++] = data; + } + + void writeTo(char* buffer, size_t bufferSize) + { + buffer[0] = 0; + + append("[", buffer, bufferSize); + + for (int i = 0; i < itemCount; i++) + { + append("'", buffer, bufferSize); + append(items[i], buffer, bufferSize); + append("'", buffer, bufferSize); + } + + append("]", buffer, bufferSize); + } + +private: + const char* items[N]; + int itemCount; + + void append(const char* source, char* dest, size_t destSize) + { + int len = strlen(dest); + strncpy(dest + len, source, destSize - len); + } }; diff --git a/JsonGeneratorTests/JsonArrayTests.cpp b/JsonGeneratorTests/JsonArrayTests.cpp index 4ea84f71..ed7e8ec1 100644 --- a/JsonGeneratorTests/JsonArrayTests.cpp +++ b/JsonGeneratorTests/JsonArrayTests.cpp @@ -7,16 +7,29 @@ namespace JsonGeneratorTests { TEST_CLASS(JsonArrayTests) { + JsonArray<32> arr; + public: TEST_METHOD(EmptyArray) { - JsonArray arr; + AssertJsonIs("[]"); + } + TEST_METHOD(OneString) + { + arr.add("hello"); + + AssertJsonIs("['hello']"); + } + + void AssertJsonIs(const char* expected) + { char buffer[256]; + arr.writeTo(buffer, sizeof(buffer)); - Assert::AreEqual("[]", buffer); + Assert::AreEqual(expected, buffer); } }; } \ No newline at end of file diff --git a/JsonParserTests/.gitignore b/JsonParserTests/.gitignore index abbcaa0a..23550d0d 100644 --- a/JsonParserTests/.gitignore +++ b/JsonParserTests/.gitignore @@ -3,3 +3,4 @@ /*.suo /Debug /ipch +/*.opensdf