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-02-22 20:56:30 +00:00
|
|
|
import "../yaml"
|
2016-09-20 19:53:38 +00:00
|
|
|
import unittest, commonTestUtils, streams
|
2016-02-22 20:56:30 +00:00
|
|
|
|
|
|
|
suite "DOM":
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Composing simple Scalar":
|
2016-04-02 15:48:22 +00:00
|
|
|
let
|
|
|
|
input = newStringStream("scalar")
|
|
|
|
result = loadDOM(input)
|
|
|
|
assert result.root.kind == yScalar
|
|
|
|
assert result.root.content == "scalar"
|
|
|
|
assert result.root.tag == "?"
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Serializing simple Scalar":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = initYamlDoc(newYamlNode("scalar"))
|
|
|
|
var result = serialize(input, initExtendedTagLibrary())
|
|
|
|
ensure(result, startDocEvent(), scalarEvent("scalar"), endDocEvent())
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Composing sequence":
|
2016-04-02 15:48:22 +00:00
|
|
|
let
|
|
|
|
input = newStringStream("- !!str a\n- !!bool no")
|
|
|
|
result = loadDOM(input)
|
|
|
|
assert result.root.kind == ySequence
|
|
|
|
assert result.root.tag == "?"
|
|
|
|
assert result.root.children.len == 2
|
|
|
|
assert result.root.children[0].kind == yScalar
|
|
|
|
assert result.root.children[0].tag == "tag:yaml.org,2002:str"
|
|
|
|
assert result.root.children[0].content == "a"
|
|
|
|
assert result.root.children[1].kind == yScalar
|
|
|
|
assert result.root.children[1].tag == "tag:yaml.org,2002:bool"
|
|
|
|
assert result.root.children[1].content == "no"
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Serializing sequence":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = initYamlDoc(newYamlNode([
|
|
|
|
newYamlNode("a", "tag:yaml.org,2002:str"),
|
|
|
|
newYamlNode("no", "tag:yaml.org,2002:bool")]))
|
|
|
|
var result = serialize(input, initExtendedTagLibrary())
|
|
|
|
ensure(result, startDocEvent(), startSeqEvent(),
|
|
|
|
scalarEvent("a", yTagString), scalarEvent("no", yTagBoolean),
|
|
|
|
endSeqEvent(), endDocEvent())
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Composing mapping":
|
2016-04-02 15:48:22 +00:00
|
|
|
let
|
|
|
|
input = newStringStream("--- !!map\n!foo bar: [a, b]")
|
|
|
|
result = loadDOM(input)
|
|
|
|
assert result.root.kind == yMapping
|
|
|
|
assert result.root.tag == "tag:yaml.org,2002:map"
|
|
|
|
assert result.root.pairs.len == 1
|
|
|
|
assert result.root.pairs[0].key.kind == yScalar
|
|
|
|
assert result.root.pairs[0].key.tag == "!foo"
|
|
|
|
assert result.root.pairs[0].key.content == "bar"
|
|
|
|
assert result.root.pairs[0].value.kind == ySequence
|
|
|
|
assert result.root.pairs[0].value.children.len == 2
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Serializing mapping":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = initYamlDoc(newYamlNode([
|
|
|
|
(key: newYamlNode("bar"), value: newYamlNode([newYamlNode("a"),
|
|
|
|
newYamlNode("b")]))]))
|
|
|
|
var result = serialize(input, initExtendedTagLibrary())
|
|
|
|
ensure(result, startDocEvent(), startMapEvent(), scalarEvent("bar"),
|
|
|
|
startSeqEvent(), scalarEvent("a"), scalarEvent("b"),
|
|
|
|
endSeqEvent(), endMapEvent(), endDocEvent())
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Composing with anchors":
|
2016-04-02 15:48:22 +00:00
|
|
|
let
|
|
|
|
input = newStringStream("- &a foo\n- &b bar\n- *a\n- *b")
|
|
|
|
result = loadDOM(input)
|
|
|
|
assert result.root.kind == ySequence
|
|
|
|
assert result.root.children.len == 4
|
|
|
|
assert result.root.children[0].kind == yScalar
|
|
|
|
assert result.root.children[0].content == "foo"
|
|
|
|
assert result.root.children[1].kind == yScalar
|
|
|
|
assert result.root.children[1].content == "bar"
|
|
|
|
assert result.root.children[0] == result.root.children[2]
|
|
|
|
assert result.root.children[1] == result.root.children[3]
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Serializing with anchors":
|
2016-04-02 15:48:22 +00:00
|
|
|
let
|
|
|
|
a = newYamlNode("a")
|
|
|
|
b = newYamlNode("b")
|
|
|
|
input = initYamlDoc(newYamlNode([a, b, newYamlNode("c"), a, b]))
|
|
|
|
var result = serialize(input, initExtendedTagLibrary())
|
|
|
|
ensure(result, startDocEvent(), startSeqEvent(),
|
|
|
|
scalarEvent("a", anchor=0.AnchorId),
|
|
|
|
scalarEvent("b", anchor=1.AnchorId), scalarEvent("c"),
|
|
|
|
aliasEvent(0.AnchorId), aliasEvent(1.AnchorId), endSeqEvent(),
|
|
|
|
endDocEvent())
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Serializing with all anchors":
|
2016-04-02 15:48:22 +00:00
|
|
|
let
|
|
|
|
a = newYamlNode("a")
|
|
|
|
input = initYamlDoc(newYamlNode([a, newYamlNode("b"), a]))
|
|
|
|
var result = serialize(input, initExtendedTagLibrary(), asAlways)
|
|
|
|
ensure(result, startDocEvent(), startSeqEvent(anchor=0.AnchorId),
|
|
|
|
scalarEvent("a", anchor=1.AnchorId),
|
|
|
|
scalarEvent("b", anchor=2.AnchorId), aliasEvent(1.AnchorId),
|
|
|
|
endSeqEvent(), endDocEvent())
|