Browse Source

Change code so it can be tested. Testing input.

Steve Thielemann 3 years ago
parent
commit
52d52dbf93
2 changed files with 47 additions and 6 deletions
  1. 3 3
      door/input.go
  2. 44 3
      door/input_test.go

+ 3 - 3
door/input.go

@@ -362,8 +362,8 @@ retry:
 
 // Outputs spaces and backspaces
 // If you have set a background color, this shows the input area.
-func (d *Door) DisplayInput(max int) {
-	d.Write(strings.Repeat(" ", max) + strings.Repeat("\x08", max))
+func DisplayInput(max int) string {
+	return strings.Repeat(" ", max) + strings.Repeat("\x08", max)
 }
 
 // Input a string of max length.
@@ -373,7 +373,7 @@ func (d *Door) Input(max int) string {
 	var line string
 
 	// draw input area
-	d.DisplayInput(max)
+	d.Write(DisplayInput(max))
 
 	var c int
 

+ 44 - 3
door/input_test.go

@@ -101,8 +101,10 @@ func TestDoorInputConnection(t *testing.T) {
 		"\x1b[A\x1b[B\x1b[C\x1b[D":         []int{XKEY_UP_ARROW, XKEY_DOWN_ARROW, XKEY_RIGHT_ARROW, XKEY_LEFT_ARROW},
 		"\x1b[H\x1b[F\x1b[K\x1b[V\x1b[U":   []int{XKEY_HOME, XKEY_END, XKEY_END, XKEY_PGUP, XKEY_PGDN},
 		"\x1b[5~\x1b[6~":                   []int{XKEY_PGUP, XKEY_PGDN},
+		"\x1b[@\x1b[2~\x1b[3~":             []int{XKEY_INSERT, XKEY_INSERT, XKEY_DELETE},
 		"\x1bOP\x1bOQ\x1bOR\x1bOS":         []int{XKEY_F1, XKEY_F2, XKEY_F3, XKEY_F4},
 		"\x1b[15~\x1b[17~\x1b[18~\x1b[19~": []int{XKEY_F5, XKEY_F6, XKEY_F7, XKEY_F8},
+		"\x1b[20~\x1b[21~\x1b[23~\x1b[24~": []int{XKEY_F9, XKEY_F10, XKEY_F11, XKEY_F12},
 	}
 
 	for send, get := range keytest {
@@ -135,13 +137,38 @@ func TestDoorInputConnection(t *testing.T) {
 		}
 	}
 
-	input := d.WaitKey(0, 50)
-	if input != -1 {
-		t.Errorf("Expected timeout, got %d / %X", input, input)
+	timeout := d.WaitKey(0, 50)
+	if timeout != -1 {
+		t.Errorf("Expected timeout, got %d / %X", timeout, timeout)
 	} else {
 		t.Logf("Ok! Buffer should be empty!  -1 (timeout)")
 	}
 
+	// Input test
+	buffer = []byte("1234567890\r")
+	server.Write(buffer)
+	input := d.Input(5)
+
+	if input != "12345" {
+		t.Errorf("Expected Input(5) = 12345, but got %#v", input)
+	}
+
+	buffer = []byte("12345678\x08\x089\r")
+	server.Write(buffer)
+	input = d.Input(5)
+
+	if input != "1239" {
+		t.Errorf("Expected Input(5) = 1239, but got %#v", input)
+	}
+
+	buffer = []byte("12\x08\x08\x08987\x00\x48654321\r")
+	server.Write(buffer)
+	input = d.Input(5)
+
+	if input != "98765" {
+		t.Errorf("Expected Input(5) = 98765, but got %#v", input)
+	}
+
 	server.Close()
 
 	hungup := d.WaitKey(1, 0)
@@ -156,3 +183,17 @@ func TestDoorInputConnection(t *testing.T) {
 	client.Close()
 
 }
+
+func TestDisplayInput(t *testing.T) {
+	verify := map[int]string{1: " \x08",
+		2: "  \x08\x08",
+		5: "     \x08\x08\x08\x08\x08",
+	}
+
+	for count, expect := range verify {
+		got := DisplayInput(count)
+		if expect != got {
+			t.Errorf("DisplayInput %d, expected %#v, got %#v", count, expect, got)
+		}
+	}
+}