diff options
| author | Marc Mueller <30130371+cdce8p@users.noreply.github.com> | 2021-07-13 22:19:16 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-13 22:19:16 +0200 |
| commit | a7e55b4caefd532d404ac9429f490edcc5ad7d5a (patch) | |
| tree | b5b4ecb299e63ced5647a32ee3a42ae90636b9fb /tests | |
| parent | 071d36a3006f4e758ab4a185ff2e904275bb0be5 (diff) | |
| download | astroid-git-a7e55b4caefd532d404ac9429f490edcc5ad7d5a.tar.gz | |
Add new If guard helper methods (#1099)
* Add new if guard helper methods
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/unittest_nodes.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/unittest_nodes.py b/tests/unittest_nodes.py index b7759d50..60909bf2 100644 --- a/tests/unittest_nodes.py +++ b/tests/unittest_nodes.py @@ -342,6 +342,64 @@ class IfNodeTest(_NodeTest): self.assertEqual(self.astroid.body[1].orelse[0].block_range(7), (7, 8)) self.assertEqual(self.astroid.body[1].orelse[0].block_range(8), (8, 8)) + @staticmethod + def test_if_sys_guard(): + code = builder.extract_node( + """ + import sys + if sys.version_info > (3, 8): #@ + pass + + if sys.version_info[:2] > (3, 8): #@ + pass + + if sys.some_other_function > (3, 8): #@ + pass + """ + ) + assert isinstance(code, list) and len(code) == 3 + + assert isinstance(code[0], nodes.If) + assert code[0].is_sys_guard() is True + assert isinstance(code[1], nodes.If) + assert code[1].is_sys_guard() is True + + assert isinstance(code[2], nodes.If) + assert code[2].is_sys_guard() is False + + @staticmethod + def test_if_typing_guard(): + code = builder.extract_node( + """ + import typing + import typing as t + from typing import TYPE_CHECKING + + if typing.TYPE_CHECKING: #@ + pass + + if t.TYPE_CHECKING: #@ + pass + + if TYPE_CHECKING: #@ + pass + + if typing.SOME_OTHER_CONST: #@ + pass + """ + ) + assert isinstance(code, list) and len(code) == 4 + + assert isinstance(code[0], nodes.If) + assert code[0].is_typing_guard() is True + assert isinstance(code[1], nodes.If) + assert code[1].is_typing_guard() is True + assert isinstance(code[2], nodes.If) + assert code[2].is_typing_guard() is True + + assert isinstance(code[3], nodes.If) + assert code[3].is_typing_guard() is False + class TryExceptNodeTest(_NodeTest): CODE = """ |
