help_test.go 2.1 KB

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