aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/styles/parsing
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-04-27 20:54:50 +0100
committerMichael Williamson <mike@zwobble.org>2016-04-28 21:38:41 +0100
commit5bdef288372d3a6a8134479a6497c44f3b17c4dc (patch)
treefaac4e716212c78394b7b982e13f916cb6523d7f /src/test/java/org/zwobble/mammoth/tests/styles/parsing
parentf8ace2ab808441e55ffc95634bc8b88575de78ca (diff)
Replace parboiled parser with hand-written parser
Diffstat (limited to 'src/test/java/org/zwobble/mammoth/tests/styles/parsing')
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/styles/parsing/DocumentMatcherParsingTests.java81
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/styles/parsing/HtmlPathParsingTests.java72
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/styles/parsing/StyleMapParserTests.java76
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/styles/parsing/StyleMappingTokeniserTests.java116
4 files changed, 345 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/styles/parsing/DocumentMatcherParsingTests.java b/src/test/java/org/zwobble/mammoth/tests/styles/parsing/DocumentMatcherParsingTests.java
new file mode 100644
index 0000000..3c00909
--- /dev/null
+++ b/src/test/java/org/zwobble/mammoth/tests/styles/parsing/DocumentMatcherParsingTests.java
@@ -0,0 +1,81 @@
+package org.zwobble.mammoth.tests.styles.parsing;
+
+import org.junit.Test;
+import org.zwobble.mammoth.internal.styles.ParagraphMatcher;
+import org.zwobble.mammoth.internal.styles.RunMatcher;
+import org.zwobble.mammoth.internal.styles.parsing.DocumentMatcherParser;
+import org.zwobble.mammoth.internal.styles.parsing.StyleMappingTokeniser;
+import org.zwobble.mammoth.internal.styles.parsing.TokenIterator;
+import org.zwobble.mammoth.internal.styles.parsing.TokenType;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
+
+public class DocumentMatcherParsingTests {
+ @Test
+ public void readsPlainParagraph() {
+ assertThat(
+ parseParagraphMatcher("p"),
+ deepEquals(ParagraphMatcher.ANY));
+ }
+
+ @Test
+ public void readsParagraphWithStyleId() {
+ assertThat(
+ parseParagraphMatcher("p.Heading1"),
+ deepEquals(ParagraphMatcher.styleId("Heading1")));
+ }
+
+ @Test
+ public void readsParagraphWithStyleName() {
+ assertThat(
+ parseParagraphMatcher("p[style-name='Heading 1']"),
+ deepEquals(ParagraphMatcher.styleName("Heading 1")));
+ }
+
+ @Test
+ public void readsParagraphOrderedList() {
+ assertThat(
+ parseParagraphMatcher("p:ordered-list(2)"),
+ deepEquals(ParagraphMatcher.orderedList("1")));
+ }
+
+ @Test
+ public void readsParagraphUnorderedList() {
+ assertThat(
+ parseParagraphMatcher("p:unordered-list(2)"),
+ deepEquals(ParagraphMatcher.unorderedList("1")));
+ }
+ @Test
+ public void readsRunParagraph() {
+ assertThat(
+ parseRunMatcher("r"),
+ deepEquals(RunMatcher.ANY));
+ }
+
+ @Test
+ public void readsRunWithStyleId() {
+ assertThat(
+ parseRunMatcher("r.Heading1Char"),
+ deepEquals(RunMatcher.styleId("Heading1Char")));
+ }
+
+ @Test
+ public void readsRunWithStyleName() {
+ assertThat(
+ parseRunMatcher("r[style-name='Heading 1 Char']"),
+ deepEquals(RunMatcher.styleName("Heading 1 Char")));
+ }
+
+ private ParagraphMatcher parseParagraphMatcher(String input) {
+ TokenIterator tokens = new TokenIterator(StyleMappingTokeniser.tokenise(input));
+ tokens.skip(TokenType.IDENTIFIER, "p");
+ return DocumentMatcherParser.parseParagraphMatcher(tokens);
+ }
+
+ private RunMatcher parseRunMatcher(String input) {
+ TokenIterator tokens = new TokenIterator(StyleMappingTokeniser.tokenise(input));
+ tokens.skip(TokenType.IDENTIFIER, "r");
+ return DocumentMatcherParser.parseRunMatcher(tokens);
+ }
+}
diff --git a/src/test/java/org/zwobble/mammoth/tests/styles/parsing/HtmlPathParsingTests.java b/src/test/java/org/zwobble/mammoth/tests/styles/parsing/HtmlPathParsingTests.java
new file mode 100644
index 0000000..1df4b0b
--- /dev/null
+++ b/src/test/java/org/zwobble/mammoth/tests/styles/parsing/HtmlPathParsingTests.java
@@ -0,0 +1,72 @@
+package org.zwobble.mammoth.tests.styles.parsing;
+
+import org.junit.Test;
+import org.zwobble.mammoth.internal.styles.HtmlPath;
+import org.zwobble.mammoth.internal.styles.HtmlPathElement;
+import org.zwobble.mammoth.internal.styles.parsing.HtmlPathParser;
+import org.zwobble.mammoth.internal.styles.parsing.StyleMappingTokeniser;
+import org.zwobble.mammoth.internal.styles.parsing.TokenIterator;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.zwobble.mammoth.internal.util.Lists.list;
+import static org.zwobble.mammoth.internal.util.Maps.map;
+import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
+
+public class HtmlPathParsingTests {
+ @Test
+ public void canParseEmptyPath() {
+ assertThat(
+ parseHtmlPath(""),
+ deepEquals(HtmlPath.EMPTY));
+ }
+
+ @Test
+ public void canReadSingleElement() {
+ assertThat(
+ parseHtmlPath("p"),
+ deepEquals(HtmlPath.collapsibleElement("p")));
+ }
+
+ @Test
+ public void canReadElementWithChoiceOfTagNames() {
+ assertThat(
+ parseHtmlPath("ul|ol"),
+ deepEquals(HtmlPath.collapsibleElement(list("ul", "ol"))));
+
+ assertThat(
+ parseHtmlPath("ul|ol|p"),
+ deepEquals(HtmlPath.collapsibleElement(list("ul", "ol", "p"))));
+ }
+
+ @Test
+ public void canReadNestedElements() {
+ assertThat(
+ parseHtmlPath("ul > li"),
+ deepEquals(new HtmlPath(list(HtmlPathElement.collapsible("ul"), HtmlPathElement.collapsible("li")))));
+ }
+
+ @Test
+ public void canReadClassOnElement() {
+ assertThat(
+ parseHtmlPath("p.tip"),
+ deepEquals(HtmlPath.collapsibleElement("p", map("class", "tip"))));
+ }
+
+ @Test
+ public void canReadMultipleClassesOnElement() {
+ assertThat(
+ parseHtmlPath("p.tip.help"),
+ deepEquals(HtmlPath.collapsibleElement("p", map("class", "tip help"))));
+ }
+
+ @Test
+ public void canReadWhenElementMustBeFresh() {
+ assertThat(
+ parseHtmlPath("p:fresh"),
+ deepEquals(HtmlPath.element("p")));
+ }
+
+ private HtmlPath parseHtmlPath(String input) {
+ return HtmlPathParser.parse(new TokenIterator(StyleMappingTokeniser.tokenise(input)));
+ }
+}
diff --git a/src/test/java/org/zwobble/mammoth/tests/styles/parsing/StyleMapParserTests.java b/src/test/java/org/zwobble/mammoth/tests/styles/parsing/StyleMapParserTests.java
new file mode 100644
index 0000000..e76a632
--- /dev/null
+++ b/src/test/java/org/zwobble/mammoth/tests/styles/parsing/StyleMapParserTests.java
@@ -0,0 +1,76 @@
+package org.zwobble.mammoth.tests.styles.parsing;
+
+import org.junit.Test;
+import org.zwobble.mammoth.internal.styles.HtmlPath;
+import org.zwobble.mammoth.internal.styles.ParagraphMatcher;
+import org.zwobble.mammoth.internal.styles.RunMatcher;
+import org.zwobble.mammoth.internal.styles.StyleMap;
+import org.zwobble.mammoth.internal.styles.parsing.StyleMapParser;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
+
+public class StyleMapParserTests {
+ @Test
+ public void emptyStringIsParsedAsEmptyStyleMap() {
+ StyleMap styleMap = StyleMapParser.parse("");
+ assertThat(styleMap, deepEquals(StyleMap.builder().build()));
+ }
+
+ @Test
+ public void canMapParagraphs() {
+ StyleMap styleMap = StyleMapParser.parse("p => p");
+ assertThat(styleMap, deepEquals(StyleMap.builder().mapParagraph(ParagraphMatcher.ANY, HtmlPath.collapsibleElement("p")).build()));
+ }
+
+ @Test
+ public void canMapRuns() {
+ StyleMap styleMap = StyleMapParser.parse("r => p");
+ assertThat(styleMap, deepEquals(StyleMap.builder().mapRun(RunMatcher.ANY, HtmlPath.collapsibleElement("p")).build()));
+ }
+
+ @Test
+ public void canMapBold() {
+ StyleMap styleMap = StyleMapParser.parse("b => em");
+ assertThat(styleMap, deepEquals(StyleMap.builder().bold(HtmlPath.collapsibleElement("em")).build()));
+ }
+
+ @Test
+ public void canMapItalic() {
+ StyleMap styleMap = StyleMapParser.parse("i => strong");
+ assertThat(styleMap, deepEquals(StyleMap.builder().italic(HtmlPath.collapsibleElement("strong")).build()));
+ }
+
+ @Test
+ public void canMapUnderline() {
+ StyleMap styleMap = StyleMapParser.parse("u => em");
+ assertThat(styleMap, deepEquals(StyleMap.builder().underline(HtmlPath.collapsibleElement("em")).build()));
+ }
+
+ @Test
+ public void canMapStrikethrough() {
+ StyleMap styleMap = StyleMapParser.parse("strike => del");
+ assertThat(styleMap, deepEquals(StyleMap.builder().strikethrough(HtmlPath.collapsibleElement("del")).build()));
+ }
+
+ @Test
+ public void blankLinesAreIgnored() {
+ StyleMap styleMap = StyleMapParser.parse("\n\n \n\np =>\n\r\n");
+ assertThat(styleMap, deepEquals(StyleMap.builder().mapParagraph(ParagraphMatcher.ANY, HtmlPath.EMPTY).build()));
+ }
+
+ @Test
+ public void lineStartingWithHashIsIgnored() {
+ StyleMap styleMap = StyleMapParser.parse("#p => p");
+ assertThat(styleMap, deepEquals(StyleMap.EMPTY));
+ }
+
+ @Test
+ public void canParseMultipleMappings() {
+ StyleMap styleMap = StyleMapParser.parse("p =>\nr =>");
+ assertThat(styleMap, deepEquals(StyleMap.builder()
+ .mapParagraph(ParagraphMatcher.ANY, HtmlPath.EMPTY)
+ .mapRun(RunMatcher.ANY, HtmlPath.EMPTY)
+ .build()));
+ }
+}
diff --git a/src/test/java/org/zwobble/mammoth/tests/styles/parsing/StyleMappingTokeniserTests.java b/src/test/java/org/zwobble/mammoth/tests/styles/parsing/StyleMappingTokeniserTests.java
new file mode 100644
index 0000000..de60249
--- /dev/null
+++ b/src/test/java/org/zwobble/mammoth/tests/styles/parsing/StyleMappingTokeniserTests.java
@@ -0,0 +1,116 @@
+package org.zwobble.mammoth.tests.styles.parsing;
+
+import org.hamcrest.Matcher;
+import org.junit.Test;
+import org.zwobble.mammoth.internal.styles.parsing.StyleMappingTokeniser;
+import org.zwobble.mammoth.internal.styles.parsing.Token;
+import org.zwobble.mammoth.internal.styles.parsing.TokenType;
+
+import java.util.List;
+
+import static java.util.Arrays.asList;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.*;
+import static org.zwobble.mammoth.internal.util.Lists.eagerConcat;
+import static org.zwobble.mammoth.internal.util.Lists.list;
+
+public class StyleMappingTokeniserTests {
+ @Test
+ public void emptyStringIsTokenisedToEndOfFileToken() {
+ assertTokens("");
+ }
+
+ @Test
+ public void whitespaceIsTokenised() {
+ assertTokens(" \t\t ", isToken(TokenType.WHITESPACE, " \t\t "));
+ }
+
+ @Test
+ public void identifiersAreTokenised() {
+ assertTokens("Overture", isToken(TokenType.IDENTIFIER, "Overture"));
+ }
+
+ @Test
+ public void integersAreTokenised() {
+ assertTokens("123", isToken(TokenType.INTEGER, "123"));
+ }
+
+ @Test
+ public void stringsAreTokenised() {
+ assertTokens("'Tristan'", isToken(TokenType.STRING, "'Tristan'"));
+ }
+
+ @Test
+ public void arrowsAreTokenised() {
+ assertTokens("=>=>", isToken(TokenType.ARROW, "=>"), isToken(TokenType.ARROW, "=>"));
+ }
+
+ @Test
+ public void dotsAreTokenised() {
+ assertTokens("..", isToken(TokenType.DOT, "."), isToken(TokenType.DOT, "."));
+ }
+
+ @Test
+ public void colonsAreTokenised() {
+ assertTokens("::", isToken(TokenType.COLON, ":"), isToken(TokenType.COLON, ":"));
+ }
+
+ @Test
+ public void greaterThansAreTokenised() {
+ assertTokens(">>", isToken(TokenType.GREATER_THAN, ">"), isToken(TokenType.GREATER_THAN, ">"));
+ }
+
+ @Test
+ public void equalsAreTokenised() {
+ assertTokens("==", isToken(TokenType.EQUALS, "="), isToken(TokenType.EQUALS, "="));
+ }
+
+ @Test
+ public void openParensAreTokenised() {
+ assertTokens("((", isToken(TokenType.OPEN_PAREN, "("), isToken(TokenType.OPEN_PAREN, "("));
+ }
+
+ @Test
+ public void closeParensAreTokenised() {
+ assertTokens("))", isToken(TokenType.CLOSE_PAREN, ")"), isToken(TokenType.CLOSE_PAREN, ")"));
+ }
+
+ @Test
+ public void openSquareBracketsAreTokenised() {
+ assertTokens("[[", isToken(TokenType.OPEN_SQUARE_BRACKET, "["), isToken(TokenType.OPEN_SQUARE_BRACKET, "["));
+ }
+
+ @Test
+ public void closeSquareBracketsAreTokenised() {
+ assertTokens("]]", isToken(TokenType.CLOSE_SQUARE_BRACKET, "]"), isToken(TokenType.CLOSE_SQUARE_BRACKET, "]"));
+ }
+
+ @Test
+ public void choicesAreTokenised() {
+ assertTokens("||", isToken(TokenType.CHOICE, "|"), isToken(TokenType.CHOICE, "|"));
+ }
+
+ @Test
+ public void canTokeniseMultipleTokens() {
+ assertTokens("The Magic Position",
+ isToken(TokenType.IDENTIFIER, "The"),
+ isToken(TokenType.WHITESPACE, " "),
+ isToken(TokenType.IDENTIFIER, "Magic"),
+ isToken(TokenType.WHITESPACE, " "),
+ isToken(TokenType.IDENTIFIER, "Position")
+ );
+ }
+
+ @SafeVarargs
+ private final void assertTokens(String input, Matcher<Token>... token) {
+ List<Matcher<? super Token>> tokens = eagerConcat(asList(token), list(isToken(TokenType.EOF, "")));
+ assertThat(StyleMappingTokeniser.tokenise(input), contains(tokens));
+ }
+
+ private Matcher<Token> isToken(TokenType tokenType, String value) {
+ return allOf(
+ hasProperty("tokenType", equalTo(tokenType)),
+ hasProperty("value", equalTo(value))
+ );
+ }
+}