Skip to content

Commit 64e8b1e

Browse files
authored
SONARGO-113 Rewrite tests IdenticalBinaryOperandCheckTest - OctalValuesCheckTest (#86)
1 parent 3ed2bee commit 64e8b1e

32 files changed

+393
-284
lines changed

sonar-go-checks/src/main/java/org/sonar/go/checks/GoCheckList.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public static List<Class<?>> checks() {
4747
IdenticalBinaryOperandCheck.class,
4848
IdenticalConditionsCheck.class,
4949
IfConditionalAlwaysTrueOrFalseCheck.class,
50-
MatchCaseTooBigCheck.class,
51-
MatchWithoutElseCheck.class,
52-
NestedMatchCheck.class,
50+
SwitchCaseTooBigCheck.class,
51+
SwitchWithoutDefaultCheck.class,
52+
NestedSwitchCheck.class,
5353
OctalValuesCheck.class,
5454
OneStatementPerLineGoCheck.class,
5555
ParsingErrorCheck.class,

sonar-go-checks/src/main/java/org/sonar/go/checks/IdenticalBinaryOperandCheck.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,16 @@
2929
@Rule(key = "S1764")
3030
public class IdenticalBinaryOperandCheck implements SlangCheck {
3131

32+
public static final String MESSAGE = "Correct one of the identical sub-expressions on both sides this operator";
33+
3234
@Override
3335
public void initialize(InitContext init) {
3436
init.register(BinaryExpressionTree.class, (ctx, tree) -> {
3537
if (tree.operator() != BinaryExpressionTree.Operator.PLUS
3638
&& tree.operator() != BinaryExpressionTree.Operator.TIMES
3739
&& !containsPlaceHolder(tree)
3840
&& areEquivalent(skipParentheses(tree.leftOperand()), skipParentheses(tree.rightOperand()))) {
39-
ctx.reportIssue(
40-
tree.rightOperand(),
41-
"Correct one of the identical sub-expressions on both sides this operator",
42-
new SecondaryLocation(tree.leftOperand()));
41+
ctx.reportIssue(tree.rightOperand(), MESSAGE, new SecondaryLocation(tree.leftOperand()));
4342
}
4443
});
4544
}

sonar-go-checks/src/main/java/org/sonar/go/checks/IdenticalConditionsCheck.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.sonarsource.slang.api.IfTree;
2525
import org.sonarsource.slang.api.MatchCaseTree;
2626
import org.sonarsource.slang.api.MatchTree;
27-
import org.sonarsource.slang.api.TextRange;
2827
import org.sonarsource.slang.api.Tree;
2928
import org.sonarsource.slang.checks.api.CheckContext;
3029
import org.sonarsource.slang.checks.api.InitContext;
@@ -57,19 +56,19 @@ private static List<Tree> collectConditions(MatchTree matchTree) {
5756

5857
private static List<Tree> collectConditions(IfTree ifTree, List<Tree> list) {
5958
list.add(skipParentheses(ifTree.condition()));
60-
Tree elseBranch = ifTree.elseBranch();
59+
var elseBranch = ifTree.elseBranch();
6160
if (elseBranch instanceof IfTree elseIfBranch) {
6261
return collectConditions(elseIfBranch, list);
6362
}
6463
return list;
6564
}
6665

6766
private static void checkConditions(CheckContext ctx, List<Tree> conditions) {
68-
for (List<Tree> group : SyntacticEquivalence.findDuplicatedGroups(conditions)) {
69-
Tree original = group.get(0);
67+
for (var group : SyntacticEquivalence.findDuplicatedGroups(conditions)) {
68+
var original = group.get(0);
7069
group.stream().skip(1)
7170
.forEach(duplicated -> {
72-
TextRange originalRange = original.metaData().textRange();
71+
var originalRange = original.metaData().textRange();
7372
ctx.reportIssue(
7473
duplicated,
7574
"This condition duplicates the one on line " + originalRange.start().line() + ".",

sonar-go-checks/src/main/java/org/sonar/go/checks/IfConditionalAlwaysTrueOrFalseCheck.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ public class IfConditionalAlwaysTrueOrFalseCheck implements SlangCheck {
4444
@Override
4545
public void initialize(InitContext init) {
4646
init.register(IfTree.class, (ctx, ifTree) -> {
47-
Tree condition = ifTree.condition();
47+
var condition = ifTree.condition();
4848
if (isAlwaysTrueOrFalse(condition)) {
49-
String message = String.format(MESSAGE_TEMPLATE, ifTree.ifKeyword().text());
49+
var message = String.format(MESSAGE_TEMPLATE, ifTree.ifKeyword().text());
5050
ctx.reportIssue(condition, message);
5151
}
5252
});
5353
}
5454

5555
private static boolean isAlwaysTrueOrFalse(Tree originalCondition) {
56-
Tree condition = skipParentheses(originalCondition);
56+
var condition = skipParentheses(originalCondition);
5757
return isBooleanLiteral(condition)
5858
|| isTrueValueLiteral(condition)
5959
|| isFalseValueLiteral(condition)

sonar-go-checks/src/main/java/org/sonar/go/checks/NestedMatchCheck.java sonar-go-checks/src/main/java/org/sonar/go/checks/NestedSwitchCheck.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@
1919
import java.text.MessageFormat;
2020
import org.sonar.check.Rule;
2121
import org.sonarsource.slang.api.MatchTree;
22+
import org.sonarsource.slang.checks.api.CheckContext;
2223
import org.sonarsource.slang.checks.api.InitContext;
2324
import org.sonarsource.slang.checks.api.SlangCheck;
2425

2526
@Rule(key = "S1821")
26-
public class NestedMatchCheck implements SlangCheck {
27+
public class NestedSwitchCheck implements SlangCheck {
2728
private static final String MESSAGE = "Refactor the code to eliminate this nested \"{0}\".";
2829

2930
@Override
3031
public void initialize(InitContext init) {
3132
init.register(MatchTree.class, (ctx, matchTree) -> ctx.ancestors().stream()
3233
.filter(MatchTree.class::isInstance)
3334
.findFirst()
34-
.ifPresent(parentMatch -> ctx.reportIssue(matchTree.keyword(), MessageFormat.format(MESSAGE, matchTree.keyword().text()))));
35+
.ifPresent(parentMatch -> reportIssue(ctx, matchTree)));
36+
}
37+
38+
private static void reportIssue(CheckContext ctx, MatchTree matchTree) {
39+
ctx.reportIssue(matchTree.keyword(), MessageFormat.format(MESSAGE, matchTree.keyword().text()));
3540
}
3641
}

sonar-go-checks/src/main/java/org/sonar/go/checks/OctalValuesCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void initialize(InitContext init) {
4242

4343
private static boolean isException(IntegerLiteralTree literalTree) {
4444
// octal literal < 8 are authorized, as well as octal literals with 3 digits, as they are often used for file permissions
45-
BigInteger value = literalTree.getIntegerValue();
45+
var value = literalTree.getIntegerValue();
4646
return value.compareTo(EIGHT) < 0 || literalTree.getNumericPart().length() == FILE_PERMISSION_MASK_LENGTH;
4747
}
4848

sonar-go-checks/src/main/java/org/sonar/go/checks/MatchCaseTooBigCheck.java sonar-go-checks/src/main/java/org/sonar/go/checks/SwitchCaseTooBigCheck.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.sonarsource.slang.checks.api.SlangCheck;
2525

2626
@Rule(key = "S1151")
27-
public class MatchCaseTooBigCheck implements SlangCheck {
27+
public class SwitchCaseTooBigCheck implements SlangCheck {
2828

2929
private static final int DEFAULT_MAX = 6;
3030
private static final String DEFAULT_MAX_VALUE = "" + DEFAULT_MAX;
@@ -40,7 +40,7 @@ public class MatchCaseTooBigCheck implements SlangCheck {
4040
@Override
4141
public void initialize(InitContext init) {
4242
init.register(MatchCaseTree.class, (ctx, matchCaseTree) -> {
43-
int linesOfCode = matchCaseTree.metaData().linesOfCode().size();
43+
var linesOfCode = matchCaseTree.metaData().linesOfCode().size();
4444
if (linesOfCode > max) {
4545
ctx.reportIssue(matchCaseTree.rangeToHighlight(), MessageFormat.format(MESSAGE, linesOfCode, max));
4646
}

sonar-go-checks/src/main/java/org/sonar/go/checks/MatchWithoutElseCheck.java sonar-go-checks/src/main/java/org/sonar/go/checks/SwitchWithoutDefaultCheck.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@
1818

1919
import org.sonar.check.Rule;
2020
import org.sonarsource.slang.api.MatchTree;
21-
import org.sonarsource.slang.api.Token;
2221
import org.sonarsource.slang.checks.api.InitContext;
2322
import org.sonarsource.slang.checks.api.SlangCheck;
2423

2524
@Rule(key = "S131")
26-
public class MatchWithoutElseCheck implements SlangCheck {
25+
public class SwitchWithoutDefaultCheck implements SlangCheck {
2726

2827
@Override
2928
public void initialize(InitContext init) {
3029
init.register(MatchTree.class, (ctx, tree) -> {
3130
if (tree.cases().stream().noneMatch(matchCase -> matchCase.expression() == null)) {
32-
Token keyword = tree.keyword();
33-
String message = String.format("Add a default clause to this \"%s\" statement.", keyword.text());
31+
var keyword = tree.keyword();
32+
var message = String.format("Add a default clause to this \"%s\" statement.", keyword.text());
3433
ctx.reportIssue(keyword, message);
3534
}
3635
});

sonar-go-checks/src/test/java/org/sonar/go/checks/IdenticalBinaryOperandCheckTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
class IdenticalBinaryOperandCheckTest {
2222

2323
@Test
24-
void test() {
25-
SlangVerifier.verify("IdenticalBinaryOperand.slang", new IdenticalBinaryOperandCheck());
24+
void shouldRaiseIssues() {
25+
GoVerifier.verify("IdenticalBinaryOperandCheck/IdenticalBinaryOperandCheck.go", new IdenticalBinaryOperandCheck());
2626
}
2727

2828
}

sonar-go-checks/src/test/java/org/sonar/go/checks/IdenticalConditionsCheckTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
class IdenticalConditionsCheckTest {
2222

2323
@Test
24-
void test() {
25-
SlangVerifier.verify("IdenticalConditions.slang", new IdenticalConditionsCheck());
24+
void shouldRaiseIssues() {
25+
GoVerifier.verify("IdenticalConditionsCheck/IdenticalConditionsCheck.go", new IdenticalConditionsCheck());
2626
}
2727

2828
}

sonar-go-checks/src/test/java/org/sonar/go/checks/IfConditionalAlwaysTrueOrFalseCheckTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
class IfConditionalAlwaysTrueOrFalseCheckTest {
2222

2323
@Test
24-
void test() {
25-
SlangVerifier.verify("IfConditionalAlwaysTrueOrFalse.slang", new IfConditionalAlwaysTrueOrFalseCheck());
24+
void shouldRaiseIssues() {
25+
GoVerifier.verify("IfConditionalAlwaysTrueOrFalseCheck/IfConditionalAlwaysTrueOrFalseCheck.go", new IfConditionalAlwaysTrueOrFalseCheck());
2626
}
2727

2828
}

sonar-go-checks/src/test/java/org/sonar/go/checks/MatchWithoutElseCheckTest.java sonar-go-checks/src/test/java/org/sonar/go/checks/NestedSwitchCheckTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21-
class MatchWithoutElseCheckTest {
21+
class NestedSwitchCheckTest {
2222

2323
@Test
24-
void test() {
25-
SlangVerifier.verify("MatchWithoutElse.slang", new MatchWithoutElseCheck());
24+
void shouldRaiseIssues() {
25+
GoVerifier.verify("NestedSwitchCheck/NestedSwitchCheck.go", new NestedSwitchCheck());
2626
}
27-
2827
}

sonar-go-checks/src/test/java/org/sonar/go/checks/OctalValuesCheckTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
class OctalValuesCheckTest {
2222

2323
@Test
24-
void test() {
25-
SlangVerifier.verify("OctalValues.slang", new OctalValuesCheck());
24+
void shouldRaiseIssues() {
25+
GoVerifier.verify("OctalValuesCheck/OctalValuesCheck.go", new OctalValuesCheck());
2626
}
2727

2828
}

sonar-go-checks/src/test/java/org/sonar/go/checks/MatchCaseTooBigCheckTest.java sonar-go-checks/src/test/java/org/sonar/go/checks/SwitchCaseTooBigCheckTest.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21-
class MatchCaseTooBigCheckTest {
21+
class SwitchCaseTooBigCheckTest {
2222

23-
private MatchCaseTooBigCheck check = new MatchCaseTooBigCheck();
23+
private final SwitchCaseTooBigCheck check = new SwitchCaseTooBigCheck();
2424

2525
@Test
26-
void max_5() {
27-
check.max = 5;
28-
SlangVerifier.verify("MatchCaseTooBig_5.slang", check);
26+
void shouldRaiseIssuesWithDefaultMaxValue() {
27+
GoVerifier.verify("SwitchCaseTooBigCheck/SwitchCaseTooBigCheck_6.go", check);
2928
}
3029

3130
@Test
32-
void max_3() {
31+
void shouldRaiseIssuesWithCustomMaxValue() {
3332
check.max = 3;
34-
SlangVerifier.verify("MatchCaseTooBig_3.slang", check);
33+
GoVerifier.verify("SwitchCaseTooBigCheck/SwitchCaseTooBigCheck_3.go", check);
3534
}
3635
}

sonar-go-checks/src/test/java/org/sonar/go/checks/NestedMatchCheckTest.java sonar-go-checks/src/test/java/org/sonar/go/checks/SwitchWithoutDefaultCheckTest.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
import org.junit.jupiter.api.Test;
2020

21-
class NestedMatchCheckTest {
21+
class SwitchWithoutDefaultCheckTest {
22+
2223
@Test
23-
void test() {
24-
SlangVerifier.verify("NestedMatch.slang", new NestedMatchCheck());
24+
void shouldRaiseIssues() {
25+
GoVerifier.verify("SwitchWithoutDefaultCheck/SwitchWithoutDefaultCheck.go", new SwitchWithoutDefaultCheck());
2526
}
27+
2628
}

sonar-go-checks/src/test/resources/checks/IdenticalBinaryOperand.slang

-21
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package IdenticalBinaryOperandCheck
2+
3+
func main() {
4+
x == 1
5+
1 == 1 // Noncompliant {{Correct one of the identical sub-expressions on both sides this operator}}
6+
// ^> ^
7+
1 == (1) // Noncompliant {{Correct one of the identical sub-expressions on both sides this operator}}
8+
(1 + 2) == 1+2 // Noncompliant
9+
(1 + 2) == (1 + 2) // Noncompliant
10+
(1 + 2) == (1 + 2 + 3)
11+
(x ==
12+
// ^>
13+
x) // Noncompliant
14+
// ^
15+
1 == 2
16+
x = x
17+
x + x
18+
x * x
19+
x <= x // Noncompliant
20+
_x <= _x // Noncompliant
21+
x_ <= x
22+
_x <= x
23+
x <= _
24+
_ <= y
25+
}

sonar-go-checks/src/test/resources/checks/IdenticalConditions.slang

-26
This file was deleted.

0 commit comments

Comments
 (0)