### Question:
LinesIterable implements Iterable<Line> { @Override public Iterator<Line> iterator() { return new LinesIterator(); } LinesIterable(LineSequence lineSequence, Predicate<Line> predicate); @Override Iterator<Line> iterator();  }### Answer:
@Test public void iteratorWithNoLines() { LinesIterable iterable = new LinesIterable(LineSequence.create(""), Predicates.<Line> alwaysTrue()); Iterator<Line> iterator = iterable.iterator(); assertNotNull(iterator); assertFalse(iterator.hasNext()); assertThrows(NoSuchElementException.class, () -> iterator.next()); }
@Test public void iteratorAdvances() { LinesIterable iterable = new LinesIterable(LineSequence.create("one\ntwo"), Predicates.<Line> alwaysTrue()); Iterator<Line> iterator = iterable.iterator(); assertNotNull(iterator); assertTrue(iterator.hasNext()); Line next = iterator.next(); assertNotNull(next); assertEquals("one", next.getText()); Line two = iterator.next(); assertNotNull(two); assertEquals("two", two.getText()); }
@Test public void iteratorRemove() { LinesIterable iterable = new LinesIterable(LineSequence.create("one"), Predicates.<Line> alwaysTrue()); Iterator<Line> iterator = iterable.iterator(); assertNotNull(iterator); assertTrue(iterator.hasNext()); Line next = iterator.next(); assertNotNull(next); assertEquals("one", next.getText()); assertThrows(UnsupportedOperationException.class, () -> iterator.remove()); }
@Test @SuppressWarnings("unchecked") public void hasNext() { Predicate<Line> predicate = mock(Predicate.class); LineSequence lineSequence = LineSequence.create("one"); Iterator<Line> iterator = new LinesIterable(lineSequence, predicate).iterator(); assertFalse(iterator.hasNext()); doReturn(true).when(predicate).apply(any(Line.class)); assertTrue(iterator.hasNext()); lineSequence.advance(); assertFalse(iterator.hasNext()); }
@Test public void iteratorWithNoLines() { LinesIterable iterable = new LinesIterable(LineSequence.create(""), Predicates.<Line> alwaysTrue()); Iterator<Line> iterator = iterable.iterator(); assertNotNull(iterator); assertFalse(iterator.hasNext()); thrown.expect(NoSuchElementException.class); iterator.next(); }
@Test public void iteratorRemove() { LinesIterable iterable = new LinesIterable(LineSequence.create("one"), Predicates.<Line> alwaysTrue()); Iterator<Line> iterator = iterable.iterator(); assertNotNull(iterator); assertTrue(iterator.hasNext()); Line next = iterator.next(); assertNotNull(next); assertEquals("one", next.getText()); thrown.expect(UnsupportedOperationException.class); iterator.remove(); }