Generator: added a test of one string in an array

This commit is contained in:
Benoît Blanchon
2014-06-24 13:34:55 +02:00
parent c8c1add4ab
commit f18f554c2f
5 changed files with 54 additions and 9 deletions

View File

@ -3,3 +3,4 @@
/*.suo
/Debug
/ipch
/*.opensdf

View File

@ -6,9 +6,3 @@
#include <string.h>
#include "JsonArray.h"
void JsonArray::writeTo(char* buffer, size_t bufferSize)
{
strncpy(buffer, "[]", bufferSize);
}

View File

@ -5,9 +5,45 @@
#pragma once
template<int N>
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);
}
};

View File

@ -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);
}
};
}

View File

@ -3,3 +3,4 @@
/*.suo
/Debug
/ipch
/*.opensdf