feat: all tests
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package core_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
)
|
||||
|
||||
func TestFileGenerator(t *testing.T) {
|
||||
// Create a temp directory structure
|
||||
tmp := t.TempDir()
|
||||
|
||||
os.Mkdir(filepath.Join(tmp, ".git"), 0755)
|
||||
os.Mkdir(filepath.Join(tmp, "src"), 0755)
|
||||
os.Mkdir(filepath.Join(tmp, "docs"), 0755)
|
||||
os.WriteFile(filepath.Join(tmp, "src", "main.go"), []byte("package main"), 0644)
|
||||
os.WriteFile(filepath.Join(tmp, "src", "utils.go"), []byte("package core"), 0644)
|
||||
os.WriteFile(filepath.Join(tmp, "README.md"), []byte("# Readme"), 0644)
|
||||
|
||||
// Helper to change CWD for test
|
||||
oldWd, _ := os.Getwd()
|
||||
os.Chdir(tmp)
|
||||
defer os.Chdir(oldWd)
|
||||
|
||||
t.Run("dirOnly shows only dirs", func(t *testing.T) {
|
||||
gen := core.FileGenerator("/")
|
||||
results := gen([]string{"cd", ""}, "cd ", "")
|
||||
for _, r := range results {
|
||||
if r.Desc != "directory" {
|
||||
t.Errorf("FileGenerator(\"/\") suggested a non-directory: %s", r.Cmd)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Filter extension", func(t *testing.T) {
|
||||
gen := core.FileGenerator(".go")
|
||||
results := gen([]string{"go", "src/"}, "go src/", "src/")
|
||||
foundMain := false
|
||||
for _, r := range results {
|
||||
if r.Cmd == "src/main.go" {
|
||||
foundMain = true
|
||||
}
|
||||
}
|
||||
if !foundMain {
|
||||
t.Errorf("FileGenerator(\".go\") did not suggest src/main.go")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Hidden files are skipped", func(t *testing.T) {
|
||||
gen := core.FileGenerator()
|
||||
results := gen([]string{"ls", ""}, "ls ", "")
|
||||
for _, r := range results {
|
||||
if strings.HasPrefix(filepath.Base(r.Cmd), ".") {
|
||||
t.Errorf("FileGenerator suggested hidden file: %s", r.Cmd)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package core_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
"github.com/versenilvis/iris/integration/shell"
|
||||
)
|
||||
|
||||
type mockAdapter struct {
|
||||
shell.BashAdapter
|
||||
}
|
||||
|
||||
func (m *mockAdapter) ScanAliases() map[string]string {
|
||||
return map[string]string{
|
||||
"gca": "git commit -a",
|
||||
"ta": "tmux a -t",
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookup(t *testing.T) {
|
||||
// Use mock adapter
|
||||
shell.Current = &mockAdapter{}
|
||||
|
||||
core.Register(&core.Spec{
|
||||
Name: "git",
|
||||
Description: "git command",
|
||||
Subcommands: []core.Subcommand{
|
||||
{Name: "commit", Description: "commit changes"},
|
||||
{Name: "remote", Description: "manage remotes", Subcommands: []core.Subcommand{
|
||||
{Name: "add", Description: "add remote"},
|
||||
}},
|
||||
},
|
||||
Options: []core.Option{
|
||||
{Name: "--verbose", Description: "verbose output"},
|
||||
},
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
minCount int
|
||||
checkCmd string
|
||||
}{
|
||||
{"Top-level suggestions", "gi", 1, "git"},
|
||||
{"Subcommand suggestions", "git ", 2, "git commit"},
|
||||
{"Alias expansion", "gca", 1, "git commit -a"},
|
||||
{"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"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
results := core.Lookup(tt.input)
|
||||
if len(results) < tt.minCount {
|
||||
t.Errorf("Lookup(%q) returned %d results; want at least %d", tt.input, len(results), tt.minCount)
|
||||
}
|
||||
if tt.checkCmd != "" {
|
||||
found := false
|
||||
for _, r := range results {
|
||||
if r.Cmd == tt.checkCmd {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("Lookup(%q) did not suggest %q", tt.input, tt.checkCmd)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package core_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/versenilvis/iris/commands/core"
|
||||
)
|
||||
|
||||
func TestTokenize(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected []string
|
||||
}{
|
||||
{"", []string{""}},
|
||||
{"git ", []string{"git", ""}},
|
||||
{"git add", []string{"git", "add"}},
|
||||
{"git commit -m \"hello world\"", []string{"git", "commit", "-m", "hello world"}},
|
||||
{"git commit -m \"hello", []string{"git", "commit", "-m", "hello"}},
|
||||
{"git commit -m 'hello world'", []string{"git", "commit", "-m", "hello world"}},
|
||||
{"git commit -m 'hello", []string{"git", "commit", "-m", "hello"}},
|
||||
{"ls -l \"file name\"", []string{"ls", "-l", "file name"}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
got := core.Tokenize(tt.input)
|
||||
if !reflect.DeepEqual(got, tt.expected) {
|
||||
t.Errorf("Tokenize(%q) = %v; want %v", tt.input, got, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasPrefix(t *testing.T) {
|
||||
tests := []struct {
|
||||
s string
|
||||
prefix string
|
||||
want bool
|
||||
}{
|
||||
{"Hello", "hel", true},
|
||||
{"Thử nghiệm", "thử", true},
|
||||
{"Iris", "Iris-Longer", false},
|
||||
{"Iris", "", true},
|
||||
{"", "a", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.s+"_"+tt.prefix, func(t *testing.T) {
|
||||
if got := core.HasPrefix(tt.s, tt.prefix); got != tt.want {
|
||||
t.Errorf("HasPrefix(%q, %q) = %v; want %v", tt.s, tt.prefix, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user