revamp tester

This commit is contained in:
Daylin Morgan 2025-01-13 17:13:22 -06:00
parent 5020199fd1
commit ac3e5663d0
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 47 additions and 19 deletions

View file

@ -8,11 +8,10 @@ let hwylCliWriteTime = getFileInfo(hwylCliSrc).lastWriteTime
if not dirExists(binDir):
createDir(binDir)
proc runTestCli(module: string, args: string, code: int = 0): string =
proc runTestCli(module: string, args: string, code: int = 0): (string, int) =
let cmd = binDir / module & " " & args
let (output, exitCode) = execCmdEx(cmd)
check code == exitCode
result = output.strip()
let (output, code) = execCmdEx(cmd)
result = (output.strip(), code)
proc preCompileWorkingModule(module: string) =
let exe = binDir / module
@ -24,6 +23,16 @@ proc preCompileWorkingModule(module: string) =
echo "cmd: ", cmd
quit "failed to precompile test module"
proc checkRunWithArgs*(module: string, args = "", output = "", code = 0) =
template okWithArgs*(module: string, args = "", output = "") =
preCompileWorkingModule(module)
check output == runTestCli(module, args, code)
test module:
let (actualOutput, code) = runTestCli(module, args)
check code == 0
check output == actualOutput
template failWithArgs*(module: string, args = "", output = "") =
preCompileWorkingModule(module)
test module:
let (actualOutput, code) = runTestCli(module, args)
check code == 1
check output == actualOutput

View file

@ -1,13 +0,0 @@
import std/[unittest]
import ./lib
suite "hwylcli":
test "positionals":
checkRunWithArgs("posFirst", "a b c d e","""first=@["a", "b", "c"], second=d, third=e""")
checkRunWithArgs("posFirst", "a b", "error missing positional args, got: 2, expected at least: 3", code = 1)
checkRunWithArgs("posLast", "a b", """first=a, second=b, third=@[]""")
checkRunWithArgs("posLastExact", "a b c d e", """first=a, second=b, third=@["c", "d", "e"]""")
checkRunWithArgs("posNoMulti", "5 b c", """first=5, second=b, third=c""")
checkRunWithArgs("posNoMulti", "5 b c d", """error missing positional args, got: 4, expected: 3""", code = 1)
checkRunWithArgs("enumFlag","--color red", "")
checkRunWithArgs("enumFlag","--color black", "")

32
tests/cli/tester.nim Normal file
View file

@ -0,0 +1,32 @@
import std/[unittest]
import ./lib
suite "hwylcli":
test "positionals":
okWithArgs(
"posFirst",
"a b c d e",
"""
first=@["a", "b", "c"], second=d, third=e
args=@[]"""
)
failWithArgs(
"posFirst",
"a b",
"error missing positional args, got: 2, expected at least: 3",
)
okWithArgs("posLast", "a b", """first=a, second=b, third=@[]""")
okWithArgs("posLastExact", "a b c d e", """first=a, second=b, third=@["c", "d", "e"]""")
okWithArgs("posNoMulti", "5 b c", """first=5, second=b, third=c""")
failWithArgs("posNoMulti", "5 b c d", """error missing positional args, got: 4, expected: 3""")
test "special flag types":
okWithArgs("enumFlag","--color red", "color=red")
failWithArgs("enumFlag","--color black", "error failed to parse value for color as enum: black expected one of: red,blue,green")
test "help":
okWithArgs("posFirst", "--help",
"""usage:
positionals first... second third [flags]
flags:
-h --help show this help""")