Skip to content

Commit

Permalink
Merge pull request #302 from commonmark/spec-test-case-separate
Browse files Browse the repository at this point in the history
Make SpecTestCase not extend RenderingTestCase
  • Loading branch information
robinst authored Jan 10, 2024
2 parents 320266c + 1ba567c commit 8cf87a3
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.commonmark.testutil.example.Example;

/**
* Spec and all extensions, with source spans enabed.
* Spec and all extensions, with source spans enabled.
*/
public class SourceSpanIntegrationTest extends SpecIntegrationTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
import org.commonmark.parser.Parser;
import org.commonmark.testutil.example.Example;
import org.commonmark.testutil.SpecTestCase;
import org.junit.Assert;
import org.junit.Test;

import java.util.*;

import static org.commonmark.testutil.Asserts.assertRendering;

/**
* Tests that the spec examples still render the same with all extensions enabled.
*/
Expand All @@ -39,17 +42,15 @@ public SpecIntegrationTest(Example example) {
}

@Test
@Override
public void testHtmlRendering() {
String expectedHtml = OVERRIDDEN_EXAMPLES.get(example.getSource());
if (expectedHtml != null) {
assertRendering(example.getSource(), expectedHtml);
assertRendering(example.getSource(), expectedHtml, render(example.getSource()));
} else {
super.testHtmlRendering();
assertRendering(example.getSource(), example.getHtml(), render(example.getSource()));
}
}

@Override
protected String render(String source) {
return RENDERER.render(PARSER.parse(source));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.commonmark.testutil;

import static org.junit.Assert.assertEquals;

public class Asserts {
public static void assertRendering(String source, String expectedRendering, String actualRendering) {
// include source for better assertion errors
String expected = showTabs(expectedRendering + "\n\n" + source);
String actual = showTabs(actualRendering + "\n\n" + source);
assertEquals(expected, actual);
}

private static String showTabs(String s) {
// Tabs are shown as "rightwards arrow" for easier comparison
return s.replace("\t", "\u2192");
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
package org.commonmark.testutil;

import static org.junit.Assert.assertEquals;

public abstract class RenderingTestCase {

protected abstract String render(String source);

protected void assertRendering(String source, String expectedResult) {
String renderedContent = render(source);

// include source for better assertion errors
String expected = showTabs(expectedResult + "\n\n" + source);
String actual = showTabs(renderedContent + "\n\n" + source);
assertEquals(expected, actual);
}

private static String showTabs(String s) {
// Tabs are shown as "rightwards arrow" for easier comparison
return s.replace("\t", "\u2192");
Asserts.assertRendering(source, expectedResult, render(source));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
import java.util.ArrayList;
import java.util.List;

import static org.commonmark.testutil.Asserts.assertRendering;

@RunWith(Parameterized.class)
public abstract class SpecTestCase extends RenderingTestCase {
public abstract class SpecTestCase {

protected final Example example;

Expand All @@ -29,9 +31,4 @@ public static List<Object[]> data() {
return data;
}

@Test
public void testHtmlRendering() {
assertRendering(example.getSource(), example.getHtml());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.commonmark.testutil.example.Example;
import org.junit.Test;

import static org.commonmark.testutil.Asserts.assertRendering;
import static org.junit.Assert.fail;

public class SpecCoreTest extends SpecTestCase {
Expand Down Expand Up @@ -49,8 +50,12 @@ protected void visitChildren(Node parent) {
});
}

@Override
protected String render(String source) {
@Test
public void testHtmlRendering() {
assertRendering(example.getSource(), example.getHtml(), render(example.getSource()));
}

private String render(String source) {
return RENDERER.render(PARSER.parse(source));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.commonmark.renderer.html.HtmlRenderer;
import org.commonmark.testutil.SpecTestCase;
import org.commonmark.testutil.example.Example;
import org.junit.Test;

import static org.commonmark.testutil.Asserts.assertRendering;

/**
* Same as {@link SpecCoreTest} but converts line endings to Windows-style CR+LF endings before parsing.
Expand All @@ -18,8 +21,12 @@ public SpecCrLfCoreTest(Example example) {
super(example);
}

@Override
protected String render(String source) {
@Test
public void testHtmlRendering() {
assertRendering(example.getSource(), example.getHtml(), render(example.getSource()));
}

private String render(String source) {
String windowsStyle = source.replace("\n", "\r\n");
return RENDERER.render(PARSER.parse(windowsStyle));
}
Expand Down

0 comments on commit 8cf87a3

Please sign in to comment.