From 42df37bbbe7f006411f9869c9f673977229cae32 Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Sun, 24 Jan 2016 19:19:03 +0100 Subject: [PATCH] Added JSON construction tests --- test/constructingJson.nim | 46 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 test/constructingJson.nim diff --git a/test/constructingJson.nim b/test/constructingJson.nim new file mode 100644 index 0000000..9113866 --- /dev/null +++ b/test/constructingJson.nim @@ -0,0 +1,46 @@ +import "../yaml" + +import unittest, json + +proc wc(line, column: int, lineContent: string, message: string) = + echo "Warning (", line, ",", column, "): ", message, "\n", lineContent + +proc ensureEqual(yamlIn, jsonIn: string) = + var + parser = newYamlParser(initCoreTagLibrary(), wc) + yamlResult = constructJson(parser.parse(newStringStream(yamlIn))) + jsonResult = parseJson(jsonIn) + assert yamlResult.len == 1 + assert(jsonResult == yamlResult[0]) + +suite "Constructing JSON": + test "Constructing JSON: Simple Sequence": + ensureEqual("- 1\n- 2\n- 3", "[1, 2, 3]") + + test "Constructing JSON: Simple Map": + ensureEqual("a: b\nc: d", """{"a": "b", "c": "d"}""") + + test "Constructing JSON: Complex Structure": + ensureEqual(""" +%YAML 1.2 +--- +Foo: + - - a + - b + - c + - bla: blubb +Numbers, bools, special values: + - 1 + - true + - ~ + - 42.23 + - no +""", """{ +"Foo": [ + [ "a", "b", "c"], + { "bla": "blubb"} +], +"Numbers, bools, special values": [ + 1, true, null, 42.23, false +] +}""") \ No newline at end of file