Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Oct 18, 2024
1 parent 5f2da8f commit c30eed8
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 69 deletions.
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ void should_convert_simple_literal() {
String html = process(content);

assertThat(html)
.isEqualTo("<div><pre>This is a literal line.</pre></div>");
.isEqualTo("<div><pre>This is a literal line.</pre></div>");
}

private String documentWithLiteralBlock() {
return "= Document tile\n\n"
+ "== Section\n\n This is a literal line.\n";
+ "== Section\n\n This is a literal line.\n";
}

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

nodeProcessor.process(node);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ void should_convert_simple_list() {
String html = process(content);

assertThat(html)
.isEqualTo("<ul>" +
"<li>unordered item 1</li>" +
"<li>unordered item 2</li>" +
"</ul>");
.isEqualTo("<ul>" +
"<li>unordered item 1</li>" +
"<li>unordered item 2</li>" +
"</ul>");
}

@Test
Expand All @@ -42,44 +42,44 @@ void should_convert_nested_list() {
String html = process(content);

assertThat(html)
.isEqualTo("<ul>" +
"<li>unordered item 1" +
"<ul>" +
"<li>unordered item 1 1</li>" +
"<li>unordered item 1 2</li>" +
"</ul>" +
"</li>" +
"<li>unordered item 2" +
"<ul>" +
"<li>unordered item 2 1" +
"<ul>" +
"<li>unordered item 2 1 1</li></ul></li></ul>" +
"</li>" +
"</ul>");
.isEqualTo("<ul>" +
"<li>unordered item 1" +
"<ul>" +
"<li>unordered item 1 1</li>" +
"<li>unordered item 1 2</li>" +
"</ul>" +
"</li>" +
"<li>unordered item 2" +
"<ul>" +
"<li>unordered item 2 1" +
"<ul>" +
"<li>unordered item 2 1 1</li></ul></li></ul>" +
"</li>" +
"</ul>");
}

private static String buildDocumentWithSimpleList() {
return "= Document tile\n\n"
+ "== Section\n\n"
+ "* unordered item 1\n"
+ "* unordered item 2\n";
+ "== Section\n\n"
+ "* unordered item 1\n"
+ "* unordered item 2\n";
}

private static String buildDocumentWithNestedLists() {
return "= Document tile\n\n"
+ "== Section\n\n"
+ "* unordered item 1\n"
+ "** unordered item 1 1\n"
+ "** unordered item 1 2\n"
+ "* unordered item 2\n"
+ "** unordered item 2 1\n"
+ "*** unordered item 2 1 1\n";
+ "== Section\n\n"
+ "* unordered item 1\n"
+ "** unordered item 1 1\n"
+ "** unordered item 1 2\n"
+ "* unordered item 2\n"
+ "** unordered item 2 1\n"
+ "*** unordered item 2 1 1\n";
}

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

nodeProcessor.process(node);

Expand Down

0 comments on commit c30eed8

Please sign in to comment.