Made syntax highlighting test more specific

This commit is contained in:
unknown
2018-09-28 13:39:53 -06:00
parent 035d825757
commit 6ab08dfbf7
2 changed files with 105 additions and 20 deletions

View File

@@ -10,6 +10,7 @@ import org.mockito.*;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
@@ -72,7 +73,7 @@ public class MarkdownFromURLUnitTest {
assertTrue(output3.contains(input3)); assertTrue(output3.contains(input3));
} }
@Test @Test
public void testSyntaxHighlighting() throws MacroExecutionException { public void testSyntaxHighlighting() throws MacroExecutionException, FailingHttpStatusCodeException, MalformedURLException, IOException {
/*Test that the javascript for syntax highlighting works*/ /*Test that the javascript for syntax highlighting works*/
// Run the macro with an input of a line of code // Run the macro with an input of a line of code
// Create a temporary HTML file containing the output // Create a temporary HTML file containing the output
@@ -106,18 +107,59 @@ public class MarkdownFromURLUnitTest {
FileWriter writer = new FileWriter(tmpHTMLFile); FileWriter writer = new FileWriter(tmpHTMLFile);
writer.write(toWrite); writer.write(toWrite);
writer.close(); writer.close();
final HtmlPage page = webClient.getPage(tmpHTMLFile.toURI().toURL().toString()); final HtmlPage page = webClient.getPage(tmpHTMLFile.toURI().toURL().toString());
HtmlElement document = page.getDocumentElement(); HtmlElement document = page.getDocumentElement();
assertTrue(document.getElementsByAttribute("span", "class", "hljs-class").size() > 0); List<HtmlElement> spans = document.getElementsByTagName("span");
assertTrue(document.getElementsByAttribute("span", "class", "hljs-keyword").size() > 0);
assertTrue(document.getElementsByAttribute("span", "class", "hljs-title").size() > 0);
tmpHTMLFile.delete(); tmpHTMLFile.delete();
} catch (FailingHttpStatusCodeException e) {
e.printStackTrace(); HtmlElement hljsClassSpan = null;
} catch (MalformedURLException e) { int numberOfHljsClassSpans = 0;
e.printStackTrace(); HtmlElement hljsKeywordSpan = null;
} catch (IOException e) { int numberOfHljsKeywordSpans = 0;
e.printStackTrace(); HtmlElement hljsTitleSpan = null;
int numberOfHljsTitleSpans = 0;
//Check the class of every span element and increment the counters accordingly
//Also define the three span objects as the first span of the correct class
for (HtmlElement span : spans) {
if (span.getAttribute("class").contains("hljs-class")) {
numberOfHljsClassSpans++;
if (numberOfHljsClassSpans == 1) {
hljsClassSpan = span;
}
}
if (span.getAttribute("class").contains("hljs-keyword")) {
numberOfHljsKeywordSpans++;
if (numberOfHljsKeywordSpans == 1) {
hljsKeywordSpan = span;
}
}
if (span.getAttribute("class").contains("hljs-title")) {
numberOfHljsTitleSpans++;
if (numberOfHljsTitleSpans == 1) {
hljsTitleSpan = span;
}
}
}
// Test that there is exactly one span with a css class of hljs-class
// and that it's parent is a code element with a css class of hljs
assertThat(numberOfHljsClassSpans, is(1));
assertTrue(hljsClassSpan.getParentNode().getNodeName().equals("code"));
assertTrue(hljsClassSpan.getParentNode().getAttributes().getNamedItem("class").getNodeValue().contains("hljs"));
// Test that there is exactly one span with a css class of hljs-keyword
// and that it's parent is a span element with a css class of hljs-class
assertThat(numberOfHljsKeywordSpans, is(1));
assertTrue(hljsKeywordSpan.getParentNode().getNodeName().equals("span"));
assertTrue(hljsKeywordSpan.getParentNode().getAttributes().getNamedItem("class").getNodeValue().contains("hljs-class"));
// Test that there is exactly one span with a css class of hljs-title
// and that it's parent is a span element with a css class of hljs-class
assertThat(numberOfHljsTitleSpans, is(1));
assertTrue(hljsTitleSpan.getParentNode().getNodeName().equals("span"));
assertTrue(hljsTitleSpan.getParentNode().getAttributes().getNamedItem("class").getNodeValue().contains("hljs-class"));
} }
} }
@Before @Before

View File

