-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathStrmanTests.java
More file actions
1251 lines (1076 loc) · 48 KB
/
StrmanTests.java
File metadata and controls
1251 lines (1076 loc) · 48 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
/*
*
* * The MIT License
* *
* * Copyright 2016 Shekhar Gulati <shekhargulati84@gmail.com>.
* *
* * Permission is hereby granted, free of charge, to any person obtaining a copy
* * of this software and associated documentation files (the "Software"), to deal
* * in the Software without restriction, including without limitation the rights
* * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* * copies of the Software, and to permit persons to whom the Software is
* * furnished to do so, subject to the following conditions:
* *
* * The above copyright notice and this permission notice shall be included in
* * all copies or substantial portions of the Software.
* *
* * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* * THE SOFTWARE.
*
*/
package strman;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.junit.Ignore;
import org.junit.Test;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsArrayContaining.hasItemInArray;
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
import static org.hamcrest.collection.IsArrayWithSize.arrayWithSize;
import static org.hamcrest.collection.IsArrayWithSize.emptyArray;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.Assert.*;
import static strman.Strman.*;
import static strman.Strman.endsWith;
public class StrmanTests {
@Test
public void append_shouldAppendStringsToEndOfValue() throws Exception {
assertThat(append("f", "o", "o", "b", "a", "r"), equalTo("foobar"));
assertThat(append("foobar"), equalTo("foobar"));
assertThat(append("", "foobar"), equalTo("foobar"));
}
@Test(expected = IllegalArgumentException.class)
public void append_shouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
append(null);
}
@Test
public void appendArray_shouldAppendStringArrayToEndOfValue() throws Exception {
assertThat(appendArray("f", new String[]{"o", "o", "b", "a", "r"}), equalTo("foobar"));
assertThat(appendArray("foobar", new String[]{}), equalTo("foobar"));
assertThat(appendArray("", new String[]{"foobar"}), equalTo("foobar"));
}
@Test(expected = IllegalArgumentException.class)
public void appendArray_ShouldThrowIllegalArgumentExceptionWhenValueIsNull() throws Exception {
appendArray(null, new String[]{});
}
@Test
public void at_shouldFindCharacterAtIndex() throws Exception {
assertThat(at("foobar", 0), equalTo(Optional.of("f")));
assertThat(at("foobar", 1), equalTo(Optional.of("o")));
assertThat(at("foobar", -1), equalTo(Optional.of("r")));
assertThat(at("foobar", -2), equalTo(Optional.of("a")));
assertThat(at("foobar", 10), equalTo(Optional.empty()));
assertThat(at("foobar", -10), equalTo(Optional.empty()));
}
@Test
public void between_shouldReturnArrayWithStringsBetweenStartAndEnd() throws Exception {
assertThat(between("[abc][def]", "[", "]"), arrayContaining("abc", "def"));
assertThat(between("<span>foo</span>", "<span>", "</span>"), arrayContaining("foo"));
assertThat(between("<span>foo</span><span>bar</span>", "<span>", "</span>"), arrayContaining("foo", "bar"));
}
@Test
public void between_shouldReturnEmptyArrayWhenStartAndEndDoesNotExist() throws Exception {
assertThat(between("[abc][def]", "{", "}").length, equalTo(0));
assertThat(between("", "{", "}").length, equalTo(0));
}
@Test
public void chars_shouldReturnAllCharactersInString() throws Exception {
final String title = "title";
assertThat(chars(title), equalTo(new String[]{"t", "i", "t", "l", "e"}));
}
@Test
public void collapseWhitespace_shouldReplaceConsecutiveWhitespaceWithSingleSpace() throws Exception {
String[] fixture = {
"foo bar",
" foo bar ",
" foo bar ",
" foo bar "
};
Arrays.stream(fixture).forEach(el -> assertThat(collapseWhitespace(el), equalTo("foo bar")));
}
@Test
public void collapseWhitespace_shouldReplaceConsecutiveWhitespaceBetweenMultipleStrings() throws Exception {
String input = " foo bar bazz hello world ";
assertThat(collapseWhitespace(input), equalTo("foo bar bazz hello world"));
}
@Test
public void containsWithCaseSensitiveFalse_shouldReturnTrueWhenStringContainsNeedle() throws Exception {
String[] fixture = {
"foo bar",
"bar foo",
"foobar",
"foo"
};
Arrays.stream(fixture).forEach(el -> assertTrue(contains(el, "FOO")));
}
@Test
public void containsWithCaseSensitiveTrue_shouldReturnTrueWhenStringContainsNeedle() throws Exception {
String[] fixture = {
"foo bar",
"bar foo",
"foobar",
"foo"
};
Arrays.stream(fixture).forEach(el -> assertFalse(contains(el, "FOO", true)));
}
@Test
public void containsAll_shouldReturnTrueOnlyWhenAllNeedlesAreContainedInValue() throws Exception {
String[] fixture = {
"foo bar",
"bar foo",
"foobar",
};
Arrays.stream(fixture).forEach(el -> assertTrue(containsAll(el, new String[]{"foo", "bar"})));
}
@Test
public void containsAll_shouldReturnFalseOnlyWhenAllNeedlesAreNotContainedInValue() throws Exception {
String[] fixture = {
"foo bar",
"bar foo",
"foobar",
};
Arrays.stream(fixture).forEach(el -> assertFalse(containsAll(el, new String[]{"FOO", "bar"}, true)));
}
@Test
public void containsAny_shouldReturnTrueWhenAnyOfSearchNeedleExistInInputValue() throws Exception {
String[] fixture = {
"foo bar",
"bar foo",
"foobar",
};
Arrays.stream(fixture).forEach(el -> assertTrue(containsAny(el, new String[]{"foo", "bar", "test"})));
}
@Test
public void containsAny_shouldReturnFalseWhenNoneOfSearchNeedleExistInInputValue() throws Exception {
String[] fixture = {
"foo bar",
"bar foo",
"foobar",
};
Arrays.stream(fixture).forEach(el -> assertFalse(containsAny(el, new String[]{"FOO", "BAR", "Test"}, true)));
}
@Test
public void countSubstr_shouldCountSubStrCountCaseInsensitiveWithoutOverlapInValue() throws Exception {
assertThat(countSubstr("aaaAAAaaa", "aaa", false, false), equalTo(3L));
}
@Test
public void countSubstr_shouldCountSubStrCountCaseSensitiveWithoutOverlapInValue() throws Exception {
assertThat(countSubstr("aaaAAAaaa", "aaa"), equalTo(2L));
}
@Test
public void countSubstr_shouldCountSubStrCountCaseInsensitiveWithOverlapInValue() throws Exception {
assertThat(countSubstr("aaaAAAaaa", "aaa", false, true), equalTo(7L));
}
@Test
public void countSubstr_shouldCountSubStrCountCaseSensitiveWithOverlapInValue() throws Exception {
assertThat(countSubstr("aaaAAAaaa", "AAA", true, true), equalTo(1L));
}
@Test
public void countSubstrTestFixture_caseSensitiveTrueAndOverlappingFalse() throws Exception {
String[] fixture = {
"aaaaaAaaAA",
"faaaAAaaaaAA",
"aaAAaaaaafA",
"AAaaafaaaaAAAA"
};
Arrays.stream(fixture).forEach(el -> assertThat(countSubstr(el, "a", true, false), equalTo(7L)));
}
@Test
public void countSubstrTestFixture_caseSensitiveFalseAndOverlappingFalse() throws Exception {
String[] fixture = {
"aaaaaaa",
"faaaaaaa",
"aaaaaaaf",
"aaafaaaa"
};
Arrays.stream(fixture).forEach(el -> assertThat(countSubstr(el, "A", false, false), equalTo(7L)));
}
@Test
public void countSubstrTestFixture_caseSensitiveTrueAndOverlappingTrue() throws Exception {
assertThat(countSubstr("aaa", "aa", true, true), equalTo(2L));
}
@Test
public void countSubstr_shouldCountSubStrWithEmptySubStr() throws Exception {
assertThat(countSubstr("aaa", "", true, true), equalTo(1L));
}
@Test
public void countSubstr_shouldCountSubStrWithEmptyValueAndEmptySubStr() throws Exception {
assertThat(countSubstr("", "", true, true), equalTo(1L));
}
@Test
public void endsWith_caseSensitive_ShouldBeTrueWhenStringEndsWithSearchStr() throws Exception {
String[] fixture = {
"foo bar",
"bar"
};
Arrays.stream(fixture).forEach(el -> assertTrue(endsWith(el, "bar")));
}
@Test
public void endsWith_notCaseSensitive_ShouldBeTrueWhenStringEndsWithSearchStr() throws Exception {
String[] fixture = {
"foo bar",
"bar"
};
Arrays.stream(fixture).forEach(el -> assertTrue(endsWith(el, "BAR", false)));
}
@Test
public void endsWith_caseSensitiveAtPosition_ShouldBeTrueWhenStringEndsWithSearchStr() throws Exception {
String[] fixture = {
"foo barr",
"barr"
};
Arrays.stream(fixture).forEach(el -> assertTrue(endsWith(el, "bar", el.length() - 1, true)));
}
@Test
public void endsWith_notCaseSensitiveAtPosition_ShouldBeTrueWhenStringEndsWithSearchStr() throws Exception {
String[] fixture = {
"foo barr",
"barr"
};
Arrays.stream(fixture).forEach(el -> assertTrue(endsWith(el, "BAR", el.length() - 1, false)));
}
@Test
public void ensureLeft_shouldEnsureValueStartsWithFoo() throws Exception {
String[] fixture = {
"foobar",
"bar"
};
Arrays.stream(fixture).forEach(el -> assertThat(ensureLeft(el, "foo"), equalTo("foobar")));
}
@Test
public void ensureLeft_notCaseSensitive_shouldEnsureValueStartsWithFoo() throws Exception {
assertThat(ensureLeft("foobar", "FOO", false), equalTo("foobar"));
assertThat(ensureLeft("bar", "FOO", false), equalTo("FOObar"));
}
@Test
public void base64Decode_shouldDecodeABase64DecodedValueToString() throws Exception {
assertThat(base64Decode("c3RybWFu"), equalTo("strman"));
assertThat(base64Decode("Zm9v"), equalTo("foo"));
assertThat(base64Decode("YmFy"), equalTo("bar"));
assertThat(base64Decode("YsOhciE="), equalTo("bár!"));
assertThat(base64Decode("5ryi"), equalTo("漢"));
}
@Test
public void base64Encode_shouldEncodeAString() throws Exception {
assertThat(base64Encode("strman"), equalTo("c3RybWFu"));
assertThat(base64Encode("foo"), equalTo("Zm9v"));
assertThat(base64Encode("bar"), equalTo("YmFy"));
assertThat(base64Encode("bár!"), equalTo("YsOhciE="));
assertThat(base64Encode("漢"), equalTo("5ryi"));
}
@Test
public void binDecode_shouldDecodeABinaryStringToAValue() throws Exception {
assertThat(
binDecode("000000000111001100000000011101000000000001110010000000000110110100000000011000010000000001101110"),
equalTo("strman"));
assertThat(binDecode("0110111100100010"), equalTo("漢"));
assertThat(binDecode("0000000001000001"), equalTo("A"));
assertThat(binDecode("0000000011000001"), equalTo("Á"));
assertThat(binDecode("00000000010000010000000001000001"), equalTo("AA"));
}
@Test
public void binEncode_shouldEncodeAStringToBinaryFormat() throws Exception {
assertThat(binEncode("漢"), equalTo("0110111100100010"));
assertThat(binEncode("A"), equalTo("0000000001000001"));
assertThat(binEncode("Á"), equalTo("0000000011000001"));
assertThat(binEncode("AA"), equalTo("00000000010000010000000001000001"));
}
@Test
public void decDecode_shouldDecodeDecimalStringToString() throws Exception {
assertThat(decDecode("28450"), equalTo("漢"));
assertThat(decDecode("00065"), equalTo("A"));
assertThat(decDecode("00193"), equalTo("Á"));
assertThat(decDecode("0006500065"), equalTo("AA"));
}
@Test
public void decEncode_shouldEncodeStringToDecimal() throws Exception {
assertThat(decEncode("漢"), equalTo("28450"));
assertThat(decEncode("A"), equalTo("00065"));
assertThat(decEncode("Á"), equalTo("00193"));
assertThat(decEncode("AA"), equalTo("0006500065"));
}
@Test
public void ensureRight_shouldEnsureStringEndsWithBar() throws Exception {
final String[] fixture = {
"foo", "foobar", "fooBAR"
};
assertThat(Arrays.stream(fixture).map(el -> ensureRight(el, "bar", false)).collect(toList()), hasItems("foobar", "foobar", "fooBAR"));
assertThat(Arrays.stream(fixture).map(el -> ensureRight(el, "bar")).collect(toList()), hasItems("foobar", "foobar", "fooBARbar"));
}
@Test
public void first_shouldReturnFirstThreeCharsOfString() throws Exception {
final String[] fixture = {
"foo", "foobar"
};
Arrays.stream(fixture).forEach(el -> assertThat(first(el, 3), equalTo(Optional.of("foo"))));
}
@Test
public void head_shouldReturnFirstCharOfString() throws Exception {
final String[] fixture = {
"foo", "foobar"
};
Arrays.stream(fixture).forEach(el -> assertThat(head(el), equalTo(Optional.of("f"))));
}
@Test
public void format_shouldFormatStringsToFooBar() throws Exception {
assertThat(format("{0} bar", "foo"), equalTo("foo bar"));
assertThat(format("foo {0}", "bar"), equalTo("foo bar"));
assertThat(format("foo {0}", "bar", "foo"), equalTo("foo bar"));
assertThat(format("{0} {1}", "foo", "bar"), equalTo("foo bar"));
assertThat(format("{1} {0}", "bar", "foo"), equalTo("foo bar"));
}
@Test(expected = IllegalArgumentException.class)
public void format_shouldThrowExceptionWhenValueDoesNotExist() throws Exception {
assertThat(format("{1} {0}"), equalTo("{1} {0}"));
}
@Test
public void hexDecode_shouldDecodeHexCodeToString() throws Exception {
assertThat(hexDecode("6f22"), equalTo("漢"));
assertThat(hexDecode("0041"), equalTo("A"));
assertThat(hexDecode("00c1"), equalTo("Á"));
assertThat(hexDecode("00410041"), equalTo("AA"));
}
@Test
public void hexEncode_shouldEncodeStringToHexadecimalFormat() throws Exception {
assertThat(hexEncode("漢"), equalTo("6f22"));
assertThat(hexEncode("A"), equalTo("0041"));
assertThat(hexEncode("Á"), equalTo("00c1"));
assertThat(hexEncode("AA"), equalTo("00410041"));
}
@Test
public void indexOf_shouldBeTrueWhenNeedleExists() throws Exception {
final String value = "foobar";
assertThat(indexOf(value, "f", 0, true), equalTo(0));
assertThat(indexOf(value, "o", 0, true), equalTo(1));
assertThat(indexOf(value, "b", 0, true), equalTo(3));
assertThat(indexOf(value, "a", 0, true), equalTo(4));
assertThat(indexOf(value, "r", 0, true), equalTo(5));
assertThat(indexOf(value, "t", 0, true), equalTo(-1));
}
@Test
public void indexOf_shouldBeTrueWhenNeedleExistCaseSensitive() throws Exception {
final String value = "foobar";
assertThat(indexOf(value, "F", 0, false), equalTo(0));
assertThat(indexOf(value, "O", 0, false), equalTo(1));
assertThat(indexOf(value, "B", 0, false), equalTo(3));
assertThat(indexOf(value, "A", 0, false), equalTo(4));
assertThat(indexOf(value, "R", 0, false), equalTo(5));
assertThat(indexOf(value, "T", 0, false), equalTo(-1));
}
@Test
public void inequal_shouldTestInequalityOfStrings() throws Exception {
assertThat(unequal("a", "b"), equalTo(true));
assertThat(unequal("a", "a"), equalTo(false));
assertThat(unequal("0", "1"), equalTo(true));
}
@Test
public void insert_shouldInsertStringAtIndex() throws Exception {
assertThat(insert("fbar", "oo", 1), equalTo("foobar"));
assertThat(insert("foo", "bar", 3), equalTo("foobar"));
assertThat(insert("foobar", "x", 5), equalTo("foobaxr"));
assertThat(insert("foobar", "x", 6), equalTo("foobarx"));
assertThat(insert("foo bar", "asadasd", 100), equalTo("foo bar"));
}
@Test
public void isLowerCase_shouldBeTrueWhenStringIsLowerCase() throws Exception {
assertThat(isLowerCase(""), equalTo(true));
assertThat(isLowerCase("foo"), equalTo(true));
assertThat(isLowerCase("foobarfoo"), equalTo(true));
}
@Test
public void isLowerCase_shouldBeFalseWhenStringIsNotLowerCase() throws Exception {
assertThat(isLowerCase("Foo"), equalTo(false));
assertThat(isLowerCase("foobarfooA"), equalTo(false));
}
@Test
public void isUpperCase_shouldBeTrueWhenStringIsUpperCase() throws Exception {
assertThat(isUpperCase(""), equalTo(true));
assertThat(isUpperCase("FOO"), equalTo(true));
assertThat(isUpperCase("FOOBARFOO"), equalTo(true));
}
@Test
public void isUpperCase_shouldBeFalseWhenStringIsNotUpperCase() throws Exception {
assertThat(isUpperCase("Foo"), equalTo(false));
assertThat(isUpperCase("foobarfooA"), equalTo(false));
}
@Test
public void last_shouldReturnLastNChars() throws Exception {
assertThat(last("foo", 3), equalTo("foo"));
assertThat(last("foobarfoo", 3), equalTo("foo"));
assertThat(last("", 3), equalTo(""));
assertThat(last("f", 3), equalTo("f"));
}
@Test
public void leftPad_shouldAddPaddingOnTheLeft() throws Exception {
assertThat(leftPad("1", "0", 5), equalTo("00001"));
assertThat(leftPad("01", "0", 5), equalTo("00001"));
assertThat(leftPad("001", "0", 5), equalTo("00001"));
assertThat(leftPad("0001", "0", 5), equalTo("00001"));
assertThat(leftPad("00001", "0", 5), equalTo("00001"));
}
@Test
public void isString_shouldBeFalseWhenValueIsNotString() throws Exception {
assertFalse(isString(1));
assertFalse(isString(false));
assertFalse(isString(1.2));
assertFalse(isString(new String[]{}));
}
@Test
public void isString_shouldBeTrueWhenValueIsString() throws Exception {
assertTrue(isString("string"));
assertTrue(isString(""));
}
@Test
public void lastIndexOf_shouldFindIndexOfNeedle() throws Exception {
final String value = "foobarfoobar";
assertThat(lastIndexOf(value, "f"), equalTo(6));
assertThat(lastIndexOf(value, "o"), equalTo(8));
assertThat(lastIndexOf(value, "b"), equalTo(9));
assertThat(lastIndexOf(value, "a"), equalTo(10));
assertThat(lastIndexOf(value, "r"), equalTo(11));
assertThat(lastIndexOf(value, "t"), equalTo(-1));
}
@Test
public void lastIndexOf_shouldFindIndexOfNeedleCaseInsensitive() throws Exception {
final String value = "foobarfoobar";
assertThat(lastIndexOf(value, "F", false), equalTo(6));
assertThat(lastIndexOf(value, "O", false), equalTo(8));
assertThat(lastIndexOf(value, "B", false), equalTo(9));
assertThat(lastIndexOf(value, "A", false), equalTo(10));
assertThat(lastIndexOf(value, "R", false), equalTo(11));
assertThat(lastIndexOf(value, "T", false), equalTo(-1));
}
@Test
public void leftTrim_shouldRemoveSpacesOnLeft() throws Exception {
assertThat(leftTrim(" strman"), equalTo("strman"));
assertThat(leftTrim(" strman "), equalTo("strman "));
}
@Test
public void prepend_shouldPrependStrings() throws Exception {
assertThat(prepend("r", "f", "o", "o", "b", "a"), equalTo("foobar"));
assertThat(prepend("foobar"), equalTo("foobar"));
assertThat(prepend("", "foobar"), equalTo("foobar"));
assertThat(prepend("bar", "foo"), equalTo("foobar"));
}
@Test
public void prependArray_shouldPrependStrings() throws Exception {
assertThat(prependArray("r", new String[]{"f", "o", "o", "b", "a"}), equalTo("foobar"));
assertThat(prependArray("foobar", new String[0]), equalTo("foobar"));
assertThat(prependArray("", new String[]{"foobar"}), equalTo("foobar"));
assertThat(prependArray("bar", new String[]{"foo"}), equalTo("foobar"));
}
@Test
public void removeEmptyStrings_shouldRemoveEmptyStrings() throws Exception {
assertThat(removeEmptyStrings(new String[]{"aa", "", " ", "bb", "cc", null}), arrayContaining("aa", "bb", "cc"));
assertThat(removeEmptyStrings(new String[0]), emptyArray());
}
@Test
public void removeLeft_shouldRemoveStringFromLeft() throws Exception {
final String[] fixture = {
"foobar",
"bar"
};
Arrays.stream(fixture).forEach(el -> assertThat(removeLeft(el, "foo"), equalTo("bar")));
assertThat(removeLeft("barfoo", "foo"), equalTo("barfoo"));
assertThat(removeLeft("foofoo", "foo"), equalTo("foo"));
}
@Test
public void removeLeft_shouldRemoveStringFromLeftCaseInSensitive() throws Exception {
final String[] fixture = {
"foobar",
"bar"
};
Arrays.stream(fixture).forEach(el -> assertThat(removeLeft(el, "FOO", false), equalTo("bar")));
}
@Test
public void removeNonWords_shouldRemoveAllNonWordsFromInputString() throws Exception {
final String[] fixture = {
"foo bar",
"foo&bar-",
"foobar"
};
Arrays.stream(fixture).forEach(el -> assertThat(removeNonWords(el), equalTo("foobar")));
}
@Test
public void removeRight_shouldRemoveStringFromRight() throws Exception {
final String[] fixture = {
"foobar",
"foo"
};
Arrays.stream(fixture).forEach(el -> assertThat(removeRight(el, "bar"), equalTo("foo")));
assertThat(removeRight("barfoo", "bar"), equalTo("barfoo"));
assertThat(removeRight("barbar", "bar"), equalTo("bar"));
}
@Test
public void removeRight_shouldRemoveStringFromRightCaseInSensitive() throws Exception {
final String[] fixture = {
"foobar",
"foo"
};
Arrays.stream(fixture).forEach(el -> assertThat(removeRight(el, "BAR", false), equalTo("foo")));
}
@Test
public void removeSpaces_shouldRemoveSpacesInTheString() throws Exception {
final String[] fixture = {
"foo bar",
"foo bar ",
" foo bar",
" foo bar "
};
Arrays.stream(fixture).forEach(el -> assertThat(removeSpaces(el), equalTo("foobar")));
}
@Test
public void repeat_shouldRepeatAStringNTimes() throws Exception {
assertThat(repeat("1", 1), equalTo("1"));
assertThat(repeat("1", 2), equalTo("11"));
assertThat(repeat("1", 3), equalTo("111"));
assertThat(repeat("1", 4), equalTo("1111"));
assertThat(repeat("1", 5), equalTo("11111"));
}
@Test
public void replace_shouldReplaceAllOccurrencesOfString() throws Exception {
assertThat(replace("foo bar", "foo", "bar", true), equalTo("bar bar"));
assertThat(replace("foo bar foo", "foo", "bar", true), equalTo("bar bar bar"));
}
@Test
public void replace_shouldReplaceAllOccurrencesOfStringCaseSensitive() throws Exception {
assertThat(replace("FOO bar", "foo", "bar", false), equalTo("bar bar"));
assertThat(replace("FOO bar foo", "foo", "bar", false), equalTo("bar bar bar"));
}
@Test
public void reverse_shouldReverseInputString() throws Exception {
assertThat(reverse(""), equalTo(""));
assertThat(reverse("foo"), equalTo("oof"));
assertThat(reverse("shekhar"), equalTo("rahkehs"));
assertThat(reverse("bar"), equalTo("rab"));
assertThat(reverse("foo_"), equalTo("_oof"));
assertThat(reverse("f"), equalTo("f"));
}
@Test
public void rightPad_shouldRightPadAString() throws Exception {
assertThat(rightPad("1", "0", 5), equalTo("10000"));
assertThat(rightPad("10", "0", 5), equalTo("10000"));
assertThat(rightPad("100", "0", 5), equalTo("10000"));
assertThat(rightPad("1000", "0", 5), equalTo("10000"));
assertThat(rightPad("10000", "0", 5), equalTo("10000"));
assertThat(rightPad("10000000", "0", 5), equalTo("10000000"));
}
@Test
public void rightTrim_shouldRemoveSpacesFromTheRight() throws Exception {
assertThat(rightTrim("strman "), equalTo("strman"));
assertThat(rightTrim(" strman"), equalTo(" strman"));
assertThat(rightTrim("strman"), equalTo("strman"));
}
@Test
public void safeTruncate_shouldSafelyTruncateStrings() throws Exception {
assertThat(safeTruncate("foo bar", 0, "."), equalTo(""));
assertThat(safeTruncate("foo bar", 4, "."), equalTo("foo."));
assertThat(safeTruncate("foo bar", 3, "."), equalTo("."));
assertThat(safeTruncate("foo bar", 2, "."), equalTo("."));
assertThat(safeTruncate("foo bar", 7, "."), equalTo("foo bar"));
assertThat(safeTruncate("foo bar", 8, "."), equalTo("foo bar"));
assertThat(safeTruncate("A Javascript string manipulation library.", 16, "..."), equalTo("A Javascript..."));
assertThat(safeTruncate("A Javascript string manipulation library.", 15, "..."), equalTo("A Javascript..."));
assertThat(safeTruncate("A Javascript string manipulation library.", 14, "..."), equalTo("A..."));
assertThat(safeTruncate("A Javascript string manipulation library.", 13, "..."), equalTo("A..."));
}
@Test
public void truncate_shouldTruncateString() throws Exception {
assertThat(truncate("foo bar", 0, "."), equalTo(""));
assertThat(truncate("foo bar", 3, "."), equalTo("fo."));
assertThat(truncate("foo bar", 2, "."), equalTo("f."));
assertThat(truncate("foo bar", 4, "."), equalTo("foo."));
assertThat(truncate("foo bar", 7, "."), equalTo("foo bar"));
assertThat(truncate("foo bar", 8, "."), equalTo("foo bar"));
assertThat(truncate("A Javascript string manipulation library.", 16, "..."), equalTo("A Javascript ..."));
assertThat(truncate("A Javascript string manipulation library.", 15, "..."), equalTo("A Javascript..."));
assertThat(truncate("A Javascript string manipulation library.", 14, "..."), equalTo("A Javascrip..."));
}
@Test
public void htmlDecode_shouldDecodeToHtml() throws Exception {
assertThat(htmlDecode("á"), equalTo("\u00E1"));
assertThat(htmlDecode("Ш"), equalTo("Ш"));
assertThat(htmlDecode("Ж"), equalTo("Ж"));
assertThat(htmlDecode("┐"), equalTo("┐"));
}
@Test
public void htmlEncode_shouldBeEncodedToHtmlEntities() throws Exception {
assertThat(htmlEncode("á"), equalTo("á"));
assertThat(htmlEncode("áéíóú"), equalTo("áéíóú"));
assertThat(htmlEncode("Ш"), equalTo("Ш"));
assertThat(htmlEncode("Ж"), equalTo("Ж"));
assertThat(htmlEncode("┐"), equalTo("┐"));
}
@Test
public void shuffle_shouldShuffleAString() throws Exception {
assertThat(shuffle("shekhar"), not(equalTo("shekhar")));
assertThat(shuffle("strman"), not(equalTo("strman")));
assertThat(shuffle(""), equalTo(""));
assertThat(shuffle("s"), equalTo("s"));
}
@Test
public void slugify_shouldBeFooBar() throws Exception {
String[] fixture = {
"foo bar",
"foo bar.",
"foo bar ",
" foo bar",
" foo bar ",
"foo------bar",
"fóõ bár",
"foo ! bar",
"foo ~~ bar",
"foo bar",
"FOO bar"
};
Arrays.stream(fixture).forEach(el -> assertThat(String.format("slugify(%s) should be foo-bar ", el), slugify(el), equalTo("foo-bar")));
}
@Test
public void slugify_shouldBeFooAndBar() throws Exception {
String[] fixture = {
"foo&bar",
"foo&bar.",
"foo&bar ",
" foo&bar",
" foo&bar ",
"foo&bar",
"fóõ-and---bár",
"foo & bar",
"FOO & bar"
};
Arrays.stream(fixture).forEach(el -> assertThat(String.format("slugify(%s) should be foo-and-bar ", el), slugify(el), equalTo("foo-and-bar")));
}
@Test
public void transliterate_shouldTransliterateTheText() throws Exception {
assertThat(transliterate("fóõ bár"), equalTo("foo bar"));
}
@Test
public void surround_shouldSurroundStringWithPrefixAndSuffix() throws Exception {
assertThat(surround("foo", "bar", null), equalTo("barfoobar"));
assertThat(surround("shekhar", "***", null), equalTo("***shekhar***"));
assertThat(surround("", ">", null), equalTo(">>"));
assertThat(surround("bar", "", null), equalTo("bar"));
assertThat(surround("f", null, null), equalTo("f"));
assertThat(surround("div", "<", ">"), equalTo("<div>"));
}
@Test
public void toCamelCase_shouldConvertStringToCamelCase() throws Exception {
String[] fixture = {
"CamelCase",
"camelCase",
"Camel case",
"Camel case",
"camel Case",
"camel-case",
"-camel--case",
"camel_case",
" camel_case",
};
Arrays.stream(fixture).forEach(el -> assertThat(String.format("toCameCase(%s) should be camelCase", el), toCamelCase(el), equalTo("camelCase")));
assertThat(toCamelCase("c"), equalTo("c"));
}
@Test
public void toDeCamelCase_shouldDeCamelCaseAString() throws Exception {
String[] fixture = {
"deCamelize",
"de-Camelize",
"de camelize",
"de camelize",
"de Camelize",
"de-camelize",
"-de--camelize",
"de_camelize",
" de_camelize"
};
Arrays.stream(fixture).forEach(el -> assertThat(String.format("toDecamelize(%s) should be de-camelize", el), toDecamelize(el, null), equalTo("de camelize")));
assertThat(toDecamelize("camelCase", "_"), equalTo("camel_case"));
}
@Test
public void toKebabCase_shouldKebabCaseAString() throws Exception {
String[] fixture = {
"deCamelize",
"de-Camelize",
"de camelize",
"de camelize",
"de Camelize",
"de-camelize",
"-de--camelize",
"de_camelize",
" de_camelize"
};
Arrays.stream(fixture).forEach(el ->
assertThat(String.format("toKebabCase(%s) should be de-camelize", el), toKebabCase(el), equalTo("de-camelize")));
}
@Test
public void toSnakeCase_shouldSnakeCaseAString() throws Exception {
String[] fixture = {
"deCamelize",
"de-Camelize",
"de camelize",
"de camelize",
"de Camelize",
"de-camelize",
"-de--camelize",
"de_camelize",
" de_camelize"
};
Arrays.stream(fixture).forEach(el -> assertThat(String.format("toSnakeCase(%s) should be de_camelize", el), toSnakeCase(el), equalTo("de_camelize")));
}
@Test
public void snakeCase_shouldConvertAStringToSnakecase() throws Exception {
String[] input = {
"Foo Bar",
"fooBar"
};
Arrays.stream(input).forEach(el ->
assertThat(String.format("%s should be foo_bar", el), toSnakeCase(el), is(equalTo("foo_bar"))));
}
@Test
public void unequal_shouldTestInequalityOfStrings() throws Exception {
assertThat(unequal("a", "b"), equalTo(true));
assertThat(unequal("a", "a"), equalTo(false));
assertThat(unequal("0", "1"), equalTo(true));
}
@Test
public void removeLeft_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = removeLeft("This HAS A THIS IN FRONT", "THIS ", false);
assertThat(result, is("HAS A THIS IN FRONT"));
}
@Test
public void replace_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = replace("One and two and THREE and Four", "and", "&", false);
assertThat(result, is("One & two & THREE & Four"));
}
@Test
public void removeRight_shouldNotLowercaseWhenCaseInsensitive() throws Exception {
String result = removeRight("Remove the END at the end", " END", false);
assertThat(result, is("Remove the END at the"));
}
@Test
public void transliterate_shouldDeburrTheString() throws Exception {
String result = transliterate("déjà vu");
assertThat(result, is(equalTo("deja vu")));
}
@Ignore
public void htmlEncode_shouldConvertCharactersToTheirHtmlEntities() throws Exception {
String result = htmlEncode("fred, barney, & pebbles");
assertThat(result, is(equalTo("fred, barney, & pebbles")));
}
@Test
public void kebabCase_shouldConvertAStringToKebabCase() throws Exception {
String[] input = {
"Foo Bar",
"fooBar"
};
Arrays.stream(input).forEach(el ->
assertThat(String.format("%s should be foo-bar", el), toKebabCase(el), is(equalTo("foo-bar"))));
}
@Test
public void join_shouldJoinArrayOfStringIntoASingleString() throws Exception {
String[] strings = {
"hello",
"world",
"123"
};
assertThat(join(strings, ";"), is(equalTo("hello;world;123")));
}
@Test(expected = IllegalArgumentException.class)
public void join_shouldThrowIllegalArgumentExceptionWhenSeparatorIsNull() throws Exception {
String[] strings = {
"hello",
"world",
"123"
};
join(strings, null);
}
@Test
public void join_shouldReturnEmptyStringWhenInputArrayIsEmpty() throws Exception {
String[] emptyArray = {};
assertThat(join(emptyArray, ","), is(equalTo("")));
}
@Test
public void capitalize_shouldCapitalizeFirstCharacterOfString() throws Exception {
String[] strings = {
"FRED",
"fRED",
"fred"
};
Arrays.stream(strings).forEach(el -> assertThat(String.format("%s should be Fred", el), capitalize(el), equalTo("Fred")));
}
@Test
public void lowerFirst_shouldLowercasedFirstCharacterOfString() throws Exception {
assertThat(lowerFirst("FRED"), is(equalTo("fRED")));
assertThat(lowerFirst("fred"), is(equalTo("fred")));
assertThat(lowerFirst("Fred"), is(equalTo("fred")));
}
@Test
public void isEnclosedBetween_shouldChekcWhetherStringIsEnclosed() throws Exception {
assertThat(isEnclosedBetween("{{shekhar}}", "{{", "}}"), is(true));
assertThat(isEnclosedBetween("shekhar", "{{", "}}"), is(false));
assertThat(isEnclosedBetween("*shekhar*", "*"), is(true));
assertThat(isEnclosedBetween("shekhar", "*"), is(false));
}
@Test(expected = IllegalArgumentException.class)
public void isEnclosedBetween_shouldThrowIllegalArgumentExceptionWhenEncloserIsNull() throws Exception {
assertThat(isEnclosedBetween("shekhar", null), is(false));
}
@Test
public void words_shouldConvertTextToWords() throws Exception {
final String line = "This is a string, with words!";
assertThat(words(line), arrayContaining("This", "is", "a", "string,", "with", "words!"));
}
@Test
public void upperFirst_shouldConvertFirstCharToUpperCase() throws Exception {
assertThat(upperFirst("fred"), is("Fred"));
}
@Test
public void upperFirst_shouldReturnSameStringIfFirstCharIsUpperCase() throws Exception {
assertThat(upperFirst("FRED"), is("FRED"));
}
@Test
public void trimStart_shouldRemoveAllWhitespaceAtStart() throws Exception {
assertThat(trimStart(" abc "), is(Optional.of("abc ")));
assertThat(trimStart("abc "), is(Optional.of("abc ")));
assertThat(trimStart("abc"), is(Optional.of("abc")));
assertThat(trimStart(""), is(Optional.empty()));
assertThat(trimStart(null), is(Optional.empty()));
}
@Test
public void trimStart_shouldRemoveSpecialCharactersAtStart() throws Exception {
assertThat(trimStart("-_-abc-_-", "_", "-"), is(Optional.of("abc-_-")));
assertThat(trimStart("-_-!abc-_-", "_", "-", "!"), is(Optional.of("abc-_-")));
assertThat(trimStart("-_-#abc-_-", "_", "-", "!", "#"), is(Optional.of("abc-_-")));
}
@Test
public void trimEnd_shouldRemoveAllTrailingWhitespace() throws Exception {
assertThat(trimEnd(" abc "), is(Optional.of(" abc")));
assertThat(trimEnd("abc "), is(Optional.of("abc")));
assertThat(trimEnd("abc"), is(Optional.of("abc")));
assertThat(trimEnd(""), is(Optional.empty()));
assertThat(trimEnd(null), is(Optional.empty()));
}