Skip to content

Commit

Permalink
(#1219) After code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fanifieiev committed Nov 8, 2019
1 parent fd65977 commit 40851be
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/cactoos/list/ImmutableListIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class ImmutableListIterator<T> implements ListIterator<T> {
* @param iter Original list iterator.
*/
public ImmutableListIterator(final ListIterator<T> iter) {
this.origin = new ListIteratorOf<>(iter);
this.origin = iter;
}

@Override
Expand Down Expand Up @@ -81,21 +81,21 @@ public int previousIndex() {
@Override
public void remove() {
throw new UnsupportedOperationException(
"Iterator is read-only and doesn't allow removing items"
"List Iterator is read-only and doesn't allow removing items"
);
}

@Override
public void set(final T item) {
throw new UnsupportedOperationException(
"Iterator is read-only and doesn't allow rewriting items"
"List Iterator is read-only and doesn't allow rewriting items"
);
}

@Override
public void add(final T item) {
throw new UnsupportedOperationException(
"Iterator is read-only and doesn't allow adding items"
"List Iterator is read-only and doesn't allow adding items"
);
}
}
81 changes: 54 additions & 27 deletions src/test/java/org/cactoos/list/ImmutableListIteratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
*/
package org.cactoos.list;

import java.util.ListIterator;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.Throws;

/**
* Test case for {@link ImmutableListIterator}.
Expand All @@ -39,9 +39,9 @@
public final class ImmutableListIteratorTest {

@Test
public void getsPreviousIndex() {
public void mustReturnPreviousIndex() {
new Assertion<>(
"List iterator returns incorrect previous index",
"Must return next previous index",
new ImmutableListIterator<>(
new ListOf<>(1).listIterator()
).previousIndex(),
Expand All @@ -50,9 +50,9 @@ public void getsPreviousIndex() {
}

@Test
public void getsPrevious() {
public void mustReturnPreviousElement() {
new Assertion<>(
"List iterator returns incorrect previous item",
"Must return previous element",
new ImmutableListIterator<>(
new ListOf<>(3, 7).listIterator(1)
).previous(),
Expand All @@ -61,9 +61,9 @@ public void getsPrevious() {
}

@Test
public void getsNextIndex() {
public void mustReturnNextIndex() {
new Assertion<>(
"List iterator returns incorrect next index",
"Must return next index",
new ImmutableListIterator<>(
new ListOf<>(1).listIterator()
).nextIndex(),
Expand All @@ -72,37 +72,64 @@ public void getsNextIndex() {
}

@Test
public void getsNext() {
public void mustReturnNextElement() {
new Assertion<>(
"List iterator returns incorrect next item",
"Must return next element",
new ImmutableListIterator<>(
new ListOf<>(5, 11, 13).listIterator(1)
).next(),
new IsEqual<>(11)
).affirm();
}

@Test(expected = UnsupportedOperationException.class)
public void doesNotSupportRemove() {
final ListIterator<Integer> iterator = new ImmutableListIterator<>(
new ListOf<>(1, 2).listIterator()
);
iterator.remove();
@Test
public void mustRaiseErrorOnListIteratorRemove() {
new Assertion<>(
"Must throw error if modified with remove operation",
() -> {
new ImmutableListIterator<>(
new ListOf<>(1, 2).listIterator()
).remove();
return 0;
},
new Throws<>(
"List Iterator is read-only and doesn't allow removing items",
UnsupportedOperationException.class
)
).affirm();
}

@Test(expected = UnsupportedOperationException.class)
public void doesNotSupportAdd() {
final ListIterator<Integer> iterator = new ImmutableListIterator<>(
new ListOf<>(1, 2).listIterator()
);
iterator.add(1);
@Test
public void mustRaiseErrorOnListIteratorAdd() {
new Assertion<>(
"Must throw error if modified with add operation",
() -> {
new ImmutableListIterator<>(
new ListOf<>(1, 2).listIterator()
).add(1);
return 0;
},
new Throws<>(
"List Iterator is read-only and doesn't allow adding items",
UnsupportedOperationException.class
)
).affirm();
}

@Test(expected = UnsupportedOperationException.class)
public void doesNotSupportSet() {
final ListIterator<Integer> iterator = new ImmutableListIterator<>(
new ListOf<>(1, 2).listIterator()
);
iterator.set(1);
@Test
public void mustRaiseErrorOnListIteratorSet() {
new Assertion<>(
"Must throw error if modified with set operation",
() -> {
new ImmutableListIterator<>(
new ListOf<>(1, 2).listIterator()
).set(1);
return 0;
},
new Throws<>(
"List Iterator is read-only and doesn't allow rewriting items",
UnsupportedOperationException.class
)
).affirm();
}
}

0 comments on commit 40851be

Please sign in to comment.