-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparameter_test.go
98 lines (95 loc) · 1.85 KB
/
parameter_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
package tl
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestParameter(t *testing.T) {
for _, tt := range []struct {
String string
Value Parameter
}{
{
String: "flags:#",
Value: Parameter{Name: "flags", Flags: true},
},
{
String: "code:int32",
Value: Parameter{Name: "code", Type: Type{Name: "int32", Bare: true}},
},
{
String: "status_code:f.1?Code",
Value: Parameter{
Name: "status_code",
Flag: &Flag{Name: "f", Index: 1},
Type: Type{Name: "Code"},
},
},
{
String: "int",
Value: Parameter{
Name: "",
Type: Type{Name: "int", Bare: true},
},
},
{
String: "{X:Type}",
Value: Parameter{
Name: "X",
typeDefinition: true,
},
},
} {
t.Run(tt.String, func(t *testing.T) {
t.Run("String", func(t *testing.T) {
if v := tt.Value.String(); v != tt.String {
t.Errorf("(%s).String = %s", tt.String, v)
}
})
t.Run("Parse", func(t *testing.T) {
var result Parameter
if err := result.Parse(tt.String); err != nil {
t.Fatal(err)
}
require.Equal(t, result, tt.Value)
})
})
}
t.Run("Error", func(t *testing.T) {
for _, s := range []string{
".1",
"",
"{a:b}",
"{c",
} {
var value Parameter
if err := value.Parse(s); err == nil {
t.Errorf("Expected error on %q", s)
}
}
})
t.Run("Conditional", func(t *testing.T) {
for _, conditional := range []Parameter{
{
Name: "Foo",
Flag: &Flag{Name: "flags", Index: 1},
},
} {
t.Run(conditional.Name, func(t *testing.T) {
if !conditional.Conditional() {
t.Error("expected conditional")
}
})
}
for _, nonConditional := range []Parameter{
{
Name: "Bar",
},
} {
t.Run(nonConditional.Name, func(t *testing.T) {
if nonConditional.Conditional() {
t.Error("expected non-conditional")
}
})
}
})
}