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-11-28 19:17:04 +00:00
|
|
|
import unittest, commonTestUtils, streams, tables
|
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")
|
2023-03-13 21:06:07 +00:00
|
|
|
result = loadAs[YamlNode](input)
|
|
|
|
assert result.kind == yScalar
|
|
|
|
assert result.content == "scalar"
|
|
|
|
assert result.tag == yTagQuestionMark
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Serializing simple Scalar":
|
2023-03-13 21:06:07 +00:00
|
|
|
let input = newYamlNode("scalar")
|
|
|
|
var result = represent(input)
|
2020-11-10 18:07:46 +00:00
|
|
|
ensure(result, startStreamEvent(), startDocEvent(), scalarEvent("scalar"),
|
|
|
|
endDocEvent(), endStreamEvent())
|
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")
|
2023-03-13 21:06:07 +00:00
|
|
|
result = loadAs[YamlNode](input)
|
|
|
|
assert result.kind == ySequence
|
|
|
|
assert result.tag == yTagQuestionMark
|
|
|
|
assert result.len == 2
|
|
|
|
assert result[0].kind == yScalar
|
|
|
|
assert result[0].tag == yTagString
|
|
|
|
assert result[0].content == "a"
|
|
|
|
assert result[1].kind == yScalar
|
|
|
|
assert result[1].tag == yTagBoolean
|
|
|
|
assert result[1].content == "no"
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Serializing sequence":
|
2023-03-13 21:06:07 +00:00
|
|
|
let input = newYamlNode([
|
2021-03-23 17:51:05 +00:00
|
|
|
newYamlNode("a", yTagString),
|
2023-03-13 21:06:07 +00:00
|
|
|
newYamlNode("no", yTagBoolean)])
|
|
|
|
var result = represent(input, tsAll)
|
2020-11-10 18:07:46 +00:00
|
|
|
ensure(result, startStreamEvent(), startDocEvent(), startSeqEvent(),
|
2016-04-02 15:48:22 +00:00
|
|
|
scalarEvent("a", yTagString), scalarEvent("no", yTagBoolean),
|
2020-11-10 18:07:46 +00:00
|
|
|
endSeqEvent(), endDocEvent(), endStreamEvent())
|
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]")
|
2023-03-13 21:06:07 +00:00
|
|
|
result = loadAs[YamlNode](input)
|
|
|
|
assert result.kind == yMapping
|
|
|
|
assert result.tag == yTagMapping
|
|
|
|
assert result.fields.len == 1
|
|
|
|
for key, value in result.fields.pairs:
|
2016-11-28 19:17:04 +00:00
|
|
|
assert key.kind == yScalar
|
2021-03-23 17:51:05 +00:00
|
|
|
assert $key.tag == "!foo"
|
2016-11-28 19:17:04 +00:00
|
|
|
assert key.content == "bar"
|
|
|
|
assert value.kind == ySequence
|
|
|
|
assert value.len == 2
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Serializing mapping":
|
2023-03-13 21:06:07 +00:00
|
|
|
let input = newYamlNode([
|
2016-04-02 15:48:22 +00:00
|
|
|
(key: newYamlNode("bar"), value: newYamlNode([newYamlNode("a"),
|
2023-03-13 21:06:07 +00:00
|
|
|
newYamlNode("b")]))])
|
|
|
|
var result = represent(input)
|
2020-11-10 18:07:46 +00:00
|
|
|
ensure(result, startStreamEvent(), startDocEvent(), startMapEvent(),
|
|
|
|
scalarEvent("bar"), startSeqEvent(), scalarEvent("a"), scalarEvent("b"),
|
|
|
|
endSeqEvent(), endMapEvent(), endDocEvent(), endStreamEvent())
|
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")
|
2023-03-13 21:06:07 +00:00
|
|
|
result = loadAs[YamlNode](input)
|
|
|
|
assert result.kind == ySequence
|
|
|
|
assert result.len == 4
|
|
|
|
assert result[0].kind == yScalar
|
|
|
|
assert result[0].content == "foo"
|
|
|
|
assert result[1].kind == yScalar
|
|
|
|
assert result[1].content == "bar"
|
|
|
|
assert cast[pointer](result[0]) == cast[pointer](result[2])
|
|
|
|
assert cast[pointer](result[1]) == cast[pointer](result[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")
|
2023-03-13 21:06:07 +00:00
|
|
|
input = newYamlNode([a, b, newYamlNode("c"), a, b])
|
|
|
|
var result = represent(input)
|
2020-11-10 18:07:46 +00:00
|
|
|
ensure(result, startStreamEvent(), startDocEvent(), startSeqEvent(),
|
2020-11-06 15:21:58 +00:00
|
|
|
scalarEvent("a", anchor="a".Anchor),
|
|
|
|
scalarEvent("b", anchor="b".Anchor), scalarEvent("c"),
|
|
|
|
aliasEvent("a".Anchor), aliasEvent("b".Anchor), endSeqEvent(),
|
2020-11-10 18:07:46 +00:00
|
|
|
endDocEvent(), endStreamEvent())
|
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")
|
2023-03-13 21:06:07 +00:00
|
|
|
input = newYamlNode([a, newYamlNode("b"), a])
|
|
|
|
var result = represent(input, a = asAlways)
|
2020-11-10 18:07:46 +00:00
|
|
|
ensure(result, startStreamEvent(), startDocEvent(),
|
|
|
|
startSeqEvent(anchor="a".Anchor),
|
2020-11-06 15:21:58 +00:00
|
|
|
scalarEvent("a", anchor = "b".Anchor),
|
|
|
|
scalarEvent("b", anchor="c".Anchor), aliasEvent("b".Anchor),
|
2022-06-05 10:27:19 +00:00
|
|
|
endSeqEvent(), endDocEvent(), endStreamEvent())
|
|
|
|
test "Deserialize parts of the input into YamlNode":
|
|
|
|
let
|
|
|
|
input = "a: b\nc: [d, e]"
|
|
|
|
type Root = object
|
|
|
|
a: string
|
|
|
|
c: YamlNode
|
|
|
|
var result = loadAs[Root](input)
|
|
|
|
assert result.a == "b"
|
|
|
|
assert result.c.kind == ySequence
|
|
|
|
assert result.c.len == 2
|
|
|
|
assert result.c[0].kind == yScalar
|
|
|
|
assert result.c[0].content == "d"
|
|
|
|
assert result.c[1].kind == yScalar
|
|
|
|
assert result.c[1].content == "e"
|
|
|
|
test "Serialize value that contains a YamlNode":
|
|
|
|
type Root = object
|
|
|
|
a: string
|
|
|
|
c: YamlNode
|
|
|
|
let value = Root(
|
|
|
|
a: "b",
|
|
|
|
c: newYamlNode([newYamlNode("d"), newYamlNode("e")]))
|
|
|
|
var result = represent(value, tsNone, handles = @[])
|
|
|
|
ensure(result, startStreamEvent(), startDocEvent(), startMapEvent(),
|
|
|
|
scalarEvent("a"), scalarEvent("b"), scalarEvent("c"), startSeqEvent(),
|
|
|
|
scalarEvent("d"), scalarEvent("e"), endSeqEvent(), endMapEvent(),
|
|
|
|
endDocEvent(), endStreamEvent())
|