From 37043a03bf85f49cad4ccb1a5cc1d63e11f3a22d Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Sun, 24 Dec 2023 21:02:10 +0200 Subject: [PATCH] Test against upstream samples --- tests/Tupfile | 1 + tests/test_samples.nim | 110 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 tests/test_samples.nim diff --git a/tests/Tupfile b/tests/Tupfile index 2cccf49..fc4fccb 100644 --- a/tests/Tupfile +++ b/tests/Tupfile @@ -1,2 +1,3 @@ include_rules +NIM_FLAGS_test_samples += -d:upstreamTestfile="$(TUP_CWD)/../../preserves/tests/samples.pr" : foreach t*.nim |> !nim_run |> | ../ diff --git a/tests/test_samples.nim b/tests/test_samples.nim new file mode 100644 index 0000000..b8c8230 --- /dev/null +++ b/tests/test_samples.nim @@ -0,0 +1,110 @@ +# SPDX-FileCopyrightText: ☭ Emery Hemingway +# SPDX-License-Identifier: Unlicense + +import std/unittest +import preserves + +type Value = Preserve[void] + +const upstreamTestfile {.strdefine.} = "" + +proc strip(pr: Preserve): Preserve = pr + +proc encodeBinary(pr: Value): Value = + result = encode(pr).toPreserve + checkpoint("encoded binary: " & $result) + +proc looseEncodeBinary(pr: Value): Value = + result = encode(pr).toPreserve + checkpoint("loose encoded binary: " & $result) + +proc annotatedBinary(pr: Value): Value = + result = encode(pr).toPreserve + checkpoint("annotated binary: " & $result) + +proc decodeBinary(pr: Value): Value = + result = decodePreserves(pr.bytes) + +proc encodeText(pr: Value): Value = + result = ($pr).toPreserve + 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(void) + 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