use of org.sonar.plugins.web.node.Node in project sonar-web by SonarSource.
the class PageLexerTest method testNestedScriptlet.
@Test
public void testNestedScriptlet() {
String fragment = "<option value=\"<%= key -%>\" <%= 'selected' if alert.operator==key -%>>";
StringReader reader = new StringReader(fragment);
PageLexer lexer = new PageLexer();
List<Node> nodeList = lexer.parse(reader);
assertEquals(1, nodeList.size());
TagNode tagNode = (TagNode) nodeList.get(0);
assertEquals(2, tagNode.getAttributes().size());
// the embedded tags are added as attributes
assertEquals(tagNode.getAttributes().get(0).getName(), "value");
assertEquals(tagNode.getAttributes().get(0).getValue(), "<%= key -%>");
assertEquals(tagNode.getAttributes().get(1).getName(), "<%= 'selected' if alert.operator==key -%>");
assertThat(tagNode.getAttributes().get(1).getValue()).isEmpty();
}
use of org.sonar.plugins.web.node.Node in project sonar-web by SonarSource.
the class PageLexerTest method testAttributeWithoutQuotes.
@Test
public void testAttributeWithoutQuotes() {
final StringReader reader = new StringReader("<img src=http://foo/sfds?sjg a=1\tb=2\r\nc=3 />");
final PageLexer lexer = new PageLexer();
final List<Node> nodeList = lexer.parse(reader);
assertEquals(1, nodeList.size());
assertTrue(nodeList.get(0) instanceof TagNode);
final TagNode node = (TagNode) nodeList.get(0);
assertEquals(4, node.getAttributes().size());
final Attribute attribute = node.getAttributes().get(0);
assertEquals("src", attribute.getName());
assertEquals("http://foo/sfds?sjg", attribute.getValue());
final Attribute attributeA = node.getAttributes().get(1);
assertEquals("a", attributeA.getName());
assertEquals("1", attributeA.getValue());
final Attribute attributeB = node.getAttributes().get(2);
assertEquals("b", attributeB.getName());
assertEquals("2", attributeB.getValue());
final Attribute attributeC = node.getAttributes().get(3);
assertEquals("c", attributeC.getName());
assertEquals("3", attributeC.getValue());
}
use of org.sonar.plugins.web.node.Node in project sonar-web by SonarSource.
the class NoSonarScannerTest method scanNoSonar.
@Test
public void scanNoSonar() {
List<Node> nodeList = new PageLexer().parse(new StringReader("<table>\n<!-- //NOSONAR --><td>\n</table>"));
WebSourceCode webSourceCode = new WebSourceCode(new DefaultInputFile("key", "dummy.jsp"));
NoSonarFilter noSonarFilter = spy(new NoSonarFilter());
HtmlAstScanner pageScanner = new HtmlAstScanner(Collections.emptyList());
pageScanner.addVisitor(new NoSonarScanner(noSonarFilter));
pageScanner.scan(nodeList, webSourceCode, Charsets.UTF_8);
verify(noSonarFilter, times(1)).noSonarInFile(any(InputFile.class), isOnlyIgnoringLine2());
}
use of org.sonar.plugins.web.node.Node in project sonar-web by SonarSource.
the class TextTokenizer method consume.
@Override
public boolean consume(CodeReader codeReader, List<Node> nodeList) {
Node node = createNode();
setStartPosition(codeReader, node);
StringBuilder stringBuilder = new StringBuilder();
if (inScript(nodeList)) {
codeReader.popTo(new EndScriptMatcher(codeReader), stringBuilder);
} else {
codeReader.popTo(endTokenMatcher, stringBuilder);
}
node.setCode(stringBuilder.toString());
setEndPosition(codeReader, node);
nodeList.add(node);
return true;
}
use of org.sonar.plugins.web.node.Node in project sonar-web by SonarSource.
the class PageCountLinesTest method testCountLinesJspFile.
@Test
public void testCountLinesJspFile() throws FileNotFoundException {
List<Node> nodeList = lexer.parse(new FileReader(TestUtils.getResource("checks/AvoidHtmlCommentCheck/document.jsp")));
String relativePath = "testdocument.jsp";
WebSourceCode webSourceCode = new WebSourceCode(new DefaultInputFile("key", relativePath).setModuleBaseDir(new File(".").toPath()));
scanner.scan(nodeList, webSourceCode, Charsets.UTF_8);
assertThat(webSourceCode.getMeasure(CoreMetrics.NCLOC)).isEqualTo(2);
assertThat(webSourceCode.getDetailedLinesOfCode()).containsOnly(1, 3);
assertThat(webSourceCode.getMeasure(CoreMetrics.COMMENT_LINES)).isEqualTo(6);
assertThat(webSourceCode.getDetailedLinesOfComments()).containsOnly(2, 4, 6, 7, 8, 10);
}
Aggregations