From b0c6c77245f5c7693ca78d662474e90675398983 Mon Sep 17 00:00:00 2001 From: Fevzi Anifieiev Date: Fri, 1 Nov 2019 10:48:14 -0400 Subject: [PATCH 1/4] (#1212) Fixed SetOf, which removed all elems after duplicated one --- src/main/java/org/cactoos/set/SetOf.java | 3 +- src/test/java/org/cactoos/set/SetOfTest.java | 52 ++++++++++++++++---- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/cactoos/set/SetOf.java b/src/main/java/org/cactoos/set/SetOf.java index 44613d37c1..d172503f72 100644 --- a/src/main/java/org/cactoos/set/SetOf.java +++ b/src/main/java/org/cactoos/set/SetOf.java @@ -26,6 +26,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; +import org.cactoos.Proc; import org.cactoos.iterable.IterableOf; import org.cactoos.scalar.And; @@ -60,7 +61,7 @@ public SetOf(final T... array) { public SetOf(final Iterable src) { super(() -> { final Set tmp = new HashSet<>(); - new And(tmp::add, src) + new And((Proc) tmp::add, src) .value(); return Collections.unmodifiableSet(tmp); }); diff --git a/src/test/java/org/cactoos/set/SetOfTest.java b/src/test/java/org/cactoos/set/SetOfTest.java index beb9074146..ea91050e96 100644 --- a/src/test/java/org/cactoos/set/SetOfTest.java +++ b/src/test/java/org/cactoos/set/SetOfTest.java @@ -23,27 +23,59 @@ */ package org.cactoos.set; -import org.hamcrest.MatcherAssert; +import org.cactoos.iterable.IterableOf; +import org.cactoos.iterable.Joined; import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValues; /** * Test case for {@link SetOf}. - * * @since 0.49.2 * @checkstyle MagicNumber (500 line) + * @checkstyle JavadocMethodCheck (500 lines) */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class SetOfTest { - /** - * Ensures that SetOf behaves as set, which means no duplicates. - */ @Test - public void behavesAsSet() { - MatcherAssert.assertThat( - "Can't behave as a set", + public void behaveAsSetWithOriginalDuplicationsInTheTail() { + new Assertion<>( + "Must keep unique numbers", new SetOf<>(1, 2, 2), - new BehavesAsSet<>(2) - ); + new HasValues<>(1, 2) + ).affirm(); + } + + @Test + public void behaveAsSetWithOriginalDuplicationsInTheHead() { + new Assertion<>( + "Must keep unique numbers", + new SetOf<>(1, 1, 2, 3), + new HasValues<>(1, 2, 3) + ).affirm(); + } + + @Test + public void behaveAsSetWithOriginalDuplicationsInTheMiddle() { + new Assertion<>( + "Must keep unique numbers", + new SetOf<>(1, 2, 2, 3), + new HasValues<>(1, 2, 3) + ).affirm(); + } + + @Test + public void behaveAsSetWithOriginalMergedCollectionsWithDuplicates() { + new Assertion<>( + "Must keep unique string literals", + new SetOf<>( + new Joined<>( + new IterableOf<>("cc"), + new IterableOf<>("aa", "bb", "cc", "dd") + ) + ), + new HasValues<>("dd", "aa", "bb", "cc") + ).affirm(); } } From 0d75b50af04fd38ff2f1827dc6b7da0f83d92191 Mon Sep 17 00:00:00 2001 From: Fevzi Anifieiev Date: Fri, 1 Nov 2019 11:40:08 -0400 Subject: [PATCH 2/4] (#1212) Fixed tests to do proper checks --- src/test/java/org/cactoos/set/SetOfTest.java | 50 ++++++++++++++++---- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/src/test/java/org/cactoos/set/SetOfTest.java b/src/test/java/org/cactoos/set/SetOfTest.java index ea91050e96..694a7b9324 100644 --- a/src/test/java/org/cactoos/set/SetOfTest.java +++ b/src/test/java/org/cactoos/set/SetOfTest.java @@ -25,15 +25,20 @@ import org.cactoos.iterable.IterableOf; import org.cactoos.iterable.Joined; +import org.hamcrest.Matcher; +import org.hamcrest.core.AllOf; +import org.hamcrest.core.IsCollectionContaining; +import org.hamcrest.core.IsEqual; import org.junit.Test; import org.llorllale.cactoos.matchers.Assertion; -import org.llorllale.cactoos.matchers.HasValues; +import org.llorllale.cactoos.matchers.HasSize; /** * Test case for {@link SetOf}. * @since 0.49.2 * @checkstyle MagicNumber (500 line) * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class SetOfTest { @@ -43,7 +48,13 @@ public void behaveAsSetWithOriginalDuplicationsInTheTail() { new Assertion<>( "Must keep unique numbers", new SetOf<>(1, 2, 2), - new HasValues<>(1, 2) + new AllOf<>( + new IterableOf>>( + new HasSize(2), + new IsCollectionContaining<>(new IsEqual<>(1)), + new IsCollectionContaining<>(new IsEqual<>(2)) + ) + ) ).affirm(); } @@ -52,7 +63,14 @@ public void behaveAsSetWithOriginalDuplicationsInTheHead() { new Assertion<>( "Must keep unique numbers", new SetOf<>(1, 1, 2, 3), - new HasValues<>(1, 2, 3) + new AllOf<>( + new IterableOf>>( + new HasSize(3), + new IsCollectionContaining<>(new IsEqual<>(1)), + new IsCollectionContaining<>(new IsEqual<>(2)), + new IsCollectionContaining<>(new IsEqual<>(3)) + ) + ) ).affirm(); } @@ -61,7 +79,14 @@ public void behaveAsSetWithOriginalDuplicationsInTheMiddle() { new Assertion<>( "Must keep unique numbers", new SetOf<>(1, 2, 2, 3), - new HasValues<>(1, 2, 3) + new AllOf<>( + new IterableOf>>( + new HasSize(3), + new IsCollectionContaining<>(new IsEqual<>(1)), + new IsCollectionContaining<>(new IsEqual<>(2)), + new IsCollectionContaining<>(new IsEqual<>(3)) + ) + ) ).affirm(); } @@ -69,13 +94,22 @@ public void behaveAsSetWithOriginalDuplicationsInTheMiddle() { public void behaveAsSetWithOriginalMergedCollectionsWithDuplicates() { new Assertion<>( "Must keep unique string literals", - new SetOf<>( - new Joined<>( - new IterableOf<>("cc"), + new SetOf( + new Joined( + new IterableOf<>("cc", "ff"), new IterableOf<>("aa", "bb", "cc", "dd") ) ), - new HasValues<>("dd", "aa", "bb", "cc") + new AllOf<>( + new IterableOf>>( + new HasSize(5), + new IsCollectionContaining<>(new IsEqual<>("aa")), + new IsCollectionContaining<>(new IsEqual<>("bb")), + new IsCollectionContaining<>(new IsEqual<>("cc")), + new IsCollectionContaining<>(new IsEqual<>("dd")), + new IsCollectionContaining<>(new IsEqual<>("ff")) + ) + ) ).affirm(); } } From 77b0162546a1fa27ef6d233245520527975eb37b Mon Sep 17 00:00:00 2001 From: Fevzi Anifieiev Date: Sun, 3 Nov 2019 20:48:10 -0500 Subject: [PATCH 3/4] (#1212) Refactoring due to code review remarks --- src/main/java/org/cactoos/set/SetOf.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/set/SetOf.java b/src/main/java/org/cactoos/set/SetOf.java index d172503f72..88dad0a83e 100644 --- a/src/main/java/org/cactoos/set/SetOf.java +++ b/src/main/java/org/cactoos/set/SetOf.java @@ -26,9 +26,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Set; -import org.cactoos.Proc; import org.cactoos.iterable.IterableOf; -import org.cactoos.scalar.And; /** * Iterable as {@link Set}. @@ -61,8 +59,7 @@ public SetOf(final T... array) { public SetOf(final Iterable src) { super(() -> { final Set tmp = new HashSet<>(); - new And((Proc) tmp::add, src) - .value(); + src.forEach(tmp::add); return Collections.unmodifiableSet(tmp); }); } From 7f5640aaed2b03e7c122283463ae0d241cd143f0 Mon Sep 17 00:00:00 2001 From: Fevzi Anifieiev Date: Mon, 11 Nov 2019 23:03:40 -0500 Subject: [PATCH 4/4] (#1212) After-review fixes with additional unit tests --- src/test/java/org/cactoos/set/SetOfTest.java | 68 ++++++++++++++++++-- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/cactoos/set/SetOfTest.java b/src/test/java/org/cactoos/set/SetOfTest.java index 694a7b9324..1337b5cb5d 100644 --- a/src/test/java/org/cactoos/set/SetOfTest.java +++ b/src/test/java/org/cactoos/set/SetOfTest.java @@ -25,6 +25,7 @@ import org.cactoos.iterable.IterableOf; import org.cactoos.iterable.Joined; +import org.cactoos.text.TextOf; import org.hamcrest.Matcher; import org.hamcrest.core.AllOf; import org.hamcrest.core.IsCollectionContaining; @@ -46,7 +47,7 @@ public final class SetOfTest { @Test public void behaveAsSetWithOriginalDuplicationsInTheTail() { new Assertion<>( - "Must keep unique numbers", + "Must keep unique integer numbers", new SetOf<>(1, 2, 2), new AllOf<>( new IterableOf>>( @@ -61,7 +62,7 @@ public void behaveAsSetWithOriginalDuplicationsInTheTail() { @Test public void behaveAsSetWithOriginalDuplicationsInTheHead() { new Assertion<>( - "Must keep unique numbers", + "Must keep unique integer numbers", new SetOf<>(1, 1, 2, 3), new AllOf<>( new IterableOf>>( @@ -77,7 +78,7 @@ public void behaveAsSetWithOriginalDuplicationsInTheHead() { @Test public void behaveAsSetWithOriginalDuplicationsInTheMiddle() { new Assertion<>( - "Must keep unique numbers", + "Must keep unique integer numbers", new SetOf<>(1, 2, 2, 3), new AllOf<>( new IterableOf>>( @@ -91,7 +92,7 @@ public void behaveAsSetWithOriginalDuplicationsInTheMiddle() { } @Test - public void behaveAsSetWithOriginalMergedCollectionsWithDuplicates() { + public void behaveAsSetMergedCollectionsOfLiteralsWithDuplicates() { new Assertion<>( "Must keep unique string literals", new SetOf( @@ -112,4 +113,63 @@ public void behaveAsSetWithOriginalMergedCollectionsWithDuplicates() { ) ).affirm(); } + + @Test + public void behaveAsSetWithOriginalDuplicationsOfCharsInTheMiddle() { + new Assertion<>( + "Must keep unique characters", + new SetOf<>('a', 'b', 'b', 'c', 'a', 'b', 'd'), + new AllOf<>( + new IterableOf>>( + new HasSize(4), + new IsCollectionContaining<>(new IsEqual<>('a')), + new IsCollectionContaining<>(new IsEqual<>('b')), + new IsCollectionContaining<>(new IsEqual<>('c')), + new IsCollectionContaining<>(new IsEqual<>('d')) + ) + ) + ).affirm(); + } + + @Test + public void behaveAsSetWithOriginalDuplicationsOfDoublesInTheMiddle() { + new Assertion<>( + "Must keep unique double numbers", + new SetOf<>(1.5d, 2.4d, 1.5d, 2.4d, 2.4d, 1.5d), + new AllOf<>( + new IterableOf>>( + new HasSize(2), + new IsCollectionContaining<>(new IsEqual<>(1.5d)), + new IsCollectionContaining<>(new IsEqual<>(2.4d)) + ) + ) + ).affirm(); + } + + @Test + public void behaveAsSetWithOriginalDuplicationsOfTextsInTheMiddle() { + new Assertion<>( + "Must keep unique TextOf objects", + new SetOf<>( + new TextOf("12345"), + new TextOf("67890"), + new TextOf("12345"), + new TextOf("00000") + ), + new AllOf<>( + new IterableOf>>( + new HasSize(3), + new IsCollectionContaining<>( + new IsEqual<>(new TextOf("12345")) + ), + new IsCollectionContaining<>( + new IsEqual<>(new TextOf("67890")) + ), + new IsCollectionContaining<>( + new IsEqual<>(new TextOf("00000")) + ) + ) + ) + ).affirm(); + } }