Files
iris-context/tests/core/lookup_test.go
T
VERSE d06526cf21 Fix: issue #4 and #5 (#6)
- Issue #4: currently there is no official release yet so we cant
download from installer
- I added an installer debugger here to test
(f091dbf13bdf109b631951e2558651f560ca8b84)
- Issue #5: my another library
[fuzzy](https://github.com/versenilvis/fuzzy/) has a logic bug so it
causes crash, fix d479c6cb4d8453e50366c9839daaabb45a98a2df

- Other:
  -  remove unneeded comments (edd9d05fdd16e682b6b416913e2fa9c3f6599956)
  - Add some tests
  - Fix reload crash (3a76e78e655fd8daafb635ec0937667325f75ee4)
- New optimized build command (56a4a815f836271b861f32795d2df3b25a3dffc0)
2026-05-23 23:22:04 +07:00

96 lines
2.2 KiB
Go

package tests
import (
"strings"
"sync"
"testing"
"github.com/versenilvis/iris/commands/core"
)
func TestLookup(t *testing.T) {
// Setup Registry
core.Registry = make(map[string]*core.Spec)
core.Register(&core.Spec{
Name: "git",
Subcommands: []core.Subcommand{
{Name: "commit", Options: []core.Option{{Name: "--message"}}, MaxArgs: 1},
{Name: "remote", Subcommands: []core.Subcommand{{Name: "add"}}},
},
Options: []core.Option{{Name: "--verbose"}},
})
// Setup Aliases
core.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 := core.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) {
found = true
break
}
}
if !found {
t.Errorf("Lookup(%q) results did not contain %q", tt.input, tt.mustContain)
}
}
})
}
}
func TestLookupConcurrent(t *testing.T) {
core.Registry = make(map[string]*core.Spec)
core.Register(&core.Spec{
Name: "git",
Subcommands: []core.Subcommand{
{Name: "commit", Options: []core.Option{{Name: "--message"}}, MaxArgs: 1},
},
})
core.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 {
_ = core.Lookup("gca")
_ = core.Lookup("git ")
}
}()
}
wg.Wait()
}