refactor: better project structure (#35)
This project is starting to grow, and I've seen flaws in the initial project structure design I think I'll restructure the entire project directory for better scalability and also cleaner
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package spec
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLookup(t *testing.T) {
|
||||
// Setup Registry
|
||||
Registry = make(map[string]*Spec)
|
||||
Register(&Spec{
|
||||
Name: "git",
|
||||
Subcommands: []Subcommand{
|
||||
{Name: "commit", Options: []Option{{Name: "--message"}}, MaxArgs: 1},
|
||||
{Name: "remote", Subcommands: []Subcommand{{Name: "add"}}},
|
||||
},
|
||||
Options: []Option{{Name: "--verbose"}},
|
||||
})
|
||||
|
||||
// Setup Aliases
|
||||
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 := 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) {
|
||||
Registry = make(map[string]*Spec)
|
||||
Register(&Spec{
|
||||
Name: "git",
|
||||
Subcommands: []Subcommand{
|
||||
{Name: "commit", Options: []Option{{Name: "--message"}}, MaxArgs: 1},
|
||||
},
|
||||
})
|
||||
|
||||
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 {
|
||||
_ = Lookup("gca")
|
||||
_ = Lookup("git ")
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
Reference in New Issue
Block a user