def test_implicit_import(self): self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo\n'), {'foo.bar.baz'}), {('foo.bar.baz', 'foo.bar.baz', ('foo', 'foo', 0, 10))}) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.quux\n'), {'foo.bar.baz'}), {('foo.bar.baz', 'foo.bar.baz', ('foo.quux', 'foo.quux', 0, 15))}) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.bar\n'), {'foo.bar.baz'}), {('foo.bar.baz', 'foo.bar.baz', ('foo.bar', 'foo.bar', 0, 14))}) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.bar.quux\n'), {'foo.bar.baz'}), {('foo.bar.baz', 'foo.bar.baz', ('foo.bar.quux', 'foo.bar.quux', 0, 19))}) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.bar.baz.quux\n'), {'foo.bar.baz'}), {('foo.bar.baz', 'foo.bar.baz', ('foo.bar.baz.quux', 'foo.bar.baz.quux', 0, 23))})
def test_implicit_from_import(self): self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'from foo.bar import quux\n'), {'foo.bar.baz'}), set()) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'from foo import bar\n'), {'bar.baz'}), {('foo.bar.baz', 'bar.baz', ('foo.bar', 'bar', 0, 19))})
def test_other_imports(self): self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import bogus\n'), {'foo'}), set()) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo\n'), {'bogus.foo'}), set()) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.bar.baz\n'), {'bogus.foo.bar.baz'}), set())
def test_multiple_imports(self): file_info = util.File('some_file.py', ('import foo\n' 'import foo.bar.baz\n' 'from foo.bar import baz\n' 'import foo.quux\n')) self._assert_localnames( model.localnames_from_localnames(file_info, {'foo.bar.baz'}), {('foo.bar.baz', 'foo.bar.baz', ('foo', 'foo', 0, 10)), ('foo.bar.baz', 'foo.bar.baz', ('foo.bar.baz', 'foo.bar.baz', 11, 29))}) self._assert_localnames( model.localnames_from_localnames(file_info, {'baz'}), {('foo.bar.baz', 'baz', ('foo.bar.baz', 'baz', 30, 53))})
def test_defined_in_this_file(self): self._assert_localnames( model.localnames_from_localnames( util.File( 'foo/bar.py', 'import baz\n' 'def some_function():\n' ' return 1\n'), {'some_function'}), {('foo.bar.some_function', 'some_function', None)})
def test_late_import(self): file_info = util.File('some_file.py', ('def f():\n' ' import foo\n')) self._assert_localnames( model.localnames_from_localnames(file_info, {'foo'}), {('foo', 'foo', ('foo', 'foo', 13, 23))}) self._assert_localnames( model.localnames_from_localnames( file_info, {'foo'}, imports=model.compute_all_imports(file_info)), {('foo', 'foo', ('foo', 'foo', 13, 23))}) self._assert_localnames( model.localnames_from_localnames( file_info, {'foo'}, imports=model.compute_all_imports(file_info, toplevel_only=True)), set())
def test_implicit_as_import(self): self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo as quux\n'), {'quux.bar.baz'}), {('foo.bar.baz', 'quux.bar.baz', ('foo', 'quux', 0, 18))}) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.bar as quux\n'), {'quux.baz'}), {('foo.bar.baz', 'quux.baz', ('foo.bar', 'quux', 0, 22))}) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.bar.quux as bogus\n'), {'foo.bar.baz'}), set()) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'from foo import bar as quux\n'), {'quux.baz'}), {('foo.bar.baz', 'quux.baz', ('foo.bar', 'quux', 0, 27))}) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'from foo.bar import quux as bogus\n'), {'foo.bar.baz'}), set()) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.bar.baz.quux as bogus\n'), {'foo.bar.baz'}), set())
def test_as_import(self): self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo as bar\n'), {'bar'}), {('foo', 'bar', ('foo', 'bar', 0, 17))}) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.bar.baz as quux\n'), {'quux'}), {('foo.bar.baz', 'quux', ('foo.bar.baz', 'quux', 0, 26))}) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'from foo.bar import baz as quux\n'), {'quux'}), {('foo.bar.baz', 'quux', ('foo.bar.baz', 'quux', 0, 31))}) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo as bar\n'), {'foo'}), set()) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.bar.baz as quux\n'), {'foo.bar.baz'}), set()) self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'from foo.bar import baz as quux\n'), {'foo.bar.baz'}), set())
def test_within_node(self): file_info = util.File('some_file.py', ('import bar\n\n\n' 'def f():\n' ' import foo as bar\n')) def_node = file_info.tree.body[1] self._assert_localnames( model.localnames_from_localnames(file_info, {'bar'}), {('bar', 'bar', ('bar', 'bar', 0, 10)), ('foo', 'bar', ('foo', 'bar', 26, 43))}) self._assert_localnames( model.localnames_from_localnames( file_info, {'bar'}, imports=model.compute_all_imports(file_info)), {('bar', 'bar', ('bar', 'bar', 0, 10)), ('foo', 'bar', ('foo', 'bar', 26, 43))}) self._assert_localnames( model.localnames_from_localnames(file_info, {'bar'}, imports=model.compute_all_imports( file_info, within_node=def_node)), {('foo', 'bar', ('foo', 'bar', 26, 43))})
def test_with_context(self): self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', ('# import foo as bar\n' 'import os\n' 'import sys\n' '\n' 'import bogus\n' 'import foo\n' '\n' 'def bogus():\n' ' return 1\n')), {'foo'}), {('foo', 'foo', ('foo', 'foo', 55, 65))})
def test_with_dots(self): self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo.bar.baz\n'), {'foo.bar.baz'}), {('foo.bar.baz', 'foo.bar.baz', ('foo.bar.baz', 'foo.bar.baz', 0, 18))})
def test_simple(self): self._assert_localnames( model.localnames_from_localnames( util.File('some_file.py', 'import foo\n'), {'foo'}), {('foo', 'foo', ('foo', 'foo', 0, 10))})