2015-12-29 14:09:37 +00:00
|
|
|
import "../yaml/serialization"
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
make_serializable:
|
|
|
|
type
|
|
|
|
Person = object
|
|
|
|
firstname, surname: string
|
|
|
|
age: int
|
|
|
|
|
|
|
|
suite "Serialization":
|
|
|
|
setup:
|
|
|
|
var parser = newParser(coreTagLibrary())
|
|
|
|
|
|
|
|
test "Load string sequence":
|
|
|
|
let input = newStringStream(" - a\n - b")
|
|
|
|
var
|
|
|
|
result: seq[string]
|
|
|
|
events = parser.parse(input)
|
|
|
|
assert events().kind == yamlStartDocument
|
|
|
|
construct(events, result)
|
|
|
|
assert events().kind == yamlEndDocument
|
|
|
|
assert result.len == 2
|
|
|
|
assert result[0] == "a"
|
|
|
|
assert result[1] == "b"
|
|
|
|
|
2015-12-29 15:10:47 +00:00
|
|
|
test "Serialize string sequence":
|
|
|
|
var input = @["a", "b"]
|
|
|
|
var output = newStringStream()
|
2015-12-29 17:22:55 +00:00
|
|
|
dump(input, output, yDumpBlockOnly)
|
2015-12-29 15:10:47 +00:00
|
|
|
assert output.data == "%YAML 1.2\n--- \n- a\n- b"
|
|
|
|
|
2015-12-29 14:09:37 +00:00
|
|
|
test "Load Table[int, string]":
|
|
|
|
let input = newStringStream("23: dreiundzwanzig\n42: zweiundvierzig")
|
|
|
|
var
|
|
|
|
result: Table[int, string]
|
|
|
|
events = parser.parse(input)
|
|
|
|
assert events().kind == yamlStartDocument
|
|
|
|
construct(events, result)
|
|
|
|
assert events().kind == yamlEndDocument
|
|
|
|
assert result.len == 2
|
|
|
|
assert result[23] == "dreiundzwanzig"
|
|
|
|
assert result[42] == "zweiundvierzig"
|
|
|
|
|
2015-12-29 15:10:47 +00:00
|
|
|
test "Serialize Table[int, string]":
|
|
|
|
var input = initTable[int, string]()
|
|
|
|
input[23] = "dreiundzwanzig"
|
|
|
|
input[42] = "zweiundvierzig"
|
|
|
|
var output = newStringStream()
|
2015-12-29 17:22:55 +00:00
|
|
|
dump(input, output, yDumpBlockOnly)
|
2015-12-29 15:10:47 +00:00
|
|
|
assert output.data == "%YAML 1.2\n--- \n23: dreiundzwanzig\n42: zweiundvierzig"
|
|
|
|
|
2015-12-29 14:09:37 +00:00
|
|
|
test "Load Sequences in Sequence":
|
|
|
|
let input = newStringStream(" - [1, 2, 3]\n - [4, 5]\n - [6]")
|
|
|
|
var
|
|
|
|
result: seq[seq[int]]
|
|
|
|
events = parser.parse(input)
|
|
|
|
assert events().kind == yamlStartDocument
|
|
|
|
construct(events, result)
|
|
|
|
assert events().kind == yamlEndDocument
|
|
|
|
assert result.len == 3
|
|
|
|
assert result[0] == @[1, 2, 3]
|
|
|
|
assert result[1] == @[4, 5]
|
|
|
|
assert result[2] == @[6]
|
|
|
|
|
2015-12-29 15:10:47 +00:00
|
|
|
test "Serialize Sequences in Sequence":
|
|
|
|
let input = @[@[1, 2, 3], @[4, 5], @[6]]
|
|
|
|
var output = newStringStream()
|
2015-12-29 17:22:55 +00:00
|
|
|
dump(input, output, yDumpDefault)
|
2015-12-29 15:10:47 +00:00
|
|
|
assert output.data == "%YAML 1.2\n--- \n- [1, 2, 3]\n- [4, 5]\n- [6]"
|
|
|
|
|
2015-12-29 14:09:37 +00:00
|
|
|
test "Load custom object":
|
|
|
|
let input = newStringStream("firstname: Peter\nsurname: Pan\nage: 12")
|
|
|
|
var
|
|
|
|
result: Person
|
|
|
|
events = parser.parse(input)
|
|
|
|
assert events().kind == yamlStartDocument
|
|
|
|
construct(events, result)
|
|
|
|
assert events().kind == yamlEndDocument
|
|
|
|
assert result.firstname == "Peter"
|
|
|
|
assert result.surname == "Pan"
|
2015-12-29 15:10:47 +00:00
|
|
|
assert result.age == 12
|
|
|
|
|
|
|
|
test "Serialize custom object":
|
|
|
|
let input = Person(firstname: "Peter", surname: "Pan", age: 12)
|
|
|
|
var output = newStringStream()
|
2015-12-29 17:22:55 +00:00
|
|
|
dump(input, output, yDumpBlockOnly)
|
2015-12-29 15:10:47 +00:00
|
|
|
assert output.data == "%YAML 1.2\n--- \nfirstname: Peter\nsurname: Pan\nage: 12"
|