diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-02-22 23:11:17 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-02-22 23:11:17 +0900 |
| commit | dfa3217037d8c24181be24484892ecb950a2a5ba (patch) | |
| tree | e2022ad6ce2e11bbb7b4d0b38731a6c67561235f /command/unreachable_test.go | |
| parent | 8000a2febb546b3b68eddd9330d5709281068e9f (diff) | |
command: implement builder and parser
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'command/unreachable_test.go')
| -rw-r--r-- | command/unreachable_test.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/command/unreachable_test.go b/command/unreachable_test.go new file mode 100644 index 00000000..9ace4794 --- /dev/null +++ b/command/unreachable_test.go @@ -0,0 +1,54 @@ +package command + +import ( + "flag" + "testing" +) + +func TestParseUnreachable(t *testing.T) { + // top level bypasses name matching and recursive calls to Parse + // returns when encountering zero-length args + t.Run("zero-length args", func(t *testing.T) { + defer checkRecover(t, "Parse", "attempted to parse with zero length args") + _ = newNode(panicWriter{}, nil, " ", " ").Parse(nil) + }) + + // top level must not have siblings + t.Run("toplevel siblings", func(t *testing.T) { + defer checkRecover(t, "Parse", "invalid toplevel state") + n := newNode(panicWriter{}, nil, " ", "") + n.append(newNode(panicWriter{}, nil, " ", " ")) + _ = n.Parse(nil) + }) + + // a node with descendents must not have a direct handler + t.Run("sub handle conflict", func(t *testing.T) { + defer checkRecover(t, "Parse", "invalid subcommand tree state") + n := newNode(panicWriter{}, nil, " ", "") + n.adopt(newNode(panicWriter{}, nil, " ", " ")) + n.f = func([]string) error { panic("unreachable") } + _ = n.Parse(nil) + }) + + // this would only happen if a node was matched twice + t.Run("parsed flag set", func(t *testing.T) { + defer checkRecover(t, "Parse", "invalid set state") + n := newNode(panicWriter{}, nil, " ", "") + set := flag.NewFlagSet("parsed", flag.ContinueOnError) + set.SetOutput(panicWriter{}) + _ = set.Parse(nil) + n.set = set + _ = n.Parse(nil) + }) +} + +type panicWriter struct{} + +func (p panicWriter) Write([]byte) (int, error) { panic("unreachable") } + +func checkRecover(t *testing.T, name, wantPanic string) { + if r := recover(); r != wantPanic { + t.Errorf("%s: panic = %v; wantPanic %v", + name, r, wantPanic) + } +} |
