Added unit test for syntax highlighting

This commit is contained in:
unknown
2018-09-13 09:51:56 -06:00
parent 9f7f1d63be
commit 7ea26df373
3 changed files with 61 additions and 15 deletions

View File

@@ -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>org.mozilla</groupId> <groupId>net.sourceforge.htmlunit</groupId>
<artifactId>rhino</artifactId> <artifactId>htmlunit</artifactId>
<version>1.7.10</version> <version>2.32</version>
<scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -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
// Assert that the page contains three spans with the correct classes
try (final WebClient webClient = new WebClient()) {
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
String output = markdownMacro.execute(new HashMap(), "`public class JavaClass {}`", conversionContext); String output = markdownMacro.execute(new HashMap(), "`class className() {}`", conversionContext);
System.out.println(output); String toWrite = "<!DOCTYPE html>\r\n" +
assertTrue(Pattern.matches("[\\S\\s]*<code.*>public class JavaClass \\{\\}<\\/code>[\\S\\s]*", output)); "<html>\r\n" +
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)); "<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

File diff suppressed because one or more lines are too long