preserves-nim/tests/test_conversions.nim

105 lines
3.2 KiB
Nim

# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/[options, tables, unittest, xmlparser, xmltree]
import preserves, preserves/xmlhooks
type
Route {.preservesRecord: "route".} = object
`transports`*: seq[Value]
`pathSteps`* {.preservesTupleTail.}: seq[Value]
suite "conversions":
test "dictionary":
type Bar = tuple
s: string
type Foobar {.preservesDictionary.} = object
a: int
b: seq[int]
c {.preservesEmbedded.}: Bar
d: Option[bool]
e: Option[bool]
let
c = Foobar(a: 1, b: @[2], c: ("ku", ), e: some(true))
b = toPreserves(c)
a = preservesTo(b, Foobar)
check($b == """{a: 1 b: [2] c: #:["ku"] e: #t}""")
check(a.isSome)
if a.isSome: check(get(a) == c)
check(b.kind == pkDictionary)
test "records":
type Bar {.preservesRecord: "bar".} = object
s: string
type Foobar {.preservesRecord: "foo".} = object
a: int
b: seq[int]
c: Bar
let
tup = Foobar(a: 1, b: @[2], c: Bar(s: "ku", ))
prs = toPreserves(tup)
check(prs.kind == pkRecord)
check($prs == """<foo 1 [2] <bar "ku">>""")
check(preservesTo(prs, Foobar) == some(tup))
test "tables":
var a: Table[int, string]
for i, s in ["a", "b", "c"]: a[i] = s
let b = toPreserves(a)
check($b == """{0: "a" 1: "b" 2: "c"}""")
var c: Table[int, string]
check(fromPreserves(c, b))
check(a == c)
test "XML":
var a: XmlNode
var b = parseXML """
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" width="10cm" height="3cm" viewBox="0 0 1000 300" version="1.1">
<desc>Example text01 - 'Hello, out there' in blue</desc>
<text x="250" y="150" font-family="Verdana" font-size="55" fill="blue">
Hello, out there
</text>
<!-- Show outline of canvas using 'rect' element -->
<rect x="1" y="1" width="998" height="298" fill="none" stroke="blue" stroke-width="2"/>
</svg>
"""
var pr = toPreserves(b)
checkpoint $pr
check fromPreserves(a, pr)
test "preservesTupleTail":
let pr = parsePreserves """<route [<tcp "localhost" 1024>] <ref {oid: "syndicate" sig: #x"69ca300c1dbfa08fba692102dd82311a"}>>"""
var route: Route
check route.fromPreserves(pr)
test "ebedded":
type
Foo {.preservesRecord: "foo".} = object
n: int
bar {.preservesEmbedded.}: Bar
Bar = ref object of RootObj
x: int
Baz = ref object of RootObj
x: int
let a = initRecord("foo", 9.toPreserves, embed Bar(x: 768))
checkpoint $a
check a.preservesTo(Foo).isSome
let b = initRecord("foo", 2.toPreserves, embed Baz(x: 999))
checkpoint $b
check not b.preservesTo(Foo).isSome
suite "toPreserve":
template check(p: Value; s: string) =
test s: check($p == s)
check false.toPreserves, "#f"
check [0, 1, 2, 3].toPreserves, "[0 1 2 3]"
test "toRecord":
let r = toRecord(Symbol"foo", "üks", "kaks", "kolm", {4..7})
check $r == """<foo "üks" "kaks" "kolm" #{4 5 6 7}>"""