Files
iris-context/tests/spec/lookup_test.go
T
VERSE ebde9ba68f refactor(commands): separate commands and its core into two folders (#32)
- I think it would be better if one for commands only, one for the spec
handling core logic so people won't be confused by the core logic inside
the commands folder (cleaner I guess)
- Regen docs
2026-07-05 11:27:30 +07:00

96 lines
2.2 KiB
Go

package tests
import (
"strings"
"sync"
"testing"
"github.com/versenilvis/iris/spec"
)
func TestLookup(t *testing.T) {
// Setup Registry
spec.Registry = make(map[string]*spec.Spec)
spec.Register(&spec.Spec{
Name: "git",
Subcommands: []spec.Subcommand{
{Name: "commit", Options: []spec.Option{{Name: "--message"}}, MaxArgs: 1},
{Name: "remote", Subcommands: []spec.Subcommand{{Name: "add"}}},
},
Options: []spec.Option{{Name: "--verbose"}},
})
// Setup Aliases
spec.ShellAliases = map[string]string{
"gca": "git commit -a",
"ta": "tmux a -t",
}
tests := []struct {
name string
input string
minResults int
mustContain string
}{
{"Top-level", "gi", 1, "git"},
{"Subcommand", "git ", 1, "git commit"},
{"Alias expansion", "gca", 1, "git commit -a"},
{"Alias with space", "ta", 1, "tmux a -t"},
{"Deep subcommand", "git remote ", 1, "git remote add"},
{"Option dedup", "git --verbose -", 0, ""},
{"Flag with value ignore", "git --output=json ", 2, "git --output=json commit"},
{"Unknown root command", "unknowncmd ", 0, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
results := spec.Lookup(tt.input)
if len(results) < tt.minResults {
t.Errorf("Lookup(%q) got %d results; want at least %d", tt.input, len(results), tt.minResults)
}
if tt.mustContain != "" {
found := false
for _, r := range results {
if strings.Contains(r.Cmd, tt.mustContain) || strings.Contains(r.Desc, tt.mustContain) {
found = true
break
}
}
if !found {
t.Errorf("Lookup(%q) results did not contain %q", tt.input, tt.mustContain)
}
}
})
}
}
func TestLookupConcurrent(t *testing.T) {
spec.Registry = make(map[string]*spec.Spec)
spec.Register(&spec.Spec{
Name: "git",
Subcommands: []spec.Subcommand{
{Name: "commit", Options: []spec.Option{{Name: "--message"}}, MaxArgs: 1},
},
})
spec.ShellAliases = map[string]string{
"gca": "git commit -a",
}
var wg sync.WaitGroup
const goroutines = 10
const iterations = 50
for range goroutines {
wg.Add(1)
go func() {
defer wg.Done()
for range iterations {
_ = spec.Lookup("gca")
_ = spec.Lookup("git ")
}
}()
}
wg.Wait()
}