autoterm_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package termutil
  10. import (
  11. "bytes"
  12. "fmt"
  13. "testing"
  14. "devt.de/krotik/common/termutil/getch"
  15. )
  16. func TestAutoCompleteConsoleLineTerminal(t *testing.T) {
  17. var out bytes.Buffer
  18. // Setup mock getch
  19. getchStart = func() error { return nil }
  20. var getchbuffer []*getch.KeyEvent
  21. addTestKeyEvent := func(kc getch.KeyCode, r rune) {
  22. getchbuffer = append(getchbuffer, &getch.KeyEvent{
  23. Code: kc,
  24. Rune: r,
  25. })
  26. }
  27. getchGetch = func() (*getch.KeyEvent, error) {
  28. e := getchbuffer[0]
  29. getchbuffer = getchbuffer[1:]
  30. return e, nil
  31. }
  32. ct, err := NewConsoleLineTerminal(&out)
  33. if err != nil {
  34. t.Error(err)
  35. return
  36. }
  37. rootDict := NewWordListDict([]string{"ll", "dir", "get", "put", "test", "test1", "test2"})
  38. chooser := func(lineWords []string, dictCache map[string]Dict) (Dict, error) {
  39. if len(lineWords) == 1 {
  40. return rootDict, nil
  41. }
  42. return NewWordListDict([]string{fmt.Sprintf("file4-%v", len(lineWords)), "file2", "file3", "file1", "test"}), nil
  43. }
  44. dict := NewMultiWordDict(chooser, nil)
  45. // Wrap the console terminal in a Auto Complete Mixin
  46. ct, err = AddAutoCompleteMixin(ct, dict)
  47. if err != nil {
  48. t.Error(err)
  49. return
  50. }
  51. // Test normal auto complete
  52. addTestKeyEvent(getch.KeyT, 'd')
  53. addTestKeyEvent(getch.KeyTab, 0x00)
  54. addTestKeyEvent(getch.KeyEnter, 0x00)
  55. if line, err := ct.NextLine(); err != nil || line != "dir " {
  56. t.Error("Unexpected result:", "#"+line+"#", err)
  57. return
  58. }
  59. addTestKeyEvent(getch.KeyT, 'd')
  60. addTestKeyEvent(getch.KeyTab, 0x00)
  61. addTestKeyEvent(getch.KeyT, 't')
  62. addTestKeyEvent(getch.KeyTab, 0x00)
  63. addTestKeyEvent(getch.KeyEnter, 0x00)
  64. if line, err := ct.NextLine(); err != nil || line != "dir test " {
  65. t.Error("Unexpected result:", "#"+line+"#", err)
  66. return
  67. }
  68. // Test auto complete with multiple suggestion and picking one by pressing tab
  69. addTestKeyEvent(getch.KeyT, 't')
  70. addTestKeyEvent(getch.KeyE, 'e')
  71. addTestKeyEvent(getch.KeyTab, 0x00) // Auto complete to test
  72. addTestKeyEvent(getch.KeyTab, 0x00) // See suggestions ("test", "test1", "test2"s)
  73. addTestKeyEvent(getch.KeyTab, 0x00) // Produce final space - "test" was accepted
  74. addTestKeyEvent(getch.KeyEnter, 0x00)
  75. if line, err := ct.NextLine(); err != nil || line != "test " {
  76. t.Error("Unexpected result:", "#"+line+"#", err)
  77. return
  78. }
  79. // Check second level suggestion
  80. addTestKeyEvent(getch.KeyT, 't')
  81. addTestKeyEvent(getch.KeyE, 'e')
  82. addTestKeyEvent(getch.KeyTab, 0x00)
  83. addTestKeyEvent(getch.KeyE, ' ')
  84. addTestKeyEvent(getch.KeyT, 'f')
  85. addTestKeyEvent(getch.KeyTab, 0x00) // No effect since there is no "file"
  86. addTestKeyEvent(getch.KeyTab, 0x00)
  87. addTestKeyEvent(getch.KeyTab, 0x00)
  88. addTestKeyEvent(getch.KeyT, '1')
  89. addTestKeyEvent(getch.KeyTab, 0x00)
  90. addTestKeyEvent(getch.KeyEnter, 0x00)
  91. if line, err := ct.NextLine(); err != nil || line != "test file1 " {
  92. t.Error("Unexpected result:", "#"+line+"#", err)
  93. return
  94. }
  95. }
  96. func TestWordListDict(t *testing.T) {
  97. wlist := []string{"bar", "foo", "test", "test1", "test2", "test3", "zanas"}
  98. wld := NewWordListDict(wlist)
  99. if res, _ := wld.Suggest("zanas"); fmt.Sprint(res) != "[zanas]" {
  100. t.Error("Unexpected result:", res)
  101. return
  102. }
  103. if res, _ := wld.Suggest("zan"); fmt.Sprint(res) != "[zanas]" {
  104. t.Error("Unexpected result:", res)
  105. return
  106. }
  107. if res, _ := wld.Suggest("zap"); fmt.Sprint(res) != "[]" {
  108. t.Error("Unexpected result:", res)
  109. return
  110. }
  111. if res, _ := wld.Suggest("t"); fmt.Sprint(res) != "[test test1 test2 test3]" {
  112. t.Error("Unexpected result:", res)
  113. return
  114. }
  115. if res, _ := wld.Suggest("test"); fmt.Sprint(res) != "[test test1 test2 test3]" {
  116. t.Error("Unexpected result:", res)
  117. return
  118. }
  119. if res, _ := wld.Suggest("b"); fmt.Sprint(res) != "[bar]" {
  120. t.Error("Unexpected result:", res)
  121. return
  122. }
  123. // Special case of empty dictionary
  124. wld = NewWordListDict([]string{})
  125. if res, _ := wld.Suggest("b"); fmt.Sprint(res) != "[]" {
  126. t.Error("Unexpected result:", res)
  127. return
  128. }
  129. }
  130. func TestMultiWordDict(t *testing.T) {
  131. rootDict := NewWordListDict([]string{"bar", "foo"})
  132. md := NewMultiWordDict(func(p []string, c map[string]Dict) (Dict, error) {
  133. var dict Dict
  134. var ok bool
  135. if p[0] == "" {
  136. return nil, nil
  137. }
  138. if p[0] == "foo" {
  139. return nil, fmt.Errorf("Testerror")
  140. }
  141. if dict, ok = c[p[0]]; !ok {
  142. dict = rootDict
  143. }
  144. return dict, nil
  145. }, nil)
  146. md.dicts["bar"] = NewWordListDict([]string{"bar", "foo", "test", "test1", "test2", "test3", "zanas"})
  147. if res, err := md.Suggest(""); err != nil || fmt.Sprint(res) != "[]" {
  148. t.Error("Unexpected result:", res, err)
  149. return
  150. }
  151. if res, err := md.Suggest("f"); err != nil || fmt.Sprint(res) != "[foo]" {
  152. t.Error("Unexpected result:", res, err)
  153. return
  154. }
  155. if res, err := md.Suggest("foo"); err == nil || err.Error() != "Testerror" {
  156. t.Error("Unexpected result:", res, err)
  157. return
  158. }
  159. if res, err := md.Suggest("b"); err != nil || fmt.Sprint(res) != "[bar]" {
  160. t.Error("Unexpected result:", res, err)
  161. return
  162. }
  163. if res, err := md.Suggest("bar"); err != nil || fmt.Sprint(res) != "[bar]" {
  164. t.Error("Unexpected result:", res, err)
  165. return
  166. }
  167. if res, err := md.Suggest("bar "); err != nil || fmt.Sprint(res) != "[bar foo test test1 test2 test3 zanas]" {
  168. t.Error("Unexpected result:", res, err)
  169. return
  170. }
  171. if res, err := md.Suggest("bar b"); err != nil || fmt.Sprint(res) != "[bar]" {
  172. t.Error("Unexpected result:", res, err)
  173. return
  174. }
  175. if res, err := md.Suggest("bar test"); err != nil || fmt.Sprint(res) != "[test test1 test2 test3]" {
  176. t.Error("Unexpected result:", res, err)
  177. return
  178. }
  179. }