help_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package door
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "os"
  7. "testing"
  8. "time"
  9. )
  10. // helper routines - to help with testing.
  11. // Using pipes causes failures.
  12. func Possible_setupSockets() (server net.Conn, client net.Conn) {
  13. // the better way to set up connections.
  14. return net.Pipe()
  15. }
  16. // setupSockets: return server and client (that's connected to server)
  17. func setupSockets() (server net.Conn, client net.Conn) {
  18. // establish network socket connection to set Comm_handle
  19. var err error
  20. var sock net.Listener
  21. sock, err = net.Listen("tcp", "127.0.0.1:0")
  22. if err != nil {
  23. panic(err)
  24. }
  25. // I only need address for making the connection.
  26. // Get address of listening socket
  27. address := sock.Addr().String()
  28. client, err = net.Dial("tcp", address)
  29. if err != nil {
  30. panic(err)
  31. }
  32. server, err = sock.Accept()
  33. if err != nil {
  34. panic(err)
  35. }
  36. sock.Close()
  37. return server, client
  38. }
  39. func clear_socket(socket net.Conn, t *testing.T) string {
  40. // Clear out the data that's pending in the socket.
  41. buffer := make([]byte, 1204)
  42. var r int
  43. var err error
  44. err = socket.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
  45. if err != nil {
  46. t.Error("socket.SetReadDeadLine:", err)
  47. }
  48. r, err = socket.Read(buffer)
  49. if err != nil {
  50. // DeadlineExceedError
  51. // Ignore DeadlineExceeded error (it is expected here)
  52. if !errors.Is(err, os.ErrDeadlineExceeded) {
  53. t.Logf("socket.Read: %#v", err)
  54. }
  55. }
  56. // t.Errorf("Buffer : %#v\n", buffer[:r])
  57. err = socket.SetReadDeadline(time.Time{})
  58. if err != nil {
  59. t.Error("socket.SetReadDeadLine:", err)
  60. }
  61. return string(buffer[:r])
  62. }
  63. // Unicode detection response with given screen width/height.
  64. func UnicodeWidthHeight(width, height int) string {
  65. return CursorReply(CursorPos{1, 1}) +
  66. CursorReply(CursorPos{3, 1}) +
  67. CursorReply(CursorPos{width, height})
  68. }
  69. // CP437 response with screen width/height. (80x25)
  70. func CP437WidthHeight(width, height int) string {
  71. return CursorReply(CursorPos{3, 1}) +
  72. CursorReply(CursorPos{4, 1}) +
  73. CursorReply(CursorPos{width, height})
  74. }
  75. // Cursor Position Reply
  76. func CursorReply(c CursorPos) string {
  77. return fmt.Sprintf("\x1b[%d;%dR", c.Y, c.X)
  78. }
  79. // Mouse Button X,Y Reply
  80. func MouseReply(m Mouse) string {
  81. return fmt.Sprintf("\x1b[M%c%c%c", m.Button+' '-1, m.X+'!'-1, m.Y+'!'-1)
  82. }