2021-08-28 10:48:50 +00:00
|
|
|
# SPDX-FileCopyrightText: 2021 ☭ Emery Hemingway
|
2021-08-31 15:00:00 +00:00
|
|
|
# SPDX-License-Identifier: Unlicense
|
2021-06-28 13:47:14 +00:00
|
|
|
|
2021-12-06 23:28:00 +00:00
|
|
|
import std/[options, tables, unittest, xmlparser, xmltree]
|
|
|
|
import preserves, preserves/xmlhooks
|
2021-06-28 13:47:14 +00:00
|
|
|
|
2023-09-12 09:35:09 +00:00
|
|
|
type
|
|
|
|
Route {.preservesRecord: "route".} = object
|
|
|
|
`transports`*: seq[Preserve[void]]
|
|
|
|
`pathSteps`* {.preservesTupleTail.}: seq[Preserve[void]]
|
|
|
|
|
2021-06-28 13:47:14 +00:00
|
|
|
suite "conversions":
|
|
|
|
test "dictionary":
|
2021-10-17 09:20:17 +00:00
|
|
|
type Bar = tuple
|
2021-06-28 13:47:14 +00:00
|
|
|
s: string
|
2021-10-17 09:20:17 +00:00
|
|
|
type Foobar {.preservesDictionary.} = object
|
2021-06-28 13:47:14 +00:00
|
|
|
a, b: int
|
2022-12-06 19:31:21 +00:00
|
|
|
c {.preservesEmbedded.}: Bar
|
2021-06-28 13:47:14 +00:00
|
|
|
let
|
2021-10-17 09:20:17 +00:00
|
|
|
c = Foobar(a: 1, b: 2, c: ("ku", ))
|
2021-06-28 13:47:14 +00:00
|
|
|
b = toPreserve(c)
|
|
|
|
a = preserveTo(b, Foobar)
|
2022-12-06 19:31:21 +00:00
|
|
|
check($b == """{a: 1 b: 2 c: #!["ku"]}""")
|
2021-08-25 11:49:35 +00:00
|
|
|
check(a.isSome and (get(a) == c))
|
2021-06-28 13:47:14 +00:00
|
|
|
check(b.kind == pkDictionary)
|
|
|
|
|
|
|
|
test "records":
|
2021-10-17 09:20:17 +00:00
|
|
|
type Bar {.preservesRecord: "bar".} = object
|
2021-06-28 13:47:14 +00:00
|
|
|
s: string
|
2021-10-17 09:20:17 +00:00
|
|
|
type Foobar {.preservesRecord: "foo".} = object
|
2021-06-28 13:47:14 +00:00
|
|
|
a, b: int
|
|
|
|
c: Bar
|
|
|
|
let
|
2021-10-17 09:20:17 +00:00
|
|
|
tup = Foobar(a: 1, b: 2, c: Bar(s: "ku", ))
|
2021-07-07 10:50:57 +00:00
|
|
|
prs = toPreserve(tup)
|
|
|
|
check(prs.kind == pkRecord)
|
2021-08-25 11:49:35 +00:00
|
|
|
check($prs == """<foo 1 2 <bar "ku">>""")
|
|
|
|
check(preserveTo(prs, Foobar) == some(tup))
|
2021-07-09 14:54:13 +00:00
|
|
|
|
2021-09-23 13:20:06 +00:00
|
|
|
test "tables":
|
|
|
|
var a: Table[int, string]
|
|
|
|
for i, s in ["a", "b", "c"]: a[i] = s
|
|
|
|
let b = toPreserve(a)
|
2022-10-29 03:12:12 +00:00
|
|
|
check($b == """{0: "a" 1: "b" 2: "c"}""")
|
2021-09-23 13:20:06 +00:00
|
|
|
var c: Table[int, string]
|
|
|
|
check(fromPreserve(c, b))
|
|
|
|
check(a == c)
|
|
|
|
|
2021-12-06 23:28:00 +00:00
|
|
|
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 = toPreserve(b, void)
|
|
|
|
checkpoint $pr
|
|
|
|
check fromPreserve(a, pr)
|
|
|
|
|
2023-09-12 09:35:09 +00:00
|
|
|
test "preservesTupleTail":
|
|
|
|
let pr = parsePreserves """<route [<tcp "localhost" 1024>] <ref {oid: "syndicate" sig: #x"69ca300c1dbfa08fba692102dd82311a"}>>"""
|
|
|
|
var route: Route
|
|
|
|
check route.fromPreserve(pr)
|
|
|
|
|
2021-12-06 23:28:00 +00:00
|
|
|
suite "toPreserve":
|
2021-07-09 14:54:13 +00:00
|
|
|
template check(p: Preserve; s: string) =
|
|
|
|
test s: check($p == s)
|
2021-08-28 09:17:23 +00:00
|
|
|
check false.toPreserve, "#f"
|
|
|
|
check [0, 1, 2, 3].toPreserve, "[0 1 2 3]"
|