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);
}
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 }])");
}
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);
}
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);
}
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);
}
Aggregations