-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathSnippetsTests.java
More file actions
721 lines (601 loc) · 25.1 KB
/
Copy pathSnippetsTests.java
File metadata and controls
721 lines (601 loc) · 25.1 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
package snippets;
import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.stream.IntStream;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class SnippetsTests {
@Test
public void gcd_of_array_containing_1_to_5_is_1() throws Exception {
OptionalInt gcd = Snippets.gcd(new int[]{1, 2, 3, 4, 5});
assertThat(gcd).isNotEmpty();
assertThat(gcd).hasValue(1);
}
@Test
public void gcd_of_array_containing_4_8_and_12_is_4() throws Exception {
OptionalInt gcd = Snippets.gcd(new int[]{4, 8, 12});
assertThat(gcd).isNotEmpty();
assertThat(gcd).hasValue(4);
}
@Test
public void lcm_of_array_containing_1_to_5_is_60() throws Exception {
OptionalInt lcm = Snippets.lcm(new int[]{1, 2, 3, 4, 5});
assertThat(lcm).isNotEmpty();
assertThat(lcm).hasValue(60);
}
@Test
public void lcm_of_array_containing_4_8_and_12_is_24() throws Exception {
OptionalInt lcm = Snippets.lcm(new int[]{4, 8, 12});
assertThat(lcm).isNotEmpty();
assertThat(lcm).hasValue(24);
}
@Test
public void max_of_array_containing_10_1_and_5_is_10() throws Exception {
OptionalInt max = Snippets.arrayMax(new int[]{10, 1, 5});
assertThat(max).hasValue(10);
}
@Test
public void min_of_array_containing_10_1_and_5_is_10() throws Exception {
OptionalInt min = Snippets.arrayMin(new int[]{10, 1, 5});
assertThat(min).hasValue(1);
}
@Test
public void chunk_breaks_input_array__with_odd_length() throws Exception {
int[][] chunks = Snippets.chunk(new int[]{1, 2, 3, 4, 5}, 2);
assertThat(chunks)
.containsExactly(
new int[]{1, 2},
new int[]{3, 4},
new int[]{5}
);
}
@Test
public void chunk_breaks_input_array__with_event_length() throws Exception {
int[][] chunks = Snippets.chunk(new int[]{1, 2, 3, 4, 5, 6}, 2);
assertThat(chunks)
.containsExactly(
new int[]{1, 2},
new int[]{3, 4},
new int[]{5, 6}
);
}
@Test
public void countOccurrences_counts_occurrences_of_a_value() throws Exception {
long count = Snippets.countOccurrences(new int[]{1, 1, 2, 1, 2, 3}, 1);
assertThat(count).isEqualTo(3);
}
@Test
public void deepFlatten_flatten_a_deeply_nested_array() throws Exception {
int[] flatten = Snippets.deepFlatten(
new Object[]{1, new Object[]{2}, new Object[]{3, 4, 5}}
);
assertThat(flatten).isEqualTo(new int[]{1, 2, 3, 4, 5});
}
@Test
public void difference_between_array_with_1_2_3_and_array_with_1_2_4_is_3() throws Exception {
int[] difference = Snippets.difference(new int[]{1, 2, 3}, new int[]{1, 2, 4});
assertThat(difference).isEqualTo(new int[]{3});
}
@Test
public void difference_between_array_with_1_2_3_and_array_with_1_2_3_is_empty_array() throws Exception {
int[] difference = Snippets.difference(new int[]{1, 2, 3}, new int[]{1, 2, 3});
assertThat(difference).isEmpty();
}
@Test
public void differenceWith_return_all_squares_that_do_not_exist_in_second() throws Exception {
int[] difference = Snippets.differenceWith(
new int[]{1, 4, 9, 16, 25},
new int[]{1, 2, 3, 6, 7},
(o1, o2) -> o1 - (o2 * o2)
);
assertThat(difference).isEqualTo(new int[]{16, 25});
}
@Test
public void differenceWith_returns_empty_array_when_two_arrays_are_equal_as_per_comparison_operation() throws Exception {
int[] difference = Snippets.differenceWith(
new int[]{1, 2, 3},
new int[]{1, 2, 3},
(o1, o2) -> o1 - o2
);
assertThat(difference).isEmpty();
}
@Test
public void differenceWith_returns_first_array_when_elements_in_second_array_are_not_comparable_as_per_comparison_operation() throws Exception {
int[] difference = Snippets.differenceWith(
new int[]{1, 2, 3},
new int[]{10, 11, 12},
(o1, o2) -> o1 - o2
);
assertThat(difference).isEqualTo(new int[]{1, 2, 3});
}
@Test
public void distinct_remove_all_duplicate_values_from_an_array() throws Exception {
int[] distinct = Snippets.distinctValuesOfArray(new int[]{1, 2, 2, 3, 4, 4, 5});
assertThat(distinct).isEqualTo(new int[]{1, 2, 3, 4, 5});
}
@Test
public void drop_elements_less_than_3() throws Exception {
int[] elements = Snippets.dropElements(new int[]{1, 2, 3, 4}, i -> i >= 3);
assertThat(elements).isEqualTo(new int[]{3, 4});
}
@Test
public void drop_elements_returns_empty_array_when_no_element_match_the_condition() throws Exception {
int[] elements = Snippets.dropElements(new int[]{1, 2, 3, 4}, i -> i < 1);
assertThat(elements).isEmpty();
}
@Test
public void drop_elements__return_all_elements_when_all_elements_match_the_condition() throws Exception {
int[] elements = Snippets.dropElements(new int[]{1, 2, 3, 4}, i -> i <= 4);
assertThat(elements).isEqualTo(new int[]{1, 2, 3, 4});
}
@Test
public void dropRight_remove_n_elements_from_right() throws Exception {
int[] elements = Snippets.dropRight(new int[]{1, 2, 3}, 1);
assertThat(elements).isEqualTo(new int[]{1, 2});
elements = Snippets.dropRight(new int[]{1, 2, 3}, 2);
assertThat(elements).isEqualTo(new int[]{1});
elements = Snippets.dropRight(new int[]{1, 2, 3}, 3);
assertThat(elements).isEmpty();
elements = Snippets.dropRight(new int[]{1, 2, 3}, 42);
assertThat(elements).isEmpty();
}
@Test
public void everyNth_return_every_2nd_element() throws Exception {
int[] elements = Snippets.everyNth(new int[]{1, 2, 3, 4, 5, 6}, 2);
assertThat(elements).isEqualTo(new int[]{2, 4, 6});
}
@Test
public void filterNonUnique_return_unique_elements() throws Exception {
int[] elements = Snippets.filterNonUnique(new int[]{1, 2, 2, 3, 4, 4, 5});
assertThat(elements).isEqualTo(new int[]{1, 3, 5});
}
@Test
public void filterNonUnique_return_same_array_when_all_unique() throws Exception {
int[] elements = Snippets.filterNonUnique(new int[]{1, 2, 3, 4, 5});
assertThat(elements).isEqualTo(new int[]{1, 2, 3, 4, 5});
}
@Test
public void filterNonUnique_return_empty_array_when_all_duplicated() throws Exception {
int[] elements = Snippets.filterNonUnique(new int[]{1, 1, 2, 2, 3, 3, 4, 4, 5, 5});
assertThat(elements).isEmpty();
}
@Test
public void flatten_flat_one_level_array() throws Exception {
int[] flatten = Snippets.flatten(new Object[]{1, new int[]{2}, 3, 4});
assertThat(flatten).isEqualTo(new int[]{1, 2, 3, 4});
}
@Test
public void flattenDepth_flatten_to_specified_depth() throws Exception {
Object[] input = {
1,
new Object[]{2},
new Object[]{
new Object[]{
new Object[]{
3
},
4
}, 5
}
};
Object[] flatten = Snippets.flattenDepth(input, 2);
assertThat(flatten).isEqualTo(new Object[]{1, 2, new Object[]{3}, 4, 5});
}
@Test
public void group_elements_by_length() throws Exception {
Map<Integer, List<String>> groups = Snippets.groupBy(new String[]{"one", "two", "three"}, String::length);
assertThat(groups)
.containsOnly(
new SimpleEntry<>(3, Arrays.asList("one", "two")),
new SimpleEntry<>(5, Collections.singletonList("three"))
);
}
@Test
public void initial_return_array_except_last_element() throws Exception {
Integer[] initial = Snippets.initial(new Integer[]{1, 2, 3});
assertThat(initial).isEqualTo(new Integer[]{1, 2});
}
@Test
public void initializeArrayWithRange_from_1_to_5() throws Exception {
int[] numbers = Snippets.initializeArrayWithRange(5, 1);
assertThat(numbers).isEqualTo(new int[]{1, 2, 3, 4, 5});
}
@Test
public void initializeArrayWithValues() throws Exception {
int[] elements = Snippets.initializeArrayWithValues(5, 2);
assertThat(elements).isEqualTo(new int[]{2, 2, 2, 2, 2});
}
@Test
public void intersection_between_two_arrays() throws Exception {
int[] elements = Snippets.intersection(new int[]{1, 2, 3}, new int[]{4, 3, 2});
assertThat(elements).isEqualTo(new int[]{2, 3});
}
@Test
public void isSorted_return_1_when_array_sorted_is_ascending_order() throws Exception {
int sorted = Snippets.isSorted(new Integer[]{0, 1, 2, 3});
assertThat(sorted).isEqualTo(1);
sorted = Snippets.isSorted(new Integer[]{0, 1, 2, 2});
assertThat(sorted).isEqualTo(1);
}
@Test
public void isSorted_return_minus_1_when_array_sorted_in_descending_order() throws Exception {
int sorted = Snippets.isSorted(new Integer[]{3, 2, 1, 0});
assertThat(sorted).isEqualTo(-1);
sorted = Snippets.isSorted(new Integer[]{3, 3, 2, 1, 0});
assertThat(sorted).isEqualTo(-1);
}
@Test
public void isSorted_returns_0_when_array_is_not_sorted() throws Exception {
int sorted = Snippets.isSorted(new Integer[]{3, 4, 1, 0});
assertThat(sorted).isEqualTo(0);
}
@Test
public void join_should_create_string_from_an_array_with_different_sep_and_end() throws Exception {
String joined = Snippets.join(new String[]{"pen", "pineapple", "apple", "pen"}, ",", "&");
assertThat(joined).isEqualTo("pen,pineapple,apple&pen");
}
@Test
public void join_should_create_string_from_an_array_with_sep_only() throws Exception {
String joined = Snippets.join(new String[]{"pen", "pineapple", "apple", "pen"}, ",");
assertThat(joined).isEqualTo("pen,pineapple,apple,pen");
}
@Test
public void join_should_create_string_from_an_array_with_default_sep() throws Exception {
String joined = Snippets.join(new String[]{"pen", "pineapple", "apple", "pen"});
assertThat(joined).isEqualTo("pen,pineapple,apple,pen");
}
@Test
public void join_should_create_empty_string_with_empty_array() throws Exception {
String joined = Snippets.join(new String[]{});
assertThat(joined).isEqualTo("");
}
@Test
public void nthElement_return_nth_element_from_start_when_n_is_greater_than_0() throws Exception {
String nthElement = Snippets.nthElement(new String[]{"a", "b", "c"}, 1);
assertThat(nthElement).isEqualTo("b");
}
@Test
public void nthElement_return_nth_element_from_end_when_n_is_less_than_0() throws Exception {
String nthElement = Snippets.nthElement(new String[]{"a", "b", "c"}, -3);
assertThat(nthElement).isEqualTo("a");
}
@Test
public void pick_should_pick_key_pairs_corresponding_to_keys() throws Exception {
Map<String, Integer> obj = new HashMap<>();
obj.put("a", 1);
obj.put("b", 2);
obj.put("c", 3);
Map<String, Integer> picked = Snippets.pick(obj, new String[]{"a", "c"});
assertThat(picked).containsOnly(new SimpleEntry<>("a", 1), new SimpleEntry<>("c", 3));
}
@Test
public void reducedFilter_Test() throws Exception {
Map<String, Object> item1 = new HashMap<>();
item1.put("id", 1);
item1.put("name", "john");
item1.put("age", 24);
Map<String, Object> item2 = new HashMap<>();
item2.put("id", 2);
item2.put("name", "mike");
item2.put("age", 50);
Map<String, Object>[] filtered = Snippets.reducedFilter((Map<String, Object>[]) new Map[]{item1, item2}, new String[]{"id", "name"}, item -> (Integer) item.get("age") > 24);
assertThat(filtered).hasSize(1);
assertThat(filtered[0])
.containsOnly(
new SimpleEntry<String, Object>("id", 2),
new SimpleEntry<String, Object>("name", "mike"));
}
@Test
public void sample_should_return_random_element() throws Exception {
Integer sample = Snippets.sample(new Integer[]{3, 7, 9, 11});
assertThat(sample).isIn(Arrays.asList(3, 7, 9, 11));
}
@Test
public void sampleSize_should_return_sample_of_size_array_length_when_sample_size_is_greater_than_array_size() throws Exception {
Integer[] sample = Snippets.sampleSize(new Integer[]{1, 2, 3}, 4);
assertThat(sample).hasSize(3);
}
@Test
public void similarity_test() throws Exception {
Integer[] arr = Snippets.similarity(new Integer[]{1, 2, 3}, new Integer[]{1, 2, 4});
assertThat(arr).isEqualTo(new Integer[]{1, 2});
}
@Test
public void empty_array() throws Exception {
String[] empty = Snippets.emptyArray(String.class);
assertThat(empty).isEmpty();
}
@Test
public void sortedIndex_descending() throws Exception {
int index = Snippets.sortedIndex(new Integer[]{5, 3, 2, 1}, 4);
assertThat(index).isEqualTo(1);
}
@Test
public void sortedIndex_ascending() throws Exception {
int index = Snippets.sortedIndex(new Integer[]{30, 50}, 40);
assertThat(index).isEqualTo(1);
}
@Test
public void sortedIndex_ascending_at_end() throws Exception {
int index = Snippets.sortedIndex(new Integer[]{30, 50}, 60);
assertThat(index).isEqualTo(2);
}
@Test
public void symmetricDifference_test() throws Exception {
Integer[] diff = Snippets.symmetricDifference(
new Integer[]{1, 2, 3},
new Integer[]{1, 2, 4}
);
assertThat(diff).isEqualTo(new Integer[]{3, 4});
}
@Test
public void union_test() throws Exception {
Integer[] union = Snippets.union(
new Integer[]{1, 2, 3},
new Integer[]{1, 2, 4}
);
assertThat(union).containsExactlyInAnyOrder(new Integer[]{1, 2, 3, 4});
}
@Test
public void without_test() throws Exception {
Integer[] without = Snippets.without(
new Integer[]{2, 1, 2, 3},
1, 2
);
assertThat(without).isEqualTo(new Integer[]{3});
}
@Test
public void zip_test() throws Exception {
List<Object[]> zipped = Snippets.zip(
new String[]{"a", "b"},
new Integer[]{1, 2},
new Boolean[]{true, false}
);
assertThat(zipped).hasSize(2);
assertThat(zipped.get(0)).isEqualTo(new Object[]{"a", 1, true});
assertThat(zipped.get(1)).isEqualTo(new Object[]{"b", 2, false});
}
@Test
public void zip_test_2() throws Exception {
List<Object[]> zipped = Snippets.zip(
new String[]{"a"},
new Integer[]{1, 2},
new Boolean[]{true, false}
);
assertThat(zipped).hasSize(2);
assertThat(zipped.get(0)).isEqualTo(new Object[]{"a", 1, true});
assertThat(zipped.get(1)).isEqualTo(new Object[]{null, 2, false});
}
@Test
public void zipObject_test_1() throws Exception {
Map<String, Object> map = Snippets.zipObject(
new String[]{"a", "b", "c"},
new Integer[]{1, 2}
);
assertThat(map).containsOnly(
new SimpleEntry<>("a", 1),
new SimpleEntry<>("b", 2),
new SimpleEntry<>("c", null)
);
}
@Test
public void zipObject_test_2() throws Exception {
Map<String, Object> map = Snippets.zipObject(
new String[]{"a", "b"},
new Integer[]{1, 2, 3}
);
assertThat(map).containsOnly(
new SimpleEntry<>("a", 1),
new SimpleEntry<>("b", 2)
);
}
@Test
public void average_of_1_to_10_is_5_dot_5() throws Exception {
double average = Snippets.average(IntStream.rangeClosed(1, 10).toArray());
assertThat(average).isEqualTo(5.5);
}
@Test
public void capitalize_test() throws Exception {
assertThat(Snippets.capitalize("fooBar", false)).isEqualTo("FooBar");
assertThat(Snippets.capitalize("fooBar", true)).isEqualTo("Foobar");
}
@Test
public void capitalizeEveryWord_test() throws Exception {
assertThat(Snippets.capitalizeEveryWord("hello world!")).isEqualTo("Hello World!");
}
@Test
public void anagrams_test() throws Exception {
List<String> anagrams = Snippets.anagrams("abc");
assertThat(anagrams)
.containsOnly("abc", "acb", "bac", "bca", "cab", "cba");
}
@Test
public void byteSize_of_smiley_is_4() throws Exception {
int length = Snippets.byteSize("\uD83D\uDE00");
assertThat(length).isEqualTo(4);
}
@Test
public void byteSize_of_hello_world_is_11() throws Exception {
assertThat(Snippets.byteSize("Hello World")).isEqualTo(11);
}
@Test
public void countVowels_test() throws Exception {
assertThat(Snippets.countVowels("foobar")).isEqualTo(3);
}
@Test
public void escapeRegex_test() throws Exception {
assertThat(Snippets.escapeRegExp("(test)")).isEqualTo("\\Q(test)\\E");
}
@Test
public void fromCamelCase_test() throws Exception {
assertThat(Snippets.fromCamelCase("someJavaProperty", "_"))
.isEqualTo("some_java_property");
assertThat(Snippets.fromCamelCase("someDatabaseFieldName", " "))
.isEqualTo("some database field name");
assertThat(Snippets.fromCamelCase("someLabelThatNeedsToBeCamelized", "-"))
.isEqualTo("some-label-that-needs-to-be-camelized");
}
@Test
public void isAbsoluteUrl_test() throws Exception {
assertThat(Snippets.isAbsoluteUrl("https://google.com")).isTrue();
assertThat(Snippets.isAbsoluteUrl("ftp://www.myserver.net")).isTrue();
assertThat(Snippets.isAbsoluteUrl("/foo/bar")).isFalse();
}
@Test
public void isLowerCase_test() throws Exception {
assertThat(Snippets.isLowerCase("abc")).isTrue();
assertThat(Snippets.isLowerCase("a3@$")).isTrue();
assertThat(Snippets.isLowerCase("Ab4")).isFalse();
}
@Test
public void mask_test() throws Exception {
assertThat(Snippets.mask("1234567890", 4, "*")).isEqualTo("******7890");
assertThat(Snippets.mask("1234567890", 3, "*")).isEqualTo("*******890");
assertThat(Snippets.mask("1234567890", -4, "*")).isEqualTo("1234******");
}
@Test
public void palindrome_test() throws Exception {
assertThat(Snippets.isPalindrome("taco cat")).isTrue();
assertThat(Snippets.isPalindrome("abc")).isFalse();
}
@Test
public void reverseString_test() throws Exception {
assertThat(Snippets.reverseString("foobar")).isEqualTo("raboof");
}
@Test
public void sortCharactersInString_test() throws Exception {
assertThat(Snippets.sortCharactersInString("cabbage")).isEqualTo("aabbceg");
}
@Test
public void splitLines_test() throws Exception {
assertThat(Snippets.splitLines("This\nis a\nmultiline\nstring.\n"))
.isEqualTo(new String[]{
"This",
"is a",
"multiline",
"string."
});
}
@Test
public void toCamelCase_test() throws Exception {
assertThat(Snippets.toCamelCase("some_database_field_name")).isEqualTo("someDatabaseFieldName");
assertThat(Snippets.toCamelCase("Some label that needs to be camelized")).isEqualTo("someLabelThatNeedsToBeCamelized");
assertThat(Snippets.toCamelCase("some-java-property")).isEqualTo("someJavaProperty");
assertThat(Snippets.toCamelCase("some-mixed_string with spaces_underscores-and-hyphens")).isEqualTo("someMixedStringWithSpacesUnderscoresAndHyphens");
}
@Test
public void toKebabCase_test() throws Exception {
assertThat(Snippets.toKebabCase("camelCase")).isEqualTo("camel-case");
assertThat(Snippets.toKebabCase("some text")).isEqualTo("some-text");
assertThat(Snippets.toKebabCase("some-mixed_string With spaces_underscores-and-hyphens")).isEqualTo("some-mixed-string-with-spaces-underscores-and-hyphens");
assertThat(Snippets.toKebabCase("AllThe-small Things")).isEqualTo("all-the-small-things");
assertThat(Snippets.toKebabCase("IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingXMLAndHTML")).isEqualTo("i-am-listening-to-fm-while-loading-different-url-on-my-browser-and-also-editing-xml-and-html");
}
@Test
public void toSnakeCase_test() throws Exception {
assertThat(Snippets.toSnakeCase("camelCase")).isEqualTo("camel_case");
assertThat(Snippets.toSnakeCase("some text")).isEqualTo("some_text");
assertThat(Snippets.toSnakeCase("some-mixed_string With spaces_underscores-and-hyphens")).isEqualTo("some_mixed_string_with_spaces_underscores_and_hyphens");
assertThat(Snippets.toSnakeCase("AllThe-small Things")).isEqualTo("all_the_small_things");
assertThat(Snippets.toSnakeCase("IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingXMLAndHTML")).isEqualTo("i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_xml_and_html");
}
@Test
public void truncateString_test() throws Exception {
assertThat(Snippets.truncateString("boomerang", 7)).isEqualTo("boom...");
}
@Test
public void words_test() throws Exception {
assertThat(Snippets.words("I love java!!")).isEqualTo(new String[]{"I", "love", "java"});
assertThat(Snippets.words("Kotlin, Java & LemonTea")).isEqualTo(new String[]{"Kotlin", "Java", "LemonTea"});
}
@Test
public void randomInts_test() throws Exception {
assertThat(Snippets.randomInts(5, 100, 200)).hasSize(5);
}
@Test
public void concat_test() throws Exception {
String[] first = {"a", "b"};
String[] second = {"c", "d"};
assertThat(Snippets.concat(first, second))
.isEqualTo(new String[]{"a", "b", "c", "d"});
}
@Test
public void getCurrentWorkingDirectoryPath_test() throws Exception {
assertThat(Snippets.getCurrentWorkingDirectoryPath()).isNotBlank();
}
@Test
public void isNumeric_test() throws Exception {
assertThat(Snippets.isNumeric("123")).isTrue();
assertThat(Snippets.isNumeric("abc")).isFalse();
assertThat(Snippets.isNumeric("")).isFalse();
}
@Test
public void findNextPositivePowerOfTwo_test() throws Exception {
assertThat(Snippets.findNextPositivePowerOfTwo(-1)).isEqualTo(1);
assertThat(Snippets.findNextPositivePowerOfTwo(3)).isEqualTo(4);
assertThat(Snippets.findNextPositivePowerOfTwo(31)).isEqualTo(32);
assertThat(Snippets.findNextPositivePowerOfTwo(32)).isEqualTo(32);
}
@Test
public void isEven_test() throws Exception {
assertThat(Snippets.isEven(1)).isFalse();
assertThat(Snippets.isEven(2)).isTrue();
assertThat(Snippets.isEven(3)).isFalse();
assertThat(Snippets.isEven(4)).isTrue();
assertThat(Snippets.isEven(-1)).isFalse();
}
@Test
public void stringToIntegers_test() throws Exception {
int[] intArray = Snippets.stringToIntegers("1 2 3 4 5");
assertThat(intArray).isEqualTo(new int[]{1, 2, 3, 4, 5});
}
private static class Class1 implements I2, I1, I5 {
// empty
}
private static class Class2 extends Class1 implements I2, I3 {
// empty
}
private interface I1 {
// empty
}
private interface I2 {
// empty
}
private interface I3 extends I4, I5 {
// empty
}
private interface I4 {
// empty
}
private interface I5 extends I6 {
// empty
}
private interface I6 {
// empty
}
@Test
public void getAllInterfaces_shouldFindAllInterfacesImplementedByAClass() {
final List<Class<?>> list = Snippets.getAllInterfaces(Class2.class);
assertThat(list).hasSize(6);
assertThat(list).containsExactly(I2.class, I3.class, I4.class, I5.class, I6.class, I1.class);
}
enum Priority {
High, Medium, Low
}
@Test
public void getEnumMap_convert_enum_to_map() throws Exception {
Map<String, Priority> map = Snippets.getEnumMap(Priority.class);
assertThat(map).hasSize(3);
assertThat(map)
.containsOnly(
new SimpleEntry<>("High", Priority.High),
new SimpleEntry<>("Medium", Priority.Medium),
new SimpleEntry<>("Low", Priority.Low)
);
}
}