From ac99291a53152e5f8863c29417c914170de2caa9 Mon Sep 17 00:00:00 2001 From: Konrad Windszus Date: Sun, 24 Nov 2024 20:06:51 +0100 Subject: [PATCH] [DOXIA-763] Distinguish between verbatim source and non-source in MarkdownSink Add non-source verbatim test for all Sink implementations --- .../doxia/sink/impl/AbstractSinkTest.java | 30 ++++++++++++++++++- .../maven/doxia/module/apt/AptSinkTest.java | 5 ++++ .../doxia/module/markdown/MarkdownSink.java | 9 ++++-- .../module/markdown/MarkdownSinkTest.java | 15 +++++++--- .../maven/doxia/module/xdoc/XdocSinkTest.java | 5 ++++ .../doxia/module/xhtml5/Xhtml5SinkTest.java | 5 ++++ 6 files changed, 62 insertions(+), 7 deletions(-) diff --git a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/AbstractSinkTest.java b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/AbstractSinkTest.java index eddc4958..755b43c3 100644 --- a/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/AbstractSinkTest.java +++ b/doxia-core/src/test/java/org/apache/maven/doxia/sink/impl/AbstractSinkTest.java @@ -856,6 +856,26 @@ public void testDivider() { assertEquals(expected, actual, "Wrong division!"); } + /** + * Checks that the sequence [verbatim(null), text(text), + * verbatim_()], invoked on the current sink, produces the + * same result as {@link #getVerbatimeBlock getVerbatimBlock}(text). + */ + @Test + public void testVerbatim() { + String text = "Text"; + sink.verbatim(null); + sink.text(text); + sink.verbatim_(); + sink.flush(); + sink.close(); + + String actual = testWriter.toString(); + String expected = getVerbatimBlock(text); + + assertEquals(expected, actual, "Wrong verbatim!"); + } + /** * Checks that the sequence [verbatim(SinkEventAttributeSet.SOURCE), text(text), * verbatim_()], invoked on the current sink, produces the @@ -873,7 +893,7 @@ public void testVerbatimSource() { String actual = testWriter.toString(); String expected = getVerbatimSourceBlock(text); - assertEquals(expected, actual, "Wrong verbatim!"); + assertEquals(expected, actual, "Wrong verbatim source block!"); } /** @@ -1486,6 +1506,14 @@ protected String getOutputDir() { * Returns a Verbatim block generated by this sink. * @param text The text to use. * @return The result of invoking a Verbatim block on the current sink. + * @see #testVerbatim() + */ + protected abstract String getVerbatimBlock(String text); + + /** + * Returns a Verbatim source block generated by this sink. + * @param text The text to use. + * @return The result of invoking a Verbatim source block on the current sink. * @see #testVerbatimSource() */ protected abstract String getVerbatimSourceBlock(String text); diff --git a/doxia-modules/doxia-module-apt/src/test/java/org/apache/maven/doxia/module/apt/AptSinkTest.java b/doxia-modules/doxia-module-apt/src/test/java/org/apache/maven/doxia/module/apt/AptSinkTest.java index 58caaaf4..c3a06156 100644 --- a/doxia-modules/doxia-module-apt/src/test/java/org/apache/maven/doxia/module/apt/AptSinkTest.java +++ b/doxia-modules/doxia-module-apt/src/test/java/org/apache/maven/doxia/module/apt/AptSinkTest.java @@ -243,6 +243,11 @@ protected String getDivisionBlock(String text) { return text; } + /** {@inheritDoc} */ + protected String getVerbatimBlock(String text) { + return EOL + EOL + AptMarkup.VERBATIM_START_MARKUP + EOL + text + EOL + AptMarkup.VERBATIM_START_MARKUP + EOL; + } + /** {@inheritDoc} */ protected String getVerbatimSourceBlock(String text) { return EOL diff --git a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java index 0c1b6b5b..48c36f88 100644 --- a/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java +++ b/doxia-modules/doxia-module-markdown/src/main/java/org/apache/maven/doxia/module/markdown/MarkdownSink.java @@ -28,6 +28,7 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.NoSuchElementException; import java.util.Queue; import java.util.function.UnaryOperator; import java.util.stream.Collectors; @@ -552,9 +553,13 @@ public void paragraph_() { @Override public void verbatim(SinkEventAttributes attributes) { - // always assume is supposed to be monospaced (i.e. emitted inside a
...
) + // if no source attribute, then don't emit an info string startContext(ElementContext.CODE_BLOCK); - writeUnescaped(VERBATIM_START_MARKUP + EOL); + writeUnescaped(VERBATIM_START_MARKUP); + if (attributes != null && attributes.containsAttributes(SinkEventAttributeSet.SOURCE)) { + writeUnescaped("unknown"); // unknown language + } + writeUnescaped(EOL); writeUnescaped(getLinePrefix()); } diff --git a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java index bafae03e..7cffac3d 100644 --- a/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java +++ b/doxia-modules/doxia-module-markdown/src/test/java/org/apache/maven/doxia/module/markdown/MarkdownSinkTest.java @@ -233,10 +233,16 @@ protected String getDivisionBlock(String text) { } /** {@inheritDoc} */ - protected String getVerbatimSourceBlock(String text) { + protected String getVerbatimBlock(String text) { return MarkdownMarkup.VERBATIM_START_MARKUP + EOL + text + EOL + MarkdownMarkup.VERBATIM_END_MARKUP + EOL + EOL; } + /** {@inheritDoc} */ + protected String getVerbatimSourceBlock(String text) { + return MarkdownMarkup.VERBATIM_START_MARKUP + "unknown" + EOL + text + EOL + MarkdownMarkup.VERBATIM_END_MARKUP + + EOL + EOL; + } + /** {@inheritDoc} */ protected String getHorizontalRuleBlock() { return MarkdownMarkup.HORIZONTAL_RULE_MARKUP + EOL + EOL; @@ -548,9 +554,10 @@ public void testMultilineVerbatimSourceAfterListItem() { sink.list_(); } - String expected = "- Before" + EOL + EOL + MarkdownMarkup.INDENT + MarkdownMarkup.VERBATIM_START_MARKUP + EOL - + MarkdownMarkup.INDENT + "codeline1" + EOL + MarkdownMarkup.INDENT + "codeline2" + EOL - + MarkdownMarkup.INDENT + MarkdownMarkup.VERBATIM_END_MARKUP + EOL + EOL + "After" + EOL; + String expected = + "- Before" + EOL + EOL + MarkdownMarkup.INDENT + MarkdownMarkup.VERBATIM_START_MARKUP + "unknown" + EOL + + MarkdownMarkup.INDENT + "codeline1" + EOL + MarkdownMarkup.INDENT + "codeline2" + EOL + + MarkdownMarkup.INDENT + MarkdownMarkup.VERBATIM_END_MARKUP + EOL + EOL + "After" + EOL; assertEquals(expected, getSinkContent(), "Wrong verbatim!"); } diff --git a/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocSinkTest.java b/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocSinkTest.java index d1c5ad77..2d4929e0 100644 --- a/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocSinkTest.java +++ b/doxia-modules/doxia-module-xdoc/src/test/java/org/apache/maven/doxia/module/xdoc/XdocSinkTest.java @@ -258,6 +258,11 @@ protected String getDivisionBlock(String text) { return "
" + text + "
"; } + /** {@inheritDoc} */ + protected String getVerbatimBlock(String text) { + return "
" + text + "
"; + } + /** {@inheritDoc} */ protected String getVerbatimSourceBlock(String text) { return "" + text + ""; diff --git a/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5SinkTest.java b/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5SinkTest.java index c5c1bf36..47789cd6 100644 --- a/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5SinkTest.java +++ b/doxia-modules/doxia-module-xhtml5/src/test/java/org/apache/maven/doxia/module/xhtml5/Xhtml5SinkTest.java @@ -253,6 +253,11 @@ protected String getDivisionBlock(String text) { return "
" + text + "
"; } + /** {@inheritDoc} */ + protected String getVerbatimBlock(String text) { + return "
" + text + "
"; + } + /** {@inheritDoc} */ protected String getVerbatimSourceBlock(String text) { return "
" + text + "
";