| /*=============================================================================# |
| # Copyright (c) 2015, 2018 David Green and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 |
| # |
| # Contributors: |
| # David Green - org.eclipse.mylyn.docs: initial API and implementation |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.docmlet.wikitext.commonmark.core; |
| |
| import static org.junit.Assert.assertEquals; |
| import static org.junit.Assert.assertNotNull; |
| import static org.junit.Assert.assertNotSame; |
| import static org.junit.Assert.assertNull; |
| import static org.junit.Assert.assertSame; |
| |
| import org.junit.Rule; |
| import org.junit.Test; |
| import org.junit.rules.ExpectedException; |
| |
| |
| public class LineSequenceTest { |
| |
| @Rule |
| public final ExpectedException thrown= ExpectedException.none(); |
| |
| |
| public LineSequenceTest() { |
| } |
| |
| |
| @Test |
| public void createRequiresContent() { |
| this.thrown.expect(NullPointerException.class); |
| LineSequence.create((String) null); |
| } |
| |
| @Test |
| public void empty() { |
| assertOneLine("", LineSequence.create("")); |
| } |
| |
| @Test |
| public void oneLine() { |
| assertOneLine("a", LineSequence.create("a")); |
| } |
| |
| @Test |
| public void twoLines() { |
| assertTwoLines(LineSequence.create("abc\r\ndefg")); |
| } |
| |
| |
| @Test |
| public void advance() { |
| assertAdvance(LineSequence.create("one")); |
| } |
| |
| @Test |
| public void lookAhead() { |
| assertLookAhead(LineSequence.create("a\nb\nc")); |
| } |
| |
| @Test |
| public void lookAheadFailsFast() { |
| assertLookAheadFailsFast(LineSequence.create("a\nb\nc")); |
| } |
| |
| private void assertLookAheadFailsFast(final LineSequence lineSequence) { |
| final LineSequence lookAhead= lineSequence.lookAhead(); |
| lineSequence.advance(); |
| this.thrown.expect(IllegalStateException.class); |
| lookAhead.advance(); |
| } |
| |
| private void assertAdvance(final LineSequence lineSequence) { |
| lineSequence.advance(); |
| assertNoLinesRemain(lineSequence); |
| lineSequence.advance(); |
| assertNoLinesRemain(lineSequence); |
| lineSequence.advance(); |
| assertNoLinesRemain(lineSequence); |
| } |
| |
| private void assertNoLinesRemain(final LineSequence lineSequence) { |
| assertNull(lineSequence.getCurrentLine()); |
| assertNull(lineSequence.getNextLine()); |
| } |
| |
| private void assertLookAhead(final LineSequence lineSequence) { |
| lineSequence.advance(); |
| assertEquals("b", lineSequence.getCurrentLine().getText()); |
| final LineSequence lookAhead= lineSequence.lookAhead(); |
| assertEquals(lineSequence.getCurrentLine(), lookAhead.getCurrentLine()); |
| lookAhead.advance(); |
| assertEquals("b", lineSequence.getCurrentLine().getText()); |
| assertEquals("c", lookAhead.getCurrentLine().getText()); |
| final LineSequence lookAhead2= lookAhead.lookAhead(); |
| assertNotNull(lookAhead2); |
| assertNotSame(lookAhead, lookAhead2); |
| assertNotSame(lookAhead2, lookAhead.lookAhead()); |
| lookAhead.advance(); |
| assertEquals("c", lookAhead2.getCurrentLine().getText()); |
| assertNoLinesRemain(lookAhead); |
| assertNoLinesRemain(lookAhead.lookAhead()); |
| assertEquals("b", lineSequence.getCurrentLine().getText()); |
| lineSequence.advance(); |
| assertEquals("c", lineSequence.getCurrentLine().getText()); |
| } |
| |
| private void assertTwoLines(final LineSequence lineSequence) { |
| final Line currentLine= lineSequence.getCurrentLine(); |
| assertNotNull(currentLine); |
| assertEquals("abc", currentLine.getText()); |
| assertEquals(0, currentLine.getStartOffset()); |
| assertEquals(0, currentLine.getLineNumber()); |
| assertSame(currentLine, lineSequence.getCurrentLine()); |
| final Line nextLine= lineSequence.getNextLine(); |
| assertNotNull(nextLine); |
| assertEquals("defg", nextLine.getText()); |
| assertEquals(5, nextLine.getStartOffset()); |
| assertEquals(1, nextLine.getLineNumber()); |
| assertSame(nextLine, lineSequence.getNextLine()); |
| |
| lineSequence.advance(); |
| |
| assertNotSame(currentLine, lineSequence.getCurrentLine()); |
| assertNotNull(lineSequence.getCurrentLine()); |
| assertEquals("defg", lineSequence.getCurrentLine().getText()); |
| assertNull(lineSequence.getNextLine()); |
| |
| lineSequence.advance(); |
| |
| assertNoLinesRemain(lineSequence); |
| } |
| |
| private void assertOneLine(final String line1, final LineSequence lineSequence) { |
| final Line currentLine= lineSequence.getCurrentLine(); |
| assertNotNull(currentLine); |
| assertEquals(line1, currentLine.getText()); |
| assertSame(currentLine, lineSequence.getCurrentLine()); |
| assertNull(lineSequence.getNextLine()); |
| lineSequence.advance(); |
| assertNoLinesRemain(lineSequence); |
| } |
| |
| } |