2016-02-27 12:09:50 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
|
|
|
# (c) Copyright 2015 Felix Krause
|
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2016-01-24 18:19:03 +00:00
|
|
|
import "../yaml"
|
|
|
|
|
|
|
|
import unittest, json
|
|
|
|
|
|
|
|
proc wc(line, column: int, lineContent: string, message: string) =
|
2016-04-02 15:48:22 +00:00
|
|
|
echo "Warning (", line, ",", column, "): ", message, "\n", lineContent
|
2016-01-24 18:19:03 +00:00
|
|
|
|
|
|
|
proc ensureEqual(yamlIn, jsonIn: string) =
|
2016-09-13 08:39:54 +00:00
|
|
|
try:
|
|
|
|
var
|
|
|
|
parser = newYamlParser(initCoreTagLibrary(), wc)
|
|
|
|
s = parser.parse(newStringStream(yamlIn))
|
|
|
|
yamlResult = constructJson(s)
|
|
|
|
jsonResult = parseJson(jsonIn)
|
|
|
|
assert yamlResult.len == 1
|
|
|
|
assert(jsonResult == yamlResult[0], "Expected: " & $jsonResult & ", got: " &
|
|
|
|
$yamlResult[0])
|
|
|
|
except YamlStreamError:
|
|
|
|
let e = (ref YamlParserError)(getCurrentException().parent)
|
|
|
|
echo "error occurred: " & e.msg
|
|
|
|
echo "line: ", e.line, ", column: ", e.column
|
|
|
|
echo e.lineContent
|
|
|
|
raise e
|
2016-01-24 18:19:03 +00:00
|
|
|
|
|
|
|
suite "Constructing JSON":
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Simple Sequence":
|
2016-04-02 15:48:22 +00:00
|
|
|
ensureEqual("- 1\n- 2\n- 3", "[1, 2, 3]")
|
2016-01-24 18:19:03 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Simple Map":
|
2016-04-02 15:48:22 +00:00
|
|
|
ensureEqual("a: b\nc: d", """{"a": "b", "c": "d"}""")
|
2016-01-24 18:19:03 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Complex Structure":
|
2016-04-02 15:48:22 +00:00
|
|
|
ensureEqual("""
|
2016-01-24 18:19:03 +00:00
|
|
|
%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
|
|
|
|
]
|
|
|
|
}""")
|