aboutsummaryrefslogtreecommitdiffhomepage
path: root/command/builder_test.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-02-22 23:11:17 +0900
committerOphestra <cat@gensokyo.uk>2025-02-22 23:11:17 +0900
commitdfa3217037d8c24181be24484892ecb950a2a5ba (patch)
treee2022ad6ce2e11bbb7b4d0b38731a6c67561235f /command/builder_test.go
parent8000a2febb546b3b68eddd9330d5709281068e9f (diff)
command: implement builder and parser
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'command/builder_test.go')
-rw-r--r--command/builder_test.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/command/builder_test.go b/command/builder_test.go
new file mode 100644
index 00000000..1cd4ce38
--- /dev/null
+++ b/command/builder_test.go
@@ -0,0 +1,56 @@
+package command_test
+
+import (
+ "testing"
+
+ "git.gensokyo.uk/security/fortify/command"
+)
+
+func TestBuild(t *testing.T) {
+ c := command.New(nil, nil, "test")
+ stubHandler := func([]string) error { panic("unreachable") }
+
+ t.Run("nil direct handler", func(t *testing.T) {
+ defer checkRecover(t, "Command", "invalid handler")
+ c.Command("name", "usage", nil)
+ })
+
+ t.Run("direct zero length", func(t *testing.T) {
+ wantPanic := "invalid subcommand"
+ t.Run("zero length name", func(t *testing.T) { defer checkRecover(t, "Command", wantPanic); c.Command("", "usage", stubHandler) })
+ t.Run("zero length usage", func(t *testing.T) { defer checkRecover(t, "Command", wantPanic); c.Command("name", "", stubHandler) })
+ })
+
+ t.Run("direct adopt unique names", func(t *testing.T) {
+ c.Command("d0", "usage", stubHandler)
+ c.Command("d1", "usage", stubHandler)
+ })
+
+ t.Run("direct adopt non-unique name", func(t *testing.T) {
+ defer checkRecover(t, "Command", "attempted to initialise subcommand with non-unique name")
+ c.Command("d0", "usage", stubHandler)
+ })
+
+ t.Run("zero length", func(t *testing.T) {
+ wantPanic := "invalid subcommand tree"
+ t.Run("zero length name", func(t *testing.T) { defer checkRecover(t, "New", wantPanic); c.New("", "usage") })
+ t.Run("zero length usage", func(t *testing.T) { defer checkRecover(t, "New", wantPanic); c.New("name", "") })
+ })
+
+ t.Run("direct adopt unique names", func(t *testing.T) {
+ c.New("t0", "usage")
+ c.New("t1", "usage")
+ })
+
+ t.Run("direct adopt non-unique name", func(t *testing.T) {
+ defer checkRecover(t, "Command", "attempted to initialise subcommand tree with non-unique name")
+ c.New("t0", "usage")
+ })
+}
+
+func checkRecover(t *testing.T, name, wantPanic string) {
+ if r := recover(); r != wantPanic {
+ t.Errorf("%s: panic = %v; wantPanic %v",
+ name, r, wantPanic)
+ }
+}