aboutsummaryrefslogtreecommitdiff
path: root/config/menu_test.go
blob: e9abef178ecc062d802a62af08545ce5d9423e8f (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
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
	// 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{
		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{
		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
}