From 620ab5589920f3dd06b7d8eed07986461485f42b Mon Sep 17 00:00:00 2001 From: Felix Krause Date: Thu, 28 Jan 2016 22:57:14 +0100 Subject: [PATCH] Added tests for ref serialization --- README.md | 3 +-- test/serializing.nim | 57 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ea0c094..47fd510 100644 --- a/README.md +++ b/README.md @@ -212,8 +212,7 @@ Output: - Add type hints for more scalar types * Serialization: - Support for more standard library types - - Support for ref and ptr types - - Support for anchors and aliases (requires ref and ptr types) + - Support for enum types - Support polymorphism (requires ref and ptr types) - Support variant objects - Support transient fields (i.e. fields that will not be (de-)serialized on diff --git a/test/serializing.nim b/test/serializing.nim index 1900361..26c43f4 100644 --- a/test/serializing.nim +++ b/test/serializing.nim @@ -6,6 +6,15 @@ serializable: Person = object firstname, surname: string age: int32 + + Node = object + value: string + next: ref Node + +proc newNode(v: string): ref Node = + new(result) + result.value = v + result.next = nil suite "Serialization": setup: @@ -116,4 +125,50 @@ suite "Serialization": let input = Person(firstname: "Peter", surname: "Pan", age: 12) var output = newStringStream() dump(input, output, psBlockOnly, tsRootOnly) - assert output.data == "%YAML 1.2\n--- !nim:Person \nfirstname: Peter\nsurname: Pan\nage: 12" \ No newline at end of file + assert output.data == "%YAML 1.2\n--- !nim:Person \nfirstname: Peter\nsurname: Pan\nage: 12" + + test "Serialization: Serialize cyclic data structure": + var + a = newNode("a") + b = newNode("b") + c = newNode("c") + a.next = b + b.next = c + c.next = a + var output = newStringStream() + dump(a, output, psBlockOnly, tsRootOnly) + assert output.data == """%YAML 1.2 +--- !nim:Node &a +value: a +next: + value: b + next: + value: c + next: *a""" + + test "Serialization: Load cyclic data structure": + let input = newStringStream("""%YAML 1.2 +--- !nim:seq(nim:Node) +- &a + value: a + next: &b + value: b + next: &c + value: c + next: *a +- *b +- *c +""") + var + result: seq[ref Node] + parser = newYamlParser(tagLib) + events = parser.parse(input) + construct(events, result) + 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]) + \ No newline at end of file