From ac3e5663d00a899668717c7070b6bfdf2a0005d6 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 13 Jan 2025 17:13:22 -0600 Subject: [PATCH] revamp tester --- tests/cli/lib.nim | 21 +++++++++++++++------ tests/cli/tcli.nim | 13 ------------- tests/cli/tester.nim | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 19 deletions(-) delete mode 100644 tests/cli/tcli.nim create mode 100644 tests/cli/tester.nim diff --git a/tests/cli/lib.nim b/tests/cli/lib.nim index 63184a3..dbb72ad 100644 --- a/tests/cli/lib.nim +++ b/tests/cli/lib.nim @@ -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 diff --git a/tests/cli/tcli.nim b/tests/cli/tcli.nim deleted file mode 100644 index 8f313e0..0000000 --- a/tests/cli/tcli.nim +++ /dev/null @@ -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", "") diff --git a/tests/cli/tester.nim b/tests/cli/tester.nim new file mode 100644 index 0000000..e0ab503 --- /dev/null +++ b/tests/cli/tester.nim @@ -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""")