aboutsummaryrefslogtreecommitdiff
path: root/pkg/client/menu_test.go
blob: 35964d8234a6f1e2d1eb10eb1859c05141359714 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package client

import (
	"io/ioutil"
	"os"
	"reflect"
	"shell-game-launcher/pkg/config"
	"testing"
)

func TestDisplayMenu(t *testing.T) {
	realStdout := os.Stdout
	t.Cleanup(func() { os.Stdout = realStdout })
	r, w, _ := os.Pipe()
	os.Stdout = w

	// Complete menu, while not logged in
	state := State{
		config: &config.Config{
			Menus: map[string]config.Menu{
				"test": config.Menu{
					Banner: "TEST TEST TEST",
					MenuEntries: []config.MenuEntry{
						config.MenuEntry{
							Key:    "q",
							Label:  "quit entry",
							Action: "quit",
						},
					},
				},
			},
		},
		currentMenu: "test",
		login:       "",
	}
	want := []byte("\033[2J" +
		"TEST TEST TEST\n" +
		"\n" +
		"Not logged in.\n" +
		"\n" +
		"q) quit entry\n")
	state.displayMenu()
	// back to normal state
	w.Close()
	out, _ := ioutil.ReadAll(r)
	if !reflect.DeepEqual(out, want) {
		t.Fatalf("menu displayed incorrectly:\nwant:%+v\ngot: %+v", want, out)
	}

	// Complete menu, while logged in
	r, w, _ = os.Pipe()
	os.Stdout = w

	// Complete menu, while not logged in
	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:       "test",
	}
	want = []byte("\033[2J" +
		"TEST TEST TEST\n" +
		"\n" +
		"Logged in as: test\n" +
		"\n" +
		"w) wait entry\n" +
		"q) quit entry\n")
	state.displayMenu()
	// back to normal state
	w.Close()
	out, _ = ioutil.ReadAll(r)
	if !reflect.DeepEqual(out, want) {
		t.Fatalf("menu displayed incorrectly:\nwant:%+v\ngot: %+v", want, out)
	}
}