add comp tests
This commit is contained in:
parent
37a7eda095
commit
a2c7656301
96
comp.go
96
comp.go
@ -5,11 +5,41 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gCmdWords = []string{"set", "map", "cmd"}
|
gCmdWords = []string{
|
||||||
|
"set",
|
||||||
|
"map",
|
||||||
|
"cmd",
|
||||||
|
"up",
|
||||||
|
"half-up",
|
||||||
|
"page-up",
|
||||||
|
"down",
|
||||||
|
"half-down",
|
||||||
|
"page-down",
|
||||||
|
"updir",
|
||||||
|
"open",
|
||||||
|
"quit",
|
||||||
|
"bot",
|
||||||
|
"top",
|
||||||
|
"read",
|
||||||
|
"read-shell",
|
||||||
|
"read-shell-wait",
|
||||||
|
"read-shell-async",
|
||||||
|
"search",
|
||||||
|
"search-back",
|
||||||
|
"toggle",
|
||||||
|
"yank",
|
||||||
|
"delete",
|
||||||
|
"paste",
|
||||||
|
"renew",
|
||||||
|
"echo",
|
||||||
|
"cd",
|
||||||
|
}
|
||||||
|
|
||||||
gOptWords = []string{
|
gOptWords = []string{
|
||||||
"hidden",
|
"hidden",
|
||||||
"nohidden",
|
"nohidden",
|
||||||
@ -44,7 +74,7 @@ func matchWord(s string, words []string) (matches []string, longest string) {
|
|||||||
matches = append(matches, w)
|
matches = append(matches, w)
|
||||||
if longest != "" {
|
if longest != "" {
|
||||||
longest = matchLongest(longest, w)
|
longest = matchLongest(longest, w)
|
||||||
} else {
|
} else if s != "" {
|
||||||
longest = w + " "
|
longest = w + " "
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,6 +88,8 @@ func matchWord(s string, words []string) (matches []string, longest string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func matchExec(s string) (matches []string, longest string) {
|
func matchExec(s string) (matches []string, longest string) {
|
||||||
|
var words []string
|
||||||
|
|
||||||
paths := strings.Split(envPath, ":")
|
paths := strings.Split(envPath, ":")
|
||||||
|
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
@ -81,21 +113,24 @@ func matchExec(s string) (matches []string, longest string) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
matches = append(matches, f.Name())
|
words = append(words, f.Name())
|
||||||
if longest != "" {
|
|
||||||
longest = matchLongest(longest, f.Name())
|
|
||||||
} else {
|
|
||||||
longest = f.Name() + " "
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if longest == "" {
|
sort.Strings(words)
|
||||||
longest = s
|
|
||||||
|
if len(words) > 0 {
|
||||||
|
uniq := words[:1]
|
||||||
|
for i := 1; i < len(words); i++ {
|
||||||
|
if words[i] != words[i-1] {
|
||||||
|
uniq = append(uniq, words[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
words = uniq
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return matchWord(s, words)
|
||||||
}
|
}
|
||||||
|
|
||||||
func matchFile(s string) (matches []string, longest string) {
|
func matchFile(s string) (matches []string, longest string) {
|
||||||
@ -129,10 +164,14 @@ func matchFile(s string) (matches []string, longest string) {
|
|||||||
if isRoot(s) || filepath.Base(s) != s {
|
if isRoot(s) || filepath.Base(s) != s {
|
||||||
name = filepath.Join(filepath.Dir(s), f.Name())
|
name = filepath.Join(filepath.Dir(s), f.Name())
|
||||||
}
|
}
|
||||||
matches = append(matches, f.Name())
|
item := f.Name()
|
||||||
|
if f.Mode().IsDir() {
|
||||||
|
item += string(filepath.Separator)
|
||||||
|
}
|
||||||
|
matches = append(matches, item)
|
||||||
if longest != "" {
|
if longest != "" {
|
||||||
longest = matchLongest(longest, name)
|
longest = matchLongest(longest, name)
|
||||||
} else {
|
} else if s != "" {
|
||||||
if f.Mode().IsRegular() {
|
if f.Mode().IsRegular() {
|
||||||
longest = name + " "
|
longest = name + " "
|
||||||
} else {
|
} else {
|
||||||
@ -150,26 +189,25 @@ func matchFile(s string) (matches []string, longest string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func compCmd(acc []rune) (matches []string, longestAcc []rune) {
|
func compCmd(acc []rune) (matches []string, longestAcc []rune) {
|
||||||
if len(acc) == 0 || acc[len(acc)-1] == ' ' {
|
|
||||||
return matches, acc
|
|
||||||
}
|
|
||||||
|
|
||||||
s := string(acc)
|
s := string(acc)
|
||||||
f := strings.Fields(s)
|
f := strings.Fields(s)
|
||||||
|
|
||||||
|
if len(f) == 0 || s[len(s)-1] == ' ' {
|
||||||
|
f = append(f, "")
|
||||||
|
}
|
||||||
|
|
||||||
var longest string
|
var longest string
|
||||||
|
|
||||||
switch len(f) {
|
switch len(f) {
|
||||||
case 0:
|
|
||||||
longestAcc = acc
|
|
||||||
case 1:
|
case 1:
|
||||||
words := gCmdWords
|
words := gCmdWords
|
||||||
for c, _ := range gOpts.cmds {
|
for c, _ := range gOpts.cmds {
|
||||||
words = append(words, c)
|
words = append(words, c)
|
||||||
}
|
}
|
||||||
|
sort.Strings(words)
|
||||||
matches, longest = matchWord(s, words)
|
matches, longest = matchWord(s, words)
|
||||||
longestAcc = []rune(longest)
|
longestAcc = []rune(longest)
|
||||||
default:
|
case 2:
|
||||||
switch f[0] {
|
switch f[0] {
|
||||||
case "set":
|
case "set":
|
||||||
matches, longest = matchWord(f[1], gOptWords)
|
matches, longest = matchWord(f[1], gOptWords)
|
||||||
@ -180,24 +218,30 @@ func compCmd(acc []rune) (matches []string, longestAcc []rune) {
|
|||||||
matches, longest = matchFile(f[len(f)-1])
|
matches, longest = matchFile(f[len(f)-1])
|
||||||
longestAcc = append(acc[:len(acc)-len(f[len(f)-1])], []rune(longest)...)
|
longestAcc = append(acc[:len(acc)-len(f[len(f)-1])], []rune(longest)...)
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
switch f[0] {
|
||||||
|
case "set", "map", "cmd":
|
||||||
|
longestAcc = acc
|
||||||
|
default:
|
||||||
|
matches, longest = matchFile(f[len(f)-1])
|
||||||
|
longestAcc = append(acc[:len(acc)-len(f[len(f)-1])], []rune(longest)...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func compShell(acc []rune) (matches []string, longestAcc []rune) {
|
func compShell(acc []rune) (matches []string, longestAcc []rune) {
|
||||||
if len(acc) == 0 || acc[len(acc)-1] == ' ' {
|
|
||||||
return matches, acc
|
|
||||||
}
|
|
||||||
|
|
||||||
s := string(acc)
|
s := string(acc)
|
||||||
f := strings.Fields(s)
|
f := strings.Fields(s)
|
||||||
|
|
||||||
|
if len(f) == 0 || s[len(s)-1] == ' ' {
|
||||||
|
f = append(f, "")
|
||||||
|
}
|
||||||
|
|
||||||
var longest string
|
var longest string
|
||||||
|
|
||||||
switch len(f) {
|
switch len(f) {
|
||||||
case 0:
|
|
||||||
longestAcc = acc
|
|
||||||
case 1:
|
case 1:
|
||||||
matches, longest = matchExec(s)
|
matches, longest = matchExec(s)
|
||||||
longestAcc = []rune(longest)
|
longestAcc = []rune(longest)
|
||||||
|
53
comp_test.go
Normal file
53
comp_test.go
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMatchLongest(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
fst string
|
||||||
|
snd string
|
||||||
|
res string
|
||||||
|
}{
|
||||||
|
{"", "", ""},
|
||||||
|
{"", "foo", ""},
|
||||||
|
{"foo", "", ""},
|
||||||
|
{"foo", "bar", ""},
|
||||||
|
{"foo", "foobar", "foo"},
|
||||||
|
{"foo", "barfoo", ""},
|
||||||
|
{"foobar", "foobaz", "fooba"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
if l := matchLongest(test.fst, test.snd); l != test.res {
|
||||||
|
t.Errorf("at input '%s' and '%s' expected '%s' but got '%s'", test.fst, test.snd, test.res, l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMatchWord(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
s string
|
||||||
|
words []string
|
||||||
|
matches []string
|
||||||
|
longest string
|
||||||
|
}{
|
||||||
|
{"fo", []string{"foo", "bar", "baz"}, []string{"foo"}, "foo "},
|
||||||
|
{"ba", []string{"foo", "bar", "baz"}, []string{"bar", "baz"}, "ba"},
|
||||||
|
{"fo", []string{"bar", "baz"}, nil, "fo"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
m, l := matchWord(test.s, test.words)
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(m, test.matches) {
|
||||||
|
t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.matches, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
if l != test.longest {
|
||||||
|
t.Errorf("at input '%s' with '%s' expected '%s' but got '%s'", test.s, test.words, test.longest, l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user