Jelajahi Sumber

Cleanup. Add helpers for cursor pos.

And also mouse.  We need mouse tests.
Steve Thielemann 2 tahun lalu
induk
melakukan
315099c198
4 mengubah file dengan 39 tambahan dan 12 penghapusan
  1. 3 0
      door/door_test.go
  2. 14 4
      door/help_test.go
  3. 8 8
      door/input_test.go
  4. 14 0
      door/line_test.go

+ 3 - 0
door/door_test.go

@@ -10,6 +10,9 @@ import (
 )
 
 // Should tests not delete the logfiles?
+// To make use of this, set KEEP_LOGS bool = true and:
+// go test -c; ./door.test -test.v -test.timeout 40s
+
 const KEEP_LOGS bool = false
 const VERBOSE_TEST bool = false
 

+ 14 - 4
door/help_test.go

@@ -70,11 +70,21 @@ func clear_socket(socket net.Conn, t *testing.T) string {
 }
 
 // Unicode detection response with given screen width/height.
-func UnicodeWidthHeight(width, height int) []byte {
-	return []byte(fmt.Sprintf("\x1b[1;1R\x1b[1;3R\x1b[%d;%dR", height, width))
+func UnicodeWidthHeight(width, height int) string {
+	return CursorReply(1, 1) + CursorReply(3, 1) + CursorReply(width, height)
 }
 
 // CP437 response with screen width/height. (80x25)
-func CP437WidthHeight(width, height int) []byte {
-	return []byte(fmt.Sprintf("\x1b[1;3R\x1b[1;4R\x1b[%d;%dR", height, width))
+func CP437WidthHeight(width, height int) string {
+	return CursorReply(3, 1) + CursorReply(4, 1) + CursorReply(width, height)
+}
+
+// Cursor Position Reply
+func CursorReply(x, y int) string {
+	return fmt.Sprintf("\x1b[%d;%dR", y, x)
+}
+
+// Mouse Button X,Y Reply
+func MouseResponse(button, x, y int8) string {
+	return fmt.Sprintf("\x1b[M%c%c%c", button+' '+1, x+'!'+1, y+'!'+1)
 }

+ 8 - 8
door/input_test.go

@@ -34,7 +34,7 @@ func TestDoorCP437(t *testing.T) {
 
 	// CP437 80x25 response
 	buffer := CP437WidthHeight(80, 25)
-	_, err = server.Write(buffer)
+	_, err = server.Write([]byte(buffer))
 	if err != nil {
 		t.Error("server.Write:", err)
 	}
@@ -175,7 +175,7 @@ func TestDoorInputConnection(t *testing.T) {
 
 	// unicode 190x43 response
 	buffer := UnicodeWidthHeight(190, 43)
-	_, err = server.Write(buffer)
+	_, err = server.Write([]byte(buffer))
 	if err != nil {
 		t.Error("server.Write:", err)
 	}
@@ -447,8 +447,8 @@ func TestDoorInputConnection(t *testing.T) {
 
 	t.Logf("Starting input test\n")
 	// Input test
-	buffer = []byte("1234567890\r")
-	_, err = server.Write(buffer)
+	buffer = "1234567890\r"
+	_, err = server.Write([]byte(buffer))
 	if err != nil {
 		t.Error("server.Write:", err)
 	}
@@ -489,8 +489,8 @@ func TestDoorInputConnection(t *testing.T) {
 		t.Error("server.SetReadDeadLine:", err)
 	}
 
-	buffer = []byte("12345678\x08\x089\r")
-	_, err = server.Write(buffer)
+	buffer = "12345678\x08\x089\r"
+	_, err = server.Write([]byte(buffer))
 	if err != nil {
 		t.Error("server.Write:", err)
 	}
@@ -502,8 +502,8 @@ func TestDoorInputConnection(t *testing.T) {
 		t.Errorf("Expected Input(5) = 1239, but got %#v", input)
 	}
 
-	buffer = []byte("12\x08\x08\x08987\x00\x48654321\r")
-	_, err = server.Write(buffer)
+	buffer = "12\x08\x08\x08987\x00\x48654321\r"
+	_, err = server.Write([]byte(buffer))
 	if err != nil {
 		t.Error("server.Write:", err)
 	}

+ 14 - 0
door/line_test.go

@@ -74,6 +74,7 @@ func TestLineUpdate(t *testing.T) {
 
 func TestLineUnicode(t *testing.T) {
 	Unicode = true
+	// code point > FFFF, use \U00000000 (not \u).
 	var line Line = Line{Text: "Howdy \U0001f920"}
 	var output string = line.Output()
 	var expect string = "Howdy 🤠"
@@ -81,6 +82,19 @@ func TestLineUnicode(t *testing.T) {
 	if output != expect {
 		t.Errorf("LineUnicode: Expected %#v, got %#v", expect, output)
 	}
+
+	if StringLen(expect) != 8 {
+		t.Errorf("LineUnicode Strlen: Expected 8, got %d", StringLen(expect))
+	}
+
+	// 🤠 = 2 chars.
+	line.Width = 9
+	output = line.Output()
+	expect = "Howdy 🤠 "
+
+	if output != expect {
+		t.Errorf("LineUnicode: Expected %#v, got %#v", expect, output)
+	}
 }
 
 func TestLineCP437(t *testing.T) {