-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfwheels_test.go
525 lines (464 loc) Β· 14.4 KB
/
cfwheels_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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package cfw_test
import (
"fmt"
"log"
"testing"
"time"
"github.com/bengarrett/cfw"
)
func ExampleDeObfuscate() {
fmt.Println(cfw.DeObfuscate("9b1c6"))
// Output: 1
}
func TestDeObfuscate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
s string
want string
}{
// values:
// https://github.com/cfwheels/cfwheels/blob/1c3b9d6db79cdfbfbe49ae6816f6dc96262ccf82
// /wheels/tests/global/public/deobfuscateparam.cfc
{"empty", "", ""},
{"ok 1", "9b1c6", "1"},
{"ok 2", "eb77359232", "999999999"},
{"ok 3", "ac10a", "99"},
{"ok 4", "b226582", "15765"},
{"ok 5", "c06d44215", "69247541"},
{"ok 6", "ac10a", "99"},
{"error 1", "becca2515", "becca2515"},
{"error 2", "a15ba9", "a15ba9"},
{"error 3", "1111111111", "1111111111"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := cfw.DeObfuscate(tt.s); got != tt.want {
t.Errorf("DeObfuscate() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleExcerpt() {
pangram := "The quick brown fox jumps over the lazy dog"
fmt.Println(cfw.Excerpt(pangram, "...", "The quick brown fox", 0))
fmt.Println(cfw.Excerpt(pangram, "...", "", 19))
fmt.Println(cfw.Excerpt(pangram, "π¦", "The quick brown ", 0))
// Output: The quick brown fox...
// The quick brown fox...
// The quick brown π¦
}
func TestExcerpt(t *testing.T) {
t.Parallel()
type args struct {
s string
replace string
phrase string
n int
}
const (
s = "CFWheels: testing the excerpt view helper to see if it works or not."
test4 = "CFWheels: testing the excerpt view helper to see if it works or no[more]"
)
tests := []struct {
name string
args args
want string
}{
{"empty", args{"", "", "", 0}, "..."},
{"1", args{s, "[more]", "CFWheels: testing the excerpt", 0}, "CFWheels: testing the excerpt[more]"},
{"2", args{s, "[more]", "testing the excerpt", 0}, "[more]testing the excerpt[more]"},
{"3", args{s, "[more]", "excerpt view helper", 10}, "[more]sting the excerpt view helper to see if[more]"},
{"4", args{s, "[more]", "excerpt view helper", 25}, test4},
{"5", args{s, "[more]", "see if it works", 25}, "[more]e excerpt view helper to see if it works or not."},
{"6", args{s, "[more]", "jklsduiermobk", 25}, ""},
{"utf8", args{"The quick brown π¦ jumps over the lazy π", "π¬", "brown π¦", 0}, "π¬brown π¦π¬"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := cfw.Excerpt(tt.args.s, tt.args.replace, tt.args.phrase, tt.args.n); got != tt.want {
t.Errorf("Excerpt() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleHumanize() {
fmt.Println(cfw.Humanize("helloWorldExample", []string{}...))
fmt.Println(cfw.Humanize("golangModOrVgo?", []string{"MOD", "VGO"}...))
// Output: Hello World Example
// Golang MOD Or VGO?
}
func TestHumanize(t *testing.T) {
t.Parallel()
type args struct {
s string
except []string
}
tests := []struct {
name string
args args
want string
}{
// values
// https://github.com/cfwheels/cfwheels/blob/1c3b9d6db79cdfbfbe49ae6816f6dc96262ccf82
// /wheels/tests/global/public/humanize.cfc
{"empty", args{"", nil}, ""},
{"lcase", args{"wheelsIsAFramework", nil}, "Wheels Is A Framework"},
{"title", args{"WheelsIsAFramework", nil}, "Wheels Is A Framework"},
{"ucase", args{"CFML", []string{"CFML"}}, "CFML"},
{"except", args{"ACfmlFramework", []string{"CFML"}}, "A CFML Framework"},
{"err 1", args{"wheelsIsACFMLFramework", nil}, "Wheels Is ACFML Framework"},
{"same", args{"Some Input", nil}, "Some Input"},
{"emoji", args{"theQuickBrownπ¦JumpsOverTheLazyπ", nil}, "The Quick Brownπ¦ Jumps Over The Lazyπ"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := cfw.Humanize(tt.args.s, tt.args.except...); got != tt.want {
t.Errorf("Humanize() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleHyphenize() {
fmt.Println(cfw.Hyphenize("aTourOfGo"))
// Output: a-tour-of-go
}
func TestHyphenize(t *testing.T) {
t.Parallel()
tests := []struct {
name string
s string
want string
}{
// values
// https://github.com/cfwheels/cfwheels/blob/13b9cb72f52a4edc7981ae572d050f8116af57ef
// /wheels/tests/global/strings.cfc
{"empty", "", ""},
{"ok 1", "wheelsIsAFramework", "wheels-is-a-framework"},
{"ok 2", "WheelsIsAFramework", "wheels-is-a-framework"},
{"ok 3", "aURLVariable", "a-url-variable"},
{"ok 4", "URLVariable", "url-variable"},
{"ucase", "ERRORMESSAGE", "errormessage"},
{"lcase", "address", "address"},
{"emoji", "TheQuickBrownπ¦JumpsOverTheLazyπ", "the-quick-brownπ¦-jumps-over-the-lazyπ"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := cfw.Hyphenize(tt.s); got != tt.want {
t.Errorf("Hyphenize() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleObfuscate() {
fmt.Println(cfw.Obfuscate("1"))
fmt.Println(cfw.Obfuscate("5551234"))
// Output: 9b1c6
// b3da865e
}
func TestObfuscate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
s string
want string
}{
// values
// https://github.com/cfwheels/cfwheels/blob/1c3b9d6db79cdfbfbe49ae6816f6dc96262ccf82
// /wheels/tests/global/public/obfuscateparam.cfc
{"empty", "", ""},
{"", "999999999", "eb77359232"},
{"", "0162823571", "0162823571"},
{"", "1", "9b1c6"},
{"", "99", "ac10a"},
{"", "15765", "b226582"},
{"", "69247541", "c06d44215"},
{"", "0413", "0413"},
{"", "per", "per"},
// in CFWheels this test fails but in Go it returns a429646180a
// {"", "1111111111", "1111111111"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := cfw.Obfuscate(tt.s); got != tt.want {
t.Errorf("Obfuscate() = %v, want %v", got, tt.want)
}
})
}
}
func TestReverseInt(t *testing.T) {
t.Parallel()
tests := []struct {
name string
i int
want int
wantErr bool
}{
{"zero", 0, 0, false},
{"ok", 2345678, 8765432, false},
{"505", 505, 505, false},
{"leading 0", 0o005, 5, false}, // expected behaviour?
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, err := cfw.ReverseInt(tt.i)
if (err != nil) != tt.wantErr {
t.Errorf("reverseInt() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("reverseInt() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleStripLinks() {
fmt.Println(cfw.StripLinks(`<a href="https://golang.org">The Go Programming Language</a>.`))
// Output: The Go Programming Language.
}
func TestStripLinks(t *testing.T) {
// values:
// https://github.com/cfwheels/cfwheels/blob/9eea7a6ac77956c8825e037f2e3e6b8c0f346267
// /wheels/tests/view/sanitize/striptags.cfc
t.Parallel()
str := `this <a href="http://www.google.com" title="google">` +
`is</a> a <a href="mailto:someone@example.com" title="invalid email">` +
`test</a> to <a name="anchortag">see</a> if this works or not.`
emoji := `The quick <b><a href="https://example.com">brown π¦</a></b> jumps over the lazy π`
tests := []struct {
name string
s string
want string
}{
{"empty", "", ""},
{"string", str, "this is a test to see if this works or not."},
{"emoji", emoji, "The quick <b>brown π¦</b> jumps over the lazy π"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := cfw.StripLinks(tt.s); got != tt.want {
t.Errorf("StripLinks() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleStripTags() {
fmt.Println(cfw.StripTags(`<h1><a href="https://golang.org">The Go Programming Language</a>.</h1>`))
// Output: The Go Programming Language.
}
func TestStripTags(t *testing.T) {
// values:
// https://github.com/cfwheels/cfwheels/blob/1c3b9d6db79cdfbfbe49ae6816f6dc96262ccf82
// /wheels/tests/view/text/excerpt.cfc
t.Parallel()
str := `<h1>this</h1><p><a href="http://www.google.com" title="google">is</a></p><p>a ` +
`<a href="mailto:someone@example.com" title="invalid email">test</a> to<br>` +
`<a name="anchortag">see</a> if this works or not.</p>`
emoji := `The quick <b><a href="https://example.com">brown π¦</a></b> jumps over the lazy π`
tests := []struct {
name string
s string
want string
}{
{"empty", "", ""},
{"string", str, "thisisa test tosee if this works or not."},
{"emoji", emoji, "The quick brown π¦ jumps over the lazy π"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := cfw.StripTags(tt.s); got != tt.want {
t.Errorf("StripTags() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleTimeDistance() {
const layout = "2006 Jan 2"
f, err := time.Parse(layout, "2000 Jan 1")
if err != nil {
log.Fatal(err)
}
t, err := time.Parse(layout, "2020 Jun 30")
if err != nil {
log.Fatal(err)
}
fmt.Println(cfw.TimeDistance(f, t, false))
// Output: over 20 years
}
func ExampleTimeDistance_seconds() {
f, err := time.Parse(time.RFC3339, "2006-01-02T15:04:06+07:00")
if err != nil {
log.Fatal(err)
}
t, err := time.Parse(time.RFC3339, "2006-01-02T15:04:10+07:00")
if err != nil {
log.Fatal(err)
}
fmt.Println(cfw.TimeDistance(f, t, true))
// Output: less than 5 seconds
}
func TestTimeDistance(t *testing.T) {
t.Parallel()
type args struct {
from time.Time
to time.Time
sec bool
}
n := time.Now()
tests := []struct {
name string
args args
want string
}{
{"empty", args{time.Time{}, time.Time{}, false}, "less than a minute"},
{"<1m", args{n, n.Add(time.Second * time.Duration(5-1)), false}, "less than a minute"},
{"<10s", args{n, n.Add(time.Second * time.Duration(10-1)), true}, "less than 10 seconds"},
{"<20s", args{n, n.Add(time.Second * time.Duration(20-1)), true}, "less than 20 seconds"},
{"1/2min", args{n, n.Add(time.Second * time.Duration(40-1)), true}, "half a minute"},
{"<1min", args{n, n.Add(time.Second * time.Duration(59)), false}, "less than a minute"},
{"1min", args{n, n.Add(time.Second * time.Duration(60+50)), false}, "1 minute"},
{"44min", args{n, n.Add(time.Minute * time.Duration(45-1)), false}, "44 minutes"},
{"1h", args{n, n.Add(time.Minute * time.Duration(90-1)), false}, "about 1 hour"},
{"23h", args{n, n.Add(time.Minute * time.Duration(1440-1)), false}, "about 23 hours"},
{"1d", args{n, n.Add(time.Minute * time.Duration(2880-1)), false}, "1 day"},
{"29d", args{n, n.Add(time.Minute * time.Duration(43200-1)), false}, "29 days"},
{"1m", args{n, n.Add(time.Minute * time.Duration(86400-1)), false}, "about 1 month"},
{"11m", args{n, n.Add(time.Minute * time.Duration(525600-1)), false}, "11 months"},
{"1y", args{n, n.Add(time.Minute * time.Duration(657000-1)), false}, "about 1 year"},
{">1y", args{n, n.Add(time.Minute * time.Duration(919800-1)), false}, "over 1 year"},
{"2y", args{n, n.Add(time.Minute * time.Duration(1051200-1)), false}, "almost 2 years"},
{">2y", args{n, n.Add(time.Minute * time.Duration(1051200)), false}, "over 2 years"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := cfw.TimeDistance(tt.args.from, tt.args.to, tt.args.sec); got != tt.want {
t.Errorf("TimeDistance() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleTruncate() {
fmt.Println(cfw.Truncate("Go is an open source programming language", "", 8))
fmt.Println(cfw.Truncate("Go is an open source programming language", "?", 6))
// Output: Go is...
// Go is?
}
func TestTruncate(t *testing.T) {
t.Parallel()
type args struct {
s string
replace string
n int
}
tests := []struct {
name string
args args
want string
}{
// values:
// https://github.com/cfwheels/cfwheels/blob/9738bd71e7b8632587cd09a0a2d7d3e779700a7a
// /wheels/tests/view/text/truncate.cfc
{"empty", args{"", "", 0}, ""},
{"ok1", args{"this is a test to see if this works or not.", "[more]", 20}, "this is a test[more]"},
{"err1", args{"", "[more]", 20}, ""},
{"ok2", args{"this is a test to see if this works or not.", "", 20}, "this is a test to..."},
{"emoji", args{"The quick brown π¦ jumps over the lazy π", "π¬", 21}, "The quick brown π¦π¬"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := cfw.Truncate(tt.args.s, tt.args.replace, tt.args.n); got != tt.want {
t.Errorf("Truncate() = %v, want %v", got, tt.want)
}
})
}
}
func ExampleWordTruncate() {
fmt.Println(cfw.WordTruncate("Go is an open source programming language", "", 2))
fmt.Println(cfw.WordTruncate("Go is an open source programming language", "?", 2))
// Output: Go is...
// Go is?
}
func TestWordTruncate(t *testing.T) {
t.Parallel()
type args struct {
s string
replace string
n int
}
tests := []struct {
name string
args args
want string
}{
// values:
// https://github.com/cfwheels/cfwheels/blob/9738bd71e7b8632587cd09a0a2d7d3e779700a7a
// /wheels/tests/view/text/wordtruncate.cfc
{"empty", args{"", "", 0}, ""},
{"ok", args{"CFWheels is a framework for ColdFusion", "", 4}, "CFWheels is a framework..."},
{"emoji", args{"The quick brown π¦ jumps over the lazy π", "π¬", 4}, "The quick brown π¦π¬"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := cfw.WordTruncate(tt.args.s, tt.args.replace, tt.args.n); got != tt.want {
t.Errorf("WordTruncate() = %v, want %v", got, tt.want)
}
})
}
}
// Test unique examples used in README.md.
func TestReadmeExamples(t *testing.T) {
t.Parallel()
var a, e, s string
s = `Go to the <strong><a href="https://github.com/bengarrett/cfw">GitHub</a></strong> repo!`
a = cfw.StripLinks(s)
e = "Go to the <strong>GitHub</strong> repo!"
if a != e {
t.Errorf("mismatch, got: %v, want: %v", a, e)
}
a = cfw.StripTags(s)
e = "Go to the GitHub repo!"
if a != e {
t.Errorf("mismatch, got: %v, want: %v", a, e)
}
from := time.Now()
to := from.Add(time.Second * time.Duration(7)) // add 7 seconds
a = cfw.TimeDistance(from, to, true)
e = "less than 10 seconds"
if a != e {
t.Errorf("mismatch, got: %v, want: %v", a, e)
}
s = `CFW contains Go ports of a few selected CFWheels helpers.`
a = cfw.Truncate(s, "", 15)
e = "CFW contains..."
if a != e {
t.Errorf("mismatch, got: %v, want: %v", a, e)
}
s = `CFW contains Go ports of a few selected CFWheels helpers.`
a = cfw.WordTruncate(s, "", 4)
e = "CFW contains Go ports..."
if a != e {
t.Errorf("mismatch, got: %v, want: %v", a, e)
}
}