help_test.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. t.Errorf("socket.Read: %#v", err)
  49. }
  50. // t.Errorf("Buffer : %#v\n", buffer[:r])
  51. err = socket.SetReadDeadline(time.Time{})
  52. if err != nil {
  53. t.Error("socket.SetReadDeadLine:", err)
  54. }
  55. return string(buffer[:r])
  56. }
  57. // Unicode detection response with given screen width/height.
  58. func UnicodeWidthHeight(width, height int) string {
  59. return CursorReply(CursorPos{1, 1}) +
  60. CursorReply(CursorPos{3, 1}) +
  61. CursorReply(CursorPos{width, height})
  62. }
  63. // CP437 response with screen width/height. (80x25)
  64. func CP437WidthHeight(width, height int) string {
  65. return CursorReply(CursorPos{3, 1}) +
  66. CursorReply(CursorPos{4, 1}) +
  67. CursorReply(CursorPos{width, height})
  68. }
  69. // Cursor Position Reply
  70. func CursorReply(c CursorPos) string {
  71. return fmt.Sprintf("\x1b[%d;%dR", c.Y, c.X)
  72. }
  73. // Mouse Button X,Y Reply
  74. func MouseReply(m Mouse) string {
  75. return fmt.Sprintf("\x1b[M%c%c%c", m.Button+' '-1, m.X+'!'-1, m.Y+'!'-1)
  76. }