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-12 19:44:38 +00:00
|
|
|
import "../yaml"
|
2016-09-20 19:53:38 +00:00
|
|
|
import unittest, strutils, streams, tables
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-02-01 18:48:42 +00:00
|
|
|
type
|
2016-04-02 15:48:22 +00:00
|
|
|
MyTuple = tuple
|
|
|
|
str: string
|
|
|
|
i: int32
|
|
|
|
b: bool
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
TrafficLight = enum
|
|
|
|
tlGreen, tlYellow, tlRed
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
Person = object
|
|
|
|
firstnamechar: char
|
|
|
|
surname: string
|
|
|
|
age: int32
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
Node = object
|
|
|
|
value: string
|
|
|
|
next: ref Node
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-04-04 18:44:16 +00:00
|
|
|
BetterInt = distinct int
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-06-08 17:15:50 +00:00
|
|
|
AnimalKind = enum
|
|
|
|
akCat, akDog
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-06-08 17:15:50 +00:00
|
|
|
Animal = object
|
|
|
|
name: string
|
|
|
|
case kind: AnimalKind
|
|
|
|
of akCat:
|
|
|
|
purringIntensity: int
|
|
|
|
of akDog:
|
|
|
|
barkometer: int
|
2016-04-04 18:44:16 +00:00
|
|
|
|
2016-10-15 15:58:29 +00:00
|
|
|
DumbEnum = enum
|
|
|
|
deA, deB, deC, deD
|
|
|
|
|
|
|
|
NonVariantWithTransient = object
|
|
|
|
a, b, c, d: string
|
|
|
|
|
|
|
|
VariantWithTransient = object
|
|
|
|
gStorable, gTemporary: string
|
|
|
|
case kind: DumbEnum
|
|
|
|
of deA, deB:
|
|
|
|
cStorable, cTemporary: string
|
|
|
|
of deC:
|
|
|
|
alwaysThere: int
|
|
|
|
of deD:
|
|
|
|
neverThere: int
|
|
|
|
|
2016-10-26 16:32:54 +00:00
|
|
|
WithDefault = object
|
|
|
|
a, b, c, d: string
|
|
|
|
|
2016-10-15 15:58:29 +00:00
|
|
|
markAsTransient(NonVariantWithTransient, a)
|
|
|
|
markAsTransient(NonVariantWithTransient, c)
|
|
|
|
|
|
|
|
markAsTransient(VariantWithTransient, gTemporary)
|
|
|
|
markAsTransient(VariantWithTransient, cTemporary)
|
|
|
|
markAsTransient(VariantWithTransient, neverThere)
|
|
|
|
|
2016-10-26 16:32:54 +00:00
|
|
|
setDefaultValue(WithDefault, b, "b")
|
|
|
|
setDefaultValue(WithDefault, d, "d")
|
2016-10-15 15:58:29 +00:00
|
|
|
|
2016-04-04 18:44:16 +00:00
|
|
|
proc `$`(v: BetterInt): string {.borrow.}
|
2016-08-17 20:50:37 +00:00
|
|
|
proc `==`(left, right: BetterInt): bool {.borrow.}
|
2016-02-01 18:48:42 +00:00
|
|
|
|
2016-04-21 16:58:53 +00:00
|
|
|
setTagUri(TrafficLight, "!tl")
|
|
|
|
setTagUri(Node, "!example.net:Node")
|
|
|
|
setTagUri(BetterInt, "!test:BetterInt")
|
2016-02-16 18:24:55 +00:00
|
|
|
|
2016-10-10 18:16:54 +00:00
|
|
|
const yamlDirs = "%YAML 1.2\n%TAG !n! tag:nimyaml.org,2016:\n--- "
|
|
|
|
|
2016-02-16 18:24:55 +00:00
|
|
|
proc representObject*(value: BetterInt, ts: TagStyle = tsNone,
|
2016-09-01 18:56:34 +00:00
|
|
|
c: SerializationContext, tag: TagId) {.raises: [].} =
|
|
|
|
var
|
|
|
|
val = $value
|
|
|
|
i = val.len - 3
|
|
|
|
while i > 0:
|
|
|
|
val.insert("_", i)
|
|
|
|
i -= 3
|
|
|
|
c.put(scalarEvent(val, tag, yAnchorNone))
|
2016-02-16 18:24:55 +00:00
|
|
|
|
|
|
|
proc constructObject*(s: var YamlStream, c: ConstructionContext,
|
|
|
|
result: var BetterInt)
|
2016-04-02 15:48:22 +00:00
|
|
|
{.raises: [YamlConstructionError, YamlStreamError].} =
|
|
|
|
constructScalarItem(s, item, BetterInt):
|
|
|
|
result = BetterInt(parseBiggestInt(item.scalarContent) + 1)
|
2016-02-15 18:46:21 +00:00
|
|
|
|
2016-02-01 18:48:42 +00:00
|
|
|
template assertStringEqual(expected, actual: string) =
|
2016-04-02 15:48:22 +00:00
|
|
|
for i in countup(0, min(expected.len, actual.len)):
|
|
|
|
if expected[i] != actual[i]:
|
|
|
|
echo "string mismatch at character #", i, "(expected:\'",
|
|
|
|
expected[i], "\', was \'", actual[i], "\'):"
|
|
|
|
echo "expected:\n", expected, "\nactual:\n", actual
|
|
|
|
assert(false)
|
2016-01-28 21:57:14 +00:00
|
|
|
|
2016-09-24 14:45:49 +00:00
|
|
|
template expectConstructionError(li, co: int, message: string, body: typed) =
|
2016-09-23 13:42:24 +00:00
|
|
|
try:
|
|
|
|
body
|
|
|
|
echo "Expected YamlConstructionError, but none was raised!"
|
|
|
|
fail()
|
|
|
|
except YamlConstructionError:
|
|
|
|
let e = (ref YamlConstructionError)(getCurrentException())
|
2016-09-24 14:45:49 +00:00
|
|
|
doAssert li == e.line, "Expected error line " & $li & ", was " & $e.line
|
|
|
|
doAssert co == e.column, "Expected error column " & $co & ", was " & $e.column
|
2016-10-19 20:04:46 +00:00
|
|
|
doAssert message == e.msg, "Expected error message \n" & escape(message) &
|
|
|
|
", got \n" & escape(e.msg)
|
2016-09-23 13:42:24 +00:00
|
|
|
|
2016-01-28 21:57:14 +00:00
|
|
|
proc newNode(v: string): ref Node =
|
2016-04-02 15:48:22 +00:00
|
|
|
new(result)
|
|
|
|
result.value = v
|
|
|
|
result.next = nil
|
2015-12-29 14:09:37 +00:00
|
|
|
|
2016-08-17 20:50:37 +00:00
|
|
|
let blockOnly = defineOptions(style=psBlockOnly)
|
2016-02-26 20:55:59 +00:00
|
|
|
|
2016-08-17 20:50:37 +00:00
|
|
|
suite "Serialization":
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load integer without fixed length":
|
2016-04-04 18:44:16 +00:00
|
|
|
var input = newStringStream("-4247")
|
|
|
|
var result: int
|
|
|
|
load(input, result)
|
2016-09-20 19:53:38 +00:00
|
|
|
assert result == -4247, "result is " & $result
|
|
|
|
|
2016-04-04 18:44:16 +00:00
|
|
|
input = newStringStream($(int64(int32.high) + 1'i64))
|
|
|
|
var gotException = false
|
|
|
|
try: load(input, result)
|
|
|
|
except: gotException = true
|
|
|
|
assert gotException, "Expected exception, got none."
|
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump integer without fixed length":
|
2016-04-04 18:44:16 +00:00
|
|
|
var input = -4247
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & "\n\"-4247\"", output
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-04-04 18:44:16 +00:00
|
|
|
when sizeof(int) == sizeof(int64):
|
|
|
|
input = int(int32.high) + 1
|
|
|
|
var gotException = false
|
2016-09-19 17:33:29 +00:00
|
|
|
try: output = dump(input, tsNone, asTidy, blockOnly)
|
2016-04-04 18:44:16 +00:00
|
|
|
except: gotException = true
|
|
|
|
assert gotException, "Expected exception, got none."
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load Hex byte (0xFF)":
|
2016-09-19 18:51:50 +00:00
|
|
|
let input = newStringStream("0xFF")
|
|
|
|
var result: byte
|
|
|
|
load(input, result)
|
|
|
|
assert(result == 255)
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load Hex byte (0xC)":
|
2016-09-19 18:51:50 +00:00
|
|
|
let input = newStringStream("0xC")
|
|
|
|
var result: byte
|
|
|
|
load(input, result)
|
|
|
|
assert(result == 12)
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load Octal byte (0o14)":
|
2016-09-19 18:51:50 +00:00
|
|
|
let input = newStringStream("0o14")
|
|
|
|
var result: byte
|
|
|
|
load(input, result)
|
|
|
|
assert(result == 12)
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load byte (14)":
|
2016-09-19 18:51:50 +00:00
|
|
|
let input = newStringStream("14")
|
|
|
|
var result: byte
|
|
|
|
load(input, result)
|
|
|
|
assert(result == 14)
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load Hex int (0xFF)":
|
2016-09-19 18:51:50 +00:00
|
|
|
let input = newStringStream("0xFF")
|
|
|
|
var result: int
|
|
|
|
load(input, result)
|
|
|
|
assert(result == 255)
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load Hex int (0xC)":
|
2016-09-19 18:51:50 +00:00
|
|
|
let input = newStringStream("0xC")
|
|
|
|
var result: int
|
|
|
|
load(input, result)
|
|
|
|
assert(result == 12)
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load Octal int (0o14)":
|
2016-09-19 18:51:50 +00:00
|
|
|
let input = newStringStream("0o14")
|
|
|
|
var result: int
|
|
|
|
load(input, result)
|
|
|
|
assert(result == 12)
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load int (14)":
|
2016-09-19 18:51:50 +00:00
|
|
|
let input = newStringStream("14")
|
|
|
|
var result: int
|
|
|
|
load(input, result)
|
|
|
|
assert(result == 14)
|
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load nil string":
|
2016-10-10 18:16:54 +00:00
|
|
|
let input = newStringStream("!<tag:nimyaml.org,2016:nil:string> \"\"")
|
2016-04-04 19:21:24 +00:00
|
|
|
var result: string
|
|
|
|
load(input, result)
|
|
|
|
assert isNil(result)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump nil string":
|
2016-04-04 19:21:24 +00:00
|
|
|
let input: string = nil
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & "\n!n!nil:string \"\"", output
|
2016-04-04 19:21:24 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load string sequence":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = newStringStream(" - a\n - b")
|
|
|
|
var result: seq[string]
|
|
|
|
load(input, result)
|
|
|
|
assert result.len == 2
|
|
|
|
assert result[0] == "a"
|
|
|
|
assert result[1] == "b"
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump string sequence":
|
2016-04-02 15:48:22 +00:00
|
|
|
var input = @["a", "b"]
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & "\n- a\n- b", output
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load nil seq":
|
2016-10-10 18:16:54 +00:00
|
|
|
let input = newStringStream("!<tag:nimyaml.org,2016:nil:seq> \"\"")
|
2016-04-04 19:21:24 +00:00
|
|
|
var result: seq[int]
|
|
|
|
load(input, result)
|
|
|
|
assert isNil(result)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump nil seq":
|
2016-04-04 19:21:24 +00:00
|
|
|
let input: seq[int] = nil
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & "\n!n!nil:seq \"\"", output
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load char set":
|
2016-04-02 16:29:26 +00:00
|
|
|
let input = newStringStream("- a\n- b")
|
|
|
|
var result: set[char]
|
|
|
|
load(input, result)
|
|
|
|
assert result.card == 2
|
|
|
|
assert 'a' in result
|
|
|
|
assert 'b' in result
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump char set":
|
2016-04-02 16:29:26 +00:00
|
|
|
var input = {'a', 'b'}
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & "\n- a\n- b", output
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load array":
|
2016-04-03 09:29:49 +00:00
|
|
|
let input = newStringStream("- 23\n- 42\n- 47")
|
|
|
|
var result: array[0..2, int32]
|
|
|
|
load(input, result)
|
|
|
|
assert result[0] == 23
|
|
|
|
assert result[1] == 42
|
|
|
|
assert result[2] == 47
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump array":
|
2016-04-03 09:29:49 +00:00
|
|
|
let input = [23'i32, 42'i32, 47'i32]
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & "\n- 23\n- 42\n- 47", output
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load Table[int, string]":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = newStringStream("23: dreiundzwanzig\n42: zweiundvierzig")
|
|
|
|
var result: Table[int32, string]
|
|
|
|
load(input, result)
|
|
|
|
assert result.len == 2
|
|
|
|
assert result[23] == "dreiundzwanzig"
|
|
|
|
assert result[42] == "zweiundvierzig"
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump Table[int, string]":
|
2016-04-02 15:48:22 +00:00
|
|
|
var input = initTable[int32, string]()
|
|
|
|
input[23] = "dreiundzwanzig"
|
|
|
|
input[42] = "zweiundvierzig"
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual(yamlDirs & "\n23: dreiundzwanzig\n42: zweiundvierzig",
|
2016-09-19 17:33:29 +00:00
|
|
|
output)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load OrderedTable[tuple[int32, int32], string]":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = newStringStream("- {a: 23, b: 42}: drzw\n- {a: 13, b: 47}: drsi")
|
|
|
|
var result: OrderedTable[tuple[a, b: int32], string]
|
2016-08-15 19:30:49 +00:00
|
|
|
load(input, result)
|
2016-04-02 15:48:22 +00:00
|
|
|
var i = 0
|
|
|
|
for key, value in result.pairs:
|
|
|
|
case i
|
|
|
|
of 0:
|
|
|
|
assert key == (a: 23'i32, b: 42'i32)
|
|
|
|
assert value == "drzw"
|
|
|
|
of 1:
|
|
|
|
assert key == (a: 13'i32, b: 47'i32)
|
|
|
|
assert value == "drsi"
|
|
|
|
else: assert false
|
|
|
|
i.inc()
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump OrderedTable[tuple[int32, int32], string]":
|
2016-04-02 15:48:22 +00:00
|
|
|
var input = initOrderedTable[tuple[a, b: int32], string]()
|
|
|
|
input.add((a: 23'i32, b: 42'i32), "dreiundzwanzigzweiundvierzig")
|
|
|
|
input.add((a: 13'i32, b: 47'i32), "dreizehnsiebenundvierzig")
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsRootOnly, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual(yamlDirs &
|
|
|
|
"""!n!tables:OrderedTable(tag:nimyaml.org;2016:tuple(tag:nimyaml.org;2016:system:int32;tag:nimyaml.org;2016:system:int32);tag:yaml.org;2002:str)
|
2016-03-25 21:22:42 +00:00
|
|
|
-
|
|
|
|
?
|
|
|
|
a: 23
|
|
|
|
b: 42
|
|
|
|
: dreiundzwanzigzweiundvierzig
|
|
|
|
-
|
|
|
|
?
|
|
|
|
a: 13
|
|
|
|
b: 47
|
2016-09-19 17:33:29 +00:00
|
|
|
: dreizehnsiebenundvierzig""", output)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load Sequences in Sequence":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = newStringStream(" - [1, 2, 3]\n - [4, 5]\n - [6]")
|
|
|
|
var result: seq[seq[int32]]
|
|
|
|
load(input, result)
|
|
|
|
assert result.len == 3
|
|
|
|
assert result[0] == @[1.int32, 2.int32, 3.int32]
|
|
|
|
assert result[1] == @[4.int32, 5.int32]
|
|
|
|
assert result[2] == @[6.int32]
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump Sequences in Sequence":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = @[@[1.int32, 2.int32, 3.int32], @[4.int32, 5.int32], @[6.int32]]
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & "\n- [1, 2, 3]\n- [4, 5]\n- [6]", output
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load Enum":
|
2016-09-19 17:33:29 +00:00
|
|
|
let input =
|
2016-10-10 18:16:54 +00:00
|
|
|
newStringStream("!<tag:nimyaml.org,2016:system:seq(tl)>\n- !tl tlRed\n- tlGreen\n- tlYellow")
|
2016-04-02 15:48:22 +00:00
|
|
|
var result: seq[TrafficLight]
|
|
|
|
load(input, result)
|
|
|
|
assert result.len == 3
|
|
|
|
assert result[0] == tlRed
|
|
|
|
assert result[1] == tlGreen
|
|
|
|
assert result[2] == tlYellow
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump Enum":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = @[tlRed, tlGreen, tlYellow]
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & "\n- tlRed\n- tlGreen\n- tlYellow", output
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load Tuple":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = newStringStream("str: value\ni: 42\nb: true")
|
|
|
|
var result: MyTuple
|
|
|
|
load(input, result)
|
|
|
|
assert result.str == "value"
|
|
|
|
assert result.i == 42
|
|
|
|
assert result.b == true
|
2016-02-01 18:48:42 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump Tuple":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = (str: "value", i: 42.int32, b: true)
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & "\nstr: value\ni: 42\nb: y", output
|
2016-09-19 18:51:50 +00:00
|
|
|
|
2016-09-21 13:40:03 +00:00
|
|
|
test "Load Tuple - unknown field":
|
|
|
|
let input = "str: value\nfoo: bar\ni: 42\nb: true"
|
|
|
|
var result: MyTuple
|
2016-09-24 14:45:49 +00:00
|
|
|
expectConstructionError(2, 1, "While constructing MyTuple: Unknown field: \"foo\""):
|
2016-09-21 13:40:03 +00:00
|
|
|
load(input, result)
|
|
|
|
|
|
|
|
test "Load Tuple - missing field":
|
|
|
|
let input = "str: value\nb: true"
|
|
|
|
var result: MyTuple
|
2016-09-24 14:45:49 +00:00
|
|
|
expectConstructionError(2, 8, "While constructing MyTuple: Missing field: \"i\""):
|
2016-09-21 13:40:03 +00:00
|
|
|
load(input, result)
|
|
|
|
|
|
|
|
test "Load Tuple - duplicate field":
|
|
|
|
let input = "str: value\ni: 42\nb: true\nb: true"
|
|
|
|
var result: MyTuple
|
2016-09-24 14:45:49 +00:00
|
|
|
expectConstructionError(4, 1, "While constructing MyTuple: Duplicate field: \"b\""):
|
2016-09-21 13:40:03 +00:00
|
|
|
load(input, result)
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load Multiple Documents":
|
2016-09-19 18:51:50 +00:00
|
|
|
let input = newStringStream("1\n---\n2")
|
|
|
|
var result: seq[int]
|
|
|
|
loadMultiDoc(input, result)
|
2016-09-19 20:51:33 +00:00
|
|
|
assert(result.len == 2)
|
2016-09-19 18:51:50 +00:00
|
|
|
assert result[0] == 1
|
|
|
|
assert result[1] == 2
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load Multiple Documents (Single Doc)":
|
2016-09-19 18:51:50 +00:00
|
|
|
let input = newStringStream("1")
|
|
|
|
var result: seq[int]
|
|
|
|
loadMultiDoc(input, result)
|
2016-09-19 20:51:33 +00:00
|
|
|
assert(result.len == 1)
|
2016-09-19 18:51:50 +00:00
|
|
|
assert result[0] == 1
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load custom object":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = newStringStream("firstnamechar: P\nsurname: Pan\nage: 12")
|
|
|
|
var result: Person
|
|
|
|
load(input, result)
|
|
|
|
assert result.firstnamechar == 'P'
|
|
|
|
assert result.surname == "Pan"
|
|
|
|
assert result.age == 12
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump custom object":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = Person(firstnamechar: 'P', surname: "Pan", age: 12)
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual(yamlDirs &
|
|
|
|
"\nfirstnamechar: P\nsurname: Pan\nage: 12", output)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-21 13:40:03 +00:00
|
|
|
test "Load custom object - unknown field":
|
2016-09-23 13:42:24 +00:00
|
|
|
let input = " firstnamechar: P\n surname: Pan\n age: 12\n occupation: free"
|
2016-09-21 13:40:03 +00:00
|
|
|
var result: Person
|
2016-09-24 14:45:49 +00:00
|
|
|
expectConstructionError(4, 3, "While constructing Person: Unknown field: \"occupation\""):
|
2016-09-21 13:40:03 +00:00
|
|
|
load(input, result)
|
|
|
|
|
|
|
|
test "Load custom object - missing field":
|
2016-09-23 13:42:24 +00:00
|
|
|
let input = "surname: Pan\nage: 12\n "
|
2016-09-21 13:40:03 +00:00
|
|
|
var result: Person
|
2016-09-24 14:45:49 +00:00
|
|
|
expectConstructionError(3, 3, "While constructing Person: Missing field: \"firstnamechar\""):
|
2016-09-21 13:40:03 +00:00
|
|
|
load(input, result)
|
|
|
|
|
|
|
|
test "Load custom object - duplicate field":
|
|
|
|
let input = "firstnamechar: P\nsurname: Pan\nage: 12\nsurname: Pan"
|
|
|
|
var result: Person
|
2016-09-24 14:45:49 +00:00
|
|
|
expectConstructionError(4, 1, "While constructing Person: Duplicate field: \"surname\""):
|
2016-09-21 13:40:03 +00:00
|
|
|
load(input, result)
|
|
|
|
|
2016-09-19 20:51:33 +00:00
|
|
|
test "Load sequence with explicit tags":
|
2016-10-10 18:16:54 +00:00
|
|
|
let input = newStringStream(yamlDirs & "!n!system:seq(" &
|
|
|
|
"tag:yaml.org;2002:str)\n- !!str one\n- !!str two")
|
2016-04-02 15:48:22 +00:00
|
|
|
var result: seq[string]
|
|
|
|
load(input, result)
|
|
|
|
assert result[0] == "one"
|
|
|
|
assert result[1] == "two"
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump sequence with explicit tags":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = @["one", "two"]
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsAll, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual(yamlDirs & "!n!system:seq(" &
|
|
|
|
"tag:yaml.org;2002:str) \n- !!str one\n- !!str two", output)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load custom object with explicit root tag":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = newStringStream(
|
2016-10-10 18:16:54 +00:00
|
|
|
"--- !<tag:nimyaml.org,2016:custom:Person>\nfirstnamechar: P\nsurname: Pan\nage: 12")
|
2016-04-02 15:48:22 +00:00
|
|
|
var result: Person
|
|
|
|
load(input, result)
|
|
|
|
assert result.firstnamechar == 'P'
|
|
|
|
assert result.surname == "Pan"
|
|
|
|
assert result.age == 12
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump custom object with explicit root tag":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = Person(firstnamechar: 'P', surname: "Pan", age: 12)
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsRootOnly, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual(yamlDirs &
|
|
|
|
"!n!custom:Person \nfirstnamechar: P\nsurname: Pan\nage: 12", output)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load custom variant object":
|
2016-06-08 17:15:50 +00:00
|
|
|
let input = newStringStream(
|
|
|
|
"---\n- - name: Bastet\n - kind: akCat\n - purringIntensity: 7\n" &
|
|
|
|
"- - name: Anubis\n - kind: akDog\n - barkometer: 13")
|
|
|
|
var result: seq[Animal]
|
|
|
|
load(input, result)
|
|
|
|
assert result.len == 2
|
|
|
|
assert result[0].name == "Bastet"
|
|
|
|
assert result[0].kind == akCat
|
|
|
|
assert result[0].purringIntensity == 7
|
|
|
|
assert result[1].name == "Anubis"
|
|
|
|
assert result[1].kind == akDog
|
|
|
|
assert result[1].barkometer == 13
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump custom variant object":
|
2016-06-08 17:15:50 +00:00
|
|
|
let input = @[Animal(name: "Bastet", kind: akCat, purringIntensity: 7),
|
|
|
|
Animal(name: "Anubis", kind: akDog, barkometer: 13)]
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsNone, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & """
|
|
|
|
|
2016-06-08 17:15:50 +00:00
|
|
|
-
|
|
|
|
-
|
|
|
|
name: Bastet
|
|
|
|
-
|
|
|
|
kind: akCat
|
|
|
|
-
|
|
|
|
purringIntensity: 7
|
|
|
|
-
|
|
|
|
-
|
|
|
|
name: Anubis
|
|
|
|
-
|
|
|
|
kind: akDog
|
|
|
|
-
|
2016-09-19 17:33:29 +00:00
|
|
|
barkometer: 13""", output
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-21 13:40:03 +00:00
|
|
|
test "Load custom variant object - missing field":
|
2016-09-23 13:42:24 +00:00
|
|
|
let input = "[{name: Bastet}, {kind: akCat}]"
|
2016-09-21 13:40:03 +00:00
|
|
|
var result: Animal
|
2016-09-24 14:45:49 +00:00
|
|
|
expectConstructionError(1, 32, "While constructing Animal: Missing field: \"purringIntensity\""):
|
2016-09-21 13:40:03 +00:00
|
|
|
load(input, result)
|
|
|
|
|
2016-10-19 20:04:46 +00:00
|
|
|
test "Load non-variant object with transient fields":
|
|
|
|
let input = "{b: b, d: d}"
|
|
|
|
var result: NonVariantWithTransient
|
|
|
|
load(input, result)
|
|
|
|
assert isNil(result.a)
|
|
|
|
assert result.b == "b"
|
|
|
|
assert isNil(result.c)
|
|
|
|
assert result.d == "d"
|
|
|
|
|
|
|
|
test "Load non-variant object with transient fields - unknown field":
|
|
|
|
let input = "{b: b, c: c, d: d}"
|
|
|
|
var result: NonVariantWithTransient
|
|
|
|
expectConstructionError(1, 9, "While constructing NonVariantWithTransient: Field \"c\" is transient and may not occur in input"):
|
|
|
|
load(input, result)
|
|
|
|
|
2016-10-15 15:58:29 +00:00
|
|
|
test "Dump non-variant object with transient fields":
|
|
|
|
let input = NonVariantWithTransient(a: "a", b: "b", c: "c", d: "d")
|
|
|
|
let output = dump(input, tsNone, asTidy, blockOnly)
|
|
|
|
assertStringEqual yamlDirs & "\nb: b\nd: d", output
|
|
|
|
|
2016-10-19 20:04:46 +00:00
|
|
|
test "Load variant object with transient fields":
|
|
|
|
let input = "[[gStorable: gs, kind: deB, cStorable: cs], [gStorable: a, kind: deD]]"
|
|
|
|
var result: seq[VariantWithTransient]
|
|
|
|
load(input, result)
|
|
|
|
assert result.len == 2
|
|
|
|
assert result[0].kind == deB
|
|
|
|
assert result[0].gStorable == "gs"
|
|
|
|
assert result[0].cStorable == "cs"
|
|
|
|
assert result[1].kind == deD
|
|
|
|
assert result[1].gStorable == "a"
|
|
|
|
|
|
|
|
test "Load variant object with transient fields":
|
|
|
|
let input = "[gStorable: gc, kind: deD, neverThere: foo]"
|
|
|
|
var result: VariantWithTransient
|
|
|
|
expectConstructionError(1, 38, "While constructing VariantWithTransient: Field \"neverThere\" is transient and may not occur in input"):
|
|
|
|
load(input, result)
|
|
|
|
|
2016-10-15 15:58:29 +00:00
|
|
|
test "Dump variant object with transient fields":
|
|
|
|
let input = @[VariantWithTransient(kind: deB, gStorable: "gs",
|
|
|
|
gTemporary: "gt", cStorable: "cs", cTemporary: "ct"),
|
|
|
|
VariantWithTransient(kind: deD, gStorable: "a", gTemporary: "b",
|
|
|
|
neverThere: 42)]
|
|
|
|
let output = dump(input, tsNone, asTidy, blockOnly)
|
|
|
|
assertStringEqual yamlDirs & """
|
|
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
gStorable: gs
|
|
|
|
-
|
|
|
|
kind: deB
|
|
|
|
-
|
|
|
|
cStorable: cs
|
|
|
|
-
|
|
|
|
-
|
|
|
|
gStorable: a
|
|
|
|
-
|
|
|
|
kind: deD""", output
|
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump cyclic data structure":
|
2016-04-02 15:48:22 +00:00
|
|
|
var
|
|
|
|
a = newNode("a")
|
|
|
|
b = newNode("b")
|
|
|
|
c = newNode("c")
|
|
|
|
a.next = b
|
|
|
|
b.next = c
|
|
|
|
c.next = a
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(a, tsRootOnly, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & """!example.net:Node &a
|
2016-01-28 21:57:14 +00:00
|
|
|
value: a
|
|
|
|
next:
|
|
|
|
value: b
|
|
|
|
next:
|
|
|
|
value: c
|
2016-09-19 17:33:29 +00:00
|
|
|
next: *a""", output
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load cyclic data structure":
|
2016-10-10 18:16:54 +00:00
|
|
|
let input = newStringStream(yamlDirs & """!n!system:seq(example.net:Node)
|
|
|
|
- &a
|
2016-01-28 21:57:14 +00:00
|
|
|
value: a
|
2016-10-10 18:16:54 +00:00
|
|
|
next: &b
|
2016-01-28 21:57:14 +00:00
|
|
|
value: b
|
2016-10-10 18:16:54 +00:00
|
|
|
next: &c
|
2016-01-28 21:57:14 +00:00
|
|
|
value: c
|
|
|
|
next: *a
|
|
|
|
- *b
|
|
|
|
- *c
|
|
|
|
""")
|
2016-04-02 15:48:22 +00:00
|
|
|
var result: seq[ref Node]
|
|
|
|
try: load(input, result)
|
|
|
|
except YamlConstructionError:
|
|
|
|
let ex = (ref YamlConstructionError)(getCurrentException())
|
|
|
|
echo "line ", ex.line, ", column ", ex.column, ": ", ex.msg
|
|
|
|
echo ex.lineContent
|
|
|
|
raise ex
|
2016-02-12 18:53:25 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
assert(result.len == 3)
|
|
|
|
assert(result[0].value == "a")
|
|
|
|
assert(result[1].value == "b")
|
|
|
|
assert(result[2].value == "c")
|
|
|
|
assert(result[0].next == result[1])
|
|
|
|
assert(result[1].next == result[2])
|
|
|
|
assert(result[2].next == result[0])
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-10-26 16:32:54 +00:00
|
|
|
test "Load object with default values":
|
|
|
|
let input = "a: abc\nc: dce"
|
|
|
|
var result: WithDefault
|
|
|
|
load(input, result)
|
|
|
|
assert result.a == "abc"
|
|
|
|
assert result.b == "b"
|
|
|
|
assert result.c == "dce"
|
|
|
|
assert result.d == "d"
|
|
|
|
|
|
|
|
test "Load object with partly default values":
|
|
|
|
let input = "a: abc\nb: bcd\nc: cde"
|
|
|
|
var result: WithDefault
|
|
|
|
load(input, result)
|
|
|
|
assert result.a == "abc"
|
|
|
|
assert result.b == "bcd"
|
|
|
|
assert result.c == "cde"
|
|
|
|
assert result.d == "d"
|
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Load nil values":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = newStringStream("- ~\n- !!str ~")
|
|
|
|
var result: seq[ref string]
|
|
|
|
try: load(input, result)
|
|
|
|
except YamlConstructionError:
|
|
|
|
let ex = (ref YamlConstructionError)(getCurrentException())
|
|
|
|
echo "line ", ex.line, ", column ", ex.column, ": ", ex.msg
|
|
|
|
echo ex.lineContent
|
|
|
|
raise ex
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-04-02 15:48:22 +00:00
|
|
|
assert(result.len == 2)
|
|
|
|
assert(result[0] == nil)
|
|
|
|
assert(result[1][] == "~")
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Dump nil values":
|
2016-04-02 15:48:22 +00:00
|
|
|
var input = newSeq[ref string]()
|
|
|
|
input.add(nil)
|
|
|
|
input.add(new string)
|
|
|
|
input[1][] = "~"
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsRootOnly, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual(yamlDirs &
|
|
|
|
"!n!system:seq(tag:yaml.org;2002:str) \n- !!null ~\n- !!str ~",
|
2016-09-19 17:33:29 +00:00
|
|
|
output)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Custom constructObject":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = newStringStream("- 1\n- !test:BetterInt 2")
|
|
|
|
var result: seq[BetterInt]
|
|
|
|
load(input, result)
|
|
|
|
assert(result.len == 2)
|
2016-04-04 18:44:16 +00:00
|
|
|
assert(result[0] == 2.BetterInt)
|
|
|
|
assert(result[1] == 3.BetterInt)
|
2016-09-20 19:53:38 +00:00
|
|
|
|
2016-09-14 16:31:09 +00:00
|
|
|
test "Custom representObject":
|
2016-04-02 15:48:22 +00:00
|
|
|
let input = @[1.BetterInt, 9998887.BetterInt, 98312.BetterInt]
|
2016-09-19 17:33:29 +00:00
|
|
|
var output = dump(input, tsAll, asTidy, blockOnly)
|
2016-10-10 18:16:54 +00:00
|
|
|
assertStringEqual yamlDirs & """!n!system:seq(test:BetterInt)
|
2016-02-16 18:24:55 +00:00
|
|
|
- !test:BetterInt 1
|
|
|
|
- !test:BetterInt 9_998_887
|
2016-09-19 17:33:29 +00:00
|
|
|
- !test:BetterInt 98_312""", output
|