mirror of
https://git.ipng.ch/ipng/golang-cli.git
synced 2026-06-15 15:25:51 +00:00
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
// SPDX-FileCopyrightText: (C) Copyright 2026 Pim van Pelt <[email protected]>
|
|||
|
|
// SPDX-License-Identifier: Apache-2.0
|
||
|
|
|
||
|
|
package cli
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
// TestValidateClean checks that a well-formed tree (the builder fixture, which
|
||
|
|
// includes a circular slot) validates without error.
|
||
|
|
func TestValidateClean(t *testing.T) {
|
||
|
|
if err := Validate(buildFixtureB()); err != nil {
|
||
|
|
t.Errorf("Validate(clean tree) = %v, want nil", err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestValidateFaults checks each fault class is reported.
|
||
|
|
func TestValidateFaults(t *testing.T) {
|
||
|
|
dyn := func(context.Context, fakeClient, []string) []string { return nil }
|
||
|
|
|
||
|
|
root := &Node[fakeClient]{Children: []*Node[fakeClient]{
|
||
|
|
// two slot children under one node
|
||
|
|
{Word: "twoslots", Help: "", Children: []*Node[fakeClient]{
|
||
|
|
{Word: "<a>", Dynamic: dyn, Run: runNoop},
|
||
|
|
{Word: "<b>", Dynamic: dyn, Run: runNoop},
|
||
|
|
}},
|
||
|
|
// duplicate fixed words
|
||
|
|
{Word: "dup", Children: []*Node[fakeClient]{
|
||
|
|
{Word: "x", Run: runNoop},
|
||
|
|
{Word: "x", Run: runNoop},
|
||
|
|
}},
|
||
|
|
// dead end: neither runs nor has children
|
||
|
|
{Word: "deadend"},
|
||
|
|
// empty word
|
||
|
|
{Word: "", Run: runNoop},
|
||
|
|
}}
|
||
|
|
|
||
|
|
err := Validate(root)
|
||
|
|
if err == nil {
|
||
|
|
t.Fatal("Validate(broken tree) = nil, want errors")
|
||
|
|
}
|
||
|
|
msg := err.Error()
|
||
|
|
for _, want := range []string{
|
||
|
|
"only the first is reachable",
|
||
|
|
`duplicate child word "x"`,
|
||
|
|
"dead end",
|
||
|
|
"empty Word",
|
||
|
|
} {
|
||
|
|
if !strings.Contains(msg, want) {
|
||
|
|
t.Errorf("Validate missing %q in:\n%s", want, msg)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|