Skip to content

Commit

Permalink
Update overrides in SpecIntegrationTest
Browse files Browse the repository at this point in the history
  • Loading branch information
robinst committed Mar 2, 2024
1 parent 203eeb2 commit a8af670
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.commonmark.integration;

import org.commonmark.Extension;
import org.commonmark.ext.autolink.AutolinkExtension;
import org.commonmark.ext.front.matter.YamlFrontMatterExtension;
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.image.attributes.ImageAttributesExtension;
import org.commonmark.ext.ins.InsExtension;
import org.commonmark.ext.task.list.items.TaskListItemsExtension;

import java.util.Arrays;
import java.util.List;

public class Extensions {

static final List<Extension> ALL_EXTENSIONS = Arrays.asList(
AutolinkExtension.create(),
ImageAttributesExtension.create(),
InsExtension.create(),
StrikethroughExtension.create(),
TablesExtension.create(),
TaskListItemsExtension.create(),
YamlFrontMatterExtension.create());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.commonmark.integration;

import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.commonmark.testutil.RenderingTestCase;
import org.junit.Test;

/**
* Tests to ensure all extensions work well together.
*/
public class ExtensionsIntegrationTest extends RenderingTestCase {

protected static final Parser PARSER = Parser.builder()
.extensions(Extensions.ALL_EXTENSIONS)
.build();
protected static final HtmlRenderer RENDERER = HtmlRenderer.builder()
.extensions(Extensions.ALL_EXTENSIONS)
.percentEncodeUrls(true)
.build();

@Test
public void testImageAttributes() {
assertRendering("![text](/url.png){height=5 width=6}", "<p><img src=\"/url.png\" alt=\"text\" height=\"5\" width=\"6\" /></p>\n");
}

@Test
public void testTaskListItems() {
assertRendering("- [ ] task to do\n- [x] task done\n",
"<ul>\n<li><input type=\"checkbox\" disabled=\"\"> task to do</li>\n" +
"<li><input type=\"checkbox\" disabled=\"\" checked=\"\"> task done</li>\n</ul>\n");

}

protected String render(String source) {
return RENDERER.render(PARSER.parse(source));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class SourceSpanIntegrationTest extends SpecIntegrationTest {

protected static final Parser PARSER = Parser.builder()
.extensions(EXTENSIONS)
.extensions(Extensions.ALL_EXTENSIONS)
.includeSourceSpans(IncludeSourceSpans.BLOCKS)
.build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package org.commonmark.integration;

import org.commonmark.Extension;
import org.commonmark.ext.autolink.AutolinkExtension;
import org.commonmark.ext.image.attributes.ImageAttributesExtension;
import org.commonmark.ext.ins.InsExtension;
import org.commonmark.ext.gfm.strikethrough.StrikethroughExtension;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.front.matter.YamlFrontMatterExtension;
import org.commonmark.ext.task.list.items.TaskListItemsExtension;
import org.commonmark.renderer.html.HtmlRenderer;
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.*;
Expand All @@ -24,17 +15,9 @@
*/
public class SpecIntegrationTest extends SpecTestCase {

protected static final List<Extension> EXTENSIONS = Arrays.asList(
AutolinkExtension.create(),
ImageAttributesExtension.create(),
InsExtension.create(),
StrikethroughExtension.create(),
TablesExtension.create(),
TaskListItemsExtension.create(),
YamlFrontMatterExtension.create());
protected static final Parser PARSER = Parser.builder().extensions(EXTENSIONS).build();
protected static final Parser PARSER = Parser.builder().extensions(Extensions.ALL_EXTENSIONS).build();
// The spec says URL-escaping is optional, but the examples assume that it's enabled.
protected static final HtmlRenderer RENDERER = HtmlRenderer.builder().extensions(EXTENSIONS).percentEncodeUrls(true).build();
protected static final HtmlRenderer RENDERER = HtmlRenderer.builder().extensions(Extensions.ALL_EXTENSIONS).percentEncodeUrls(true).build();
protected static final Map<String, String> OVERRIDDEN_EXAMPLES = getOverriddenExamples();

public SpecIntegrationTest(Example example) {
Expand All @@ -59,7 +42,7 @@ private static Map<String, String> getOverriddenExamples() {
Map<String, String> m = new HashMap<>();

// Not a spec autolink because of space, but the resulting text contains a valid URL
m.put("<http://foo.bar/baz bim>\n", "<p>&lt;<a href=\"http://foo.bar/baz\">http://foo.bar/baz</a> bim&gt;</p>\n");
m.put("<https://foo.bar/baz bim>\n", "<p>&lt;<a href=\"https://foo.bar/baz\">https://foo.bar/baz</a> bim&gt;</p>\n");

// Not a spec autolink, but the resulting text contains a valid email
m.put("<foo\\[email protected]>\n", "<p>&lt;<a href=\"mailto:[email protected]\">[email protected]</a>&gt;</p>\n");
Expand All @@ -68,10 +51,10 @@ private static Map<String, String> getOverriddenExamples() {
m.put("<heck://bing.bong>\n", "<p>&lt;<a href=\"heck://bing.bong%3E\">heck://bing.bong&gt;</a></p>\n");

// Not a spec autolink because of spaces, but autolink extension doesn't limit schemes
m.put("< http://foo.bar >\n", "<p>&lt; <a href=\"http://foo.bar\">http://foo.bar</a> &gt;</p>\n");
m.put("< https://foo.bar >\n", "<p>&lt; <a href=\"https://foo.bar\">https://foo.bar</a> &gt;</p>\n");

// Plain autolink
m.put("http://example.com\n", "<p><a href=\"http://example.com\">http://example.com</a></p>\n");
m.put("https://example.com\n", "<p><a href=\"https://example.com\">https://example.com</a></p>\n");

// Plain autolink
m.put("[email protected]\n", "<p><a href=\"mailto:[email protected]\">[email protected]</a></p>\n");
Expand All @@ -80,13 +63,6 @@ private static Map<String, String> getOverriddenExamples() {
m.put("---\nFoo\n---\nBar\n---\nBaz\n", "<h2>Bar</h2>\n<p>Baz</p>\n");
m.put("---\n---\n", "");

// Image attributes
m.put("![text](/url.png){height=5 width=6}", "<img src=\"/url.png\" alt=\"text\" height=\"5\" width=\"6\" />");

// Task list items
m.put("- [ ] task to do\n- [x] task done\n", "<ul>\n<li><input type=\"checkbox\" disabled=\"\"> task to do</li>\n" +
"<li><input type=\"checkbox\" disabled=\"\" checked=\"\"> task done</li>\n</ul>\n");

return m;
}

Expand Down

0 comments on commit a8af670

Please sign in to comment.