Advertisement
Stanbey

LineIteratorTest

Dec 3rd, 2015
222
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.moozvine.strings;
  2.  
  3. import com.moozvine.testing.Verify;
  4. import org.junit.Rule;
  5. import org.junit.Test;
  6. import org.junit.rules.ExpectedException;
  7.  
  8. import java.util.Iterator;
  9.  
  10. public class LineIteratorTest {
  11.   @Rule public Verify verify = new Verify();
  12.   @Rule public ExpectedException expectedException = ExpectedException.none();
  13.  
  14.  
  15.   @Test
  16.   public void stringIteratorWithMultipleLinesShouldReturnAllLines() {
  17.     final Iterator<String> lines = LineIterator.of("one\ntwo\nthree");
  18.     verify.isTrue(lines.hasNext());
  19.     verify.areEqual("one", lines.next());
  20.     verify.isTrue(lines.hasNext());
  21.     verify.areEqual("two", lines.next());
  22.     verify.isTrue(lines.hasNext());
  23.     verify.areEqual("three", lines.next());
  24.     verify.isFalse(lines.hasNext());
  25.   }
  26.  
  27.   @Test
  28.   public void stringIteratorWithSingleLinesShouldReturnOneLine() {
  29.     final Iterator<String> lines = LineIterator.of("one\ttwo\tthree");
  30.     verify.isTrue(lines.hasNext());
  31.     verify.areEqual("one\ttwo\tthree", lines.next());
  32.     verify.isFalse(lines.hasNext());
  33.   }
  34.  
  35.   @Test
  36.   public void emptyStringIteratorShouldReturnOneEmptyLine() {
  37.     final Iterator<String> lines = LineIterator.of("");
  38.     verify.isTrue(lines.hasNext());
  39.     verify.areEqual("", lines.next());
  40.     verify.isFalse(lines.hasNext());
  41.   }
  42.  
  43.   @Test
  44.   public void stringIteratorShouldNotSupportRemoval() {
  45.     final Iterator<String> lines = LineIterator.of("one\ntwo\nthree");
  46.     verify.isTrue(lines.hasNext());
  47.     verify.areEqual("one", lines.next());
  48.     expectedException.expect(UnsupportedOperationException.class);
  49.     lines.remove();
  50.   }
  51.  
  52.   @Test
  53.   public void stringBuilderIteratorWithMultipleLinesShouldReturnAllLines() {
  54.     final Iterator<String> lines = LineIterator.of(new StringBuilder("one\ntwo\nthree"));
  55.     verify.isTrue(lines.hasNext());
  56.     verify.areEqual("one", lines.next());
  57.     verify.isTrue(lines.hasNext());
  58.     verify.areEqual("two", lines.next());
  59.     verify.isTrue(lines.hasNext());
  60.     verify.areEqual("three", lines.next());
  61.     verify.isFalse(lines.hasNext());
  62.   }
  63.  
  64.   @Test
  65.   public void stringBuilderIteratorWithSingleLinesShouldReturnOneLine() {
  66.     final Iterator<String> lines = LineIterator.of(new StringBuilder("one\ttwo\tthree"));
  67.     verify.isTrue(lines.hasNext());
  68.     verify.areEqual("one\ttwo\tthree", lines.next());
  69.     verify.isFalse(lines.hasNext());
  70.   }
  71.  
  72.   @Test
  73.   public void emptyStringBuilderIteratorShouldReturnOneEmptyLine() {
  74.     final Iterator<String> lines = LineIterator.of(new StringBuilder());
  75.     verify.isTrue(lines.hasNext());
  76.     verify.areEqual("", lines.next());
  77.     verify.isFalse(lines.hasNext());
  78.   }
  79.  
  80.   @Test
  81.   public void stringBuilderIteratorWithMultipleLinesShouldSupportFirstLineRemoval() {
  82.     final StringBuilder stringBuilder = new StringBuilder("one\nand two\nand three");
  83.     final Iterator<String> lines = LineIterator.of(stringBuilder);
  84.     verify.isTrue(lines.hasNext());
  85.     verify.areEqual("one", lines.next());
  86.     lines.remove();
  87.     verify.isTrue(lines.hasNext());
  88.     verify.areEqual("and two", lines.next());
  89.     verify.isTrue(lines.hasNext());
  90.     verify.areEqual("and three", lines.next());
  91.     verify.isFalse(lines.hasNext());
  92.     verify.areEqual("and two\nand three", stringBuilder.toString());
  93.   }
  94.  
  95.   @Test
  96.   public void stringBuilderIteratorWithMultipleLinesShouldSupportMiddleLineRemoval() {
  97.     final StringBuilder stringBuilder = new StringBuilder("one\nand two\nand three");
  98.     final Iterator<String> lines = LineIterator.of(stringBuilder);
  99.     verify.isTrue(lines.hasNext());
  100.     verify.areEqual("one", lines.next());
  101.     verify.isTrue(lines.hasNext());
  102.     verify.areEqual("and two", lines.next());
  103.     lines.remove();
  104.     verify.isTrue(lines.hasNext());
  105.     verify.areEqual("and three", lines.next());
  106.     verify.isFalse(lines.hasNext());
  107.     verify.areEqual("one\nand three", stringBuilder.toString());
  108.   }
  109.  
  110.   @Test
  111.   public void stringBuilderIteratorWithMultipleLinesShouldSupportLastLineRemoval() {
  112.     final StringBuilder stringBuilder = new StringBuilder("one\nand two\nand three");
  113.     final Iterator<String> lines = LineIterator.of(stringBuilder);
  114.     verify.isTrue(lines.hasNext());
  115.     verify.areEqual("one", lines.next());
  116.     verify.isTrue(lines.hasNext());
  117.     verify.areEqual("and two", lines.next());
  118.     verify.isTrue(lines.hasNext());
  119.     verify.areEqual("and three", lines.next());
  120.     lines.remove();
  121.     verify.isFalse(lines.hasNext());
  122.     verify.areEqual("one\nand two\n", stringBuilder.toString());
  123.   }
  124. }
Advertisement
RAW Paste Data Copied
Advertisement