Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Sep 8, 2020
2 parents 1ddee1b + 3794cf4 commit cb090b1
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 87 deletions.
26 changes: 26 additions & 0 deletions src/test/java/org/cactoos/list/ImmutableTest.java
Expand Up @@ -560,4 +560,30 @@ public void testToString() {
new IsEqual<>(new ListOf<>("a", "b", "c").toString())
).affirm();
}

@Test
public void subListReturnsListIteratorWithSupportedSet() {
new Assertion<>(
"subList's iterator must be immutable",
() -> {
final ListIterator<String> iterator = new Immutable<>(
new ListOf<String>("one", "two", "three")
)
.subList(0, 2)
.listIterator(0);
iterator.next();
iterator.set("zero");
return new Object();
},
new Throws<>(
new MatcherOf<>(
(String msg) -> msg.equals(
"List Iterator is read-only and doesn't allow rewriting items"
)
),
UnsupportedOperationException.class
)
).affirm();
}

}
173 changes: 86 additions & 87 deletions src/test/java/org/cactoos/list/ListEnvelopeTest.java
Expand Up @@ -23,16 +23,15 @@
*/
package org.cactoos.list;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.collection.IsEmptyCollection;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.MatcherOf;
import org.llorllale.cactoos.matchers.Throws;
import org.llorllale.cactoos.matchers.HasValues;