@@ -4,6 +4,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import com.gargoylesoftware.htmlunit.*; import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*; import com.gargoylesoftware.htmlunit.html.*;
@@ -14,6 +15,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import java.io.*; import java.io.*;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import com.atlassian.confluence.content.render.xhtml.ConversionContext; import com.atlassian.confluence.content.render.xhtml.ConversionContext;
import com.atlassian.confluence.macro.MacroExecutionException; import com.atlassian.confluence.macro.MacroExecutionException;
@@ -43,7 +45,7 @@ public class MarkdownUnitTest {
assertTrue(output.contains("<em>Italic</em>")); assertTrue(output.contains("<em>Italic</em>"));
} }
@Test @Test
public void testSyntaxHighlighting() throws MacroExecutionException { public void testSyntaxHighlighting() throws MacroExecutionException, IOException {
/*Test that the javascript for syntax highlighting works*/ /*Test that the javascript for syntax highlighting works*/
// Run the macro with an input of a line of code // Run the macro with an input of a line of code
// Create a temporary HTML file containing the output // Create a temporary HTML file containing the output
@@ -76,18 +78,59 @@ public class MarkdownUnitTest {
FileWriter writer = new FileWriter(tmpHTMLFile); FileWriter writer = new FileWriter(tmpHTMLFile);
writer.write(toWrite); writer.write(toWrite);
writer.close(); writer.close();
final HtmlPage page = webClient.getPage(tmpHTMLFile.toURI().toURL().toString()); final HtmlPage page = webClient.getPage(tmpHTMLFile.toURI().toURL().toString());
HtmlElement document = page.getDocumentElement(); HtmlElement document = page.getDocumentElement();
assertTrue(document.getElementsByAttribute("span", "class", "hljs-class").size() > 0); List<HtmlElement> spans = document.getElementsByTagName("span");
assertTrue(document.getElementsByAttribute("span", "class", "hljs-keyword").size() > 0);
assertTrue(document.getElementsByAttribute("span", "class", "hljs-title").size() > 0);
tmpHTMLFile.delete(); tmpHTMLFile.delete();
} catch (FailingHttpStatusCodeException e) {
e.printStackTrace(); HtmlElement hljsClassSpan = null;
} catch (MalformedURLException e) { int numberOfHljsClassSpans = 0;
e.printStackTrace(); HtmlElement hljsKeywordSpan = null;
} catch (IOException e) { int numberOfHljsKeywordSpans = 0;
e.printStackTrace(); HtmlElement hljsTitleSpan = null;
int numberOfHljsTitleSpans = 0;
//Check the class of every span element and increment the counters accordingly
//Also define the three span objects as the first span of the correct class
for (HtmlElement span : spans) {
if (span.getAttribute("class").contains("hljs-class")) {
numberOfHljsClassSpans++;
if (numberOfHljsClassSpans == 1) {
hljsClassSpan = span;
}
}
if (span.getAttribute("class").contains("hljs-keyword")) {
numberOfHljsKeywordSpans++;
if (numberOfHljsKeywordSpans == 1) {
hljsKeywordSpan = span;
}
}
if (span.getAttribute("class").contains("hljs-title")) {
numberOfHljsTitleSpans++;
if (numberOfHljsTitleSpans == 1) {
hljsTitleSpan = span;
}
}
}
// Test that there is exactly one span with a css class of hljs-class
// and that it's parent is a code element with a css class of hljs
assertThat(numberOfHljsClassSpans, is(1));
assertTrue(hljsClassSpan.getParentNode().getNodeName().equals("code"));
assertTrue(hljsClassSpan.getParentNode().getAttributes().getNamedItem("class").getNodeValue().contains("hljs"));
// Test that there is exactly one span with a css class of hljs-keyword
// and that it's parent is a span element with a css class of hljs-class
assertThat(numberOfHljsKeywordSpans, is(1));
assertTrue(hljsKeywordSpan.getParentNode().getNodeName().equals("span"));
assertTrue(hljsKeywordSpan.getParentNode().getAttributes().getNamedItem("class").getNodeValue().contains("hljs-class"));
// Test that there is exactly one span with a css class of hljs-title
// and that it's parent is a span element with a css class of hljs-class
assertThat(numberOfHljsTitleSpans, is(1));
assertTrue(hljsTitleSpan.getParentNode().getNodeName().equals("span"));
assertTrue(hljsTitleSpan.getParentNode().getAttributes().getNamedItem("class").getNodeValue().contains("hljs-class"));
} }
} }