-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_test.go
108 lines (91 loc) · 1.83 KB
/
lib_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package props
import (
"testing"
)
func TestFlagRegex(t *testing.T) {
// given
given := "-abc"
// when
arr := propFlagRegex.FindStringSubmatch(given)
// then
if len(arr) < 2 {
t.Error("propFlagRegex match should contain two elements")
}
}
func TestGetProp(t *testing.T) {
// given
args := []string{
"-abc",
"-x",
"-y",
"--bool",
"--prop",
"prop-val",
"arg1",
"--prop-eq1=prop-eq-val1",
"arg2",
"--prop-eq2=prop-eq-val2",
"--prop-last",
}
// when
globalProps = parseArgs(args)
// then
for _, v := range []string{"a", "b", "c", "x", "y", "bool", "prop", "prop-eq1", "prop-eq2", "prop-last"} {
if !IsSet(v) {
t.Errorf("flag `%s` must be set", v)
}
}
for k, v := range map[string]string{"prop": "prop-val"} {
if GetProp(k) != v {
t.Errorf("expected `--%[1]s=%[2]s`, but got `--%[1]s=%[3]s`", k, v, GetProp(k))
}
}
}
func TestInvalidProps(t *testing.T) {
// given
args := []string{
"arg1",
"x-x",
"y-y",
"bool--bool",
"prop",
"prop-val",
}
// when
globalProps = parseArgs(args)
resultArgs := GetArgs()
// then
a := len(resultArgs)
b := len(args)
if a != b {
t.Errorf("Expected `%d` arguments, but got `%d`", b, a)
} else {
var i int
for _, v := range args {
if v != resultArgs[i] {
t.Errorf("arg `%s` should match %s", v, resultArgs[i])
}
i++
}
}
}
func TestPropNames(t *testing.T) {
// given
args := []string{
"--prop-with-dashes=val1",
"--prop.with.periods=val2",
"--prop_with_underscores=val3",
}
// when
globalProps = parseArgs(args)
// then
if IsSet("prop-with-dashes") != true {
t.Error("Expected prop-with-dashes to be set")
}
if IsSet("prop.with.periods") != true {
t.Error("Expected prop.with.periods to be set")
}
if IsSet("prop_with_underscores") != true {
t.Errorf("Expected prop_with_underscores to be set `true`")
}
}