|
23 | 23 | */ |
24 | 24 | package com.github.underscore; |
25 | 25 |
|
26 | | -import org.junit.Test; |
| 26 | +import static java.util.Arrays.asList; |
| 27 | +import static org.junit.Assert.assertArrayEquals; |
| 28 | +import static org.junit.Assert.assertEquals; |
| 29 | +import static org.junit.Assert.assertNull; |
27 | 30 |
|
28 | 31 | import java.util.ArrayList; |
29 | 32 | import java.util.Arrays; |
30 | 33 | import java.util.Collection; |
31 | 34 | import java.util.Collections; |
| 35 | +import java.util.HashSet; |
32 | 36 | import java.util.List; |
33 | 37 | import java.util.NoSuchElementException; |
| 38 | +import java.util.Set; |
34 | 39 |
|
35 | | -import static java.util.Arrays.asList; |
36 | | -import static org.junit.Assert.assertArrayEquals; |
37 | | -import static org.junit.Assert.assertEquals; |
38 | | -import static org.junit.Assert.assertNull; |
| 40 | +import org.junit.Test; |
39 | 41 |
|
40 | 42 | /** |
41 | 43 | * Underscore library unit test. |
@@ -464,6 +466,81 @@ public void drop() { |
464 | 466 | assertEquals("[3, 2, 1]", asList(resultArray2).toString()); |
465 | 467 | } |
466 | 468 |
|
| 469 | +/* |
| 470 | +_.replace([1, 2, 3, 4], predicate(a) { return a > 2; }, 100); |
| 471 | +=> [1, 2, 100, 100] |
| 472 | +_.replace([1, 2, 3, 4], null, 100); |
| 473 | +=> [1, 2, 3, 4] |
| 474 | +*/ |
| 475 | + @SuppressWarnings("serial") |
| 476 | + @Test |
| 477 | + public void replace() { |
| 478 | + assertEquals("[100, 1, 100, 3, 100, 5]", U.replace(U.newIntegerList(U.range(6)), |
| 479 | + new Predicate<Integer>() { |
| 480 | + @Override |
| 481 | + public boolean test(Integer arg) { |
| 482 | + return arg % 2 == 0; |
| 483 | + } |
| 484 | + }, 100).toString()); |
| 485 | + assertEquals("[0, 1, 2, 3, 4]", U.replace(U.newIntegerList(U.range(5)), null, 100).toString()); |
| 486 | + assertEquals("[a, aa, b, b]", new U<String>(asList("a", "aa", "aaa", "aaaa")).replace( |
| 487 | + new Predicate<String>() { |
| 488 | + @Override |
| 489 | + public boolean test(String arg) { |
| 490 | + return arg.length() > 2; |
| 491 | + } |
| 492 | + }, "b").toString()); |
| 493 | + assertEquals("[a, aa, cc, ccc]", new U<String>(asList("a", "aa", "cc", "ccc")).replace( |
| 494 | + null, "b").toString()); |
| 495 | + Set<Integer> set = new HashSet<Integer>() { { |
| 496 | + addAll(U.newIntegerList(U.range(7))); |
| 497 | + } }; |
| 498 | + assertEquals("[0, 1, 2, 100, 100, 100, 100]", U.chain(set).replace( |
| 499 | + new Predicate<Integer>() { |
| 500 | + @Override |
| 501 | + public boolean test(Integer arg) { |
| 502 | + return arg > 2; |
| 503 | + } |
| 504 | + }, 100).toString()); |
| 505 | + } |
| 506 | + |
| 507 | +/* |
| 508 | +_.replaceIndexed([a, b, c, d], predicateIndexed(a, b) { return a > 2; }, z); |
| 509 | +=> [a, b, z, z] |
| 510 | +_.replaceIndexed([a, b, c, d], null, z); |
| 511 | +=> [a, b, c, d] |
| 512 | +*/ |
| 513 | + @SuppressWarnings("serial") |
| 514 | + @Test |
| 515 | + public void replaceIndexed() { |
| 516 | + assertEquals("[0, 1, 2, 3, 100, 100]", U.replaceIndexed(U.newIntegerList(U.range(6)), |
| 517 | + new PredicateIndexed<Integer>() { |
| 518 | + @Override |
| 519 | + public boolean test(int i, Integer arg) { |
| 520 | + return i > 2 && arg > 3; |
| 521 | + } |
| 522 | + }, 100).toString()); |
| 523 | + assertEquals("[0, 1, 2, 3, 4]", U.replaceIndexed(U.newIntegerList(U.range(5)), null, 100).toString()); |
| 524 | + assertEquals("[a, bc, ddd, f]", new U<String>(asList("a", "bc", "ddd", "eeee")).replaceIndexed( |
| 525 | + new PredicateIndexed<String>() { |
| 526 | + @Override |
| 527 | + public boolean test(int i, String arg) { |
| 528 | + return arg.length() > 2 && i > 2; |
| 529 | + } |
| 530 | + }, "f").toString()); |
| 531 | + assertEquals("[a, aa, cc, ccc]", new U<String>(asList("a", "aa", "cc", "ccc")).replaceIndexed( |
| 532 | + null, "b").toString()); |
| 533 | + List<Integer> list = new ArrayList<Integer>() { { |
| 534 | + add(100); add(22); add(88); add(6530); add(-25); add(-1000); |
| 535 | + } }; |
| 536 | + assertEquals("[100, 0, 88, 6530, 0, -1000]", U.chain(list).replaceIndexed( |
| 537 | + new PredicateIndexed<Integer>() { |
| 538 | + @Override |
| 539 | + public boolean test(int i, Integer arg) { |
| 540 | + return arg < 23 && i < 5; |
| 541 | + } |
| 542 | + }, 0).toString()); |
| 543 | + } |
467 | 544 |
|
468 | 545 | /* |
469 | 546 | _.initial([5, 4, 3, 2, 1]); |
|
0 commit comments