Search in sources :

Example 16 with HtmlSourceCode

use of org.sonar.plugins.html.visitor.HtmlSourceCode in project sonar-web by SonarSource.

the class PageCountLinesTest method testCountLinesHtmlFile.

@Test
public void testCountLinesHtmlFile() {
    List<Node> nodeList = lexer.parse(readFile("checks/AvoidHtmlCommentCheck/document.html"));
    HtmlSourceCode htmlSourceCode = createHtmlSourceCode("test/document.html");
    scanner.scan(nodeList, htmlSourceCode);
    assertThat(htmlSourceCode.getMeasure(CoreMetrics.NCLOC)).isEqualTo(8);
    assertThat(htmlSourceCode.getDetailedLinesOfCode()).containsOnly(1, 2, 3, 4, 6, 7, 8, 9);
    assertThat(htmlSourceCode.getMeasure(CoreMetrics.COMMENT_LINES)).isEqualTo(1);
}
Also used : Node(org.sonar.plugins.html.node.Node) HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) Test(org.junit.Test)

Example 17 with HtmlSourceCode

use of org.sonar.plugins.html.visitor.HtmlSourceCode in project sonar-web by SonarSource.

the class TestHelper method scan.

public static HtmlSourceCode scan(File file, DefaultNodeVisitor visitor) {
    FileReader fileReader;
    try {
        fileReader = new FileReader(file);
    } catch (FileNotFoundException e) {
        throw new IllegalStateException(e);
    }
    HtmlSourceCode result = new HtmlSourceCode(new TestInputFileBuilder("key", file.getPath()).setLanguage(HtmlConstants.LANGUAGE_KEY).setType(InputFile.Type.MAIN).setModuleBaseDir(new File(".").toPath()).setCharset(StandardCharsets.UTF_8).build());
    HtmlAstScanner walker = new HtmlAstScanner(Arrays.asList(new PageCountLines(), new ComplexityVisitor()));
    walker.addVisitor(visitor);
    walker.scan(new PageLexer().parse(fileReader), result);
    return result;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) PageLexer(org.sonar.plugins.html.lex.PageLexer) ComplexityVisitor(org.sonar.plugins.html.analyzers.ComplexityVisitor) FileNotFoundException(java.io.FileNotFoundException) PageCountLines(org.sonar.plugins.html.analyzers.PageCountLines) FileReader(java.io.FileReader) HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) HtmlAstScanner(org.sonar.plugins.html.visitor.HtmlAstScanner)

Example 18 with HtmlSourceCode

use of org.sonar.plugins.html.visitor.HtmlSourceCode in project sonar-web by SonarSource.

the class RequiredAttributeCheckTest method custom.

@Test
public void custom() {
    RequiredAttributeCheck check = new RequiredAttributeCheck();
    check.attributes = "img.alt,script.type";
    HtmlSourceCode sourceCode = TestHelper.scan(new File("src/test/resources/checks/RequiredAttributeCheck.html"), check);
    checkMessagesVerifier.verify(sourceCode.getIssues()).next().atLine(2).withMessage("Add the missing \"alt\" to element \"img\".").next().atLine(7).withMessage("Add the missing \"type\" to element \"script\".");
}
Also used : HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) File(java.io.File) Test(org.junit.Test)

Example 19 with HtmlSourceCode

use of org.sonar.plugins.html.visitor.HtmlSourceCode in project sonar-web by SonarSource.

the class DoubleQuotesCheckTest method test.

@Test
public void test() throws Exception {
    HtmlSourceCode sourceCode = TestHelper.scan(new File("src/test/resources/checks/doubleQuotesCheck.html"), new DoubleQuotesCheck());
    checkMessagesVerifier.verify(sourceCode.getIssues()).next().atLine(1).withMessage("Use double quotes instead of single ones.");
}
Also used : HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) File(java.io.File) Test(org.junit.Test)

Example 20 with HtmlSourceCode

use of org.sonar.plugins.html.visitor.HtmlSourceCode in project sonar-web by SonarSource.

the class InternationalizationCheckTest method custom.

@Test
public void custom() {
    InternationalizationCheck check = new InternationalizationCheck();
    check.attributes = "outputLabel.value";
    HtmlSourceCode sourceCode = TestHelper.scan(new File("src/test/resources/checks/InternationalizationCheck.html"), check);
    checkMessagesVerifier.verify(sourceCode.getIssues()).next().atLocation(1, 13, 1, 16).withMessage("Define this label in the resource bundle.").next().atLocation(2, 0, 2, 25).withMessage("Define this label in the resource bundle.").next().atLine(9).withMessage("Define this label in the resource bundle.").next().atLine(10).withMessage("Define this label in the resource bundle.").next().atLine(11).withMessage("Define this label in the resource bundle.");
}
Also used : HtmlSourceCode(org.sonar.plugins.html.visitor.HtmlSourceCode) File(java.io.File) Test(org.junit.Test)

Aggregations

HtmlSourceCode (org.sonar.plugins.html.visitor.HtmlSourceCode)115 File (java.io.File)111 Test (org.junit.Test)111 InputFile (org.sonar.api.batch.fs.InputFile)8 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)3 PageLexer (org.sonar.plugins.html.lex.PageLexer)3 Node (org.sonar.plugins.html.node.Node)3 HtmlAstScanner (org.sonar.plugins.html.visitor.HtmlAstScanner)3 FileReader (java.io.FileReader)2 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ExpectedException (org.junit.rules.ExpectedException)1 FilePredicates (org.sonar.api.batch.fs.FilePredicates)1 FileSystem (org.sonar.api.batch.fs.FileSystem)1 ComplexityVisitor (org.sonar.plugins.html.analyzers.ComplexityVisitor)1 PageCountLines (org.sonar.plugins.html.analyzers.PageCountLines)1 VueLexer (org.sonar.plugins.html.lex.VueLexer)1