Added unit test for syntax highlighting
This commit is contained in:
10
pom.xml
10
pom.xml
@@ -91,11 +91,11 @@
|
|||||||
<version>1.9.0</version>
|
<version>1.9.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/org.mozilla/rhino -->
|
<dependency>
|
||||||
<dependency>
|
<groupId>net.sourceforge.htmlunit</groupId>
|
||||||
<groupId>org.mozilla</groupId>
|
<artifactId>htmlunit</artifactId>
|
||||||
<artifactId>rhino</artifactId>
|
<version>2.32</version>
|
||||||
<version>1.7.10</version>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -4,11 +4,18 @@ import org.junit.Before;
|
|||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
|
|
||||||
|
import net.sourceforge.htmlunit.*;
|
||||||
|
import com.gargoylesoftware.htmlunit.*;
|
||||||
|
import com.gargoylesoftware.htmlunit.html.*;
|
||||||
|
|
||||||
import org.mockito.*;
|
import org.mockito.*;
|
||||||
import org.mockito.runners.MockitoJUnitRunner;
|
import org.mockito.runners.MockitoJUnitRunner;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@@ -17,7 +24,6 @@ import com.atlassian.confluence.macro.MacroExecutionException;
|
|||||||
import com.atlassian.webresource.api.assembler.PageBuilderService;
|
import com.atlassian.webresource.api.assembler.PageBuilderService;
|
||||||
import com.atlassian.webresource.api.assembler.RequiredResources;
|
import com.atlassian.webresource.api.assembler.RequiredResources;
|
||||||
import com.atlassian.webresource.api.assembler.WebResourceAssembler;
|
import com.atlassian.webresource.api.assembler.WebResourceAssembler;
|
||||||
|
|
||||||
import com.atlassian.plugins.confluence.markdown.MarkdownMacro;
|
import com.atlassian.plugins.confluence.markdown.MarkdownMacro;
|
||||||
|
|
||||||
@RunWith (MockitoJUnitRunner.class)
|
@RunWith (MockitoJUnitRunner.class)
|
||||||
@@ -45,14 +51,52 @@ public class MarkdownUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testSyntaxHighlighting() throws MacroExecutionException {
|
public void testSyntaxHighlighting() throws MacroExecutionException {
|
||||||
/*Test that the correct JavaScript is returned for highlight.js to work*/
|
/*Test that the correct JavaScript is returned for highlight.js to work*/
|
||||||
// Run the macro using input of a line of code in a code block,
|
// Run the macro with an input of a line of code
|
||||||
// then assert that a <code> block is returned.
|
// Create a temporary HTML file containing the output
|
||||||
// Intended only as a temporary test until I can program a better one
|
// Parse the HTML file with htmlunit
|
||||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
// Assert that the page contains three spans with the correct classes
|
||||||
String output = markdownMacro.execute(new HashMap(), "`public class JavaClass {}`", conversionContext);
|
try (final WebClient webClient = new WebClient()) {
|
||||||
System.out.println(output);
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||||
assertTrue(Pattern.matches("[\\S\\s]*<code.*>public class JavaClass \\{\\}<\\/code>[\\S\\s]*", output));
|
String output = markdownMacro.execute(new HashMap(), "`class className() {}`", conversionContext);
|
||||||
assertTrue(Pattern.matches("[\\S\\s]*<script>\\sAJS\\.\\$\\('\\[data\\-macro\\-name=\"markdown\"\\] code'\\)\\.each\\(function\\(i, block\\) \\{\\s hljs\\.highlightBlock\\(block\\);\\s \\}\\);\\s<\\/script>[\\S\\s]*", output));
|
String toWrite = "<!DOCTYPE html>\r\n" +
|
||||||
|
"<html>\r\n" +
|
||||||
|
"<head>\r\n" +
|
||||||
|
" <title>Syntax Highlighting Page</title>\r\n" +
|
||||||
|
" <script src=\"./jquery-3.3.1.min.js\"></script>\r\n" +
|
||||||
|
" <script src=\"../../main/resources/js/highlight.min.js\"></script>\r\n" +
|
||||||
|
" <link href=\"../../main/resources/css/highlight.min.css\" rel=\"stylesheet\" />\r\n" +
|
||||||
|
"</head>\r\n" +
|
||||||
|
"<body>\r\n" +
|
||||||
|
" <script>\r\n" +
|
||||||
|
" var AJS = {\r\n" +
|
||||||
|
" $: $\r\n" +
|
||||||
|
" };\r\n" +
|
||||||
|
" </script>\r\n" +
|
||||||
|
"<div data-macro-name='markdown'>" +
|
||||||
|
output +
|
||||||
|
"</div>" +
|
||||||
|
"</body>\r\n" +
|
||||||
|
"</html>";
|
||||||
|
File tmpHTMLFile = File.createTempFile("syntax-highlighting-", ".html", new File("src/test/resources"));
|
||||||
|
FileWriter writer = new FileWriter(tmpHTMLFile);
|
||||||
|
writer.write(toWrite);
|
||||||
|
writer.close();
|
||||||
|
final HtmlPage page = webClient.getPage(tmpHTMLFile.toURI().toURL().toString());
|
||||||
|
HtmlElement document = page.getDocumentElement();
|
||||||
|
assertTrue(document.getElementsByAttribute("span", "class", "hljs-class").size() > 0);
|
||||||
|
assertTrue(document.getElementsByAttribute("span", "class", "hljs-keyword").size() > 0);
|
||||||
|
assertTrue(document.getElementsByAttribute("span", "class", "hljs-title").size() > 0);
|
||||||
|
tmpHTMLFile.delete();
|
||||||
|
} catch (FailingHttpStatusCodeException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
// System.out.println(output);
|
||||||
|
// assertTrue(Pattern.matches("[\\S\\s]*<code.*>public class JavaClass \\{\\}<\\/code>[\\S\\s]*", output));
|
||||||
|
// assertTrue(Pattern.matches("[\\S\\s]*<script>\\sAJS\\.\\$\\('\\[data\\-macro\\-name=\"markdown\"\\] code'\\)\\.each\\(function\\(i, block\\) \\{\\s hljs\\.highlightBlock\\(block\\);\\s \\}\\);\\s<\\/script>[\\S\\s]*", output));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
|
|||||||
2
src/test/resources/jquery-3.3.1.min.js
vendored
Normal file
2
src/test/resources/jquery-3.3.1.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user