-
Notifications
You must be signed in to change notification settings - Fork 2
/
macro_test.go
266 lines (241 loc) · 7.91 KB
/
macro_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package specfile
import (
"reflect"
"strings"
"testing"
)
func TestParseMacroWithNotMacro(t *testing.T) {
str := "test"
var m Macro
err := (&m).Parse(str)
if err == nil {
t.Error("[macro]parse a non-macro but the err is nil")
}
}
func TestParseMacroWithSingleLineVariable(t *testing.T) {
str := "%define fcitx5_version 5.0.1"
var m Macro
err := (&m).Parse(str)
if err != nil {
t.Error("[macro]parse a macro but the err is non-nil")
}
if m.Indicator != "%define" {
t.Errorf("[macro]parsed indicator is wrong, expected %%define, got '%s'", m.Indicator)
}
if m.Name != "fcitx5_version" {
t.Errorf("[macro]parsed name is wrong, expected fcitx5_version, got '%s'", m.Name)
}
if m.Value != "5.0.1" {
t.Errorf("[macro]parsed value is wrong, expected 5.0.1, got '%s'", m.Value)
}
}
func TestParseMacroWithMultiLineVariable(t *testing.T) {
str := "%define fcitx5_version\\\n 5.0.1"
var m Macro
err := (&m).Parse(str)
if err != nil {
t.Error("[macro]parse a macro but the err is non-nil")
}
if m.Indicator != "%define" {
t.Errorf("[macro]parsed indicator is wrong, expected %%define, got '%s'", m.Indicator)
}
if m.Name != "fcitx5_version" {
t.Errorf("[macro]parsed name is wrong, expected fcitx5_version, got '%s'", m.Name)
}
if m.Value != "5.0.1" {
t.Errorf("[macro]parsed value is wrong, expected 5.0.1, got '%s'", m.Value)
}
}
func TestParseMacroWithSingleLineFunction(t *testing.T) {
str := "%define fcitx5_version() %(fcitx5 -v)"
var m Macro
err := (&m).Parse(str)
if err != nil {
t.Error("[macro]parse a macro but the err is non-nil")
}
if m.Indicator != "%define" {
t.Errorf("[macro]parsed indicator is wrong, expected %%define, got '%s'", m.Indicator)
}
if m.Name != "fcitx5_version()" {
t.Errorf("[macro]parsed name is wrong, expected fcitx5_version(), got '%s'", m.Name)
}
if m.Value != "%(fcitx5 -v)" {
t.Errorf("[macro]parsed value is wrong, expected %%(fcitx5 -v), got '%s'", m.Value)
}
}
func TestParseMacroWithMultiLineFunction(t *testing.T) {
str := "%define fcitx5_version()\\\n%(fcitx5 -v)"
var m Macro
err := (&m).Parse(str)
if err != nil {
t.Error("[macro]parse a macro but the err is non-nil")
}
if m.Indicator != "%define" {
t.Errorf("[macro]parsed indicator is wrong, expected %%define, got '%s'", m.Indicator)
}
if m.Name != "fcitx5_version()" {
t.Errorf("[macro]parsed name is wrong, expected fcitx5_version(), got '%s'", m.Name)
}
if m.Value != "%(fcitx5 -v)" {
t.Errorf("[macro]parsed value is wrong, expected %%(fcitx5 -v), got '%s'", m.Value)
}
}
func TestParseMacroWithSpaceSeparator(t *testing.T) {
str := "%define fcitx5_version 5.0.1"
var m Macro
err := (&m).Parse(str)
if err != nil {
t.Error("[macro]parse a macro but the err is non-nil")
}
if m.Indicator != "%define" {
t.Errorf("[macro]parsed indicator is wrong, expected %%define, got '%s'", m.Indicator)
}
if m.Name != "fcitx5_version" {
t.Errorf("[macro]parsed name is wrong, expected fcitx5_version, got '%s'", m.Name)
}
}
func TestParseMacroWithTabSeparator(t *testing.T) {
str := "%define\tfcitx5_version 5.0.1"
var m Macro
err := (&m).Parse(str)
if err != nil {
t.Error("[macro]parse a macro but the err is non-nil")
}
if m.Indicator != "%define" {
t.Errorf("[macro]parsed indicator is wrong, expected %%define, got '%s'", m.Indicator)
}
if m.Name != "fcitx5_version" {
t.Errorf("[macro]parsed name is wrong, expected fcitx5_version, got '%s'", m.Name)
}
}
func TestParseMacroWithDefinition(t *testing.T) {
str := "%define fcitx5_version 5.0.1"
var m Macro
err := (&m).Parse(str)
if err != nil {
t.Error("[macro]parse a macro but the err is non-nil")
}
if m.Indicator != "%define" {
t.Errorf("[macro]parsed indicator is wrong, expected %%define, got '%s'", m.Indicator)
}
}
func TestParseMacroWithNoDefinition(t *testing.T) {
str := "%fcitx5_version 5.0.1"
var m Macro
err := (&m).Parse(str)
if err != nil {
t.Error("[macro]parse a macro but the err is non-nil")
}
if m.Indicator != "" {
t.Errorf("[macro]parsed indicator is wrong, expected empty, got '%s'", m.Indicator)
}
if m.Name != "fcitx5_version" {
t.Errorf("[macro]parsed name is wrong, expected fcitx5_version, got '%s'", m.Name)
}
if m.Value != "5.0.1" {
t.Errorf("[macro]parsed value is wrong, expected 5.0.1, got '%s'", m.Value)
}
}
func TestParseMacroFile(t *testing.T) {
macros, err := parseMacroFile(strings.NewReader("%define fcitx5_version() %(fcitx5 -v)\n%fcitx5_name fcitx5"))
result := Macros{Macro{"%define", "function", item{"fcitx5_version()", "%(fcitx5 -v)", "", "", nil}}, Macro{"", "variable", item{"fcitx5_name", "fcitx5", "", "", nil}}}
if !reflect.DeepEqual(macros, result) || err != nil {
t.Error("[macro]parseMacroFile test failed")
}
}
func TestUpdateMacro(t *testing.T) {
m := Macro{"", "variable", item{"fcitx_name", "fcitx5", "", "", nil}}
m.Update("fcitx6")
if m.Value != "fcitx6" {
t.Error("[macro]update macro test failed")
}
}
func TestFindMacro(t *testing.T) {
m := Macro{"", "variable", item{"%fcitx_name", "fcitx5", "", "", nil}}
macros := Macros{m}
if macros.Find(m) < 0 {
t.Error("[macro]find macro test failed")
}
}
func TestConcatMacros(t *testing.T) {
macros := Macros{Macro{"", "variable", item{"%fcitx5_name", "fcitx5", "", "", nil}}}
macros1 := Macros{Macro{"", "variable", item{"%fcitx5_name", "fcitx6", "", "", nil}}}
macros.Concat(macros1)
if !reflect.DeepEqual(macros, macros1) {
t.Error("[macro]concat macros test failed")
}
}
func TestParseBuildConfig(t *testing.T) {
str := "%define gcc_version 5\nConflict: kiwi:systemd-mini\nMacros:\n%rubySTOP() %nil\n:Macros"
macros, err := parseBuildConfig(strings.NewReader(str))
result := Macros{Macro{"%define", "variable", item{"gcc_version", "5", "", "", nil}}, Macro{"", "function", item{"rubySTOP()", "%nil", "", "", nil}}}
if err != nil || !reflect.DeepEqual(macros, result) {
t.Error("[macro]parseBuildConfig test failed")
}
}
func TestTrim(t *testing.T) {
str := "%{%{version}}"
if trim(str) != "%{version}" {
t.Error("[macro]trim test failed")
}
}
func TestSplitConditionalMacro(t *testing.T) {
str := "%{?version}"
str, dft, ok := splitConditionalMacro(str)
if str != "version" || len(dft) != 0 || ok {
t.Error("[macro]splitConditionalMacro test failed")
}
}
func TestSplitConditionalMacroWithNoSymbol(t *testing.T) {
str := "%{version}"
str, dft, ok := splitConditionalMacro(str)
if str != "version" || len(dft) != 0 || ok {
t.Error("[macro]splitConditionalMacro no '!?' or '?' test failed")
}
}
func TestSplitConditionalMacroWithNonExistence(t *testing.T) {
str := "%{!?version}"
str, dft, ok := splitConditionalMacro(str)
if str != "version" || len(dft) != 0 || !ok {
t.Error("[macro]splitConditionalMacro with !? test failed")
}
}
func TestSplitConditionalMacroWithNonExistenceAndDefaultValue(t *testing.T) {
str := "%{!?version:5}"
str, dft, ok := splitConditionalMacro(str)
if str != "version" || dft != "5" || !ok {
t.Error("[macro]splitConditionalMacro with !? and default value test failed")
}
}
func TestCallShell(t *testing.T) {
if callShell("echo true") != "true" {
t.Error("[macro]callShell test failed")
}
}
func TestExpand(t *testing.T) {
str := "%{expand:%%{expand:%%{version}}}"
if expand(str) != "%{version}" {
t.Error("[macro]expand macro test failed")
}
}
func TestExpandWithNoExpand(t *testing.T) {
str := "%{version}"
if expand(str) != "%{version}" {
t.Error("[macro]expand macro with no expand test failed")
}
}
func TestExpandWithSingleExpand(t *testing.T) {
str := "%{expand:%%{version}}"
if expand(str) != "%{version}" {
t.Error("[macro]expand macro with single expand test failed")
}
}
func TestParseMacro(t *testing.T) {
var spec Specfile
token := NewTokenizer("Macro", "%define build_ada 1")
last := NewTokenizer("Comment", "This is a comment.")
ParseMacro(token, last, Macros{}, &spec)
if len(spec.Macros) != 1 || spec.Macros[0].Name != "build_ada" || spec.Macros[0].Value != "1" || spec.Macros[0].Comment != last.Content {
t.Error("[macro]ParseMacro test failed")
}
}