-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add stronger processor tests (#167)
* Add expected output file for BigDecimalDOubleConstructor.java * Add expected output file for ArrayHashCodeAndToString.java * Add processor test with reference output * Refactor tests to be more maintainable * Add a sanity check that the expected output has no issues * Further reduce code duplication between tests * Fix formatting
- Loading branch information
Showing
4 changed files
with
235 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...sor_test_files/2111_BigDecimalDoubleConstructor/BigDecimalDoubleConstructor.java.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Test for rule s2111 | ||
|
||
import java.math.BigDecimal; | ||
import java.math.MathContext; | ||
|
||
public class BigDecimalDoubleConstructor { | ||
|
||
// Tests from https://rules.sonarsource.com/java/type/Bug/RSPEC-2111 | ||
public void main(String[] args) { | ||
double d = 1.1; | ||
BigDecimal bd1 = BigDecimal.valueOf(d); | ||
BigDecimal bd2 = BigDecimal.valueOf(1.1); | ||
} | ||
|
||
// Tests from https://github.com/SonarSource/sonar-java/blob/master/java-checks-test-sources/src/main/java/checks/BigDecimalDoubleConstructorCheck.java | ||
public void main2(String[] args) { | ||
MathContext mc; | ||
BigDecimal bd1 = new BigDecimal("1"); | ||
BigDecimal bd2 = BigDecimal.valueOf(2.0); | ||
BigDecimal bd4 = new BigDecimal("2.0", mc); | ||
BigDecimal bd5 = BigDecimal.valueOf(2.0f); | ||
BigDecimal bd6 = new BigDecimal("2.0", mc); | ||
BigDecimal bd3 = BigDecimal.valueOf(2.0); | ||
} | ||
|
||
// Aditional tests | ||
public void foo(String[] args) { | ||
double d = 1.1; | ||
float f = 2.2; | ||
float f1 = 2f; | ||
BigDecimal bd3 = BigDecimal.valueOf(f); | ||
BigDecimal bd4 = BigDecimal.valueOf(f1); | ||
BigDecimal bd5 = BigDecimal.valueOf(d); | ||
BigDecimal bd6 = new BigDecimal("1.1"); | ||
BigDecimal bd7 = BigDecimal.valueOf(f); | ||
BigDecimal bd8 = BigDecimal.valueOf(f1); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...processor_test_files/2116_ArrayHashCodeAndToString/ArrayHashCodeAndToString.java.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
// Test for rule s2116 | ||
|
||
public class ArrayHashCodeAndToString { | ||
|
||
// Tests from https://rules.sonarsource.com/java/type/Bug/RSPEC-2116 | ||
public static void main( String[] args ) { | ||
String argStr = java.util.Arrays.toString(args); // Noncompliant | ||
int argHash = java.util.Arrays.hashCode(args); // Noncompliant | ||
} | ||
|
||
// Tests from https://github.com/SonarSource/sonar-java/blob/master/java-checks-test-sources/src/main/java/checks/ArrayHashCodeAndToStringCheck.java | ||
void method(String[] args, String string) { | ||
Class class1 = args.getClass(); | ||
String str = string.toString(); | ||
int hash = string.hashCode(); | ||
} | ||
|
||
// Aditional tests | ||
public void foo(String[] args) { | ||
String[] array1 = {"F", "O", "O"}; | ||
System.out.println(java.util.Arrays.toString(array1)); // Noncompliant | ||
varargsTest(1, 2, 3); | ||
} | ||
|
||
private void varargsTest(int... array2) { | ||
String a = java.util.Arrays.toString(array2); // Noncompliant | ||
} | ||
|
||
} |