Skip to content

Commit

Permalink
Move unit test resources
Browse files Browse the repository at this point in the history
Signed-off-by: Jarno Elovirta <jarno@elovirta.com>
  • Loading branch information
jelovirt committed Nov 4, 2023
1 parent 49c01fb commit 8be8b02
Show file tree
Hide file tree
Showing 112 changed files with 65 additions and 447 deletions.
123 changes: 50 additions & 73 deletions src/test/java/com/elovirta/dita/markdown/AstToMarkdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,107 +10,84 @@
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class AstToMarkdownTest {

private byte[] run(final String input) throws Exception {
final ByteArrayOutputStream o = new ByteArrayOutputStream();
try (
InputStream style = getClass().getResourceAsStream("/ast.xsl");
InputStream ri = getClass().getResourceAsStream("/" + input)
) {
final TransformerFactory tf = TransformerFactory.newInstance();
tf.setURIResolver(new ClasspathURIResolver(tf.getURIResolver()));
final Transformer t = tf.newTransformer(new StreamSource(style, "classpath:///ast.xsl"));
t.transform(new StreamSource(ri), new StreamResult(o));
}
Files.write(
Paths.get(
"/Users/jelovirt/Work/github.com/jelovirt/org.lwdita/src/test/resources/out/" +
input.substring(4, input.indexOf(".")) +
".md"
),
o.toByteArray()
);
return o.toByteArray();
}

@ParameterizedTest
@ValueSource(
strings = {
"abbreviation",
"admonition",
"ast",
"body_attributes",
"codeblock",
"dita2abbreviation",
"dita2admonition",
"dita2ast",
"dita2body_attributes",
"dita2codeblock",
"dita2comment",
"dita2concept",
"dita2conkeyref",
"dita2conref",
"dita2dl",
"dita2entity",
"dita2escape",
"dita2footnote",
"dita2hdita",
"dita2header",
"dita2header_attributes",
"dita2html",
"dita2image-size",
"dita2image",
"dita2inline",
"dita2inline_extended",
"dita2jekyll",
"dita2keyref",
"dita2keys",
"dita2linebreak",
"dita2link",
"dita2missing_root_header",
"dita2missing_root_header_with_yaml",
"dita2multiple_top_level",
"dita2multiple_top_level_specialized",
"dita2note",
"dita2ol",
"dita2pandoc_header",
"dita2quote",
"dita2reference",
"dita2short",
"dita2shortdesc",
"dita2table-width",
"dita2table",
"dita2task",
"dita2taskOneStep",
"dita2taskTight",
"dita2testBOM",
"dita2testNoBOM",
"dita2topic",
"dita2ul",
"dita2unsupported_html",
"dita2yaml",
"comment",
"concept",
"conkeyref",
"conref",
"dl",
"entity",
"escape",
"footnote",
"hdita",
"header",
"header_attributes",
"html",
"image-size",
"image",
"inline",
"inline_extended",
"jekyll",
"keyref",
"keys",
"linebreak",
"link",
"missing_root_header",
"missing_root_header_with_yaml",
"multiple_top_level",
"multiple_top_level_specialized",
"note",
"ol",
"pandoc_header",
"quote",
"reference",
"short",
"shortdesc",
"table-width",
"table",
"task",
"taskOneStep",
"taskTight",
"testBOM",
"testNoBOM",
"topic",
"ul",
"unsupported_html",
"yaml",
}
)
public void testAst(String name) throws Exception {
final byte[] act = run("ast/" + name + ".xml");
final byte[] exp = read("out/" + name + ".md");
final byte[] act = run("output/ast/" + name + ".xml");
final byte[] exp = read("output/markdown/" + name + ".md");
assertArrayEquals(exp, act);
}

private byte[] run(final String input) throws Exception {
final ByteArrayOutputStream o = new ByteArrayOutputStream();
try (
InputStream style = getClass().getResourceAsStream("/ast.xsl");
InputStream ri = getClass().getResourceAsStream("/" + input)
) {
final TransformerFactory tf = TransformerFactory.newInstance();
tf.setURIResolver(new ClasspathURIResolver(tf.getURIResolver()));
final Transformer t = tf.newTransformer(new StreamSource(style, "classpath:///ast.xsl"));
t.transform(new StreamSource(ri), new StreamResult(o));
}
return o.toByteArray();
}

private byte[] read(final String input) throws Exception {
return Files.readAllBytes(Paths.get(getClass().getResource("/" + input).toURI()));
}
Expand Down
47 changes: 15 additions & 32 deletions src/test/java/com/elovirta/dita/markdown/DitaToAstTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,11 @@

public class DitaToAstTest {

@Test
public void testAst() throws Exception {
final String name = "ast";
final Document act = run("dita/" + name + ".dita");
final Document exp = read("ast/dita2" + name + ".xml");
try {
final Diff diff = DiffBuilder
.compare(clean(act))
.withTest(clean(exp))
.normalizeWhitespace()
.ignoreWhitespace()
.ignoreComments()
.checkForIdentical()
.build();
assertFalse(diff.hasDifferences());
} catch (AssertionFailedError e) {
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(exp), new StreamResult(System.out));
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(act), new StreamResult(System.out));
throw e;
}
private final TransformerFactory transformerFactory;

public DitaToAstTest() {
transformerFactory = TransformerFactory.newInstance();
transformerFactory.setURIResolver(new ClasspathURIResolver(transformerFactory.getURIResolver()));
}

@ParameterizedTest
Expand Down Expand Up @@ -107,7 +92,7 @@ public void testAst() throws Exception {
)
public void testAst(String name) throws Exception {
final Document act = run("dita/" + name + ".dita");
final Document exp = read("ast/dita2" + name + ".xml");
final Document exp = read("output/ast/" + name + ".xml");
try {
final Diff diff = DiffBuilder
.compare(clean(act))
Expand All @@ -119,8 +104,8 @@ public void testAst(String name) throws Exception {
.build();
assertFalse(diff.hasDifferences());
} catch (AssertionFailedError e) {
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(exp), new StreamResult(System.out));
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(act), new StreamResult(System.out));
transformerFactory.newTransformer().transform(new DOMSource(exp), new StreamResult(System.out));
transformerFactory.newTransformer().transform(new DOMSource(act), new StreamResult(System.out));
throw e;
}
}
Expand All @@ -131,9 +116,7 @@ private Document run(final String input) throws Exception {
InputStream style = getClass().getResourceAsStream("/dita2ast.xsl");
InputStream ri = getClass().getResourceAsStream("/" + input)
) {
final TransformerFactory tf = TransformerFactory.newInstance();
tf.setURIResolver(new ClasspathURIResolver(tf.getURIResolver()));
final Transformer t = tf.newTransformer(new StreamSource(style, "classpath:///dita2ast.xsl"));
final Transformer t = transformerFactory.newTransformer(new StreamSource(style, "classpath:///dita2ast.xsl"));
t.transform(new StreamSource(ri), new DOMResult(output));
}
return output;
Expand All @@ -146,12 +129,12 @@ private Document clean(Document doc) {
doc.removeChild(node);
}
}
final NodeList elems = doc.getElementsByTagName("*");
for (int i = 0; i < elems.getLength(); i++) {
final Element elem = (Element) elems.item(i);
// elem.removeAttribute("domains");
// elem.removeAttributeNS("http://dita.oasis-open.org/architecture/2005/", "DITAArchVersion");
}
// final NodeList elems = doc.getElementsByTagName("*");
// for (int i = 0; i < elems.getLength(); i++) {
// final Element elem = (Element) elems.item(i);
// elem.removeAttribute("domains");
// elem.removeAttributeNS("http://dita.oasis-open.org/architecture/2005/", "DITAArchVersion");
// }
doc.normalizeDocument();
return doc;
}
Expand Down
Loading

0 comments on commit 8be8b02

Please sign in to comment.