-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgofh_test.go
89 lines (83 loc) · 2.46 KB
/
gofh_test.go
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
package gofh
import (
"testing"
)
func TestFlags(t *testing.T) {
f := Init()
initCalled := false
f.HandleCommand("init", func(options map[string]string) {
initCalled = true
})
f.Parse([]string{"init"})
if !initCalled {
t.Error("Expected init callback to be called")
}
}
func TestFlagsWithArguments(t *testing.T) {
f := Init()
f.HandleCommand("new :name", func(options map[string]string) {
if options["name"] != "application" {
t.Errorf("Expected 'application' argument, but got '%v'", options["name"])
}
})
f.Parse([]string{"new", "application"})
}
func TestDefaultHandler(t *testing.T) {
f := Init()
f.HandleCommand("new :name", func(options map[string]string) {
t.Error("Not expected to handle new command")
})
visitedDefaultHandler := false
f.SetDefaultHandler(func() {
visitedDefaultHandler = true
})
f.Parse([]string{"help"})
if !visitedDefaultHandler {
t.Error("Did not call callback for default handler")
}
}
func TestCommandWithBooleanFlags(t *testing.T) {
f := Init()
options := []*Option{
&Option{Name: "no-views", Boolean: true},
&Option{Name: "no-components", Boolean: true},
}
handlerVisited := false
f.HandleCommandWithOptions("new :name", options, func(options map[string]string) {
handlerVisited = true
if options["name"] != "application" {
t.Errorf("Expected 'application' argument, but got '%v'", options["name"])
}
if options["no-views"] != "true" {
t.Errorf("Expected 'no-views' argument, but got '%v'", options["no-views"])
}
})
f.Parse([]string{"new", "application", "--no-views"})
if !handlerVisited {
t.Error("Did not call callback for command with options handler")
}
}
func TestCommandWithValueFlags(t *testing.T) {
f := Init()
options := []*Option{
&Option{Name: "db"},
&Option{Name: "no-css", Boolean: true},
}
handlerVisited := false
f.HandleCommandWithOptions("create :name :folder", options, func(options map[string]string) {
handlerVisited = true
if options["db"] != "mysql" {
t.Errorf("Expected 'db' argument value to be 'mysql', but got '%v'", options["db"])
}
if options["no-css"] != "true" {
t.Errorf("Expected 'no-css' argument to be 'true', but got '%v'", options["no-css"])
}
if options["folder"] != "/path" {
t.Errorf("Expected 'folder' argument to be '/path', but got '%v'", options["folder"])
}
})
f.Parse([]string{"create", "myapp", "--no-css", "--db", "mysql", "/path"})
if !handlerVisited {
t.Error("Did not call callback for command with value options")
}
}