hwylterm/tests/cli/tester.nim

218 lines
4.3 KiB
Nim
Raw Normal View History

2025-01-26 13:31:34 -06:00
import std/[os, unittest]
2025-01-13 17:13:22 -06:00
import ./lib
2025-01-26 13:31:34 -06:00
if commandLineParams().len == 0:
preCompileTestModules()
2025-01-24 17:42:29 -06:00
2025-01-28 15:58:25 -06:00
suite "positional":
okWithArgs("posBasic", "a b c d e", """notFirst=a rest=@["b", "c", "d", "e"]""")
2025-01-26 14:17:36 -06:00
okWithArgs("posFirst", "a b c d e", """first=@["a", "b", "c"], second=d, third=e""")
2025-01-23 19:25:09 -06:00
failWithArgs(
2025-01-26 14:17:36 -06:00
"posFirst", "a b", "error missing positional args, got: 2, expected at least: 3"
2025-01-23 19:25:09 -06:00
)
okWithArgs("posLast", "a b", """first=a, second=b, third=@[]""")
okWithArgs("posNoMulti", "5 b c", """first=5, second=b, third=c""")
2025-01-26 14:17:36 -06:00
failWithArgs(
"posNoMulti", "5 b c d",
"""error unexepected positional args, got: 4, expected: 3""",
)
2025-01-23 19:25:09 -06:00
2025-01-28 15:58:25 -06:00
suite "flags":
2025-01-26 14:17:36 -06:00
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",
)
2025-02-05 01:21:45 -06:00
okWithArgs("flagKey", "--key", "key set")
2025-02-05 18:40:31 -06:00
okWithArgs("multiShortFlags", "--help","""
usage:
multiple-short-flags [flags]
flags:
-a first short
-b second short
-h --help show this help
""")
okWithArgs("allFlagKinds", "--help", """
usage:
flag-kinds [flags]
flags:
-a kind: Command
-b --bbbb kind: InfixCommand
--cccc kind: Stmt
-d --dddd kind: InfixStmt
-e string kind: Call
-f --ffff kind: InfixCallStmt
--gggg string kind: CallStmt (default: default)
-h --hhhh kind: InfixCall
--help show this help
""")
2025-01-23 19:25:09 -06:00
2025-01-28 15:58:25 -06:00
suite "subcommands":
2025-01-26 14:17:36 -06:00
okWithArgs("subcommands", "a b c", """input=b outputs=@["c"]""")
failWithArgs("subcommands", "b b c", """error got unexpected positionals args: b c""")
okWithArgs(
"subcommands", "b --input in --outputs out1 --outputs out2",
"""input=in outputs=@["out1", "out2"]""",
)
2025-01-23 19:25:09 -06:00
2025-01-27 11:52:53 -06:00
okWithArgs("subcommands", "ccccc", """no flags :)""")
okWithArgs("subcommands", "c", """no flags :)""")
2025-01-28 15:58:25 -06:00
suite "help":
2025-01-26 14:17:36 -06:00
okWithArgs(
"posFirst", "--help",
"""
usage:
2025-01-13 17:13:22 -06:00
positionals first... second third [flags]
flags:
2025-01-30 13:22:07 -06:00
-h --help show this help
2025-01-26 14:17:36 -06:00
""",
)
2025-01-24 17:42:29 -06:00
2025-01-26 14:17:36 -06:00
okWithArgs(
"flagSettings", "--help",
"""
usage:
2025-01-24 17:42:29 -06:00
flag-settings [flags]
flags:
2025-01-30 13:22:07 -06:00
--input string flag with default hidden
--count Count a count var with default (default: 0)
-h --help show this help
2025-01-26 14:17:36 -06:00
""",
)
2025-01-24 17:42:29 -06:00
2025-01-26 14:17:36 -06:00
okWithArgs(
2025-01-30 17:09:31 -06:00
"hideDefaultSetting", "--help",
2025-01-26 14:17:36 -06:00
"""
usage:
2025-01-24 17:42:29 -06:00
setting-hide-default [flags]
flags:
2025-01-30 13:22:07 -06:00
--input string flag with default hidden
--count Count a count var with default
-h --help show this help
2025-01-26 14:17:36 -06:00
""",
)
2025-01-24 17:42:29 -06:00
2025-01-26 14:17:36 -06:00
okWithArgs(
"customHelp", "--help",
"""
usage:
2025-01-25 15:23:15 -06:00
custom-help [flags]
flags:
--input
input (input.txt)
--output
output (output.txt)
-h --help
2025-01-26 14:17:36 -06:00
show this help
""",
)
2025-01-29 16:11:16 -06:00
okWithArgs(
"helpSettings", "--help",
"""
usage:
help-switches subcmd [flags]
subcommands:
required
flags:
-h --help show this help
"""
)
okWithArgs(
"helpSettings", "required --help",
"""
usage:
help-switches required [flags]
flags:
--input required input
-k predefined flag
-h --help show this help
"""
)
2025-01-28 15:58:25 -06:00
suite "hooks":
2025-01-26 14:17:36 -06:00
okWithArgs(
"subHooks", "a",
"""
preSub from root!
2025-01-26 13:31:34 -06:00
inside sub 'a'
2025-01-26 14:17:36 -06:00
postSub from root!
""",
)
okWithArgs(
"subHooks", "b a",
"""
preSub from root!
2025-01-26 13:31:34 -06:00
inside sub 'b a'
postSub from root!
2025-01-26 14:17:36 -06:00
inside sub 'b'
""",
)
okWithArgs(
"subHooks", "c a",
"""
preSub from 'c'!
2025-01-26 13:31:34 -06:00
inside sub 'c a'
postSub from root!
2025-01-26 14:17:36 -06:00
inside sub c
""",
)
2025-01-28 15:58:25 -06:00
2025-01-29 15:35:05 -06:00
suite "parent-child":
okWithArgs(
"inheritFlags", "first --always", "always=true,first=false"
)
okWithArgs(
"inheritFlags", "second --always --misc2", "always=true,misc1=false,misc2=true"
)
okWithArgs(
"inheritFlags", "third --misc1", "always=false,misc1=true"
)
2025-01-30 17:09:31 -06:00
2025-01-28 15:58:25 -06:00
suite "settings":
2025-01-28 15:19:15 -06:00
okWithArgs(
"inferShort", "-i input -o output","""input=input, output=output, count=0, nancy=false, ignore=false"""
)
2025-01-30 17:09:31 -06:00
okWithArgs(
"propagateSetting", "one --help",
"""
usage:
setting-propagate one [flags]
flags:
-i --input string input flag (default: the default)
-c --count Count a count var with default (default: 0)
-h --help show this help
"""
)
okWithArgs(
"propagateSetting", "two --help",
"""
usage:
setting-propagate two [flags]
flags:
-i --input string input flag
-c --count Count a count var with default
-h --help show this help
"""
)