aboutsummaryrefslogtreecommitdiff
path: root/config/menu_test.go
blob: 095b699f98bbefa208d24acbfdd7618e3242cc0e (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
package config

import "testing"

func TestMenuValidate(t *testing.T) {
	// menu name
	menu := Menu{}
	if err := menu.validate(""); err == nil {
		t.Fatal("Empty menu name is not valid")
	}
	if err := menu.validate("test test"); err == nil {
		t.Fatal("non alphanumeric menu name is not valid")
	}
	// Banner is just any string, nothing to validate
	// XOffset
	menu = Menu{XOffset: -1}
	if err := menu.validate("test"); err == nil {
		t.Fatal("Negative XOffset should not be valid")
	}
	// YOffset
	menu = Menu{YOffset: -1}
	if err := menu.validate("test"); err == nil {
		t.Fatal("Negative YOffset should not be valid")
	}
	// MenuEntries are mostly tested bellow
	menu = Menu{}
	if err := menu.validate("test"); err == nil {
		t.Fatal("A menu without menu entries should not be valid")
	}
	// loop menu
	menu = Menu{
		XOffset: 1,
		YOffset: 1,
		MenuEntries: []MenuEntry{
			MenuEntry{
				Key:    "a",
				Label:  "test",
				Action: "menu test",
			},
		},
	}
	if err := menu.validate("test"); err == nil {
		t.Fatal("A menu should not be able to loop on itself")
	}
	// A valid menu
	menu = Menu{
		XOffset: 1,
		YOffset: 1,
		MenuEntries: []MenuEntry{
			MenuEntry{
				Key:    "a",
				Label:  "test",
				Action: "quit",
			},
		},
	}
	if err := menu.validate("test"); err != nil {
		t.Fatal("A valid menu should pass")
	}
}

func TestMenuEntryValidate(t *testing.T) {
	// Key
	menuEntry := MenuEntry{}
	if err := menuEntry.validate(); err == nil {
		t.Fatal("A Key cannot be empty")
	}
	menuEntry = MenuEntry{Key: "ab"}
	if err := menuEntry.validate(); err == nil {
		t.Fatal("A Key should be only one character")
	}
	menuEntry = MenuEntry{Key: " "}
	if err := menuEntry.validate(); err == nil {
		t.Fatal("A Key should be a printable character")
	}
	// Label
	menuEntry = MenuEntry{
		Key:   "l",
		Label: "",
	}
	if err := menuEntry.validate(); err == nil {
		t.Fatal("A Label cannot be empty")
	}
	// Actions are tested in action_test.go
}