Search in sources :

Example 6 with UastNode

use of org.sonar.uast.UastNode in project sonar-go by SonarSource.

the class SwitchLikeTest method test.

@Test
void test() throws Exception {
    UastNode node = Uast.from(new StringReader("{ kinds: ['SWITCH'] }"));
    SwitchLike switchLike = SwitchLike.from(node);
    assertThat(switchLike).isNotNull();
    assertThat(switchLike.caseNodes()).isEmpty();
    node = Uast.from(new StringReader("{ kinds: ['SWITCH'], children: [ { kinds: ['CASE'] }] }"));
    switchLike = SwitchLike.from(node);
    assertThat(switchLike).isNotNull();
    assertThat(switchLike.caseNodes()).hasSize(1);
}
Also used : StringReader(java.io.StringReader) UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 7 with UastNode

use of org.sonar.uast.UastNode in project sonar-go by SonarSource.

the class IssueTest method message_to_string.

@Test
void message_to_string() {
    Set<UastNode.Kind> noKind = Collections.emptySet();
    List<UastNode> noChild = Collections.emptyList();
    UastNode node1 = new UastNode(noKind, "", new UastNode.Token(42, 7, "{"), noChild);
    UastNode node2 = new UastNode(noKind, "", new UastNode.Token(54, 3, "}"), noChild);
    Issue.Message message1 = new Issue.Message(node1, node2, "a message");
    assertThat(message1.toString()).isEqualTo("([42:7 {], [54:3 }]) a message");
    Issue.Message message2 = new Issue.Message(node1, node2, null);
    assertThat(message2.toString()).isEqualTo("([42:7 {], [54:3 }])");
}
Also used : UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 8 with UastNode

use of org.sonar.uast.UastNode in project sonar-go by SonarSource.

the class AssignmentLikeTest method test.

@Test
void test() {
    UastNode target = node(UastNode.Kind.ASSIGNMENT_TARGET);
    UastNode operator = node(UastNode.Kind.ASSIGNMENT_OPERATOR);
    UastNode value = node(UastNode.Kind.ASSIGNMENT_VALUE);
    UastNode assignment = node(UastNode.Kind.ASSIGNMENT, target, operator, value);
    AssignmentLike assignmentLike = AssignmentLike.from(assignment);
    assertEquals(assignmentLike.node(), assignment);
    assertEquals(assignmentLike.target(), target);
    assertEquals(assignmentLike.operator(), operator);
    assertEquals(assignmentLike.value(), value);
    assertFalse(assignmentLike.isMultiple());
    List<AssignmentLike> assignmentsTuples = assignmentLike.assignmentsTuples();
    assertEquals(assignmentsTuples.size(), 1);
    assertEquals(assignmentsTuples.get(0), assignmentLike);
    // same instance, not recomputed
    assertTrue(assignmentLike.assignmentsTuples() == assignmentsTuples);
}
Also used : UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 9 with UastNode

use of org.sonar.uast.UastNode in project sonar-go by SonarSource.

the class CaseLikeTest method test.

@Test
void test() throws Exception {
    UastNode node = Uast.from(new StringReader("{ kinds: ['CASE'], children: [{kinds: ['CONDITION']}, {kinds: ['CONDITION']}]}"));
    CaseLike caseLike = CaseLike.from(node);
    assertThat(caseLike).isNotNull();
    assertThat(caseLike.conditions()).hasSize(2);
}
Also used : StringReader(java.io.StringReader) UastNode(org.sonar.uast.UastNode) Test(org.junit.jupiter.api.Test)

Example 10 with UastNode

use of org.sonar.uast.UastNode in project sonar-go by SonarSource.

the class CognitiveComplexity method visitBinaryExpression.

private void visitBinaryExpression(UastNode node) {
    List<BinaryExpressionLike> expressionAsList = new ArrayList<>();
    flattenBinaryExpressions(node, expressionAsList);
    UastNode.Kind lastLogicalOperatorKind = null;
    for (BinaryExpressionLike binaryExpression : expressionAsList) {
        UastNode.Kind kind = logicalOperatorKind(binaryExpression);
        if (kind != null) {
            if (binaryExpression.node() != node) {
                ignoredNode.add(binaryExpression.node());
            }
            if (kind != lastLogicalOperatorKind) {
                increaseComplexityByOne(binaryExpression.operator());
            }
        }
        lastLogicalOperatorKind = kind;
    }
    visitChildren(node);
}
Also used : ArrayList(java.util.ArrayList) UastNode(org.sonar.uast.UastNode) BinaryExpressionLike(org.sonar.uast.helpers.BinaryExpressionLike)

Aggregations

UastNode (org.sonar.uast.UastNode)31 Test (org.junit.jupiter.api.Test)16 StringReader (java.io.StringReader)10 InputFile (org.sonar.api.batch.fs.InputFile)5 ArrayList (java.util.ArrayList)4 List (java.util.List)2 Set (java.util.Set)2 BinaryExpressionLike (org.sonar.uast.helpers.BinaryExpressionLike)2 CaseLike (org.sonar.uast.helpers.CaseLike)2 IfLike (org.sonar.uast.helpers.IfLike)2 SwitchLike (org.sonar.uast.helpers.SwitchLike)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1