aboutsummaryrefslogtreecommitdiff
path: root/client/input_test.go
blob: fe8feeb3ffb490514c475c0fa42a297f7b215a05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package client

import (
	"os"
	"shell-game-launcher/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)
	}
}