Skip to content

Commit

Permalink
Add tests for conversion to DocBook
Browse files Browse the repository at this point in the history
  • Loading branch information
0x8000-0000 committed Apr 13, 2020
1 parent 1b41c5c commit c4ad87d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/signbit/samx/XmlTextVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ public Exception visitIncludeFile(SamXParser.IncludeFileContext ctx)
StringBuilder builder = new StringBuilder();

builder.append("<!-- ");
builder.append("include: ");
builder.append("begin include: ");

final String reference = ctx.reference.getText();

Expand Down Expand Up @@ -661,7 +661,7 @@ public Exception visitIncludeFile(SamXParser.IncludeFileContext ctx)

StringBuilder endBuilder = new StringBuilder();
endBuilder.append("<!-- ");
endBuilder.append("include: ");
endBuilder.append("end include: ");
endBuilder.append(reference);
endBuilder.append(" -->");
append(endBuilder);
Expand Down
42 changes: 39 additions & 3 deletions src/test/java/net/signbit/samx/parser/XmlConverterTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package net.signbit.samx.parser;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;

import org.junit.Test;

import net.signbit.samx.Parser;
import net.signbit.samx.XmlTextVisitor;

import static org.junit.Assert.assertEquals;

public class XmlConverterTest
Expand All @@ -26,13 +30,33 @@ private static String convert(String inputString)
return writer.toString();
}

private static String convert(File inputFile, String root, String namespace, String version) throws IOException
{
HashMap<String, Parser.Result> includedDocuments = new HashMap<>();
HashMap<String, IOException> includedExceptions = new HashMap<>();
Parser.Result result = Parser.parse(inputFile, includedDocuments, includedExceptions);

StringWriter writer = new StringWriter();

XmlTextVisitor visitor = new XmlTextVisitor(writer, result.includedDocuments, result.includedExceptions, result.referencePaths);
visitor.setTokenStream(result.tokens);
visitor.setTopElement(root);
visitor.setTopElementNamespace(namespace);
visitor.setTopElementVersion(version);

visitor.visit(result.document);
writer.flush();

return writer.toString();
}

private void testConversion(String resourceName, String prettifiedResource)
{
final String original = TestUtils.getResourceContents(resourceName);

final String converted = convert(original);
final String converted = convert(original);

final String expected = TestUtils.getResourceContents(prettifiedResource);
final String expected = TestUtils.getResourceContents(prettifiedResource);

assertEquals(expected, converted);
}
Expand All @@ -44,4 +68,16 @@ public void testLists()

testConversion("lists/nested_lists.samx", "lists/nested_lists.xml");
}

@Test
public void testDocBook() throws IOException
{
final File inputFile = new File("build/resources/test/docbook/main.samx");

final String converted = convert(inputFile, "book", "http://docbook.org/ns/docbook", "5.1");

final String expected = TestUtils.getResourceContents("docbook/book.xml");

assertEquals(expected, converted);
}
}
47 changes: 47 additions & 0 deletions src/test/resources/docbook/book.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8" ?>
<book xmlns="http://docbook.org/ns/docbook" version="5.1">
<info>
<title>My First Book</title>
<author>
<personname>
<firstname>Sign</firstname>
<surname>Bit</surname>
</personname>
</author>
<copyright>
<year>2020</year>
<holder>Sign Bit</holder>
</copyright>
<revhistory>
<revision>
<revnumber>0.1</revnumber>
<date>2020-04-07</date>
<authorinitials>sb</authorinitials>
<revremark>First draft</revremark>
</revision>
</revhistory>
</info>
<preface>
<title>Foreword</title>
</preface>
<!-- begin include: chapter1.samx -->
<chapter>
<title>My First Chapter</title>
<para>Introductory paragraph, terminated with.</para>
<section>
<title>First Section</title>
<para>More paragraphs</para>
</section>
</chapter>
<!-- end include: chapter1.samx -->
<!-- begin include: chapter2.samx -->
<chapter>
<title>My Second Chapter</title>
<para>Introductory paragraph</para>
<section>
<title>First Section</title>
<para>More paragraphs indeed</para>
</section>
</chapter>
<!-- end include: chapter2.samx -->
</book>

0 comments on commit c4ad87d

Please sign in to comment.