menu_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package door
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/ioutil"
  6. "net"
  7. "os"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. func TestMenuConnection(t *testing.T) {
  13. tmpFile, err := ioutil.TempFile(os.TempDir(), "test-")
  14. if err != nil {
  15. panic("Cannot create temporary file")
  16. }
  17. // Remember to clean up the file afterwards
  18. defer os.Remove(tmpFile.Name())
  19. // establish network socket connection to set Comm_handle
  20. server, client := setupSockets()
  21. // We're not testing closed connections, so:
  22. defer server.Close()
  23. defer client.Close()
  24. // unicode 90x40 response
  25. buffer := []byte("\x1b[1;1R\x1b[2;3R\x1b[40;90R")
  26. server.Write(buffer)
  27. // Access Fd (File descriptor) of client for dropfile
  28. client_conn := client.(*net.TCPConn)
  29. client_file, err := client_conn.File()
  30. var fd int = int(client_file.Fd())
  31. // Create door32.sys file
  32. dfc := DropfileConfig{2, fd, 1800, "Test BBSID", 1701, "Real Username", "Handle", 880, 28, 0, 12}
  33. tmpFile.WriteString(fmt.Sprintf("%d\n%d\n%d\n%s\n%d\n%s\n%s\n%d\n%d\n%d\n%d\n",
  34. dfc.Comm_type, dfc.Comm_handle, dfc.Baudrate, dfc.BBSID, dfc.User_number, dfc.Real_name, dfc.Handle,
  35. dfc.Security_level, dfc.Time_left, dfc.Emulation, dfc.Node))
  36. tmpFile.Close()
  37. d := Door{}
  38. // If I call d.Init() more then once flag complains about flag redefined.
  39. // Reset flags
  40. flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
  41. // preset commandline args so door can init
  42. os.Args = []string{"door", "-d", tmpFile.Name()}
  43. d.Init("menu-test")
  44. // clean up log file
  45. defer os.Remove("menu-test-12.log")
  46. // Ok!
  47. if !Unicode {
  48. t.Errorf("Unicode not true %t", Unicode)
  49. }
  50. if Width != 90 {
  51. t.Errorf("Width not 90: %d", Width)
  52. }
  53. if Height != 40 {
  54. t.Errorf("Height not 40: %d", Height)
  55. }
  56. // These are the commands sent to detect ... throw this all away.
  57. buffer = make([]byte, 1024)
  58. server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
  59. _, err = server.Read(buffer)
  60. // t.Errorf("Buffer : %#v\n", buffer[:r])
  61. server.SetReadDeadline(time.Time{})
  62. // Ready to test!
  63. m := Menu{Panel: Panel{Width: 10,
  64. X: 2,
  65. Y: 2,
  66. Style: DOUBLE,
  67. }}
  68. // Use simple renders for testing
  69. m.SelectedR = func(text string) string {
  70. return ColorText("BLACK ON WHITE") + text
  71. }
  72. m.UnselectedR = func(text string) string {
  73. return ColorText("WHI ON BLA") + text
  74. }
  75. m.AddSelection("A", "ART")
  76. m.AddSelection("B", "BOO")
  77. m.AddSelection("C", "Cat")
  78. var choice int
  79. // preload the key buffer with commands
  80. // BUG: I can't use \x1b[A because of the way extended codes work there.
  81. keys := "\x00\x50\x00\x50\r" // down, down, ENTER
  82. server.Write([]byte(keys))
  83. choice = m.Choose(&d)
  84. if choice != 3 {
  85. t.Errorf("Expected menu option C, got %d / %c", choice, m.GetOption(choice))
  86. }
  87. // Read the display output
  88. server.SetReadDeadline(time.Now().Add(time.Millisecond * 200))
  89. r, err := server.Read(buffer)
  90. server.SetReadDeadline(time.Time{})
  91. output := string(buffer[:r])
  92. // t.Errorf("OUTPUT: (len %d) %#v", r, output)
  93. parts := strings.SplitAfter(output, "H")
  94. for _, part := range parts {
  95. t.Logf("Got: %#v", part)
  96. }
  97. m.Chosen = 1
  98. keys = "\x00\x48\r" // up, ENTER
  99. server.Write([]byte(keys))
  100. choice = m.Choose(&d)
  101. if choice != 1 {
  102. t.Errorf("Expected menu option A, got %d / %c", choice, m.GetOption(choice))
  103. }
  104. // Read the display output
  105. server.SetReadDeadline(time.Now().Add(time.Millisecond * 200))
  106. r, err = server.Read(buffer)
  107. server.SetReadDeadline(time.Time{})
  108. output = string(buffer[:r])
  109. }