Search in sources :

Example 1 with TokensLine

use of org.sonar.duplications.internal.pmd.TokensLine in project sonar-go by SonarSource.

the class CpdVisitorTest method test.

@Test
void test() throws IOException {
    String filename = "lets.go";
    String code = readTestResource(getClass(), filename);
    String codeJson = readTestResource(getClass(), filename + ".uast.json");
    InputFile inputFile = createInputFile("lets.go", code);
    sensorContext.fileSystem().add(inputFile);
    CpdVisitor cpdVisitor = new CpdVisitor(sensorContext, inputFile);
    UastNode node = Uast.from(new StringReader(codeJson));
    cpdVisitor.scan(node);
    cpdVisitor.save();
    List<TokensLine> tokensLines = sensorContext.cpdTokens("module:" + inputFile.filename());
    assertThat(tokensLines).isNotNull().hasSize(5);
    assertThat(tokensLines).extracting("value").isEqualTo(Arrays.asList("packagemain", "funcfun()string{", "a:=LITERAL", "returna", "}"));
    assertThat(tokensLines).extracting("startLine").isEqualTo(Arrays.asList(1, 3, 4, 5, 6));
}
Also used : StringReader(java.io.StringReader) TokensLine(org.sonar.duplications.internal.pmd.TokensLine) UastNode(org.sonar.uast.UastNode) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.jupiter.api.Test)

Example 2 with TokensLine

use of org.sonar.duplications.internal.pmd.TokensLine in project sonarqube by SonarSource.

the class DefaultCpdTokens method addNewTokensLine.

private static void addNewTokensLine(ImmutableList.Builder<TokensLine> result, int startUnit, int endUnit, int startLine, StringBuilder sb) {
    if (sb.length() != 0) {
        result.add(new TokensLine(startUnit, endUnit, startLine, sb.toString()));
        sb.setLength(0);
    }
}
Also used : TokensLine(org.sonar.duplications.internal.pmd.TokensLine)

Aggregations

TokensLine (org.sonar.duplications.internal.pmd.TokensLine)2 StringReader (java.io.StringReader)1 Test (org.junit.jupiter.api.Test)1 InputFile (org.sonar.api.batch.fs.InputFile)1 UastNode (org.sonar.uast.UastNode)1