2016-04-01 20:23:53 +03:00
|
|
|
package readline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/chzyer/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func rs(s [][]rune) []string {
|
|
|
|
ret := make([]string, len(s))
|
|
|
|
for idx, ss := range s {
|
|
|
|
ret[idx] = string(ss)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func sr(s ...string) [][]rune {
|
|
|
|
ret := make([][]rune, len(s))
|
|
|
|
for idx, ss := range s {
|
|
|
|
ret[idx] = []rune(ss)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRetSegment(t *testing.T) {
|
|
|
|
defer test.New(t)
|
|
|
|
// a
|
|
|
|
// |- a1
|
|
|
|
// |--- a11
|
|
|
|
// |--- a12
|
|
|
|
// |- a2
|
|
|
|
// |--- a21
|
|
|
|
// b
|
2016-04-01 21:00:16 +03:00
|
|
|
// add
|
|
|
|
// adddomain
|
2016-04-01 20:23:53 +03:00
|
|
|
ret := []struct {
|
|
|
|
Segments [][]rune
|
|
|
|
Cands [][]rune
|
|
|
|
idx int
|
|
|
|
Ret [][]rune
|
|
|
|
pos int
|
|
|
|
}{
|
2016-04-01 21:00:16 +03:00
|
|
|
{sr(""), sr("a", "b", "add", "adddomain"), 0, sr("a", "b", "add", "adddomain"), 0},
|
|
|
|
{sr("a"), sr("a", "add", "adddomain"), 1, sr("", "dd", "dddomain"), 1},
|
2016-04-01 20:23:53 +03:00
|
|
|
{sr("a", ""), sr("a1", "a2"), 0, sr("a1", "a2"), 0},
|
|
|
|
{sr("a", "a"), sr("a1", "a2"), 1, sr("1", "2"), 1},
|
|
|
|
{sr("a", "a1"), sr("a1"), 2, sr(""), 2},
|
2016-04-01 21:00:16 +03:00
|
|
|
{sr("add"), sr("add", "adddomain"), 2, sr("", "domain"), 2},
|
2016-04-01 20:23:53 +03:00
|
|
|
}
|
|
|
|
for idx, r := range ret {
|
|
|
|
ret, pos := RetSegment(r.Segments, r.Cands, r.idx)
|
|
|
|
test.Equal(ret, r.Ret, fmt.Errorf("%v", idx))
|
|
|
|
test.Equal(pos, r.pos, fmt.Errorf("%v", idx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitSegment(t *testing.T) {
|
|
|
|
defer test.New(t)
|
|
|
|
// a
|
|
|
|
// |- a1
|
|
|
|
// |--- a11
|
|
|
|
// |--- a12
|
|
|
|
// |- a2
|
|
|
|
// |--- a21
|
|
|
|
// b
|
|
|
|
ret := []struct {
|
|
|
|
Line string
|
|
|
|
Pos int
|
|
|
|
Segments [][]rune
|
|
|
|
Idx int
|
|
|
|
}{
|
|
|
|
{"", 0, sr(""), 0},
|
|
|
|
{"a", 1, sr("a"), 1},
|
|
|
|
{"a ", 2, sr("a", ""), 0},
|
|
|
|
{"a a", 3, sr("a", "a"), 1},
|
|
|
|
{"a a1", 4, sr("a", "a1"), 2},
|
|
|
|
{"a a1 ", 5, sr("a", "a1", ""), 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, r := range ret {
|
|
|
|
ret, idx := SplitSegment([]rune(r.Line), r.Pos)
|
|
|
|
test.Equal(rs(ret), rs(r.Segments), fmt.Errorf("%v", i))
|
|
|
|
test.Equal(idx, r.Idx, fmt.Errorf("%v", i))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tree struct {
|
|
|
|
Name string
|
|
|
|
Children []Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSegmentCompleter(t *testing.T) {
|
|
|
|
defer test.New(t)
|
|
|
|
|
|
|
|
tree := Tree{"", []Tree{
|
|
|
|
{"a", []Tree{
|
|
|
|
{"a1", []Tree{
|
|
|
|
{"a11", nil},
|
|
|
|
{"a12", nil},
|
|
|
|
}},
|
|
|
|
{"a2", []Tree{
|
|
|
|
{"a21", nil},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
{"b", nil},
|
2016-04-01 21:00:16 +03:00
|
|
|
{"route", []Tree{
|
|
|
|
{"add", nil},
|
|
|
|
{"adddomain", nil},
|
|
|
|
}},
|
2016-04-01 20:23:53 +03:00
|
|
|
}}
|
|
|
|
s := SegmentFunc(func(ret [][]rune, n int) [][]rune {
|
|
|
|
tree := tree
|
|
|
|
main:
|
2016-04-01 21:00:16 +03:00
|
|
|
for level := 0; level < len(ret)-1; {
|
2016-04-01 20:23:53 +03:00
|
|
|
name := string(ret[level])
|
|
|
|
for _, t := range tree.Children {
|
|
|
|
if t.Name == name {
|
|
|
|
tree = t
|
|
|
|
level++
|
|
|
|
continue main
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 21:00:16 +03:00
|
|
|
ret = make([][]rune, len(tree.Children))
|
|
|
|
for idx, r := range tree.Children {
|
|
|
|
ret[idx] = []rune(r.Name)
|
|
|
|
}
|
|
|
|
return ret
|
2016-04-01 20:23:53 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
// a
|
|
|
|
// |- a1
|
|
|
|
// |--- a11
|
|
|
|
// |--- a12
|
|
|
|
// |- a2
|
|
|
|
// |--- a21
|
|
|
|
// b
|
|
|
|
ret := []struct {
|
|
|
|
Line string
|
|
|
|
Pos int
|
|
|
|
Ret [][]rune
|
|
|
|
Share int
|
|
|
|
}{
|
2016-04-01 21:00:16 +03:00
|
|
|
{"", 0, sr("a", "b", "route"), 0},
|
2016-04-01 20:23:53 +03:00
|
|
|
{"a", 1, sr(""), 1},
|
|
|
|
{"a ", 2, sr("a1", "a2"), 0},
|
|
|
|
{"a a", 3, sr("1", "2"), 1},
|
|
|
|
{"a a1", 4, sr(""), 2},
|
|
|
|
{"a a1 ", 5, sr("a11", "a12"), 0},
|
|
|
|
{"a a1 a", 6, sr("11", "12"), 1},
|
|
|
|
{"a a1 a1", 7, sr("1", "2"), 2},
|
|
|
|
{"a a1 a11", 8, sr(""), 3},
|
2016-04-01 21:00:16 +03:00
|
|
|
{"route add", 9, sr("", "domain"), 3},
|
2016-04-01 20:23:53 +03:00
|
|
|
}
|
2016-04-10 20:54:54 +03:00
|
|
|
for _, r := range ret {
|
|
|
|
for idx, rr := range r.Ret {
|
|
|
|
r.Ret[idx] = append(rr, ' ')
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 20:23:53 +03:00
|
|
|
for i, r := range ret {
|
|
|
|
newLine, length := s.Do([]rune(r.Line), r.Pos)
|
2016-04-01 21:00:16 +03:00
|
|
|
test.Equal(rs(newLine), rs(r.Ret), fmt.Errorf("%v", i))
|
2016-04-01 20:23:53 +03:00
|
|
|
test.Equal(length, r.Share, fmt.Errorf("%v", i))
|
|
|
|
}
|
|
|
|
}
|