Skip to content

Commit

Permalink
(yegor256#1166) Remove some more non OO matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Sep 28, 2019
1 parent 903dd2f commit 08e9cb0
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 169 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ The MIT License (MIT)
<signaturesFile>./src/test/resources/forbidden-apis.txt</signaturesFile>
</signaturesFiles>
<!--
@todo #1119:30min In the continuation of #588, all the calls
@todo #1166:30min In the continuation of #588, all the calls
to Matchers should be replaced with their OO counterparts.
This todo should be updated with a new one until everything is
done. The newly covered classes should be added to the include
Expand Down
82 changes: 46 additions & 36 deletions src/test/java/org/cactoos/iterator/HeadOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@

import java.util.NoSuchElementException;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.LengthOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasSize;
import org.llorllale.cactoos.matchers.HasValues;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test Case for {@link HeadOf}.
Expand All @@ -41,55 +42,64 @@ public final class HeadOfTest {
@Test
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void headIterator() throws Exception {
MatcherAssert.assertThat(
"Can't skip elements in iterator",
() -> new HeadOf<>(
2,
new IteratorOf<>(
"one", "two", "three", "four"
new Assertion<>(
"must skip elements in iterator",
new IterableOf<>(
new HeadOf<>(
2,
new IteratorOf<>(
"one", "two", "three", "four"
)
)
),
Matchers.contains(
new HasValues<>(
"one",
"two"
)
);
).affirm();
}

@Test
public void returnsIntactIterator() throws Exception {
MatcherAssert.assertThat(
new LengthOf(
new IterableOf<>(
new HeadOf<>(
3,
new IteratorOf<>(
"one", "two"
)
new Assertion<>(
"must return an intact iterator",
new IterableOf<>(
new HeadOf<>(
3,
new IteratorOf<>(
"one", "two"
)
)
).intValue(),
Matchers.equalTo(2)
);
),
new HasSize(2)
).affirm();
}

@Test(expected = NoSuchElementException.class)
@Test
public void returnsEmptyIterator() throws Exception {
new HeadOf<>(
0,
new IteratorOf<>(
"one", "two"
)
).next();
new Assertion<>(
"must throw an exception if empty",
() -> new HeadOf<>(
0,
new IteratorOf<>(
"one", "two"
)
).next(),
new Throws<>(NoSuchElementException.class)
).affirm();
}

@Test(expected = NoSuchElementException.class)
@Test
public void emptyIteratorForNegativeSize() throws Exception {
new HeadOf<>(
-1,
new IteratorOf<>(
"one", "two"
)
).next();
new Assertion<>(
"must throw an exception for negative size",
() -> new HeadOf<>(
-1,
new IteratorOf<>(
"one", "two"
)
).next(),
new Throws<>(NoSuchElementException.class)
).affirm();
}
}
56 changes: 34 additions & 22 deletions src/test/java/org/cactoos/iterator/IteratorOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
package org.cactoos.iterator;

import java.util.NoSuchElementException;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.cactoos.iterable.IterableOf;
import org.hamcrest.core.IsNot;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasValues;
import org.llorllale.cactoos.matchers.IsTrue;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link IteratorOf}.
Expand All @@ -39,45 +42,54 @@ public final class IteratorOfTest {

@Test
public void emptyIteratorDoesNotHaveNext() {
MatcherAssert.assertThat(
"Can't create empty iterator",
new Assertion<>(
"must create empty iterator",
new IteratorOf<>().hasNext(),
CoreMatchers.equalTo(false)
);
new IsNot<>(new IsTrue())
).affirm();
}

@Test(expected = NoSuchElementException.class)
@Test
public void emptyIteratorThrowsException() {
new IteratorOf<>().next();
new Assertion<>(
"must throw an exception if empty",
() -> new IteratorOf<>().next(),
new Throws<>(NoSuchElementException.class)
).affirm();
}

@Test
public void nonEmptyIteratorDoesNotHaveNext() {
final IteratorOf<Integer> iterator = this.iteratorWithFetchedElements();
MatcherAssert.assertThat(
"Can't create non empty iterator",
new Assertion<>(
"must create non empty iterator",
iterator.hasNext(),
CoreMatchers.equalTo(false)
);
new IsNot<>(new IsTrue())
).affirm();
}

@Test(expected = NoSuchElementException.class)
@Test
public void nonEmptyIteratorThrowsException() {
final IteratorOf<Integer> iterator = this.iteratorWithFetchedElements();
iterator.next();
new Assertion<>(
"must throw an exception if consumed",
() -> this.iteratorWithFetchedElements().next(),
new Throws<>(NoSuchElementException.class)
).affirm();
}

@Test
public void convertStringsToIterator() {
MatcherAssert.assertThat(
"Can't create an iterator of strings",
() -> new IteratorOf<>(
"a", "b", "c"
new Assertion<>(
"must create an iterator of strings",
new IterableOf<>(
new IteratorOf<>(
"a", "b", "c"
)
),
Matchers.contains(
new HasValues<>(
"a", "b", "c"
)
);
).affirm();
}

private IteratorOf<Integer> iteratorWithFetchedElements() {
Expand Down
51 changes: 26 additions & 25 deletions src/test/java/org/cactoos/iterator/JoinedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,57 @@
*/
package org.cactoos.iterator;

import java.util.Iterator;
import java.util.NoSuchElementException;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.LengthOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.cactoos.iterable.Mapped;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasSize;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link Joined}.
* @since 0.14
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class JoinedTest {

@Test
public void joinsIterators() {
MatcherAssert.assertThat(
"Can't concatenate mapped iterators together",
new LengthOf(
new org.cactoos.iterable.NoNulls<>(
new IterableOf<>(
new Joined<Iterator<String>>(
new Mapped<>(
input -> new IteratorOf<>(input),
new IteratorOf<>("x")
)
)
new Assertion<>(
"must concatenate mapped iterators together",
new IterableOf<>(
new Joined<>(
new Mapped<>(
IteratorOf::new,
new IterableOf<>("x", "y")
)
)
).intValue(),
Matchers.equalTo(1)
);
),
new HasSize(2)
).affirm();
}

@Test
public void callsNextDirectlyOnNonEmptyIterator() {
MatcherAssert.assertThat(
"Cannot call next method directly on non-empty iterator",
new Assertion<>(
"must call next method directly on non-empty iterator",
new Joined<Integer>(
new IteratorOf<>(1),
new IteratorOf<>(2)
).next(),
Matchers.equalTo(1)
);
new IsEqual<>(1)
).affirm();
}

@Test(expected = NoSuchElementException.class)
@Test
public void throwsExceptionWhenCallNextOnEmptyIterator() {
new Joined<Integer>(new IteratorOf<>()).next();
new Assertion<>(
"must throw an exception",
() -> new Joined<Integer>(new IteratorOf<>()).next(),
new Throws<>(NoSuchElementException.class)
).affirm();
}

}
33 changes: 15 additions & 18 deletions src/test/java/org/cactoos/iterator/RepeatedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
package org.cactoos.iterator;

import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.LengthOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasSize;

/**
* Test case for {@link Repeated}.
Expand All @@ -41,25 +40,23 @@ public final class RepeatedTest {
public void allSameTest() throws Exception {
final int size = 42;
final int element = 11;
MatcherAssert.assertThat(
"Can't generate an iterable with fixed size",
new LengthOf(
new IterableOf<>(
new Repeated<>(
size, element
)
new Assertion<>(
"must generate an iterable with fixed size",
new IterableOf<>(
new Repeated<>(
size, element
)
).intValue(),
Matchers.equalTo(size)
);
),
new HasSize(size)
).affirm();
}

@Test
public void emptyTest() throws Exception {
MatcherAssert.assertThat(
"Can't generate an empty iterator",
(Iterable<Integer>) () -> new Repeated<>(0, 0),
Matchers.iterableWithSize(0)
);
new Assertion<>(
"must generate an empty iterator",
new IterableOf<>(new Repeated<>(0, 0)),
new HasSize(0)
).affirm();
}
}
23 changes: 12 additions & 11 deletions src/test/java/org/cactoos/iterator/ShuffledTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
package org.cactoos.iterator;

import org.cactoos.iterable.IterableOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.HasValues;

/**
* Test Case for {@link Shuffled}.
Expand All @@ -36,19 +36,20 @@
public final class ShuffledTest {

@Test
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void shuffleIterable() throws Exception {
MatcherAssert.assertThat(
"Can't shuffle elements in iterator",
() -> new Shuffled<>(
new IterableOf<>(
"a", "b"
).iterator()
new Assertion<>(
"must shuffle elements in iterator",
new IterableOf<>(
new Shuffled<>(
new IteratorOf<>(
"a", "b"
)
)
),
Matchers.containsInAnyOrder(
new HasValues<>(
"a", "b"
)
);
).affirm();
}

}
Loading

0 comments on commit 08e9cb0

Please sign in to comment.