/**
* Test case for {@link ListEnvelope}.
Expand All @@ -42,24 +41,13 @@
* @checkstyle JavadocTypeCheck (500 lines)
* @checkstyle MagicNumberCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @todo #898:30min Get rid of the Immutable in StringList nested class
* That's because this test should check the original behavior of ListEnvelope
* Now this test checks behavior of the Immutable decorator
*/
@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"})
public final class ListEnvelopeTest {

@Test(expected = UnsupportedOperationException.class)
public void returnsListIteratorWithUnsupportedRemove() {
final ListEnvelope<String> list = new StringList("one");
final Iterator<String> iterator = list.listIterator();
iterator.next();
iterator.remove();
}

@Test()
@Test
public void returnsListIteratorWithSupportedSet() {
final ListEnvelope<String> list = new MutableStringList("one", "two");
final ListEnvelope<String> list = new StringList("one", "two");
final ListIterator<String> iterator = list.listIterator(1);
iterator.next();
iterator.set("zero");
Expand All @@ -73,103 +61,119 @@ public void returnsListIteratorWithSupportedSet() {
);
}

@Test(expected = UnsupportedOperationException.class)
public void returnsListIteratorWithUnsupportedAdd() {
final ListEnvelope<String> list = new StringList("one");
final ListIterator<String> iterator = list.listIterator();
iterator.next();
iterator.add("two");
}

@Test(expected = UnsupportedOperationException.class)
public void subListReturnsListIteratorWithUnsupportedRemove() {
@Test
public void removeIsDelegated() {
final ListEnvelope<String> list = new StringList("one");
final Iterator<String> iterator = list.subList(0, 1)
.listIterator();
iterator.next();
iterator.remove();
}

@Test()
public void subListReturnsListIteratorWithSupportedSet() {
list.remove(0);
new Assertion<>(
"subList.listIterator().set() must throw exception",
() -> {
final ListIterator<String> iterator = new StringList("one", "two", "three")
.subList(0, 2)
.listIterator(0);
iterator.next();
iterator.set("zero");
return new Object();
},
new Throws<>(
new MatcherOf<>(
(String msg) -> msg.equals(
"List Iterator is read-only and doesn't allow rewriting items"
)
),
UnsupportedOperationException.class
)
"must be empty after removal of 0th element",
list,
new IsEmptyCollection<>()
).affirm();
}

@Test(expected = UnsupportedOperationException.class)
public void subListReturnsListIteratorWithUnsupportedAdd() {
@Test
public void indexOfIsDelegated() {
final ListEnvelope<String> list = new StringList("one");
final ListIterator<String> iterator = list.subList(0, 1)
.listIterator();
iterator.next();
iterator.add("two");
new Assertion<>(
"must return correct index of element",
list.indexOf("one"),
new IsEqual<>(0)
).affirm();
}

@Test(expected = UnsupportedOperationException.class)
public void removeIsNotSupported() {
@Test
public void addAllIsDelegated() {
final ListEnvelope<String> list = new StringList("one");
list.remove(0);
list.addAll(0, new StringList("two"));
new Assertion<>(
"element must be added at 0th position",
list,
new IsEqual<>(new ListOf<>("two", "one"))
).affirm();
}

@Test(expected = UnsupportedOperationException.class)
public void setIsNotSupported() {
@Test
public void setIsDelegatedToTheOriginal() {
final ListEnvelope<String> list = new StringList("one");
list.set(0, "zero");
new Assertion<>(
"value must be changed",
list,
new HasValues<>("zero")
).affirm();
}

@Test(expected = UnsupportedOperationException.class)
public void addIsNotSupported() {
@Test
public void addIsDelegated() {
final ListEnvelope<String> list = new StringList("one");
list.add("two");
new Assertion<>(
"value must be added",
list,
new HasValues<>("one", "two")
).affirm();
}

@Test(expected = UnsupportedOperationException.class)
public void returnsSubListWithUnsupportedRemove() {
public void returnsSubListWithRemove() {
final ListEnvelope<String> list = new StringList("one");
list.subList(0, 1).remove(0);
new Assertion<>(
"must be empty after removal of 0th element via subList",
list,
new IsEmptyCollection<>()
).affirm();
}

@Test()
@Test
public void returnsSubListWithSupportedSet() {
final List<String> list = new StringList("one");
list.subList(0, 1).set(0, "zero");
new Assertion<>(
"subList.set() must throw exception",
() -> new StringList("one").subList(0, 1).set(0, "zero"),
new Throws<>(
new MatcherOf<>(
(String msg) -> msg.equals("#set(): the list is read-only")
),
UnsupportedOperationException.class
"ListEnvelope().subList(...).set() must change the original list",
list,
new HasValues<>(
"zero"
)
).affirm();
}

@Test(expected = UnsupportedOperationException.class)
public void returnsSubListWithUnsupportedAdd() {
@Test
public void addsAtGivenIndex() {
final ListEnvelope<String> list = new StringList("one");
list.subList(0, 1).add("two");
list.add(0, "two");
new Assertion<>(
"must add value at given index",
list,
new HasValues<>("two")
).affirm();
}

@Test
public void getsAtGivenIndex() {
final ListEnvelope<String> list = new StringList("one");
new Assertion<>(
"must get 0th value",
list.get(0),
new IsEqual<>("one")
).affirm();
}

@Test
public void getsLastIndexOfValue() {
final ListEnvelope<String> list = new StringList("one");
list.add(1, "one");
new Assertion<>(
"must return correct last index of element",
list.lastIndexOf("one"),
new IsEqual<>(1)
).affirm();
}

@Test
public void mustReturnPreviousIndex() {
new Assertion<>(
"List Iterator must return previous index",
"List iterator must return previous index",
new ListIteratorOf<>(
new ListOf<>(1)
).previousIndex(),
Expand All @@ -180,7 +184,7 @@ public void mustReturnPreviousIndex() {
@Test
public void mustReturnPreviousElement() {
new Assertion<>(
"List Iterator must return previous element",
"List iterator must return previous element",
new ListIteratorOf<>(
new ListOf<>(3, 7),
1
Expand All @@ -205,7 +209,7 @@ public void mustReturnNextElement() {
new Assertion<>(
"List iterator must return next item",
new ListIteratorOf<>(
new ListOf<>(5, 11, 13),
new ListOf<>(5, 11, 13),
1
).next(),
new IsEqual<>(11)
Expand All @@ -214,13 +218,8 @@ public void mustReturnNextElement() {

private static final class StringList extends ListEnvelope<String> {
StringList(final String... elements) {
super(new Immutable<>(new ListOf<>(elements)));
super(new ListOf<>(elements));
}
}

private static final class MutableStringList extends ListEnvelope<String> {
MutableStringList(final String... elements) {
super(new LinkedList<>(new ListOf<>(elements)));
}
}
}

1 comment on commit cb090b1

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on cb090b1 Sep 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 898-0bea8a51 disappeared from src/test/java/org/cactoos/list/ListEnvelopeTest.java, that's why I closed #1217. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.