Debian Codegraph @

tpl/strings/strings_test.go

raw
1 // Copyright 2017 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package strings
15
16 import (
17 "html/template"
18 stds "strings"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 "github.com/gohugoio/hugo/config/testconfig"
23 "github.com/gohugoio/hugo/deps"
24 "github.com/spf13/cast"
25 )
26
27 var ns = New(&deps.Deps{
28 Conf: testconfig.GetTestConfig(nil, nil),
29 })
30
31 type tstNoStringer struct{}
32
33 func TestChomp(t *testing.T) {
34 t.Parallel()
35 c := qt.New(t)
36
37 for _, test := range []struct {
38 s any
39 expect any
40 }{
41 {"\n a\n", "\n a"},
42 {"\n a\n\n", "\n a"},
43 {"\n a\r\n", "\n a"},
44 {"\n a\n\r\n", "\n a"},
45 {"\n a\r\r", "\n a"},
46 {"\n a\r", "\n a"},
47 // errors
48 {tstNoStringer{}, false},
49 } {
50
51 result, err := ns.Chomp(test.s)
52
53 if b, ok := test.expect.(bool); ok && !b {
54 c.Assert(err, qt.Not(qt.IsNil))
55 continue
56 }
57
58 c.Assert(err, qt.IsNil)
59 c.Assert(result, qt.Equals, test.expect)
60
61 // repeat the check with template.HTML input
62 result, err = ns.Chomp(template.HTML(cast.ToString(test.s)))
63 c.Assert(err, qt.IsNil)
64 c.Assert(result, qt.Equals, template.HTML(cast.ToString(test.expect)))
65 }
66 }
67
68 func TestContains(t *testing.T) {
69 t.Parallel()
70 c := qt.New(t)
71
72 for _, test := range []struct {
73 s any
74 substr any
75 expect bool
76 isErr bool
77 }{
78 {"", "", true, false},
79 {"123", "23", true, false},
80 {"123", "234", false, false},
81 {"123", "", true, false},
82 {"", "a", false, false},
83 {123, "23", true, false},
84 {123, "234", false, false},
85 {123, "", true, false},
86 {template.HTML("123"), []byte("23"), true, false},
87 {template.HTML("123"), []byte("234"), false, false},
88 {template.HTML("123"), []byte(""), true, false},
89 // errors
90 {"", tstNoStringer{}, false, true},
91 {tstNoStringer{}, "", false, true},
92 } {
93
94 result, err := ns.Contains(test.s, test.substr)
95
96 if test.isErr {
97 c.Assert(err, qt.Not(qt.IsNil))
98 continue
99 }
100
101 c.Assert(err, qt.IsNil)
102 c.Assert(result, qt.Equals, test.expect)
103 }
104 }
105
106 func TestContainsAny(t *testing.T) {
107 t.Parallel()
108 c := qt.New(t)
109
110 for _, test := range []struct {
111 s any
112 substr any
113 expect bool
114 isErr bool
115 }{
116 {"", "", false, false},
117 {"", "1", false, false},
118 {"", "123", false, false},
119 {"1", "", false, false},
120 {"1", "1", true, false},
121 {"111", "1", true, false},
122 {"123", "789", false, false},
123 {"123", "729", true, false},
124 {"a☺b☻c☹d", "uvw☻xyz", true, false},
125 {1, "", false, false},
126 {1, "1", true, false},
127 {111, "1", true, false},
128 {123, "789", false, false},
129 {123, "729", true, false},
130 {[]byte("123"), template.HTML("789"), false, false},
131 {[]byte("123"), template.HTML("729"), true, false},
132 {[]byte("a☺b☻c☹d"), template.HTML("uvw☻xyz"), true, false},
133 // errors
134 {"", tstNoStringer{}, false, true},
135 {tstNoStringer{}, "", false, true},
136 } {
137
138 result, err := ns.ContainsAny(test.s, test.substr)
139
140 if test.isErr {
141 c.Assert(err, qt.Not(qt.IsNil))
142 continue
143 }
144
145 c.Assert(err, qt.IsNil)
146 c.Assert(result, qt.Equals, test.expect)
147 }
148 }
149
150 func TestContainsNonSpace(t *testing.T) {
151 t.Parallel()
152 c := qt.New(t)
153
154 for _, test := range []struct {
155 s any
156 expect bool
157 isErr bool
158 }{
159 {"", false, false},
160 {" ", false, false},
161 {" ", false, false},
162 {"\t", false, false},
163 {"\r", false, false},
164 {"a", true, false},
165 {" a", true, false},
166 {"a\n", true, false},
167 // error
168 {tstNoStringer{}, false, true},
169 } {
170
171 result, err := ns.ContainsNonSpace(test.s)
172
173 if test.isErr {
174 c.Assert(err, qt.IsNotNil)
175 continue
176 }
177
178 c.Assert(err, qt.IsNil)
179 c.Assert(result, qt.Equals, test.expect)
180
181 }
182 }
183
184 func TestCountRunes(t *testing.T) {
185 t.Parallel()
186 c := qt.New(t)
187
188 for _, test := range []struct {
189 s any
190 expect any
191 }{
192 {"foo bar", 6},
193 {"旁边", 2},
194 {`<div class="test">旁边</div>`, 2},
195 // errors
196 {tstNoStringer{}, false},
197 } {
198
199 result, err := ns.CountRunes(test.s)
200
201 if b, ok := test.expect.(bool); ok && !b {
202 c.Assert(err, qt.Not(qt.IsNil))
203 continue
204 }
205
206 c.Assert(err, qt.IsNil)
207 c.Assert(result, qt.Equals, test.expect)
208 }
209 }
210
211 func TestRuneCount(t *testing.T) {
212 t.Parallel()
213 c := qt.New(t)
214
215 for _, test := range []struct {
216 s any
217 expect any
218 }{
219 {"foo bar", 7},
220 {"旁边", 2},
221 {`<div class="test">旁边</div>`, 26},
222 // errors
223 {tstNoStringer{}, false},
224 } {
225
226 result, err := ns.RuneCount(test.s)
227
228 if b, ok := test.expect.(bool); ok && !b {
229 c.Assert(err, qt.Not(qt.IsNil))
230 continue
231 }
232
233 c.Assert(err, qt.IsNil)
234 c.Assert(result, qt.Equals, test.expect)
235 }
236 }
237
238 func TestCountWords(t *testing.T) {
239 t.Parallel()
240 c := qt.New(t)
241
242 for _, test := range []struct {
243 s any
244 expect any
245 }{
246 {"Do Be Do Be Do", 5},
247 {"旁边", 2},
248 {`<div class="test">旁边</div>`, 2},
249 {"Here's to you...", 3},
250 {"Here’s to you...", 3},
251 {"Here’s to you…", 3},
252 // errors
253 {tstNoStringer{}, false},
254 } {
255
256 result, err := ns.CountWords(test.s)
257
258 if b, ok := test.expect.(bool); ok && !b {
259 c.Assert(err, qt.Not(qt.IsNil))
260 continue
261 }
262
263 c.Assert(err, qt.IsNil)
264 c.Assert(result, qt.Equals, test.expect)
265 }
266 }
267
268 func TestHasPrefix(t *testing.T) {
269 t.Parallel()
270 c := qt.New(t)
271
272 for _, test := range []struct {
273 s any
274 prefix any
275 expect any
276 isErr bool
277 }{
278 {"abcd", "ab", true, false},
279 {"abcd", "cd", false, false},
280 {template.HTML("abcd"), "ab", true, false},
281 {template.HTML("abcd"), "cd", false, false},
282 {template.HTML("1234"), 12, true, false},
283 {template.HTML("1234"), 34, false, false},
284 {[]byte("abcd"), "ab", true, false},
285 // errors
286 {"", tstNoStringer{}, false, true},
287 {tstNoStringer{}, "", false, true},
288 } {
289
290 result, err := ns.HasPrefix(test.s, test.prefix)
291
292 if test.isErr {
293 c.Assert(err, qt.Not(qt.IsNil))
294 continue
295 }
296
297 c.Assert(err, qt.IsNil)
298 c.Assert(result, qt.Equals, test.expect)
299 }
300 }
301
302 func TestHasSuffix(t *testing.T) {
303 t.Parallel()
304 c := qt.New(t)
305
306 for _, test := range []struct {
307 s any
308 suffix any
309 expect any
310 isErr bool
311 }{
312 {"abcd", "cd", true, false},
313 {"abcd", "ab", false, false},
314 {template.HTML("abcd"), "cd", true, false},
315 {template.HTML("abcd"), "ab", false, false},
316 {template.HTML("1234"), 34, true, false},
317 {template.HTML("1234"), 12, false, false},
318 {[]byte("abcd"), "cd", true, false},
319 // errors
320 {"", tstNoStringer{}, false, true},
321 {tstNoStringer{}, "", false, true},
322 } {
323
324 result, err := ns.HasSuffix(test.s, test.suffix)
325
326 if test.isErr {
327 c.Assert(err, qt.Not(qt.IsNil))
328 continue
329 }
330
331 c.Assert(err, qt.IsNil)
332 c.Assert(result, qt.Equals, test.expect)
333 }
334 }
335
336 func TestReplace(t *testing.T) {
337 t.Parallel()
338 c := qt.New(t)
339
340 for _, test := range []struct {
341 s any
342 old any
343 new any
344 limit any
345 expect any
346 }{
347 {"aab", "a", "b", nil, "bbb"},
348 {"11a11", 1, 2, nil, "22a22"},
349 {12345, 1, 2, nil, "22345"},
350 {"aab", "a", "b", 1, "bab"},
351 {"11a11", 1, 2, 2, "22a11"},
352 // errors
353 {tstNoStringer{}, "a", "b", nil, false},
354 {"a", tstNoStringer{}, "b", nil, false},
355 {"a", "b", tstNoStringer{}, nil, false},
356 } {
357
358 var (
359 result string
360 err error
361 )
362
363 if test.limit != nil {
364 result, err = ns.Replace(test.s, test.old, test.new, test.limit)
365 } else {
366 result, err = ns.Replace(test.s, test.old, test.new)
367 }
368
369 if b, ok := test.expect.(bool); ok && !b {
370 c.Assert(err, qt.Not(qt.IsNil))
371 continue
372 }
373
374 c.Assert(err, qt.IsNil)
375 c.Assert(result, qt.Equals, test.expect)
376 }
377 }
378
379 func TestReplacePairs(t *testing.T) {
380 t.Parallel()
381 c := qt.New(t)
382
383 for _, test := range []struct {
384 args []any
385 expect string
386 }{
387 // slice form
388 {[]any{[]string{"a", "b"}, "aab"}, "bbb"},
389 {[]any{[]string{"a", "b", "b", "c"}, "aab"}, "bbc"},
390 {[]any{[]string{"app", "pear", "apple", "orange"}, "apple"}, "pearle"},
391 {[]any{[]string{}, "aab"}, "aab"},
392 {[]any{[]string{"remove-me", ""}, "text remove-me"}, "text "},
393 {[]any{[]string{"", "X"}, "ab"}, "XaXbX"},
394 {[]any{[]string{"a", "b"}, template.HTML("aab")}, "bbb"}, // template.HTML source
395 {[]any{[]string{"a", "b"}, 42}, "42"}, // int source (cast: 42→"42")
396 {[]any{[]any{"a", "b"}, "s"}, "s"}, // []any with all strings
397 {[]any{[]any{1, "one"}, "1abc"}, "oneabc"}, // []any with int pair (cast: 1→"1")
398 // inline form
399 {[]any{"a", "b", "aab"}, "bbb"},
400 {[]any{"a", "b", "b", "c", "aab"}, "bbc"},
401 {[]any{"app", "pear", "apple", "orange", "apple"}, "pearle"},
402 {[]any{"a", "b", ""}, ""}, // empty source
403 {[]any{template.HTML("a"), "b", "aab"}, "bbb"}, // template.HTML pair
404 {[]any{1, "one", "1abc"}, "oneabc"}, // int pair (cast: 1→"1")
405 } {
406 result, err := ns.ReplacePairs(test.args...)
407 c.Assert(err, qt.IsNil)
408 c.Assert(result, qt.Equals, test.expect)
409 }
410
411 for _, test := range []struct {
412 args []any
413 errMatch string
414 }{
415 {[]any{}, "requires at least 2"}, // 0 args
416 {[]any{"s"}, "requires at least 2"}, // 1 arg
417 {[]any{42, "s"}, "first must be a slice"}, // 2 args: non-slice first arg
418 {[]any{"a", "s"}, "first must be a slice"}, // 2 args: string first arg (not a slice)
419 {[]any{[]string{"a"}, "s"}, "uneven number"}, // slice: odd pairs
420 {[]any{"a", "b", "c", "s"}, "uneven number"}, // inline: 3 pairs
421 {[]any{[]any{tstNoStringer{}, "b"}, "s"}, "unable to cast"}, // non-castable slice element
422 {[]any{tstNoStringer{}, "b", "s"}, "unable to cast"}, // non-castable inline pair value
423 {[]any{[]string{"a", "b"}, tstNoStringer{}}, "unable to cast"}, // non-castable source
424 } {
425 _, err := ns.ReplacePairs(test.args...)
426 c.Assert(err, qt.ErrorMatches, ".*"+test.errMatch+".*")
427 }
428 }
429
430 func BenchmarkReplacePairs(b *testing.B) {
431 twoPairs := []string{"a", "A", "b", "B"}
432 threePairs := []string{"a", "A", "b", "B", "c", "C"}
433 s := "aabbcc"
434
435 b.Run("TwoPairs/cached", func(b *testing.B) {
436 b.ReportAllocs()
437 for b.Loop() {
438 ns.ReplacePairs(twoPairs, s)
439 }
440 })
441
442 b.Run("TwoPairs/uncached", func(b *testing.B) {
443 b.ReportAllocs()
444 for b.Loop() {
445 stds.NewReplacer(twoPairs...).Replace(s)
446 }
447 })
448
449 b.Run("ThreePairs/cached", func(b *testing.B) {
450 b.ReportAllocs()
451 for b.Loop() {
452 ns.ReplacePairs(threePairs, s)
453 }
454 })
455
456 b.Run("ThreePairs/uncached", func(b *testing.B) {
457 b.ReportAllocs()
458 for b.Loop() {
459 stds.NewReplacer(threePairs...).Replace(s)
460 }
461 })
462 }
463
464 func TestSliceString(t *testing.T) {
465 t.Parallel()
466 c := qt.New(t)
467
468 var err error
469 for _, test := range []struct {
470 v1 any
471 v2 any
472 v3 any
473 expect any
474 }{
475 {"abc", 1, 2, "b"},
476 {"abc", 1, 3, "bc"},
477 {"abcdef", 1, int8(3), "bc"},
478 {"abcdef", 1, int16(3), "bc"},
479 {"abcdef", 1, int32(3), "bc"},
480 {"abcdef", 1, int64(3), "bc"},
481 {"abc", 0, 1, "a"},
482 {"abcdef", nil, nil, "abcdef"},
483 {"abcdef", 0, 6, "abcdef"},
484 {"abcdef", 0, 2, "ab"},
485 {"abcdef", 2, nil, "cdef"},
486 {"abcdef", int8(2), nil, "cdef"},
487 {"abcdef", int16(2), nil, "cdef"},
488 {"abcdef", int32(2), nil, "cdef"},
489 {"abcdef", int64(2), nil, "cdef"},
490 {123, 1, 3, "23"},
491 {"abcdef", 6, nil, false},
492 {"abcdef", 4, 7, false},
493 {"abcdef", -1, nil, false},
494 {"abcdef", -1, 7, false},
495 {"abcdef", 1, -1, false},
496 {tstNoStringer{}, 0, 1, false},
497 {"ĀĀĀ", 0, 1, "Ā"}, // issue #1333
498 {"a", t, nil, false},
499 {"a", 1, t, false},
500 } {
501
502 var result string
503 if test.v2 == nil {
504 result, err = ns.SliceString(test.v1)
505 } else if test.v3 == nil {
506 result, err = ns.SliceString(test.v1, test.v2)
507 } else {
508 result, err = ns.SliceString(test.v1, test.v2, test.v3)
509 }
510
511 if b, ok := test.expect.(bool); ok && !b {
512 c.Assert(err, qt.Not(qt.IsNil))
513 continue
514 }
515
516 c.Assert(err, qt.IsNil)
517 c.Assert(result, qt.Equals, test.expect)
518 }
519
520 // Too many arguments
521 _, err = ns.SliceString("a", 1, 2, 3)
522 if err == nil {
523 t.Errorf("Should have errored")
524 }
525 }
526
527 func TestSplit(t *testing.T) {
528 t.Parallel()
529 c := qt.New(t)
530
531 for _, test := range []struct {
532 v1 any
533 v2 string
534 expect any
535 }{
536 {"a, b", ", ", []string{"a", "b"}},
537 {"a & b & c", " & ", []string{"a", "b", "c"}},
538 {"http://example.com", "http://", []string{"", "example.com"}},
539 {123, "2", []string{"1", "3"}},
540 {tstNoStringer{}, ",", false},
541 } {
542
543 result, err := ns.Split(test.v1, test.v2)
544
545 if b, ok := test.expect.(bool); ok && !b {
546 c.Assert(err, qt.Not(qt.IsNil))
547 continue
548 }
549
550 c.Assert(err, qt.IsNil)
551 c.Assert(result, qt.DeepEquals, test.expect)
552 }
553 }
554
555 func TestSubstr(t *testing.T) {
556 t.Parallel()
557 c := qt.New(t)
558
559 var err error
560 for _, test := range []struct {
561 v1 any
562 v2 any
563 v3 any
564 expect any
565 }{
566 {"abc", 1, 2, "bc"},
567 {"abc", 0, 1, "a"},
568 {"abcdef", 0, 0, ""},
569 {"abcdef", 1, 0, ""},
570 {"abcdef", -1, 0, ""},
571 {"abcdef", -1, 2, "f"},
572 {"abcdef", -3, 3, "def"},
573 {"abcdef", -1, nil, "f"},
574 {"abcdef", -2, nil, "ef"},
575 {"abcdef", -3, 1, "d"},
576 {"abcdef", 0, -1, "abcde"},
577 {"abcdef", 2, -1, "cde"},
578 {"abcdef", 4, -4, ""},
579 {"abcdef", 7, 1, ""},
580 {"abcdef", 6, nil, ""},
581 {"abcdef", 1, 100, "bcdef"},
582 {"abcdef", -100, 3, "abc"},
583 {"abcdef", -3, -1, "de"},
584 {"abcdef", 2, nil, "cdef"},
585 {"abcdef", int8(2), nil, "cdef"},
586 {"abcdef", int16(2), nil, "cdef"},
587 {"abcdef", int32(2), nil, "cdef"},
588 {"abcdef", int64(2), nil, "cdef"},
589 {"abcdef", 2, int8(3), "cde"},
590 {"abcdef", 2, int16(3), "cde"},
591 {"abcdef", 2, int32(3), "cde"},
592 {"abcdef", 2, int64(3), "cde"},
593 {123, 1, 3, "23"},
594 {1.2e3, 0, 4, "1200"},
595 {tstNoStringer{}, 0, 1, false},
596 {"abcdef", 2.0, nil, "cdef"},
597 {"abcdef", 2.0, 2, "cd"},
598 {"abcdef", 2, 2.0, "cd"},
599 {"ĀĀĀ", 1, 2, "ĀĀ"}, // # issue 1333
600 {"abcdef", "doo", nil, false},
601 {"abcdef", "doo", "doo", false},
602 {"abcdef", 1, "doo", false},
603 {"", 0, nil, ""},
604 } {
605
606 var result string
607
608 if test.v3 == nil {
609 result, err = ns.Substr(test.v1, test.v2)
610 } else {
611 result, err = ns.Substr(test.v1, test.v2, test.v3)
612 }
613
614 if b, ok := test.expect.(bool); ok && !b {
615 c.Check(err, qt.Not(qt.IsNil), qt.Commentf("%v", test))
616 continue
617 }
618
619 c.Assert(err, qt.IsNil, qt.Commentf("%v", test))
620 c.Check(result, qt.Equals, test.expect, qt.Commentf("%v", test))
621 }
622
623 _, err = ns.Substr("abcdef")
624 c.Assert(err, qt.Not(qt.IsNil))
625
626 _, err = ns.Substr("abcdef", 1, 2, 3)
627 c.Assert(err, qt.Not(qt.IsNil))
628 }
629
630 func TestTitle(t *testing.T) {
631 t.Parallel()
632 c := qt.New(t)
633
634 for _, test := range []struct {
635 s any
636 expect any
637 }{
638 {"test", "Test"},
639 {template.HTML("hypertext"), "Hypertext"},
640 {[]byte("bytes"), "Bytes"},
641 // errors
642 {tstNoStringer{}, false},
643 } {
644
645 result, err := ns.Title(test.s)
646
647 if b, ok := test.expect.(bool); ok && !b {
648 c.Assert(err, qt.Not(qt.IsNil))
649 continue
650 }
651
652 c.Assert(err, qt.IsNil)
653 c.Assert(result, qt.Equals, test.expect)
654 }
655 }
656
657 func TestToLower(t *testing.T) {
658 t.Parallel()
659 c := qt.New(t)
660
661 for _, test := range []struct {
662 s any
663 expect any
664 }{
665 {"TEST", "test"},
666 {template.HTML("LoWeR"), "lower"},
667 {[]byte("BYTES"), "bytes"},
668 // errors
669 {tstNoStringer{}, false},
670 } {
671
672 result, err := ns.ToLower(test.s)
673
674 if b, ok := test.expect.(bool); ok && !b {
675 c.Assert(err, qt.Not(qt.IsNil))
676 continue
677 }
678
679 c.Assert(err, qt.IsNil)
680 c.Assert(result, qt.Equals, test.expect)
681 }
682 }
683
684 func TestToUpper(t *testing.T) {
685 t.Parallel()
686 c := qt.New(t)
687
688 for _, test := range []struct {
689 s any
690 expect any
691 }{
692 {"test", "TEST"},
693 {template.HTML("UpPeR"), "UPPER"},
694 {[]byte("bytes"), "BYTES"},
695 // errors
696 {tstNoStringer{}, false},
697 } {
698
699 result, err := ns.ToUpper(test.s)
700
701 if b, ok := test.expect.(bool); ok && !b {
702 c.Assert(err, qt.Not(qt.IsNil))
703 continue
704 }
705
706 c.Assert(err, qt.IsNil)
707 c.Assert(result, qt.Equals, test.expect)
708 }
709 }
710
711 func TestTrim(t *testing.T) {
712 t.Parallel()
713 c := qt.New(t)
714
715 for _, test := range []struct {
716 s any
717 cutset any
718 expect any
719 }{
720 {"abba", "a", "bb"},
721 {"abba", "ab", ""},
722 {"<tag>", "<>", "tag"},
723 {`"quote"`, `"`, "quote"},
724 {1221, "1", "22"},
725 {1221, "12", ""},
726 {template.HTML("<tag>"), "<>", "tag"},
727 {[]byte("<tag>"), "<>", "tag"},
728 // errors
729 {"", tstNoStringer{}, false},
730 {tstNoStringer{}, "", false},
731 } {
732
733 result, err := ns.Trim(test.s, test.cutset)
734
735 if b, ok := test.expect.(bool); ok && !b {
736 c.Assert(err, qt.Not(qt.IsNil))
737 continue
738 }
739
740 c.Assert(err, qt.IsNil)
741 c.Assert(result, qt.Equals, test.expect)
742 }
743 }
744
745 func TestTrimLeft(t *testing.T) {
746 t.Parallel()
747 c := qt.New(t)
748
749 for _, test := range []struct {
750 s any
751 cutset any
752 expect any
753 }{
754 {"abba", "a", "bba"},
755 {"abba", "ab", ""},
756 {"<tag>", "<>", "tag>"},
757 {`"quote"`, `"`, `quote"`},
758 {1221, "1", "221"},
759 {1221, "12", ""},
760 {"007", "0", "7"},
761 {template.HTML("<tag>"), "<>", "tag>"},
762 {[]byte("<tag>"), "<>", "tag>"},
763 // errors
764 {"", tstNoStringer{}, false},
765 {tstNoStringer{}, "", false},
766 } {
767
768 result, err := ns.TrimLeft(test.cutset, test.s)
769
770 if b, ok := test.expect.(bool); ok && !b {
771 c.Assert(err, qt.Not(qt.IsNil))
772 continue
773 }
774
775 c.Assert(err, qt.IsNil)
776 c.Assert(result, qt.Equals, test.expect)
777 }
778 }
779
780 func TestTrimPrefix(t *testing.T) {
781 t.Parallel()
782 c := qt.New(t)
783
784 for _, test := range []struct {
785 s any
786 prefix any
787 expect any
788 }{
789 {"aabbaa", "a", "abbaa"},
790 {"aabb", "b", "aabb"},
791 {1234, "12", "34"},
792 {1234, "34", "1234"},
793 // errors
794 {"", tstNoStringer{}, false},
795 {tstNoStringer{}, "", false},
796 } {
797
798 result, err := ns.TrimPrefix(test.prefix, test.s)
799
800 if b, ok := test.expect.(bool); ok && !b {
801 c.Assert(err, qt.Not(qt.IsNil))
802 continue
803 }
804
805 c.Assert(err, qt.IsNil)
806 c.Assert(result, qt.Equals, test.expect)
807 }
808 }
809
810 func TestTrimRight(t *testing.T) {
811 t.Parallel()
812 c := qt.New(t)
813
814 for _, test := range []struct {
815 s any
816 cutset any
817 expect any
818 }{
819 {"abba", "a", "abb"},
820 {"abba", "ab", ""},
821 {"<tag>", "<>", "<tag"},
822 {`"quote"`, `"`, `"quote`},
823 {1221, "1", "122"},
824 {1221, "12", ""},
825 {"007", "0", "007"},
826 {template.HTML("<tag>"), "<>", "<tag"},
827 {[]byte("<tag>"), "<>", "<tag"},
828 // errors
829 {"", tstNoStringer{}, false},
830 {tstNoStringer{}, "", false},
831 } {
832
833 result, err := ns.TrimRight(test.cutset, test.s)
834
835 if b, ok := test.expect.(bool); ok && !b {
836 c.Assert(err, qt.Not(qt.IsNil))
837 continue
838 }
839
840 c.Assert(err, qt.IsNil)
841 c.Assert(result, qt.Equals, test.expect)
842 }
843 }
844
845 func TestTrimSuffix(t *testing.T) {
846 t.Parallel()
847 c := qt.New(t)
848
849 for _, test := range []struct {
850 s any
851 suffix any
852 expect any
853 }{
854 {"aabbaa", "a", "aabba"},
855 {"aabb", "b", "aab"},
856 {1234, "12", "1234"},
857 {1234, "34", "12"},
858 // errors
859 {"", tstNoStringer{}, false},
860 {tstNoStringer{}, "", false},
861 } {
862
863 result, err := ns.TrimSuffix(test.suffix, test.s)
864
865 if b, ok := test.expect.(bool); ok && !b {
866 c.Assert(err, qt.Not(qt.IsNil))
867 continue
868 }
869
870 c.Assert(err, qt.IsNil)
871 c.Assert(result, qt.Equals, test.expect)
872 }
873 }
874
875 func TestRepeat(t *testing.T) {
876 t.Parallel()
877 c := qt.New(t)
878
879 for _, test := range []struct {
880 s any
881 n any
882 expect any
883 }{
884 {"yo", "2", "yoyo"},
885 {"~", "16", "~~~~~~~~~~~~~~~~"},
886 {"<tag>", "0", ""},
887 {"yay", "1", "yay"},
888 {1221, "1", "1221"},
889 {1221, 2, "12211221"},
890 {template.HTML("<tag>"), "2", "<tag><tag>"},
891 {[]byte("<tag>"), 2, "<tag><tag>"},
892 // errors
893 {"", tstNoStringer{}, false},
894 {tstNoStringer{}, "", false},
895 {"ab", -1, false},
896 } {
897
898 result, err := ns.Repeat(test.n, test.s)
899
900 if b, ok := test.expect.(bool); ok && !b {
901 c.Assert(err, qt.Not(qt.IsNil))
902 continue
903 }
904
905 c.Assert(err, qt.IsNil)
906 c.Assert(result, qt.Equals, test.expect)
907 }
908 }
909
910 func TestDiff(t *testing.T) {
911 t.Parallel()
912 c := qt.New(t)
913
914 for _, test := range []struct {
915 oldname string
916 old any
917 newname string
918 new any
919 expect any
920 }{
921 {"old", "foo\n", "new", "bar\n", "diff old new\n--- old\n+++ new\n@@ -1,1 +1,1 @@\n-foo\n+bar\n"},
922 {"old", "foo\n", "new", "foo\n", ""},
923 {"old", "foo\n", "new", "", "diff old new\n--- old\n+++ new\n@@ -1,1 +0,0 @@\n-foo\n"},
924 {"old", "foo\n", "new", nil, "diff old new\n--- old\n+++ new\n@@ -1,1 +0,0 @@\n-foo\n"},
925 {"old", "", "new", "", ""},
926 // errors
927 {"old", tstNoStringer{}, "new", "foo", false},
928 {"old", "foo", "new", tstNoStringer{}, false},
929 } {
930
931 result, err := ns.Diff(test.oldname, test.old, test.newname, test.new)
932
933 if b, ok := test.expect.(bool); ok && !b {
934 c.Assert(err, qt.Not(qt.IsNil))
935 continue
936 }
937
938 c.Assert(err, qt.IsNil)
939 c.Assert(result, qt.Equals, test.expect)
940
941 }
942 }
943
944 func TestTrimSpace(t *testing.T) {
945 t.Parallel()
946 c := qt.New(t)
947
948 for _, test := range []struct {
949 s any
950 expect any
951 }{
952 {"\n\r test \n\r", "test"},
953 {template.HTML("\n\r test \n\r"), "test"},
954 {[]byte("\n\r test \n\r"), "test"},
955 // errors
956 {tstNoStringer{}, false},
957 } {
958
959 result, err := ns.TrimSpace(test.s)
960
961 if b, ok := test.expect.(bool); ok && !b {
962 c.Assert(err, qt.Not(qt.IsNil))
963 continue
964 }
965
966 c.Assert(err, qt.IsNil)
967 c.Assert(result, qt.Equals, test.expect)
968 }
969 }