blob: cb643d25ee3d71dccab15daa09d4b67ec93fe84b [file] [log] [blame]
/*=============================================================================#
# 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.inlines;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.eclipse.statet.jcommons.collections.ImCollections;
import org.eclipse.statet.internal.docmlet.wikitext.commonmark.core.CommonmarkAsserts;
import org.eclipse.statet.internal.docmlet.wikitext.commonmark.core.Line;
import org.eclipse.statet.internal.docmlet.wikitext.commonmark.core.LineSequence;
import org.eclipse.statet.internal.docmlet.wikitext.commonmark.core.TextSegment;
public class InlineParserTest {
@Rule
public final ExpectedException thrown= ExpectedException.none();
private final Line line= new Line(0, 1, 0, "test", "\n");
public InlineParserTest() {
}
@Test
public void requiresSpans() {
this.thrown.expect(NullPointerException.class);
new InlineParser(null);
}
@Test
public void parse() {
assertParse("");
assertParse("one\ntwo",
new Characters(this.line, 0, 3, 3, "one"),
new SoftLineBreak(this.line, 3, 1, 1),
new Characters(this.line, 4, 3, 3, "two") );
assertParse("one\ntwo three",
new Characters(this.line, 0, 3, 3, "one"),
new SoftLineBreak(this.line, 3, 1, 1),
new Characters(this.line, 4, 9, 9, "two three") );
}
@Test
public void toStringContent() {
final InlineParser parser= new InlineParser(ImCollections.newList(
new CodeSpan(), new AllCharactersSpan() ));
final String stringContent= parser.toStringContent(CommonmarkAsserts.newContext(),
new TextSegment(ImCollections.newList(new Line(1, 0, 0, "one `two` three", "\n"))));
assertEquals("one two three", stringContent);
}
private void assertParse(final String content, final Inline... inlines) {
final List<Inline> expected= Arrays.asList(inlines);
final List<Inline> actual= createInlines().parse(CommonmarkAsserts.newContext(),
new TextSegment(LineSequence.create(content)), true );
for (int x= 0; x < expected.size() && x < actual.size(); ++x) {
assertEquivalent(x, expected.get(x), actual.get(x));
}
assertEquals(expected, actual);
}
private void assertEquivalent(final int index, final Inline expected, final Inline actual) {
final String message= "inline at " + index;
assertEquals(message + " type", expected.getClass(), actual.getClass());
assertEquals(message + " startOffset", expected.getStartOffset(), actual.getStartOffset());
assertEquals(message + " length", expected.getLength(), actual.getLength());
assertEquals(message, expected, actual);
}
private InlineParser createInlines() {
return new InlineParser(ImCollections.newList(
new LineBreakSpan(), new StringCharactersSpan(), new AllCharactersSpan() ));
}
}