-
Notifications
You must be signed in to change notification settings - Fork 8
/
find_utils_test.go
501 lines (430 loc) · 14.1 KB
/
find_utils_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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package test_main
import (
"fmt"
"strings"
"testing"
. "github.com/bjartek/overflow"
"github.com/hexops/autogold"
"github.com/onflow/cadence"
"github.com/sanity-io/litter"
"github.com/stretchr/testify/assert"
)
func TestFindUtils(t *testing.T) {
otu := NewOverflowTest(t)
o := otu.O
devCheckContainsChar := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(string: String, char:Character) : Bool {
return FindUtils.containsChar(string, char: char)
}`
// containsChar
t.Run("containsChar should return false if string does not contain", func(t *testing.T) {
o.Script(devCheckContainsChar,
WithArg("string", "bam.find"),
WithArg("char", cadence.Character(",")),
).
AssertWant(t, autogold.Want("false", false))
})
t.Run("containsChar should return true if string contains", func(t *testing.T) {
o.Script(devCheckContainsChar,
WithArg("string", "bam.find"),
WithArg("char", cadence.Character(".")),
).
AssertWant(t, autogold.Want("true", true))
})
devCheckContains := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(string: String, element:String) : Bool {
return FindUtils.contains(string, element: element)
}`
// contains
t.Run("contains should be able to check contains", func(t *testing.T) {
o.Script(devCheckContains,
WithArg("string", "string"),
WithArg("element", "string"),
).
AssertWant(t, autogold.Want("true", true))
})
t.Run("contains should return false if string does not contain", func(t *testing.T) {
o.Script(devCheckContains,
WithArg("string", "string"),
WithArg("element", "stt"),
).
AssertWant(t, autogold.Want("false", false))
})
t.Run("contains should return true if string partially contains", func(t *testing.T) {
o.Script(devCheckContains,
WithArg("string", "string"),
WithArg("element", "ing"),
).
AssertWant(t, autogold.Want("true", true))
})
t.Run("contains should return false if element is longer than string", func(t *testing.T) {
o.Script(devCheckContains,
WithArg("string", "string"),
WithArg("element", "substring"),
).
AssertWant(t, autogold.Want("false", false))
})
t.Run("contains should return true if element is empty string", func(t *testing.T) {
o.Script(devCheckContains,
WithArg("string", "string"),
WithArg("element", `""`),
).
AssertWant(t, autogold.Want("true", true))
})
t.Run("contains should return false if string is empty string", func(t *testing.T) {
o.Script(devCheckContains,
WithArg("string", `""`),
WithArg("element", "string"),
).
AssertWant(t, autogold.Want("false", false))
})
devCheckHasSuffix := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(string: String, suffix:String) : Bool {
return FindUtils.hasSuffix(string, suffix: suffix)
}`
// hasSuffix
t.Run("hasSuffix should return true if suffix is valid", func(t *testing.T) {
o.Script(devCheckHasSuffix,
WithArg("string", "bam.find"),
WithArg("suffix", ".find"),
).
AssertWant(t, autogold.Want("true", true))
})
t.Run("hasSuffix should return false if suffix is not valid", func(t *testing.T) {
o.Script(devCheckHasSuffix,
WithArg("string", "bam.find"),
WithArg("suffix", "..find"),
).
AssertWant(t, autogold.Want("false", false))
})
t.Run("hasSuffix should return true if entire string is passed in", func(t *testing.T) {
o.Script(devCheckHasSuffix,
WithArg("string", "bam.find"),
WithArg("suffix", "bam.find"),
).
AssertWant(t, autogold.Want("true", true))
})
t.Run("hasSuffix should return true if empty string is passed in", func(t *testing.T) {
o.Script(devCheckHasSuffix,
WithArg("string", "bam.find"),
WithArg("suffix", `""`),
).
AssertWant(t, autogold.Want("true", true))
})
t.Run("hasSuffix should return false if suffix is longer than string", func(t *testing.T) {
o.Script(devCheckHasSuffix,
WithArg("string", "bam.find"),
WithArg("suffix", "bambambambambam"),
).
AssertWant(t, autogold.Want("false", false))
})
devCheckHasPrefix := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(string: String, prefix:String) : Bool {
return FindUtils.hasPrefix(string, prefix: prefix)
}`
// hasPrefix
t.Run("hasPrefix should return true if prefix is valid", func(t *testing.T) {
o.Script(devCheckHasPrefix,
WithArg("string", "bam.find"),
WithArg("prefix", "bam."),
).
AssertWant(t, autogold.Want("true", true))
})
t.Run("hasPrefix should return false if prefix is not valid", func(t *testing.T) {
o.Script(devCheckHasPrefix,
WithArg("string", "bam.find"),
WithArg("prefix", "bamm"),
).
AssertWant(t, autogold.Want("false", false))
})
t.Run("hasPrefix should return true if entire string is passed in", func(t *testing.T) {
o.Script(devCheckHasPrefix,
WithArg("string", "bam.find"),
WithArg("prefix", "bam.find"),
).
AssertWant(t, autogold.Want("true", true))
})
t.Run("hasPrefix should return true if empty string is passed in", func(t *testing.T) {
o.Script(devCheckHasPrefix,
WithArg("string", "bam.find"),
WithArg("prefix", `""`),
).
AssertWant(t, autogold.Want("true", true))
})
t.Run("hasPrefix should return false if prefix is longer than string", func(t *testing.T) {
o.Script(devCheckHasPrefix,
WithArg("string", "bam.find"),
WithArg("prefix", "bambambambambam"),
).
AssertWant(t, autogold.Want("false", false))
})
t.Run("hasPrefix should return false if prefix is longer than string", func(t *testing.T) {
o.Script(devCheckHasPrefix,
WithArg("string", "bam.find"),
WithArg("prefix", "bambambambambam"),
).
AssertWant(t, autogold.Want("false", false))
})
devCheckToUpper := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(string: String) : String {
return FindUtils.toUpper(string)
}
`
// toUpper
t.Run("toUpper should return upper cases", func(t *testing.T) {
s := "bam.find"
o.Script(devCheckToUpper,
WithArg("string", s),
).
AssertWant(t, autogold.Want("should return upper cases", strings.ToUpper(s)))
})
t.Run("toUpper should return upper cases if they are already upper cases", func(t *testing.T) {
s := "bam.find"
o.Script(devCheckToUpper,
WithArg("string", strings.ToUpper(s)),
).
AssertWant(t, autogold.Want("if they are already upper cases", strings.ToUpper(s)))
})
devCheckFirstUpperLetter := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(string: String) : String {
return FindUtils.firstUpperLetter(string)
}
`
// first Upper Letter
t.Run("firstUpperLetter should return upper case for first letter", func(t *testing.T) {
s := "Bam.find"
o.Script(devCheckFirstUpperLetter,
WithArg("string", strings.ToLower(s)),
).
AssertWant(t, autogold.Want("should return upper case for first letter", s))
})
t.Run("firstUpperLetter should returns same if first letter is already upper case", func(t *testing.T) {
s := "Bam.find"
o.Script(devCheckFirstUpperLetter,
WithArg("string", s),
).
AssertWant(t, autogold.Want("should returns same if first letter is already upper case", s))
})
t.Run("firstUpperLetter should returns same if first letter is not alphabet", func(t *testing.T) {
s := ".Bam.find"
o.Script(devCheckFirstUpperLetter,
WithArg("string", s),
).
AssertWant(t, autogold.Want("should returns same if first letter is already upper case", s))
})
dev_check_to_snake_case := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(string: String) : String {
return FindUtils.to_snake_case(string)
}
`
// to_snake_case
t.Run("to_snake_case should returns snake case CamelCase", func(t *testing.T) {
s := "CamelCase"
o.Script(dev_check_to_snake_case,
WithArg("string", s),
).
AssertWant(t, autogold.Want("CamelCase", "camel_case"))
})
t.Run("to_snake_case should returns snake case Camel Case", func(t *testing.T) {
s := "Camel Case"
o.Script(dev_check_to_snake_case,
WithArg("string", s),
).
AssertWant(t, autogold.Want("Camel Case", "camel_case"))
})
t.Run("to_snake_case should returns snake case Camel case", func(t *testing.T) {
s := "Camel case"
o.Script(dev_check_to_snake_case,
WithArg("string", s),
).
AssertWant(t, autogold.Want("Camel case", "camel_case"))
})
devCheckToCamelCase := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(string: String) : String {
return FindUtils.toCamelCase(string)
}
`
// toCamelCase
t.Run("toCamelCase should returns camel case Camel case", func(t *testing.T) {
s := "Camel case"
o.Script(devCheckToCamelCase,
WithArg("string", s),
).
AssertWant(t, autogold.Want("CamelCase", "camelCase"))
})
t.Run("toCamelCase should returns camel case Camel Case", func(t *testing.T) {
s := "Camel Case"
o.Script(devCheckToCamelCase,
WithArg("string", s),
).
AssertWant(t, autogold.Want("Camel Case", "camelCase"))
})
t.Run("toCamelCase should returns camel case Camel_case", func(t *testing.T) {
s := "Camel_case"
o.Script(devCheckToCamelCase,
WithArg("string", s),
).
AssertWant(t, autogold.Want("Camel case", "camelCase"))
})
t.Run("toCamelCase should returns camel case Camel_Case", func(t *testing.T) {
s := "Camel_Case"
o.Script(devCheckToCamelCase,
WithArg("string", s),
).
AssertWant(t, autogold.Want("Camel case", "camelCase"))
})
devTrimSuffix := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(string: String, suffix:String) : String {
return FindUtils.trimSuffix(string, suffix:suffix)
}
`
t.Run("trimSuffix should trim bam.find", func(t *testing.T) {
o.Script(devTrimSuffix,
WithArg("string", "bam.find"),
WithArg("suffix", ".find"),
).
AssertWant(t, autogold.Want("trimSuffix : bam.find", "bam"))
})
t.Run("trimSuffix should not trim christian.fine", func(t *testing.T) {
o.Script(devTrimSuffix,
WithArg("string", "christian.fine"),
WithArg("suffix", ".find"),
).
AssertWant(t, autogold.Want("trimSuffix : christian.fine", "christian.fine"))
})
t.Run("trimSuffix should not trim bam", func(t *testing.T) {
o.Script(devTrimSuffix,
WithArg("string", "bam"),
WithArg("suffix", ".find"),
).
AssertWant(t, autogold.Want("trimSuffix : bam", "bam"))
})
// Extra tests on trimFindSuffix on FIND
devTrimFindSuffix := `import FIND from "../contracts/FIND.cdc"
pub fun main(name: String) : String {
return FIND.trimFindSuffix(name)
}
`
t.Run("trimFindSuffix should trim bam.find", func(t *testing.T) {
o.Script(devTrimFindSuffix,
WithArg("name", "bam.find"),
).
AssertWant(t, autogold.Want("trimFindSuffix : bam.find", "bam"))
})
t.Run("trimFindSuffix should return bam", func(t *testing.T) {
o.Script(devTrimFindSuffix,
WithArg("name", "bam"),
).
AssertWant(t, autogold.Want("trimFindSuffix : bam", "bam"))
})
// splitString
devSplitString := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(string: String, sep: Character) : [String] {
return FindUtils.splitString(string, sep:sep)
}
`
t.Run("splitString should split Hello_World_And_All", func(t *testing.T) {
o.Script(devSplitString,
WithArg("string", "Hello_World_And_All"),
WithArg("sep", cadence.Character("_")),
).
AssertWant(t, autogold.Want("splitString : Hello_World_And_All", litter.Sdump([]interface{}{"Hello", "World", "And", "All"})))
})
t.Run("splitString should split Hello_World.And_All", func(t *testing.T) {
o.Script(devSplitString,
WithArg("string", "Hello_World.And_All"),
WithArg("sep", cadence.Character("_")),
).
AssertWant(t, autogold.Want("splitString : Hello_World.And_All", litter.Sdump([]interface{}{"Hello", "World.And", "All"})))
})
t.Run("splitString should not split Hello.World.And.All", func(t *testing.T) {
o.Script(devSplitString,
WithArg("string", "Hello.World.And.All"),
WithArg("sep", cadence.Character("_")),
).
AssertWant(t, autogold.Want("splitString : Hello.World.And.All", litter.Sdump([]interface{}{"Hello.World.And.All"})))
})
t.Run("splitString should not split HelloWorldAndAll", func(t *testing.T) {
o.Script(devSplitString,
WithArg("string", "HelloWorldAndAll"),
WithArg("sep", cadence.Character("_")),
).
AssertWant(t, autogold.Want("splitString : HelloWorldAndAll", litter.Sdump([]interface{}{"HelloWorldAndAll"})))
})
// joinMapToString
devJoinMapToString := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(map: {String : String}) : String {
return FindUtils.joinMapToString(map)
}
`
t.Run("joinMapToString should join the map and return as expected", func(t *testing.T) {
o.Script(devJoinMapToString,
WithArg("map", map[string]string{
"1+1": "2",
"2+2": "4",
"3*3": "9",
}),
).
AssertWant(t, autogold.Want("joinMapToString : 1+1=2 2+2=4 3*3=9", "2+2=4 3*3=9 1+1=2"))
})
// joinString
devJoinString := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(s: [String], sep: String) : String {
return FindUtils.joinString(s, sep:sep)
}
`
t.Run("devJoinString should join the string with sep and return as expected", func(t *testing.T) {
o.Script(devJoinString,
WithArg("s", []string{
"1+1=2",
"2+2=4",
"3*3=9",
}),
WithArg("sep", " "),
).
AssertWant(t, autogold.Want("devJoinString : 1+1=2 2+2=4 3*3=9", "1+1=2 2+2=4 3*3=9"))
})
// deDupTypeArray
devDeDupTypeArray := `import FindUtils from "../contracts/FindUtils.cdc"
pub fun main(s: [String]) : [Type] {
var typ : [Type] = []
for t in s {
typ.append(CompositeType(t)!)
}
return FindUtils.deDupTypeArray(typ)
}
`
t.Run("deDupTypeArray should dedup duplicated as expected", func(t *testing.T) {
flow, err := o.QualifiedIdentifier("FlowToken", "Vault")
assert.NoError(t, err)
ft, err := o.QualifiedIdentifier("FungibleToken", "Vault")
assert.NoError(t, err)
nft, err := o.QualifiedIdentifier("NonFungibleToken", "NFT")
assert.NoError(t, err)
collection, err := o.QualifiedIdentifier("NonFungibleToken", "Collection")
assert.NoError(t, err)
o.Script(devDeDupTypeArray,
WithArg("s", []string{
flow,
ft,
nft,
nft,
collection,
flow,
flow,
flow,
ft,
ft,
ft,
nft,
nft,
}),
).
AssertWant(t, autogold.Want("deDupTypeArray : flow, ft, nft, collection", fmt.Sprintf(`[]interface {}{
"%s",
"%s",
"%s",
"%s",
}`, flow, ft, nft, collection)))
})
}