Unit tests for SmartRest

Dependencies:   C027 SmartRest mbed

Revision:
0:789029e49ea1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/values/CharValueTest.cpp	Mon Mar 24 10:12:45 2014 +0000
@@ -0,0 +1,62 @@
+#include "CharValueTest.h"
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+
+CharValueTest::CharValueTest()
+{
+}
+
+void CharValueTest::test()
+{
+    testValue("abc", "abc");
+    testValue("hello world", "hello world");
+    testValue("hello, world", "\"hello, world\"");
+    testValue(" hello world", "\" hello world\"");
+    testValue("hello world ", "\"hello world \"");
+    testValue("hello\"world", "\"hello\"\"world\"");
+    testValue("hello\"\"world", "\"hello\"\"\"\"world\"");
+    testValue("hello\",world ", "\"hello\"\",world \"");
+    testNull();
+}
+
+void CharValueTest::testValue(const char *string, const char *expected)
+{
+    printf("Expecting '%s' for value '%s'\n", expected, string);
+    CharValue value(string);
+    assert(value.length() == strlen(expected));
+    assert(value.valueType() == VALUE_CHARACTER);
+    assert(value.characterValue() == string);
+    value.write(sink);
+    assert(strcmp(expected, sink.value()) == 0);
+    sink.clear();
+
+    Value *copy = value.copy();
+
+    assert(copy->length() == value.length());
+    assert(copy->valueType() == value.valueType());
+    assert(strcmp(copy->characterValue(), value.characterValue()) == 0);
+    assert(copy->characterValue() != value.characterValue());
+    delete copy;
+}
+
+void CharValueTest::testNull()
+{
+    puts("Testing character null value.");
+
+    CharValue value("");
+    assert(value.length() == 0);
+    assert(value.valueType() == VALUE_NULL);
+    assert(value.characterValue() == NULL);
+    value.write(sink);
+    assert(strcmp("", sink.value()) == 0);
+    sink.clear();
+
+    Value *copy = value.copy();
+
+    assert(copy->length() == value.length());
+    assert(copy->valueType() == value.valueType());
+    assert(copy->characterValue() == value.characterValue());
+    delete copy;
+}
+