aboutsummaryrefslogtreecommitdiff
path: root/pkg/client/input_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/client/input_test.go')
-rw-r--r--pkg/client/input_test.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/pkg/client/input_test.go b/pkg/client/input_test.go
new file mode 100644
index 0000000..f74a097
--- /dev/null
+++ b/pkg/client/input_test.go
@@ -0,0 +1,60 @@
+package client
+
+import (
+ "os"
+ "shell-game-launcher/pkg/config"
+ "testing"
+)
+
+func TestGetValidInput(t *testing.T) {
+ realStdin := os.Stdin
+ t.Cleanup(func() { os.Stdin = realStdin })
+
+ // Complete menu, no input error
+ state := State{
+ config: &config.Config{
+ Menus: map[string]config.Menu{
+ "test": config.Menu{
+ Banner: "TEST TEST TEST",
+ MenuEntries: []config.MenuEntry{
+ config.MenuEntry{
+ Key: "w",
+ Label: "wait entry",
+ Action: "wait",
+ },
+ config.MenuEntry{
+ Key: "q",
+ Label: "quit entry",
+ Action: "quit",
+ },
+ },
+ },
+ },
+ },
+ currentMenu: "test",
+ login: "",
+ }
+ r, w, _ := os.Pipe()
+ os.Stdin = r
+
+ // Simply test quit entry
+ w.WriteString("q")
+ if cmd, err := state.getValidInput(); err != nil || cmd != "quit" {
+ t.Fatalf("Input handled incorrectly:\nwant: wait\ngot: %s\nerror: %s\n", cmd, err)
+ }
+ // test quit entry after wrong keys
+ w.WriteString("abcdq")
+ if cmd, err := state.getValidInput(); err != nil || cmd != "quit" {
+ t.Fatalf("Input handled incorrectly:\nwant: wait\ngot: %s\nerror: %s\n", cmd, err)
+ }
+ // test wait entry with valid quit after
+ w.WriteString("wq")
+ if cmd, err := state.getValidInput(); err != nil || cmd != "wait" {
+ t.Fatalf("Input handled incorrectly:\nwant: wait\ngot: %s\nerror: %s\n", cmd, err)
+ }
+ // test input error
+ w.Close()
+ if cmd, err := state.getValidInput(); err == nil {
+ t.Fatalf("Input handled incorrectly:\nwant: wait\ngot: %s\nerror: %s\n", cmd, err)
+ }
+}