You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.2 KiB

  1. package readline
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/chzyer/test"
  6. )
  7. func rs(s [][]rune) []string {
  8. ret := make([]string, len(s))
  9. for idx, ss := range s {
  10. ret[idx] = string(ss)
  11. }
  12. return ret
  13. }
  14. func sr(s ...string) [][]rune {
  15. ret := make([][]rune, len(s))
  16. for idx, ss := range s {
  17. ret[idx] = []rune(ss)
  18. }
  19. return ret
  20. }
  21. func TestRetSegment(t *testing.T) {
  22. defer test.New(t)
  23. // a
  24. // |- a1
  25. // |--- a11
  26. // |--- a12
  27. // |- a2
  28. // |--- a21
  29. // b
  30. // add
  31. // adddomain
  32. ret := []struct {
  33. Segments [][]rune
  34. Cands [][]rune
  35. idx int
  36. Ret [][]rune
  37. pos int
  38. }{
  39. {sr(""), sr("a", "b", "add", "adddomain"), 0, sr("a", "b", "add", "adddomain"), 0},
  40. {sr("a"), sr("a", "add", "adddomain"), 1, sr("", "dd", "dddomain"), 1},
  41. {sr("a", ""), sr("a1", "a2"), 0, sr("a1", "a2"), 0},
  42. {sr("a", "a"), sr("a1", "a2"), 1, sr("1", "2"), 1},
  43. {sr("a", "a1"), sr("a1"), 2, sr(""), 2},
  44. {sr("add"), sr("add", "adddomain"), 2, sr("", "domain"), 2},
  45. }
  46. for idx, r := range ret {
  47. ret, pos := RetSegment(r.Segments, r.Cands, r.idx)
  48. test.Equal(ret, r.Ret, fmt.Errorf("%v", idx))
  49. test.Equal(pos, r.pos, fmt.Errorf("%v", idx))
  50. }
  51. }
  52. func TestSplitSegment(t *testing.T) {
  53. defer test.New(t)
  54. // a
  55. // |- a1
  56. // |--- a11
  57. // |--- a12
  58. // |- a2
  59. // |--- a21
  60. // b
  61. ret := []struct {
  62. Line string
  63. Pos int
  64. Segments [][]rune
  65. Idx int
  66. }{
  67. {"", 0, sr(""), 0},
  68. {"a", 1, sr("a"), 1},
  69. {"a ", 2, sr("a", ""), 0},
  70. {"a a", 3, sr("a", "a"), 1},
  71. {"a a1", 4, sr("a", "a1"), 2},
  72. {"a a1 ", 5, sr("a", "a1", ""), 0},
  73. }
  74. for i, r := range ret {
  75. ret, idx := SplitSegment([]rune(r.Line), r.Pos)
  76. test.Equal(rs(ret), rs(r.Segments), fmt.Errorf("%v", i))
  77. test.Equal(idx, r.Idx, fmt.Errorf("%v", i))
  78. }
  79. }
  80. type Tree struct {
  81. Name string
  82. Children []Tree
  83. }
  84. func TestSegmentCompleter(t *testing.T) {
  85. defer test.New(t)
  86. tree := Tree{"", []Tree{
  87. {"a", []Tree{
  88. {"a1", []Tree{
  89. {"a11", nil},
  90. {"a12", nil},
  91. }},
  92. {"a2", []Tree{
  93. {"a21", nil},
  94. }},
  95. }},
  96. {"b", nil},
  97. {"route", []Tree{
  98. {"add", nil},
  99. {"adddomain", nil},
  100. }},
  101. }}
  102. s := SegmentFunc(func(ret [][]rune, n int) [][]rune {
  103. tree := tree
  104. main:
  105. for level := 0; level < len(ret)-1; {
  106. name := string(ret[level])
  107. for _, t := range tree.Children {
  108. if t.Name == name {
  109. tree = t
  110. level++
  111. continue main
  112. }
  113. }
  114. }
  115. ret = make([][]rune, len(tree.Children))
  116. for idx, r := range tree.Children {
  117. ret[idx] = []rune(r.Name)
  118. }
  119. return ret
  120. })
  121. // a
  122. // |- a1
  123. // |--- a11
  124. // |--- a12
  125. // |- a2
  126. // |--- a21
  127. // b
  128. ret := []struct {
  129. Line string
  130. Pos int
  131. Ret [][]rune
  132. Share int
  133. }{
  134. {"", 0, sr("a", "b", "route"), 0},
  135. {"a", 1, sr(""), 1},
  136. {"a ", 2, sr("a1", "a2"), 0},
  137. {"a a", 3, sr("1", "2"), 1},
  138. {"a a1", 4, sr(""), 2},
  139. {"a a1 ", 5, sr("a11", "a12"), 0},
  140. {"a a1 a", 6, sr("11", "12"), 1},
  141. {"a a1 a1", 7, sr("1", "2"), 2},
  142. {"a a1 a11", 8, sr(""), 3},
  143. {"route add", 9, sr("", "domain"), 3},
  144. }
  145. for _, r := range ret {
  146. for idx, rr := range r.Ret {
  147. r.Ret[idx] = append(rr, ' ')
  148. }
  149. }
  150. for i, r := range ret {
  151. newLine, length := s.Do([]rune(r.Line), r.Pos)
  152. test.Equal(rs(newLine), rs(r.Ret), fmt.Errorf("%v", i))
  153. test.Equal(length, r.Share, fmt.Errorf("%v", i))
  154. }
  155. }