-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathstringy_test.go
More file actions
1643 lines (1500 loc) · 40.9 KB
/
stringy_test.go
File metadata and controls
1643 lines (1500 loc) · 40.9 KB
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package stringy
import (
"errors"
"fmt"
"strings"
"sync"
"testing"
)
var sm StringManipulation = New("This is example.")
// Test Acronym
func TestInput_Acronym(t *testing.T) {
acronym := New("Laugh Out Loud")
val := acronym.Acronym().Get()
if val != "LOL" {
t.Errorf("Expected: %s but got: %s", "LOL", val)
}
if acronym.Error() != nil {
t.Errorf("Expected no error but got: %v", acronym.Error())
}
}
// Test Between - positive case
func TestInput_Between(t *testing.T) {
val := sm.Between("This", "example").Trim().ToUpper()
if val != "IS" {
t.Errorf("Expected: %s but got: %s", "IS", val)
}
if sm.Error() != nil {
t.Errorf("Expected no error but got: %v", sm.Error())
}
}
// Test Between - empty input
func TestInput_EmptyBetween(t *testing.T) {
sm := New("This is example.")
val := sm.Between("", "").ToUpper()
if val != "THIS IS EXAMPLE." {
t.Errorf("Expected: %s but got: %s", "THIS IS EXAMPLE.", val)
}
if sm.Error() != nil {
t.Errorf("Expected no error but got: %v", sm.Error())
}
}
// Test Between - no match
func TestInput_EmptyNoMatchBetween(t *testing.T) {
sm := New("This is example.")
result := sm.Between("hello", "test")
if result.Get() != "" {
t.Errorf("Expected: \"\" but got: %s", result.Get())
}
if sm.Error() != nil {
t.Errorf("Expected no error but got: %v", sm.Error())
}
}
// Test Match - positive case
func TestInput_MatchBetween(t *testing.T) {
sm := New("This is example.")
result := sm.Between("This", "example").Trim()
if result.Get() != "is" {
t.Errorf("Expected: %s but got: %s", "is", result.Get())
}
if sm.Error() != nil {
t.Errorf("Expected no error but got: %v", sm.Error())
}
}
// Test Boolean - true values
func TestInput_BooleanTrue(t *testing.T) {
strs := []string{"on", "On", "yes", "YES", "1", "true"}
for _, s := range strs {
t.Run(s, func(t *testing.T) {
sm := New(s)
if val := sm.Boolean(); !val {
t.Errorf("Expected: to be true but got: %v", val)
}
if sm.Error() != nil {
t.Errorf("Expected no error but got: %v", sm.Error())
}
})
}
}
// Test Boolean - false values
func TestInput_BooleanFalse(t *testing.T) {
strs := []string{"off", "Off", "no", "NO", "0", "false"}
for _, s := range strs {
t.Run(s, func(t *testing.T) {
sm := New(s)
if val := sm.Boolean(); val {
t.Errorf("Expected: to be false but got: %v", val)
}
if sm.Error() != nil {
t.Errorf("Expected no error but got: %v", sm.Error())
}
})
}
}
// Test Boolean - error case
func TestInput_BooleanError(t *testing.T) {
strs := []string{"invalid", "-1", ""}
for _, s := range strs {
t.Run(s, func(t *testing.T) {
sm := New(s)
val := sm.Boolean()
if val != false {
t.Errorf("Expected false as default value on error but got: %v", val)
}
if sm.Error() == nil {
t.Errorf("Expected error but got none")
}
})
}
}
// Test CamelCase - standard case
func TestInput_CamelCase(t *testing.T) {
str := New("Camel case this_complicated__string%%")
against := "camelCaseThisComplicatedString"
if val := str.CamelCase("%", "").Get(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test CamelCase - no rule case
func TestInput_CamelCaseNoRule(t *testing.T) {
str := New("Camel case this_complicated__string%%")
against := "camelCaseThisComplicatedString%%"
if val := str.CamelCase().Get(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test CamelCase - odd rule error
func TestInput_CamelCaseOddRuleError(t *testing.T) {
str := New("Camel case this_complicated__string%%")
result := str.CamelCase("%")
if str.Error() == nil {
t.Errorf("Expected error but got none")
}
if result.Get() != "" {
t.Errorf("Expected empty result when error occurs, got: %s", result.Get())
}
}
// Test PascalCase - standard case
func TestInput_PascalCaseNoRule(t *testing.T) {
str := New("pascal case this_complicated__string%%")
against := "PascalCaseThisComplicatedString%%"
if val := str.PascalCase().Get(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test PascalCase - odd rule error
func TestInput_PascalCaseOddRuleError(t *testing.T) {
str := New("pascal case this_complicated__string%%")
result := str.PascalCase("%")
if str.Error() == nil {
t.Errorf("Expected error but got none")
}
if result.Get() != "" {
t.Errorf("Expected empty result when error occurs, got: %s", result.Get())
}
}
// Test ContainsAll
func TestInput_ContainsAll(t *testing.T) {
contains := New("hello mam how are you??")
if val := contains.ContainsAll("mam", "?"); !val {
t.Errorf("Expected value to be true but got false")
}
if val := contains.ContainsAll("non existent"); val {
t.Errorf("Expected value to be false but got true")
}
if contains.Error() != nil {
t.Errorf("Expected no error but got: %v", contains.Error())
}
}
// Test Delimited - with rules
func TestInput_Delimited(t *testing.T) {
str := New("Delimited case this_complicated__string@@")
against := "delimited.case.this.complicated.string"
if val := str.Delimited(".", "@", "").ToLower(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test Delimited - no delimiter
func TestInput_DelimitedNoDelimeter(t *testing.T) {
str := New("Delimited case this_complicated__string@@")
against := "delimited.case.this.complicated.string@@"
if val := str.Delimited("").ToLower(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test Delimited - odd rule error
func TestInput_DelimitedOddRuleError(t *testing.T) {
str := New("Delimited case this_complicated__string@@")
result := str.Delimited(".", "@")
if str.Error() == nil {
t.Errorf("Expected error but got none")
}
if result.Get() != "" {
t.Errorf("Expected empty result when error occurs, got: %s", result.Get())
}
}
// Test KebabCase
func TestInput_KebabCase(t *testing.T) {
str := New("Kebab case this-complicated___string@@")
against := "Kebab-case-this-complicated-string"
if val := str.KebabCase("@", "").Get(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test KebabCase - odd rule error
func TestInput_KebabCaseOddRuleError(t *testing.T) {
str := New("Kebab case this-complicated___string@@")
result := str.KebabCase("@")
if str.Error() == nil {
t.Errorf("Expected error but got none")
}
if result.Get() != "" {
t.Errorf("Expected empty result when error occurs, got: %s", result.Get())
}
}
// Test LcFirst
func TestInput_LcFirst(t *testing.T) {
tests := []struct {
name string
arg string
want string
}{
{
name: "leading uppercase",
arg: "This is an all lower",
want: "this is an all lower",
},
{
name: "empty string",
arg: "",
want: "",
},
{
name: "multi-byte leading character",
arg: "ΔΔΔ",
want: "δΔΔ",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sm := New(tt.arg)
if got := sm.LcFirst(); got != tt.want {
t.Errorf("LcFirst(%v) = %v, want %v", tt.arg, got, tt.want)
}
if sm.Error() != nil {
t.Errorf("Expected no error but got: %v", sm.Error())
}
})
}
}
// Test Lines
func TestInput_Lines(t *testing.T) {
lines := New("fòô\r\nbàř\nyolo")
strSlic := lines.Lines()
if len(strSlic) != 3 {
t.Errorf("Length expected to be 3 but got: %d", len(strSlic))
}
if strSlic[0] != "fòô" {
t.Errorf("Expected: %s but got: %s", "fòô", strSlic[0])
}
if lines.Error() != nil {
t.Errorf("Expected no error but got: %v", lines.Error())
}
}
// Test Lines - empty input
func TestInput_LinesEmpty(t *testing.T) {
lines := New("")
strSlic := lines.Lines()
if len(strSlic) != 0 {
t.Errorf("Length expected to be 0 but got: %d", len(strSlic))
}
if lines.Error() != nil {
t.Errorf("Expected no error but got: %v", lines.Error())
}
}
// Test Pad
func TestInput_Pad(t *testing.T) {
pad := New("Roshan")
if result := pad.Pad(10, "0", "both"); result != "00Roshan00" {
t.Errorf("Expected: %s but got: %s", "00Roshan00", result)
}
if result := pad.Pad(10, "0", "left"); result != "0000Roshan" {
t.Errorf("Expected: %s but got: %s", "0000Roshan", result)
}
if result := pad.Pad(10, "0", "right"); result != "Roshan0000" {
t.Errorf("Expected: %s but got: %s", "Roshan0000", result)
}
if pad.Error() != nil {
t.Errorf("Expected no error but got: %v", pad.Error())
}
}
// Test Pad - invalid length
func TestInput_PadInvalidLength(t *testing.T) {
pad := New("Roshan")
if result := pad.Pad(6, "0", "both"); result != "Roshan" {
t.Errorf("Expected: %s but got: %s", "Roshan", result)
}
if result := pad.Pad(6, "0", "left"); result != "Roshan" {
t.Errorf("Expected: %s but got: %s", "Roshan", result)
}
if result := pad.Pad(6, "0", "right"); result != "Roshan" {
t.Errorf("Expected: %s but got: %s", "Roshan", result)
}
if result := pad.Pad(13, "0", "middle"); result != "Roshan" {
t.Errorf("Expected: %s but got: %s", "Roshan", result)
}
if pad.Error() != nil {
t.Errorf("Expected no error but got: %v", pad.Error())
}
}
// Test RemoveSpecialCharacter
func TestInput_RemoveSpecialCharacter(t *testing.T) {
cleanString := New("special@#remove%%%%")
against := "specialremove"
if result := cleanString.RemoveSpecialCharacter(); result != against {
t.Errorf("Expected: %s but got: %s", against, result)
}
if cleanString.Error() != nil {
t.Errorf("Expected no error but got: %v", cleanString.Error())
}
}
// Test ReplaceFirst
func TestInput_ReplaceFirst(t *testing.T) {
replaceFirst := New("Hello My name is Roshan and his name is Alis.")
against := "Hello My nombre is Roshan and his name is Alis."
if result := replaceFirst.ReplaceFirst("name", "nombre"); result != against {
t.Errorf("Expected: %s but got: %s", against, result)
}
if replaceFirst.Error() != nil {
t.Errorf("Expected no error but got: %v", replaceFirst.Error())
}
}
// Test ReplaceFirst - empty input
func TestInput_ReplaceFirstEmptyInput(t *testing.T) {
replaceFirst := New("")
against := ""
if result := replaceFirst.ReplaceFirst("name", "nombre"); result != against {
t.Errorf("Expected: %s but got: %s", against, result)
}
if replaceFirst.Error() != nil {
t.Errorf("Expected no error but got: %v", replaceFirst.Error())
}
}
// Test ReplaceLast
func TestInput_ReplaceLast(t *testing.T) {
replaceLast := New("Hello My name is Roshan and his name is Alis.")
against := "Hello My name is Roshan and his nombre is Alis."
if result := replaceLast.ReplaceLast("name", "nombre"); result != against {
t.Errorf("Expected: %s but got: %s", against, result)
}
if replaceLast.Error() != nil {
t.Errorf("Expected no error but got: %v", replaceLast.Error())
}
}
// Test Reverse
func TestInput_Reverse(t *testing.T) {
reverseString := New("roshan")
against := "nahsor"
if result := reverseString.Reverse(); result != against {
t.Errorf("Expected: %s but got: %s", against, result)
}
if reverseString.Error() != nil {
t.Errorf("Expected no error but got: %v", reverseString.Error())
}
}
// Test Shuffle
func TestInput_Shuffle(t *testing.T) {
check := "roshan"
shuffleString := New(check)
if result := shuffleString.Shuffle(); len(result) != len(check) && check == result {
t.Errorf("Shuffle string gave wrong output")
}
if shuffleString.Error() != nil {
t.Errorf("Expected no error but got: %v", shuffleString.Error())
}
}
// Test SnakeCase
func TestInput_SnakeCase(t *testing.T) {
str := New("SnakeCase this-complicated___string@@")
against := "snake_case_this_complicated_string"
if val := str.SnakeCase("@", "").ToLower(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test SnakeCase - odd rule error
func TestInput_SnakeCaseOddRuleError(t *testing.T) {
str := New("SnakeCase this-complicated___string@@")
result := str.SnakeCase("@")
if str.Error() == nil {
t.Errorf("Expected error but got none")
}
if result.Get() != "" {
t.Errorf("Expected empty result when error occurs, got: %s", result.Get())
}
}
// Test Surround
func TestInput_Surround(t *testing.T) {
str := New("this")
against := "__this__"
if val := str.Surround("__"); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test Tease
func TestInput_Tease(t *testing.T) {
str := New("This is just simple paragraph on lorem ipsum.")
against := "This is just..."
if val := str.Tease(12, "..."); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test Tease - empty or shorter than length
func TestInput_TeaseEmpty(t *testing.T) {
str := New("This is just simple paragraph on lorem ipsum.")
against := "This is just simple paragraph on lorem ipsum."
if val := str.Tease(200, "..."); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test Title
func TestInput_Title(t *testing.T) {
str := New("this is just AN eXample")
against := "This Is Just An Example"
if val := str.Title(); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test UcFirst
func TestInput_UcFirst(t *testing.T) {
tests := []struct {
name string
arg string
want string
}{
{
name: "leading lowercase",
arg: "test input",
want: "Test input",
},
{
name: "empty string",
arg: "",
want: "",
},
{
name: "multi-byte leading character",
arg: "δδδ",
want: "Δδδ",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sm := New(tt.arg)
if got := sm.UcFirst(); got != tt.want {
t.Errorf("UcFirst(%v) = %v, want %v", tt.arg, got, tt.want)
}
if sm.Error() != nil {
t.Errorf("Expected no error but got: %v", sm.Error())
}
})
}
}
// Test First
func TestInput_First(t *testing.T) {
fcn := New("4111 1111 1111 1111")
against := "4111"
if first := fcn.First(4); first != against {
t.Errorf("Expected: to be %s but got: %s", against, first)
}
if fcn.Error() != nil {
t.Errorf("Expected no error but got: %v", fcn.Error())
}
}
// Test First - error case
func TestInput_FirstError(t *testing.T) {
fcn := New("4111 1111 1111 1111")
result := fcn.First(100)
if fcn.Error() == nil {
t.Errorf("Error expected but got none")
}
if result != "" {
t.Errorf("Expected empty result when error occurs, got: %s", result)
}
}
// Test Last
func TestInput_Last(t *testing.T) {
lcn := New("4111 1111 1111 1348")
against := "1348"
if last := lcn.Last(4); last != against {
t.Errorf("Expected: to be %s but got: %s", against, last)
}
if lcn.Error() != nil {
t.Errorf("Expected no error but got: %v", lcn.Error())
}
}
// Test Last - error case
func TestInput_LastError(t *testing.T) {
lcn := New("4111 1111 1111 1348")
result := lcn.Last(100)
if lcn.Error() == nil {
t.Errorf("Error expected but got none")
}
if result != "" {
t.Errorf("Expected empty result when error occurs, got: %s", result)
}
}
// Test Prefix
func TestInput_Prefix(t *testing.T) {
str := New("foobar")
against := "foobar"
if val := str.Prefix("foo"); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
str = New("foobar")
against = "foofoofoobar"
if val := str.Prefix("foofoo"); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test Suffix
func TestInput_Suffix(t *testing.T) {
str := New("foobar")
against := "foobar"
if val := str.Suffix("bar"); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
str = New("foobar")
against = "foobarbarbar"
if val := str.Suffix("barbar"); val != against {
t.Errorf("Expected: to be %s but got: %s", against, val)
}
if str.Error() != nil {
t.Errorf("Expected no error but got: %v", str.Error())
}
}
// Test Error method
func TestInput_Error(t *testing.T) {
// Test that Error() returns nil for new object
str := New("test")
if str.Error() != nil {
t.Errorf("Expected nil error but got: %v", str.Error())
}
// Test that Error() returns the correct error after setting it
str = New("invalid")
str.Boolean() // This should set an error
if str.Error() == nil {
t.Errorf("Expected error but got nil")
}
if str.Error().Error() != InvalidLogicalString {
t.Errorf("Expected error message '%s' but got: %s", InvalidLogicalString, str.Error().Error())
}
// Test that Error() is reset when using New()
str = New("test")
if str.Error() != nil {
t.Errorf("Expected nil error after New() but got: %v", str.Error())
}
}
// Test Release method
func TestInput_Release(t *testing.T) {
i := inputPool.Get().(*input)
i.Input = "test"
i.Result = "result"
i.err = errors.New("test error")
i.Release()
if i.Input != "" || i.Result != "" || i.err != nil {
t.Errorf("Release didn't reset the fields properly. Input: %s, Result: %s, err: %v",
i.Input, i.Result, i.err)
}
}
// Test method chaining with errors
func TestInput_MethodChainingWithErrors(t *testing.T) {
// Test that an error in one method is preserved through a chain
str := New("test")
result := str.CamelCase("%").ToUpper()
if str.Error() == nil {
t.Errorf("Expected error to be preserved in chain but got nil")
}
if result != "" {
t.Errorf("Expected empty result after error but got: %s", result)
}
}
// Test Error persistence
func TestInput_ErrorPersistence(t *testing.T) {
// Test that after an error is set, it remains until a new object is created
str := New("test")
str.First(100) // Should set error
if str.Error() == nil {
t.Errorf("Expected error but got nil")
}
// Try another operation
str.ToUpper()
// Error should still be present
if str.Error() == nil {
t.Errorf("Expected error to persist but it was cleared")
}
// Create new object
newStr := New("test")
if newStr.Error() != nil {
t.Errorf("Expected nil error for new object but got: %v", newStr.Error())
}
}
// Test for handling multi-byte characters in all methods
func TestInput_MultiByteCharacters(t *testing.T) {
// Test with emoji and international characters
str := New("😀 Hello 世界")
// Test CamelCase with multi-byte
camelResult := str.CamelCase().Get()
if camelResult != "😀Hello世界" {
t.Errorf("CamelCase multi-byte - Expected: %s but got: %s", "😀Hello世界", camelResult)
}
// Test SnakeCase with multi-byte - update expectation
str = New("😀 Hello 世界")
snakeResult := str.SnakeCase().Get()
if snakeResult != "😀_Hello_世界" {
t.Errorf("SnakeCase multi-byte - Expected: %s but got: %s", "😀_Hello_世界", snakeResult)
}
// Test KebabCase with multi-byte - update expectation
str = New("😀 Hello 世界")
kebabResult := str.KebabCase().Get()
if kebabResult != "😀-Hello-世界" {
t.Errorf("KebabCase multi-byte - Expected: %s but got: %s", "😀-Hello-世界", kebabResult)
}
}
// Test concurrent use of the package using goroutines
func TestInput_Concurrency(t *testing.T) {
const goroutines = 100
var wg sync.WaitGroup
wg.Add(goroutines)
for i := 0; i < goroutines; i++ {
go func(id int) {
defer wg.Done()
// Use different operations in each goroutine
str := New(fmt.Sprintf("Test%d", id))
// Mix of operations
switch id % 5 {
case 0:
result := str.CamelCase().Get()
if result != fmt.Sprintf("test%d", id) {
t.Errorf("Concurrent CamelCase - Expected: test%d but got: %s", id, result)
}
case 1:
result := str.SnakeCase().Get()
if result != fmt.Sprintf("Test_%d", id) {
t.Errorf("Concurrent SnakeCase - Expected: Test_%d but got: %s", id, result)
}
case 2:
result := str.Between("T", fmt.Sprintf("%d", id)).Get()
if result != "est" {
t.Errorf("Concurrent Between - Expected: est but got: %s", result)
}
case 3:
result := str.ToUpper()
if result != fmt.Sprintf("TEST%d", id) {
t.Errorf("Concurrent ToUpper - Expected: TEST%d but got: %s", id, result)
}
case 4:
result := str.Reverse()
expected := fmt.Sprintf("%dseT", id)
if result != expected {
t.Errorf("Concurrent Reverse - Expected: %s but got: %s", expected, result)
}
}
// Test Release
input := inputPool.Get().(*input)
input.Input = "test"
input.Release()
}(i)
}
wg.Wait()
}
// Additional test cases for SnakeCase
func TestInput_SnakeCaseRobustness(t *testing.T) {
testCases := []struct {
name string
input string
rules []string
expected string
}{
{
name: "basic conversion",
input: "ThisIsATest",
rules: []string{},
expected: "This_Is_A_Test",
},
{
name: "with special characters",
input: "This@Is#A$Test",
rules: []string{"@", " ", "#", " ", "$", " "},
expected: "This_Is_A_Test",
},
{
name: "with mixed case",
input: "thisIsATest",
rules: []string{},
expected: "this_Is_A_Test",
},
{
name: "with existing underscores",
input: "this_is_a_test",
rules: []string{},
expected: "this_is_a_test",
},
{
name: "with mixed separators",
input: "this-is.a test",
rules: []string{},
expected: "this_is_a_test",
},
{
name: "with consecutive separators",
input: "this__is...a test",
rules: []string{},
expected: "this_is_a_test",
},
{
name: "with control characters",
input: "this\nis\ta\rtest",
rules: []string{},
expected: "this_is_a_test",
},
{
name: "with empty input",
input: "",
rules: []string{},
expected: "",
},
{
name: "with only separators",
input: "___...- ",
rules: []string{},
expected: "",
},
{
name: "with multi-byte characters",
input: "こんにちは世界",
rules: []string{},
expected: "こんにちは世界",
},
{
name: "with multi-byte characters and separators",
input: "こんにちは_世界",
rules: []string{},
expected: "こんにちは_世界",
},
{
name: "complex mix",
input: "ThisIs-A__complex.123 Test with@#$Stuff",
rules: []string{"@", " ", "#", " ", "$", " "},
expected: "This_Is_A_complex_123_Test_with_Stuff",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
str := New(tc.input)
var result string
if len(tc.rules) > 0 {
result = str.SnakeCase(tc.rules...).Get()
} else {
result = str.SnakeCase().Get()
}
if result != tc.expected {
t.Errorf("Expected: %q, but got: %q", tc.expected, result)
}
})
}
}
// Test edge cases for all string manipulations
func TestInput_EdgeCases(t *testing.T) {
// Test with empty string
empty := New("")
// All these operations should handle empty strings gracefully
if empty.CamelCase().Get() != "" {
t.Errorf("CamelCase empty - Expected empty string")
}
if empty.SnakeCase().Get() != "" {
t.Errorf("SnakeCase empty - Expected empty string")
}
if empty.KebabCase().Get() != "" {
t.Errorf("KebabCase empty - Expected empty string")
}
if empty.Reverse() != "" {
t.Errorf("Reverse empty - Expected empty string")
}
if empty.RemoveSpecialCharacter() != "" {
t.Errorf("RemoveSpecialCharacter empty - Expected empty string")
}
if empty.Tease(10, "...") != "" {
t.Errorf("Tease empty - Expected empty string")
}
if empty.Pad(10, "0", "both") != "0000000000" {
t.Errorf("Pad empty - Expected 10 zeros but got: %s", empty.Pad(10, "0", "both"))
}
// Test with only special characters
special := New("@#$%^&*")
if special.RemoveSpecialCharacter() != "" {
t.Errorf("RemoveSpecialCharacter special - Expected empty string but got: %s",
special.RemoveSpecialCharacter())
}
// Test with extremely long input
longStr := strings.Repeat("a", 10000)
long := New(longStr)
if len(long.Tease(100, "...")) != 103 {
t.Errorf("Tease long - Expected length 103 but got: %d", len(long.Tease(100, "...")))
}
if len(long.First(100)) != 100 {
t.Errorf("First long - Expected length 100 but got: %d", len(long.First(100)))
}
if len(long.Last(100)) != 100 {
t.Errorf("Last long - Expected length 100 but got: %d", len(long.Last(100)))
}
}
// Additional test for Between with various cases
func TestInput_BetweenEdgeCases(t *testing.T) {
// Test with matching start but no matching end
str := New("Hello World")
result := str.Between("Hello", "Goodbye").Get()
if result != "" {
t.Errorf("Between with no matching end - Expected empty but got: %s", result)
}
// Test with matching end but no matching start
result = str.Between("Goodbye", "World").Get()
if result != "" {
t.Errorf("Between with no matching start - Expected empty but got: %s", result)
}
// Test with multiple occurrences of start and end - updated expectation
str = New("start middle start middle end end")
result = str.Between("start", "end").Get()
if result != " middle start middle " {
t.Errorf("Between with multiple occurrences - Expected: ' middle start middle ' but got: %s", result)
}
// Test with overlapping start and end - modified expectation
str = New("startend")
result = str.Between("start", "end").Get()
if result != "" {
t.Errorf("Between with overlapping - Expected empty but got: %s", result)
}
// Test case sensitivity - updated expectation
str = New("START middle END")
result = str.Between("start", "end").Get()
if result != " middle " {
t.Errorf("Between case insensitive - Expected: ' middle ' but got: %s", result)
}
}
// Additional test for method chaining
func TestInput_MethodChaining(t *testing.T) {
str := New("this is a TEST string")
// Chain multiple operations
result := str.CamelCase().Between("this", "string").ToUpper()
if result != "ISATEST" {
t.Errorf("Method chaining - Expected: ISATEST but got: %s", result)
}
// Chain with error in the middle
str = New("this is a TEST string")
result = str.SnakeCase("%").ToUpper() // Should error due to odd rule
if result != "" {
t.Errorf("Method chaining with error - Expected empty but got: %s", result)
}
if str.Error() == nil {
t.Errorf("Method chaining with error - Expected error but got nil")
}
}
// Test for Prefix and Suffix with edge cases
func TestInput_PrefixSuffixEdgeCases(t *testing.T) {
// Test empty string
str := New("")
if str.Prefix("prefix") != "prefix" {