Skip to content

Commit 69a89c0

Browse files
Add additional identification test cases
1 parent 8b83d56 commit 69a89c0

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

tests/unit/test_identify.py

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,49 @@
55

66

77
def imports_in_code(code: str, **kwargs) -> List[identify.Import]:
8-
return list(identify.imports(StringIO(code, **kwargs)))
8+
return list(identify.imports(StringIO(code), **kwargs))
99

1010

11-
def test_yield_edge_cases():
11+
def test_top_only():
12+
imports_in_function = """
13+
import abc
14+
15+
def xyz():
16+
import defg
17+
"""
18+
assert len(imports_in_code(imports_in_function)) == 2
19+
assert len(imports_in_code(imports_in_function, top_only=True)) == 1
20+
21+
imports_after_class = """
22+
import abc
23+
24+
class MyObject:
25+
pass
26+
27+
import defg
28+
"""
29+
assert len(imports_in_code(imports_after_class)) == 2
30+
assert len(imports_in_code(imports_after_class, top_only=True)) == 1
31+
32+
33+
def test_top_doc_string():
34+
assert (
35+
len(
36+
imports_in_code(
37+
'''
38+
#! /bin/bash import x
39+
"""import abc
40+
from y import z
41+
"""
42+
import abc
43+
'''
44+
)
45+
)
46+
== 1
47+
)
48+
49+
50+
def test_yield_and_raise_edge_cases():
1251
assert not imports_in_code(
1352
"""
1453
raise SomeException("Blah") \\
@@ -137,3 +176,25 @@ def generator_function():
137176
from \\
138177
"""
139178
)
179+
assert (
180+
len(
181+
imports_in_code(
182+
"""
183+
def generator_function():
184+
(
185+
(
186+
((((
187+
(((((
188+
((
189+
(((
190+
raise \\
191+
from \\
192+
import c
193+
194+
import abc
195+
import xyz
196+
"""
197+
)
198+
)
199+
== 2
200+
)

0 commit comments

Comments
 (0)