Skip to content

Commit c3bda5e

Browse files
committed
Test scoped matching
1 parent 9b38f21 commit c3bda5e

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

tests/test_analysis.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,57 @@ def test_common_known_types(self, search_name, import_path):
265265
assert type_name == search_name.split(".")[-1]
266266
assert py_import is not None
267267
assert py_import.from_ == import_path
268+
269+
def test_scoped_types(self, module_factory):
270+
types = {
271+
"sub.module:January": PyImport(implicit="sub.module:January"),
272+
}
273+
matcher = TypeMatcher(types=types)
274+
275+
# Shouldn't match because the current module isn't set
276+
type_name, py_import = matcher.match("January")
277+
assert type_name is None
278+
assert py_import is None
279+
280+
# Set current module to something that doesn't match scope
281+
module_path = module_factory(src="", module_name="other.module")
282+
matcher.current_file = module_path
283+
# Still shouldn't match because the current module doesn't match the scope
284+
type_name, py_import = matcher.match("January")
285+
assert type_name is None
286+
assert py_import is None
287+
288+
# Set current module to match the scope
289+
module_path = module_factory(src="", module_name="sub.module")
290+
matcher.current_file = module_path
291+
# Now we should find the type
292+
type_name, py_import = matcher.match("January")
293+
assert type_name == "January"
294+
assert py_import == PyImport(implicit="sub.module:January")
295+
296+
def test_scoped_type_prefix(self, module_factory):
297+
type_prefixes = {
298+
"sub.module:cal": PyImport(implicit="sub.module:cal"),
299+
}
300+
matcher = TypeMatcher(type_prefixes=type_prefixes)
301+
302+
# Shouldn't match because the current module isn't set
303+
type_name, py_import = matcher.match("cal.January")
304+
assert type_name is None
305+
assert py_import is None
306+
307+
# Set current module to something that doesn't match scope
308+
module_path = module_factory(src="", module_name="other.module")
309+
matcher.current_file = module_path
310+
# Still shouldn't match because the current module doesn't match the scope
311+
type_name, py_import = matcher.match("cal.January")
312+
assert type_name is None
313+
assert py_import is None
314+
315+
# Set current module to match the scope
316+
module_path = module_factory(src="", module_name="sub.module")
317+
matcher.current_file = module_path
318+
# Now we should find the prefix
319+
type_name, py_import = matcher.match("cal.January")
320+
assert type_name == "cal.January"
321+
assert py_import == PyImport(implicit="sub.module:cal")

0 commit comments

Comments
 (0)