preserves-nim/tests/test_samples.nim

109 lines
3.5 KiB
Nim

# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
import std/unittest
import preserves
const upstreamTestfile {.strdefine.} = ""
proc strip(pr: Value): Value = pr
proc encodeBinary(pr: Value): Value =
result = encode(pr).toPreserves
checkpoint("encoded binary: " & $result)
proc looseEncodeBinary(pr: Value): Value =
result = encode(pr).toPreserves
checkpoint("loose encoded binary: " & $result)
proc annotatedBinary(pr: Value): Value =
result = encode(pr).toPreserves
checkpoint("annotated binary: " & $result)
proc decodeBinary(pr: Value): Value =
result = decodePreserves(pr.bytes)
proc encodeText(pr: Value): Value =
result = ($pr).toPreserves
checkpoint("encoded text: " & result.string)
proc decodeText(pr: Value): Value =
result = parsePreserves(pr.string)
checkpoint("decoded text " & $pr)
if upstreamTestfile != "":
let samples = readFile(upstreamTestfile).parsePreserves()
assert samples.isRecord("TestCases")
var binary, annotatedValue, stripped, text, bytes: Value
for n in { 1..8, 20..22, 30..32 }:
suite $n:
for name, testcase in samples[0]:
assert testcase.isRecord
assert testcase.label.isSymbol
var testMatched: bool
case testcase.label.symbol.string
of "Test":
testMatched = (n in { 1..8 })
if testMatched:
binary = testcase[0]
annotatedValue = testcase[1]
stripped = strip(annotatedValue)
of "NondeterministicTest":
testMatched = (n in { 1..7 })
if testMatched:
binary = testcase[0]
annotatedValue = testcase[1]
stripped = strip(annotatedValue)
of "ParseError":
testMatched = (n in { 20 })
if testMatched: text = testcase[0]
of "ParseShort":
testMatched = (n in { 21 })
if testMatched: text = testcase[0]
of "ParseEOF":
testMatched = (n in { 22 })
if testMatched: text = testcase[0]
of "DecodeError":
testMatched = (n in { 30 })
if testMatched: bytes = testcase[0]
of "DecodeShort":
testMatched = (n in { 31 })
if testMatched: bytes = testcase[0]
of "DecodeEOF":
testMatched = (n in { 32 })
if testMatched: bytes = testcase[0]
else:
assert false
if testMatched:
test $name:
checkpoint $testcase
case n
of 1: check decodeBinary(encodeBinary(annotatedValue)) == stripped
of 2: check strip(decodeBinary(binary)) == stripped
of 3:
# check decodeBinary(binary) == annotatedValue
discard
of 4:
# check decodeBinary(annotatedBinary(annotatedValue)) == annotatedValue
discard
of 5: check decodeText(encodeText(stripped)) == stripped
of 6: check decodeText(encodeText(annotatedValue)) == annotatedValue
of 7:
# check annotatedBinary(annotatedValue) == binary
discard
of 8:
# check looseEncodeBinary(annotatedValue) == binary
discard
of 20, 21, 22:
# TODO: be specific about which error is raised
expect ValueError, IOError:
discard decodeText(text)
of 30, 31, 32:
expect ValueError, IOError:
discard decodeBinary(bytes)
else:
assert false