fileterm_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "io/ioutil"
  14. "os"
  15. "testing"
  16. "devt.de/krotik/common/termutil/getch"
  17. )
  18. func TestFileReadingTerminal(t *testing.T) {
  19. var ct ConsoleLineTerminal
  20. var err error
  21. var out bytes.Buffer
  22. // Take over the low-level input interface
  23. stdin = bytes.NewBufferString("test line\n")
  24. // First test the fallback mode
  25. getchStart = func() error { return fmt.Errorf("Something is wrong") }
  26. getchGetch = func() (*getch.KeyEvent, error) { return nil, fmt.Errorf("baa") }
  27. // Write a line file
  28. ioutil.WriteFile("test.cmd", []byte(`Test1
  29. Test2
  30. TestEnd
  31. `), 0666)
  32. defer os.Remove("test.cmd")
  33. // Create a basic terminal
  34. ct, err = NewConsoleLineTerminal(&out)
  35. if err != nil {
  36. t.Error("Console terminal should go into fallback without error:", err)
  37. return
  38. }
  39. // Wrap the terminal in a file reading terminal
  40. file, _ := os.Open("test.cmd")
  41. defer file.Close()
  42. ct, err = AddFileReadingWrapper(ct, file, false)
  43. if err != nil {
  44. t.Error(err)
  45. return
  46. }
  47. ct.StartTerm()
  48. defer ct.StopTerm()
  49. if l, err := ct.NextLine(); err != nil || l != "Test1" {
  50. t.Error("Unexpected result:", l, err)
  51. return
  52. }
  53. if l, err := ct.NextLine(); err != nil || l != "Test2" {
  54. t.Error("Unexpected result:", l, err)
  55. return
  56. }
  57. if l, err := ct.NextLine(); err != nil || l != "TestEnd" {
  58. t.Error("Unexpected result:", l, err)
  59. return
  60. }
  61. if l, err := ct.NextLine(); err != nil || l != "test line" {
  62. t.Error("Unexpected result:", l, err)
  63. return
  64. }
  65. if l, err := ct.NextLine(); err != nil || l != "" {
  66. t.Error("Unexpected result:", l, err)
  67. return
  68. }
  69. ct.(*filereadingTerminalMixin).termOnEOF = true
  70. if l, err := ct.NextLine(); err != nil || l != "\x04" {
  71. t.Error("Unexpected result:", l, err)
  72. return
  73. }
  74. }