preserves-nim/tests/test_conversions.nim

48 lines
1.2 KiB
Nim
Raw Normal View History

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
import std/[options, tables, unittest]
import bigints, preserves
suite "conversions":
test "dictionary":
2021-07-01 10:48:07 +00:00
type Bar = object
s: string
2021-07-01 10:48:07 +00:00
type Foobar = tuple
a, b: int
c: Bar
let
2021-09-01 09:37:49 +00:00
c: Foobar = (a: 1, b: 2, c: Bar(s: "ku", ))
b = toPreserve(c)
a = preserveTo(b, Foobar)
check(a.isSome and (get(a) == c))
check(b.kind == pkDictionary)
test "records":
2021-07-01 10:48:07 +00:00
type Bar {.record: "bar".} = object
s: string
2021-07-01 10:48:07 +00:00
type Foobar {.record: "foo".} = tuple
a, b: int
c: Bar
let
2021-09-01 09:37:49 +00:00
tup: Foobar = (a: 1, b: 2, c: Bar(s: "ku", ))
prs = toPreserve(tup)
check(prs.kind == pkRecord)
check($prs == """<foo 1 2 <bar "ku">>""")
check(preserveTo(prs, Foobar) == some(tup))
test "tables":
var a: Table[int, string]
for i, s in ["a", "b", "c"]: a[i] = s
let b = toPreserve(a)
check($b == """{0: "a" 1: "b" 2: "c"}""")
var c: Table[int, string]
check(fromPreserve(c, b))
check(a == c)
suite "%":
template check(p: Preserve; s: string) =
test s: check($p == s)
check false.toPreserve, "#f"
check [0, 1, 2, 3].toPreserve, "[0 1 2 3]"