-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
invalid-range-index
bug where valid range calls were flagged as…
… invalid (#969)
- Loading branch information
1 parent
09af15c
commit 2547006
Showing
4 changed files
with
148 additions
and
20 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
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
121 changes: 121 additions & 0 deletions
121
tests/test_custom_checkers/test_invalid_range_index_checker.py
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,121 @@ | ||
import astroid | ||
import pylint.testutils | ||
|
||
from python_ta.checkers.invalid_range_index_checker import InvalidRangeIndexChecker | ||
|
||
|
||
class TestInvalidRangeIndexChecker(pylint.testutils.CheckerTestCase): | ||
CHECKER_CLASS = InvalidRangeIndexChecker | ||
|
||
def test_valid_range_one_arg(self): | ||
src = """ | ||
range(10) | ||
""" | ||
range_node = astroid.extract_node(src) | ||
with self.assertNoMessages(): | ||
self.checker.visit_call(range_node) | ||
|
||
def test_valid_range_two_arg(self): | ||
src = """ | ||
range(2, 8) | ||
""" | ||
range_node = astroid.extract_node(src) | ||
with self.assertNoMessages(): | ||
self.checker.visit_call(range_node) | ||
|
||
def test_valid_range_three_arg(self): | ||
src = """ | ||
range(2, 8, 2) | ||
""" | ||
range_node = astroid.extract_node(src) | ||
with self.assertNoMessages(): | ||
self.checker.visit_call(range_node) | ||
|
||
def test_valid_range_three_arg_negative(self): | ||
src = """ | ||
range(-10, -20, -2) | ||
""" | ||
range_node = astroid.extract_node(src) | ||
with self.assertNoMessages(): | ||
self.checker.visit_call(range_node) | ||
|
||
def test_invalid_range_empty(self): | ||
src = """ | ||
range() | ||
""" | ||
range_node = astroid.extract_node(src) | ||
with self.assertAddsMessages( | ||
pylint.testutils.MessageTest( | ||
msg_id="invalid-range-index", | ||
node=range_node, | ||
args="2", | ||
), | ||
ignore_position=True, | ||
): | ||
self.checker.visit_call(range_node) | ||
|
||
def test_invalid_range_one_arg(self): | ||
src = """ | ||
range(-10) | ||
""" | ||
range_node = astroid.extract_node(src) | ||
with self.assertAddsMessages( | ||
pylint.testutils.MessageTest( | ||
msg_id="invalid-range-index", | ||
node=range_node, | ||
args="2", | ||
), | ||
ignore_position=True, | ||
): | ||
self.checker.visit_call(range_node) | ||
|
||
def test_invalid_range_two_arg(self): | ||
src = """ | ||
range(5, 6) | ||
""" | ||
range_node = astroid.extract_node(src) | ||
with self.assertAddsMessages( | ||
pylint.testutils.MessageTest( | ||
msg_id="invalid-range-index", | ||
node=range_node, | ||
args="2", | ||
), | ||
ignore_position=True, | ||
): | ||
self.checker.visit_call(range_node) | ||
|
||
def test_invalid_range_three_arg(self): | ||
src = """ | ||
range(2, 15, 20) | ||
""" | ||
range_node = astroid.extract_node(src) | ||
with self.assertAddsMessages( | ||
pylint.testutils.MessageTest( | ||
msg_id="invalid-range-index", | ||
node=range_node, | ||
args="2", | ||
), | ||
ignore_position=True, | ||
): | ||
self.checker.visit_call(range_node) | ||
|
||
def test_invalid_range_zero_step(self): | ||
src = """ | ||
range(1, 10, 0) | ||
""" | ||
range_node = astroid.extract_node(src) | ||
with self.assertAddsMessages( | ||
pylint.testutils.MessageTest( | ||
msg_id="invalid-range-index", | ||
node=range_node, | ||
args="2", | ||
), | ||
ignore_position=True, | ||
): | ||
self.checker.visit_call(range_node) | ||
|
||
|
||
if __name__ == "__main__": | ||
import pytest | ||
|
||
pytest.main(["test_invalid_range_index_checker.py"]) |