histterm_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "io/ioutil"
  13. "os"
  14. "testing"
  15. "devt.de/krotik/common/termutil/getch"
  16. )
  17. func TestHistoryConsoleLineTerminal(t *testing.T) {
  18. var out bytes.Buffer
  19. DefaultHistoryBufferSize = 3 // Set very small default history size
  20. // Setup mock getch
  21. getchStart = func() error { return nil }
  22. var getchbuffer []*getch.KeyEvent
  23. addTestKeyEvent := func(kc getch.KeyCode, r rune) {
  24. getchbuffer = append(getchbuffer, &getch.KeyEvent{
  25. Code: kc,
  26. Rune: r,
  27. })
  28. }
  29. getchGetch = func() (*getch.KeyEvent, error) {
  30. e := getchbuffer[0]
  31. getchbuffer = getchbuffer[1:]
  32. return e, nil
  33. }
  34. ct, err := NewConsoleLineTerminal(&out)
  35. if err != nil {
  36. t.Error(err)
  37. return
  38. }
  39. // Wrap the console terminal in a History Mixin
  40. ct, err = AddHistoryMixin(ct, "testhistory", func(l string) bool {
  41. return l == "exit"
  42. })
  43. if err != nil {
  44. t.Error(err)
  45. return
  46. }
  47. defer os.Remove("testhistory")
  48. addTestKeyEvent(getch.KeyT, 't')
  49. addTestKeyEvent(getch.KeyE, 'e')
  50. addTestKeyEvent(getch.KeyS, 's')
  51. addTestKeyEvent(getch.KeyT, 't')
  52. addTestKeyEvent(getch.KeyEnter, 0x00)
  53. if line, err := ct.NextLine(); err != nil || line != "test" {
  54. t.Error("Unexpected result:", "#"+line+"#", err)
  55. return
  56. }
  57. // Test repeat previous line
  58. addTestKeyEvent(getch.KeyArrowUp, 0x00)
  59. addTestKeyEvent(getch.KeyEnter, 0x00)
  60. if line, err := ct.NextLine(); err != nil || line != "test" {
  61. t.Error("Unexpected result:", "#"+line+"#", err)
  62. return
  63. }
  64. addTestKeyEvent(getch.Key1, '1')
  65. addTestKeyEvent(getch.KeyEnter, 0x00)
  66. ct.NextLine()
  67. addTestKeyEvent(getch.Key1, '2')
  68. addTestKeyEvent(getch.KeyEnter, 0x00)
  69. ct.NextLine()
  70. // Next line should be ignored
  71. addTestKeyEvent(getch.KeyT, 'e')
  72. addTestKeyEvent(getch.KeyE, 'x')
  73. addTestKeyEvent(getch.KeyS, 'i')
  74. addTestKeyEvent(getch.KeyT, 't')
  75. addTestKeyEvent(getch.KeyEnter, 0x00)
  76. if line, err := ct.NextLine(); err != nil || line != "exit" {
  77. t.Error("Unexpected result:", "#"+line+"#", err)
  78. return
  79. }
  80. addTestKeyEvent(getch.Key1, '3')
  81. addTestKeyEvent(getch.KeyEnter, 0x00)
  82. ct.NextLine()
  83. // Check that the first lines have fallen off
  84. if res, _ := ioutil.ReadFile("testhistory"); string(res) != `1
  85. 2
  86. 3` {
  87. t.Error("Unexpected history:", string(res))
  88. return
  89. }
  90. ct.StopTerm()
  91. // Recreate the terminal
  92. ct, err = NewConsoleLineTerminal(&out)
  93. if err != nil {
  94. t.Error(err)
  95. return
  96. }
  97. // Wrap the console terminal in a History Mixin
  98. ct, err = AddHistoryMixin(ct, "testhistory", func(l string) bool {
  99. return l == "exit"
  100. })
  101. if err != nil {
  102. t.Error(err)
  103. return
  104. }
  105. // Check we have history entries
  106. addTestKeyEvent(getch.KeyArrowUp, 0x00)
  107. addTestKeyEvent(getch.KeyArrowUp, 0x00)
  108. addTestKeyEvent(getch.KeyArrowUp, 0x00)
  109. addTestKeyEvent(getch.KeyArrowDown, 0x00)
  110. addTestKeyEvent(getch.KeyEnter, 0x00)
  111. if line, err := ct.NextLine(); err != nil || line != "2" {
  112. t.Error("Unexpected result:", "#"+line+"#", err)
  113. return
  114. }
  115. addTestKeyEvent(getch.KeyArrowUp, 0x00)
  116. addTestKeyEvent(getch.KeyArrowUp, 0x00)
  117. addTestKeyEvent(getch.KeyEnter, 0x00)
  118. if line, err := ct.NextLine(); err != nil || line != "3" {
  119. t.Error("Unexpected result:", "#"+line+"#", err)
  120. return
  121. }
  122. addTestKeyEvent(getch.KeyT, 't')
  123. addTestKeyEvent(getch.KeyE, 'e')
  124. addTestKeyEvent(getch.KeyArrowUp, 0x00)
  125. addTestKeyEvent(getch.KeyArrowUp, 0x00)
  126. addTestKeyEvent(getch.KeyArrowDown, 0x00)
  127. addTestKeyEvent(getch.KeyArrowDown, 0x00)
  128. addTestKeyEvent(getch.KeyEnter, 0x00)
  129. if line, err := ct.NextLine(); err != nil || line != "te" {
  130. t.Error("Unexpected result:", "#"+line+"#", err)
  131. return
  132. }
  133. ct.StopTerm()
  134. }