Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Oct 19, 2024
1 parent 5f2da8f commit d77ba1c
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 152 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Improvements::
* Added support for AsciidoctorJ v3.0.0 (#651)
* Add compatibility with maven-site-plugin v3.20.0 and Doxia v2.0.0 (#933)
* Add support for code blocks titles in asciidoctor-parser-doxia-module (#935)
* Refactor AST traversal method in asciidoctor-parser-doxia-module (#944)
* Empty titles in document or empty literals no longer generate <h1> or <pre> in asciidoctor-parser-doxia-module (#944)
* Sections are now wrapped in <div> in asciidoctor-parser-doxia-module (#944)

Build / Infrastructure::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import java.util.logging.Logger;

import org.apache.maven.doxia.parser.AbstractTextParser;
Expand All @@ -28,6 +29,7 @@
import org.asciidoctor.maven.site.SiteConversionConfiguration;
import org.asciidoctor.maven.site.SiteConversionConfigurationParser;
import org.asciidoctor.maven.site.SiteLogHandlerDeserializer;
import org.asciidoctor.maven.site.parser.processors.DescriptionListNodeProcessor;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.xml.Xpp3Dom;
Expand Down Expand Up @@ -82,7 +84,7 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
final Asciidoctor asciidoctor = Asciidoctor.Factory.create();

SiteConversionConfiguration conversionConfig = new SiteConversionConfigurationParser(project)
.processAsciiDocConfig(siteConfig, defaultOptions(siteDirectory), defaultAttributes());
.processAsciiDocConfig(siteConfig, defaultOptions(siteDirectory), defaultAttributes());
for (String require : conversionConfig.getRequires()) {
requireLibrary(asciidoctor, require);
}
Expand All @@ -98,7 +100,7 @@ public void parse(Reader reader, Sink sink, String reference) throws ParseExcept
try {
// process log messages according to mojo configuration
new LogRecordsProcessors(logHandler, siteDirectory, errorMessage -> logger.error(errorMessage))
.processLogRecords(memoryLogHandler);
.processLogRecords(memoryLogHandler);

} catch (Exception exception) {
throw new ParseException(exception.getMessage(), exception);
Expand Down Expand Up @@ -146,15 +148,15 @@ protected File resolveSiteDirectory(MavenProject project, Xpp3Dom siteConfig) {

protected OptionsBuilder defaultOptions(File siteDirectory) {
return Options.builder()
.backend("xhtml")
.safe(SafeMode.UNSAFE)
.baseDir(new File(siteDirectory, ROLE_HINT));
.backend("xhtml")
.safe(SafeMode.UNSAFE)
.baseDir(new File(siteDirectory, ROLE_HINT));
}

protected AttributesBuilder defaultAttributes() {
return Attributes.builder()
.attribute("idprefix", "@")
.attribute("showtitle", "@");
.attribute("idprefix", "@")
.attribute("showtitle", "@");
}

private void requireLibrary(Asciidoctor asciidoctor, String require) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.asciidoctor.maven.site.parser.processors.UnorderedListNodeProcessor;

/**
* Factory and repository for NodeProcessors.
*
* @author abelsromero
* @since 3.1.0
*/
Expand All @@ -29,7 +31,6 @@ public class NodeSinker {

private final NodeProcessor noOpProcessor;

// TODO this should not be public
public NodeSinker(Sink sink) {
nodeProcessors = Arrays.asList(
new DescriptionListNodeProcessor(sink, this),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.asciidoctor.maven.site.parser.NodeProcessor;
import org.asciidoctor.maven.site.parser.NodeSinker;

import static org.asciidoctor.maven.commons.StringUtils.isNotBlank;

/**
* Root document processor.
*
Expand Down Expand Up @@ -32,11 +34,12 @@ public void process(StructuralNode node) {
final Sink sink = getSink();

sink.body();
// TODO review how this fits with Section titles also being 1
sink.sectionTitle1();
sink.rawText(node.getTitle());
sink.sectionTitle1_();

String title = node.getTitle();
if (isNotBlank(title)) {
sink.sectionTitle1();
sink.rawText(title);
sink.sectionTitle1_();
}
node.getBlocks()
.forEach(this::sink);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.apache.maven.doxia.sink.Sink;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.jruby.ast.impl.BlockImpl;
import org.asciidoctor.maven.commons.StringUtils;
import org.asciidoctor.maven.site.parser.NodeProcessor;
import org.asciidoctor.maven.site.parser.NodeSinker;
Expand Down Expand Up @@ -37,7 +36,6 @@ public boolean applies(StructuralNode node) {
return "listing".equals(node.getNodeName());
}

// TODO test with a table or other elements inside: I assume they don't create notes because it's verbatim
@Override
public void process(StructuralNode node) {
final StringBuilder contentBuilder = new StringBuilder();
Expand Down Expand Up @@ -67,7 +65,8 @@ public void process(StructuralNode node) {
contentBuilder.append("<pre>");
}

contentBuilder.append(((BlockImpl) node).getSource());
// Use 'content' (not 'source') to apply substitutions of special characters
contentBuilder.append((String) node.getContent());

if (isSourceBlock) {
contentBuilder.append("</code>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.maven.doxia.sink.Sink;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.jruby.ast.impl.BlockImpl;
import org.asciidoctor.maven.commons.StringUtils;
import org.asciidoctor.maven.site.parser.NodeProcessor;
import org.asciidoctor.maven.site.parser.NodeSinker;

Expand Down Expand Up @@ -33,10 +34,13 @@ public boolean applies(StructuralNode node) {
public void process(StructuralNode node) {
final Sink sink = getSink();

sink.division();
sink.rawText("<pre>");
sink.rawText(((BlockImpl) node).getSource());
sink.rawText("</pre>");
sink.division_();
String source = ((BlockImpl) node).getSource();
if (StringUtils.isNotBlank(source)) {
sink.division();
sink.rawText("<pre>");
sink.rawText(source);
sink.rawText("</pre>");
sink.division_();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package org.asciidoctor.maven.site.parser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.StringReader;
import java.io.StringWriter;

import lombok.SneakyThrows;
import org.apache.maven.doxia.parser.AbstractTextParser;
import org.apache.maven.doxia.parser.ParseException;
Expand All @@ -10,8 +16,6 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.io.*;

import static org.asciidoctor.maven.site.parser.AsciidoctorAstDoxiaParserTest.TestMocks.mockAsciidoctorDoxiaParser;
import static org.asciidoctor.maven.site.parser.processors.test.ReflectionUtils.extractField;
import static org.asciidoctor.maven.site.parser.processors.test.StringTestUtils.clean;
Expand Down Expand Up @@ -45,17 +49,23 @@ void should_convert_html_without_any_configuration() throws FileNotFoundExceptio

assertThat(result)
.isEqualTo("<h1>Document Title</h1><p>Preamble paragraph.</p>" +
"<div>"+
"<h2><a id=\"id_section_a\"></a>Section A</h2>" +
"<p><strong>Section A</strong> paragraph.</p>" +
"<div>"+
"<h3><a id=\"id_section_a_subsection\"></a>Section A Subsection</h3>" +
"<p><strong>Section A</strong> 'subsection' paragraph.</p>" +
"</div>"+
"</div>"+
"<div>"+
"<h2><a id=\"id_section_b\"></a>Section B</h2>" +
"<p><strong>Section B</strong> paragraph.</p>" +
"<ul>" +
"<li>Item 1</li>" +
"<li>Item 2</li>" +
"<li>Item 3</li></ul>" +
"<div class=\"source\"><pre class=\"prettyprint\"><code>require 'asciidoctor'</code></pre></div>");
"<div class=\"source\"><pre class=\"prettyprint\"><code>require 'asciidoctor'</code></pre></div>" +
"</div>");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import org.apache.maven.doxia.sink.Sink;
import org.asciidoctor.ast.Block;
import org.asciidoctor.ast.Document;
import org.asciidoctor.ast.ListItem;
import org.asciidoctor.ast.StructuralNode;
Expand All @@ -27,7 +28,6 @@ class NodeSinkerTest {
private NodeSinker nodeSinker;
private StringWriter sinkWriter;


@BeforeEach
void setup() throws NoSuchFieldException, IllegalAccessException {
Sink sink = createSink();
Expand All @@ -53,6 +53,7 @@ void should_not_fail_when_processing_invalid_node() {
@Test
void should_process_document_node() {
StructuralNode mockNode = mockNode("document");
Mockito.when(mockNode.getTitle()).thenReturn("Something");

nodeSinker.sink(mockNode);

Expand Down Expand Up @@ -171,6 +172,9 @@ private static StructuralNode mockNode(String nodeName) {
private static <T> T mockNode(String nodeName, Class<? extends StructuralNode> clazz) {
StructuralNode mockNode = Mockito.mock(clazz);
Mockito.when(mockNode.getNodeName()).thenReturn(nodeName);
if (Block.class.isAssignableFrom(clazz)) {
Mockito.when(((Block) mockNode).getSource()).thenReturn("Something");
}
return (T) mockNode;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.asciidoctor.maven.site.parser.processors;

import java.io.StringWriter;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Options;
import org.asciidoctor.ast.StructuralNode;
import org.asciidoctor.maven.site.parser.NodeProcessor;
import org.asciidoctor.maven.site.parser.processors.test.NodeProcessorTest;
import org.junit.jupiter.api.Test;

import java.io.StringWriter;

import static org.assertj.core.api.Assertions.assertThat;

@NodeProcessorTest(DocumentNodeProcessor.class)
Expand All @@ -18,14 +18,24 @@ class DocumentNodeProcessorTest {
private NodeProcessor nodeProcessor;
private StringWriter sinkWriter;

@Test
void should_not_fail_if_document_is_empty() {
String content = "";

String html = process(content, 0);

assertThat(html)
.isEmpty();
}

@Test
void should_convert_document_title() {
String content = "= Document tile";

String html = process(content, 0);

assertThat(html)
.isEqualTo("<h1>Document tile</h1>");
.isEqualTo("<h1>Document tile</h1>");
}

@Test
Expand All @@ -35,7 +45,7 @@ void should_convert_document_title_with_markup() {
String html = process(content, 0);

assertThat(html)
.isEqualTo("<h1><strong>Document</strong> <em>tile</em></h1>");
.isEqualTo("<h1><strong>Document</strong> <em>tile</em></h1>");
}

private String process(String content, int level) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.asciidoctor.maven.site.parser.processors;

import java.io.StringWriter;
import java.util.Collections;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Options;
import org.asciidoctor.ast.StructuralNode;
Expand All @@ -9,9 +12,6 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.StringWriter;
import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

@NodeProcessorTest(ListItemNodeProcessor.class)
Expand All @@ -25,51 +25,51 @@ class ListItemNodeProcessorTest {
@ValueSource(strings = {"*", "-"})
void should_convert_list_item(String marker) {
String content = new DocumentBuilder()
.listItem(marker)
.toString();
.listItem(marker)
.toString();

String html = process(content);

assertThat(html)
.isEqualTo(htmlListItem());
.isEqualTo(htmlListItem());
}

@Test
void should_convert_ordered_list_item() {
String content = new DocumentBuilder()
.listItem(".")
.toString();
.listItem(".")
.toString();

String html = process(content);

assertThat(html)
.isEqualTo(htmlListItem());
.isEqualTo(htmlListItem());
}

@ParameterizedTest
@ValueSource(strings = {"*", "-"})
void should_convert_ordered_list_item_with_formatting() {
String content = new DocumentBuilder()
.formattedListItem("*")
.toString();
.formattedListItem("*")
.toString();

String html = process(content);

assertThat(html)
.isEqualTo(htmlListItemWithFormatting());
.isEqualTo(htmlListItemWithFormatting());
}

@ParameterizedTest
@ValueSource(strings = {"*", "-"})
void should_convert_ordered_list_item_with_link(String marker) {
String content = new DocumentBuilder()
.linkListItem(marker)
.toString();
.linkListItem(marker)
.toString();

String html = process(content);

assertThat(html)
.isEqualTo(htmlListItemWithLink());
.isEqualTo(htmlListItemWithLink());
}

private static String htmlListItem() {
Expand Down Expand Up @@ -116,8 +116,8 @@ public String toString() {

private String process(String content) {
StructuralNode node = asciidoctor.load(content, Options.builder().build())
.findBy(Collections.singletonMap("context", ":list_item"))
.get(0);
.findBy(Collections.singletonMap("context", ":list_item"))
.get(0);

nodeProcessor.process(node);

Expand Down
Loading

0 comments on commit d77ba1c

Please sign in to comment.