11# stdlib
22import ast
33import re
4+ import sys
45from typing import List
56
67# 3rd party
910from domdf_python_tools .terminal_colours import Fore
1011
1112# this package
12- from flake8_dunder_all import Visitor , check_and_add_all
13+ from flake8_dunder_all import Visitor , check_and_add_all , mark_text_ranges
1314from tests .common import (
1415 mangled_source ,
1516 results ,
2122 testing_source_f ,
2223 testing_source_g ,
2324 testing_source_h ,
24- testing_source_i
25+ testing_source_i ,
26+ testing_source_j
2527 )
2628
2729
4446 ),
4547 pytest .param (testing_source_h , set (), id = "from import" ),
4648 pytest .param (testing_source_i , {'0:0: DALL000 Module lacks __all__.' }, id = "lots of lines" ),
49+ pytest .param (testing_source_j , {'0:0: DALL000 Module lacks __all__.' }, id = "multiline import" ),
4750 ]
4851 )
4952def test_plugin (source , expects ):
@@ -73,6 +76,7 @@ def test_plugin(source, expects):
7376 pytest .param (testing_source_g , ["a_function" ], False , 3 , id = "async function no __all__" ),
7477 pytest .param (testing_source_h , [], False , 1 , id = "from import" ),
7578 pytest .param (testing_source_i , ["a_function" ], False , 3 , id = "lots of lines" ),
79+ pytest .param (testing_source_j , ["a_function" ], False , 2 , id = "multiline import" ),
7680 ]
7781 )
7882def test_visitor (source , members , found_all , last_import ):
@@ -84,6 +88,43 @@ def test_visitor(source, members, found_all, last_import):
8488 assert visitor .last_import is last_import
8589
8690
91+ @pytest .mark .parametrize (
92+ "source, members, found_all, last_import" ,
93+ [
94+ pytest .param ('import foo' , [], False , 1 , id = "just an import" ),
95+ pytest .param ('"""a docstring"""' , [], False , 0 , id = "just a docstring" ),
96+ pytest .param (testing_source_a , [], False , 3 , id = "import and docstring" ),
97+ pytest .param (testing_source_b , ['a_function' ], False , 3 , id = "function no __all__" ),
98+ pytest .param (testing_source_c , ['Foo' ], False , 3 , id = "class no __all__" ),
99+ pytest .param (
100+ testing_source_d , ['Foo' , 'a_function' ], False , 3 , id = "function and class no __all__"
101+ ),
102+ pytest .param (
103+ testing_source_e , ['Foo' , 'a_function' ], True , 3 , id = "function and class with __all__"
104+ ),
105+ pytest .param (
106+ testing_source_f , ['Foo' , 'a_function' ],
107+ True ,
108+ 3 ,
109+ id = "function and class with __all__ and extra variable"
110+ ),
111+ pytest .param (testing_source_g , ["a_function" ], False , 3 , id = "async function no __all__" ),
112+ pytest .param (testing_source_h , [], False , 1 , id = "from import" ),
113+ pytest .param (testing_source_i , ["a_function" ], False , 3 , id = "lots of lines" ),
114+ pytest .param (testing_source_j , ["a_function" ], False , 14 , id = "multiline import" ),
115+ ]
116+ )
117+ def test_visitor_endlineno (source , members , found_all , last_import ):
118+ visitor = Visitor (True )
119+ tree = ast .parse (source )
120+ mark_text_ranges (tree , source )
121+ visitor .visit (tree )
122+
123+ assert visitor .members == members
124+ assert visitor .found_all is found_all
125+ assert visitor .last_import is last_import
126+
127+
87128@pytest .mark .parametrize (
88129 "source, members, ret" ,
89130 [
@@ -105,7 +146,6 @@ def test_visitor(source, members, found_all, last_import):
105146 ]
106147 )
107148def test_check_and_add_all (tmpdir , source , members : List [str ], ret ):
108-
109149 tmpfile = PathPlus (tmpdir ) / "source.py"
110150 tmpfile .write_text (source )
111151
@@ -116,6 +156,7 @@ def test_check_and_add_all(tmpdir, source, members: List[str], ret):
116156 assert f"__all__ = [{ members_string } ]" in tmpfile .read_text ()
117157
118158
159+ @pytest .mark .skipif (condition = not (sys .version_info < (3 , 8 )), reason = "Not required after python 3.8" )
119160@pytest .mark .parametrize (
120161 "source, members, ret" ,
121162 [
@@ -131,7 +172,6 @@ def test_check_and_add_all(tmpdir, source, members: List[str], ret):
131172 ]
132173 )
133174def test_check_and_add_all_single_quotes (tmpdir , source , members : List [str ], ret ):
134-
135175 tmpfile = PathPlus (tmpdir ) / "source.py"
136176 tmpfile .write_text (source )
137177
@@ -146,7 +186,6 @@ def test_check_and_add_all_single_quotes(tmpdir, source, members: List[str], ret
146186 pytest .param (mangled_source , [], id = "mangled" ),
147187 ])
148188def test_check_and_add_all_mangled (tmpdir , capsys , source , members ):
149-
150189 tmpfile = PathPlus (tmpdir ) / "source.py"
151190 tmpfile .write_text (source )
152191 assert check_and_add_all (tmpfile ) == 4
0 commit comments