aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core/src/test
diff options
context:
space:
mode:
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>2013-04-08 16:26:30 +0200
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>2013-04-08 16:26:30 +0200
commitdc9d62ed4dd34b021499c96fb758f6907ecad884 (patch)
tree9b5f7c347544d08f26e2c4c8dc218cca8a1ed476 /sonar-core/src/test
parent771c65b034641ad70caa4dd17b11837d7d9a4a42 (diff)
downloadsonarqube-dc9d62ed4dd34b021499c96fb758f6907ecad884.tar.gz
sonarqube-dc9d62ed4dd34b021499c96fb758f6907ecad884.zip
(SONAR-3893) Improve the highlighter API to not depend on sonar-channel and allow to work on multi-line tokens - Added CRLF support in HTML text decoration
Diffstat (limited to 'sonar-core/src/test')
-rw-r--r--sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java80
1 files changed, 73 insertions, 7 deletions
diff --git a/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java b/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java
index 51c1bffe9c4..6424583f9ba 100644
--- a/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java
@@ -25,11 +25,11 @@ import org.sonar.api.scan.source.HighlightableTextType;
import org.sonar.api.scan.source.SyntaxHighlightingRuleSet;
import static org.fest.assertions.Assertions.assertThat;
+import static org.sonar.core.source.HtmlTextWrapper.CR_END_OF_LINE;
+import static org.sonar.core.source.HtmlTextWrapper.LF_END_OF_LINE;
public class HtmlTextWrapperTest {
- private static final String NEW_LINE = "\n";
-
@Test
public void should_decorate_simple_character_range() throws Exception {
@@ -51,18 +51,20 @@ public class HtmlTextWrapperTest {
String secondCommentLine = " * Test";
String thirdCommentLine = " */";
- String blockComment = firstCommentLine + NEW_LINE + secondCommentLine + NEW_LINE + thirdCommentLine + NEW_LINE;
+ String blockComment = firstCommentLine + LF_END_OF_LINE
+ + secondCommentLine + LF_END_OF_LINE
+ + thirdCommentLine + LF_END_OF_LINE;
- SyntaxHighlightingRuleSet syntaxHighlighting = new SyntaxHighlightingRuleSet.Builder()
+ SyntaxHighlightingRuleSet syntaxHighlighting = SyntaxHighlightingRuleSet.builder()
.registerHighlightingRule(0, 14, HighlightableTextType.BLOCK_COMMENT).build();
HtmlTextWrapper htmlTextWrapper = new HtmlTextWrapper();
String htmlOutput = htmlTextWrapper.wrapTextWithHtml(blockComment, syntaxHighlighting);
assertThat(htmlOutput).isEqualTo(
- "<tr><td><span class=\"cppd\">" + firstCommentLine + "</span></td></tr>" + NEW_LINE +
- "<tr><td><span class=\"cppd\">" + secondCommentLine + "</span></td></tr>" + NEW_LINE +
- "<tr><td><span class=\"cppd\">" + thirdCommentLine + "</span></td></tr>" + NEW_LINE
+ "<tr><td><span class=\"cppd\">" + firstCommentLine + "</span></td></tr>" + LF_END_OF_LINE +
+ "<tr><td><span class=\"cppd\">" + secondCommentLine + "</span></td></tr>" + LF_END_OF_LINE +
+ "<tr><td><span class=\"cppd\">" + thirdCommentLine + "</span></td></tr>" + LF_END_OF_LINE
);
}
@@ -83,4 +85,68 @@ public class HtmlTextWrapperTest {
assertThat(htmlOutput).isEqualTo(
"<tr><td><span class=\"k\">public</span> <span class=\"k\">class</span> MyClass <span class=\"k\">implements</span> MyInterface {</td></tr>\n");
}
+
+ @Test
+ public void should_allow_multiple_levels_highlighting() throws Exception {
+
+ String javaDocSample =
+ "/**" + LF_END_OF_LINE +
+ " * Creates a FormulaDecorator" + LF_END_OF_LINE +
+ " *" + LF_END_OF_LINE +
+ " * @param metric the metric should have an associated formula" + LF_END_OF_LINE +
+ " * " + LF_END_OF_LINE +
+ " * @throws IllegalArgumentException if no formula is associated to the metric" + LF_END_OF_LINE +
+ " */" + LF_END_OF_LINE;
+
+ SyntaxHighlightingRuleSet syntaxHighlighting = SyntaxHighlightingRuleSet.builder()
+ .registerHighlightingRule(0, 184, HighlightableTextType.BLOCK_COMMENT)
+ .registerHighlightingRule(47, 53, HighlightableTextType.KEYWORD)
+ .build();
+
+ HtmlTextWrapper htmlTextWrapper = new HtmlTextWrapper();
+ String htmlOutput = htmlTextWrapper.wrapTextWithHtml(javaDocSample, syntaxHighlighting);
+
+ assertThat(htmlOutput).isEqualTo(
+ "<tr><td><span class=\"cppd\">/**</span></td></tr>" + LF_END_OF_LINE +
+ "<tr><td><span class=\"cppd\"> * Creates a FormulaDecorator</span></td></tr>" + LF_END_OF_LINE +
+ "<tr><td><span class=\"cppd\"> *</span></td></tr>" + LF_END_OF_LINE +
+ "<tr><td><span class=\"cppd\"> * @param <span class=\"k\">metric</span> the metric should have an associated formula</span></td></tr>" + LF_END_OF_LINE +
+ "<tr><td><span class=\"cppd\"> * </span></td></tr>" + LF_END_OF_LINE +
+ "<tr><td><span class=\"cppd\"> * @throws IllegalArgumentException if no formula is associated to the metric</span></td></tr>" + LF_END_OF_LINE +
+ "<tr><td><span class=\"cppd\"> */</span></td></tr>" + LF_END_OF_LINE
+ );
+ }
+
+ @Test
+ public void should_support_crlf_line_breaks() throws Exception {
+
+ String crlfCodeSample =
+ "/**" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "* @return metric generated by the decorator" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "*/" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "@DependedUpon" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "public Metric generatesMetric() {" + CR_END_OF_LINE + LF_END_OF_LINE +
+ " return metric;" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "}" + CR_END_OF_LINE + LF_END_OF_LINE;
+
+ SyntaxHighlightingRuleSet syntaxHighlighting = SyntaxHighlightingRuleSet.builder()
+ .registerHighlightingRule(0, 52, HighlightableTextType.BLOCK_COMMENT)
+ .registerHighlightingRule(54, 67, HighlightableTextType.ANNOTATION)
+ .registerHighlightingRule(69, 75, HighlightableTextType.KEYWORD)
+ .registerHighlightingRule(106, 112, HighlightableTextType.KEYWORD)
+ .build();
+
+ HtmlTextWrapper htmlTextWrapper = new HtmlTextWrapper();
+ String htmlOutput = htmlTextWrapper.wrapTextWithHtml(crlfCodeSample, syntaxHighlighting);
+
+ assertThat(htmlOutput).isEqualTo(
+ "<tr><td><span class=\"cppd\">/**</span></td></tr>" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "<tr><td><span class=\"cppd\">* @return metric generated by the decorator</span></td></tr>" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "<tr><td><span class=\"cppd\">*/</span></td></tr>" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "<tr><td><span class=\"a\">@DependedUpon</span></td></tr>" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "<tr><td><span class=\"k\">public</span> Metric generatesMetric() {</td></tr>" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "<tr><td> <span class=\"k\">return</span> metric;</td></tr>" + CR_END_OF_LINE + LF_END_OF_LINE +
+ "<tr><td>}</td></tr>" + CR_END_OF_LINE + LF_END_OF_LINE
+ );
+ }
}