Skip to content

Commit

Permalink
SONARPY-2283 Add unit tests on S905 and S5914 when an imported class …
Browse files Browse the repository at this point in the history
…has a property, and its getter is called (#2142)
  • Loading branch information
thomas-serre-sonarsource authored Nov 8, 2024
1 parent d8b1171 commit 0278d5c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,35 @@
*/
package org.sonar.python.checks;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.sonar.python.checks.utils.PythonCheckVerifier;

class UselessStatementCheckTest {

@Test
void test() {
PythonCheckVerifier.verify("src/test/resources/checks/uselessStatement.py", new UselessStatementCheck());
}

@Test
void test_ignore_manifest() {
PythonCheckVerifier.verifyNoIssue("src/test/resources/checks/__manifest__.py", new UselessStatementCheck());
PythonCheckVerifier.verify("src/test/resources/checks/uselessStatement/uselessStatement.py", new UselessStatementCheck());
}

@Test
void custom() {
UselessStatementCheck check = new UselessStatementCheck();
check.reportOnStrings = true;
check.ignoredOperators = "<<,+";
PythonCheckVerifier.verify("src/test/resources/checks/uselessStatementCustom.py", check);
PythonCheckVerifier.verify("src/test/resources/checks/uselessStatement/uselessStatementCustom.py", check);
}

@Test
void test_import() {
PythonCheckVerifier.verify(
List.of("src/test/resources/checks/uselessStatement/uselessStatementImported.py", "src/test/resources/checks/uselessStatement/uselessStatementImport.py"),
new UselessStatementCheck()
);
}

@Test
void test_ignore_manifest() {
PythonCheckVerifier.verifyNoIssue("src/test/resources/checks/__manifest__.py", new UselessStatementCheck());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.python.checks.tests;

import java.util.List;
import org.junit.jupiter.api.Test;
import org.sonar.plugins.python.api.PythonCheck;
import org.sonar.python.checks.utils.PythonCheckVerifier;
Expand All @@ -29,7 +30,15 @@ class UnconditionalAssertionCheckTest {

@Test
void test() {
PythonCheckVerifier.verify("src/test/resources/checks/tests/unconditionalAssertion.py", new UnconditionalAssertionCheck());
PythonCheckVerifier.verify("src/test/resources/checks/unconditionalAssertion/unconditionalAssertion.py", new UnconditionalAssertionCheck());
}

@Test
void test_import() {
PythonCheckVerifier.verify(
List.of("src/test/resources/checks/unconditionalAssertion/unconditionalAssertionImport.py", "src/test/resources/checks/unconditionalAssertion/unconditionalAssertionImported.py"),
new UnconditionalAssertionCheck()
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import unittest
from uselessStatementImported import ClassWithProperty

class TestClass(unittest.TestCase):
def test(self):
self.assertTrue(42) # Noncompliant
self.assertTrue(ClassWithProperty().my_property) # OK property access that is not a constant and might have side effects
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ClassWithProperty:

@property
def my_property(self): ...
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from uselessStatementImported import ClassWithProperty

ClassWithProperty().method # Noncompliant
ClassWithProperty().my_property # OK property access that is not a constant and might have side effects
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ClassWithProperty:

def method(self): ...

@property
def my_property(self): ...

0 comments on commit 0278d5c

Please sign in to comment.