-
Notifications
You must be signed in to change notification settings - Fork 2
/
util_test.go
167 lines (148 loc) · 3.31 KB
/
util_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package goql
import "testing"
// TestToLowerCamelCase tests the toLowerCamelCase function.
func TestToLowerCamelCase(t *testing.T) {
tt := []struct {
Name string
Input string
ExpectedOutput string
}{
{
Name: "SpaceSeparator",
Input: "lower camel case this",
ExpectedOutput: "lowerCamelCaseThis",
},
{
Name: "HyphenSeparator",
Input: "lower-camel-case-this",
ExpectedOutput: "lowerCamelCaseThis",
},
{
Name: "UnderscoreSeparator",
Input: "lower_camel_case_this",
ExpectedOutput: "lowerCamelCaseThis",
},
{
Name: "PeriodSeparator",
Input: "lower.camel.case.this",
ExpectedOutput: "lowerCamelCaseThis",
},
{
Name: "StartsWithCapitalLatter",
Input: "LowerCamelCaseThis",
ExpectedOutput: "lowerCamelCaseThis",
},
}
for _, test := range tt {
test := test
fn := func(t *testing.T) {
t.Parallel()
if e, a := test.ExpectedOutput, toLowerCamelCase(test.Input); e != a {
t.Errorf("expected output to be %s, got %s", e, a)
}
}
t.Run(test.Name, fn)
}
}
// TestStack tests the stack type and its receiver functions.
func TestStack(t *testing.T) {
t.Parallel()
// Test stack to be used throughout this test.
var st stack
// Test field to be used throughout this test.
f := field{
Decl: declaration{
Name: "user",
Alias: "",
Tokens: []token{
{
Kind: "String!",
Name: "name",
Arg: "name",
},
},
Template: "getUser",
},
Directives: []directive{
{
Type: "include",
Token: token{
Kind: "Boolean!",
Name: "ifAdmin",
Arg: "ifAdmin",
},
Template: "",
},
},
Keep: true,
}
// expectedLength keeps track of what the length of the stack should be at
// any given time.
expectedLength := 0
if e, a := expectedLength, st.length(); e != a {
t.Errorf("expected length to be %d, got %d", e, a)
}
for i := 0; i < 3; i++ {
st.push(&f)
expectedLength++
}
if e, a := expectedLength, st.length(); e != a {
t.Errorf("expected length to be %d, got %d", e, a)
}
if e, a := f.Decl.Template, st.top().Decl.Template; e != a {
t.Errorf("expected declaration template on top to be %s, got %s", e, a)
}
for i := 0; i < expectedLength-1; i++ {
_ = st.pop()
expectedLength--
}
if e, a := expectedLength, st.length(); e != a {
t.Errorf("expected length to be %d, got %d", e, a)
}
newDeclTemplate := "getUser2"
st.apply(func(f *field) {
f.Decl.Template = newDeclTemplate
})
if e, a := newDeclTemplate, st.top().Decl.Template; e != a {
t.Errorf("expected declaration template on top to be %s, got %s", e, a)
}
}
// BenchmarkStack benchmarks the receiver functions for the stack type.
func BenchmarkStack(b *testing.B) {
var st stack
f := field{
Decl: declaration{
Name: "user",
Alias: "",
Tokens: []token{
{
Kind: "String!",
Name: "name",
Arg: "name",
},
},
Template: "getUser",
},
Directives: []directive{
{
Type: "include",
Token: token{
Kind: "Boolean!",
Name: "ifAdmin",
Arg: "ifAdmin",
},
Template: "",
},
},
Keep: true,
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
st.push(&f)
_ = st.top()
st.apply(func(f *field) {
f.Decl.Template = "getUser2"
})
_ = st.pop()
}
}