def test_user_statement_on_import(): """github #285""" s = "from datetime import (\n" \ " time)" for pos in [(2, 1), (2, 4)]: u = Parser(s, user_position=pos).user_stmt assert isinstance(u, pr.Import) assert u.defunct == False assert [str(n) for n in u.get_defined_names()] == ['time']
def test_module(): module = Parser('asdf', 'example.py', no_docstr=True).module name = module.name assert str(name) == 'example' assert name.start_pos == (0, 0) assert name.end_pos == (0, 0) module = Parser('asdf', no_docstr=True).module name = module.name assert str(name) == '' assert name.start_pos == (0, 0) assert name.end_pos == (0, 0)
def test_get_code(): """Use the same code that the parser also generates, to compare""" s = \ '''"""a docstring""" class SomeClass(object, mixin): def __init__(self): self.xy = 3.0 'statement docstr' def some_method(self): return 1 def yield_method(self): while hasattr(self, 'xy'): yield True for x in [1, 2]: yield x def empty(self): pass class Empty: pass class WithDocstring: """class docstr""" pass def method_with_docstring(): """class docstr""" pass ''' assert Parser(s).module.get_code() == s
def test_dont_break_imports_without_namespaces(): """ The code checking for ``from __future__ import absolute_import`` shouldn't assume that all imports have non-``None`` namespaces. """ src = "from __future__ import absolute_import\nimport xyzzy" parser = Parser(src, "test.py") assert parser.module.has_explicit_absolute_import
def test_explicit_absolute_imports(): """ Detect modules with ``from __future__ import absolute_import``. """ parser = Parser("from __future__ import absolute_import", "test.py") assert parser.module.has_explicit_absolute_import
def get_import(self, source): return Parser(source).module.imports[0]
def get_sub(self, source): return Parser(source).module.subscopes[0]
def get_call(self, source): stmt = Parser(source, no_docstr=True).module.statements[0] return stmt.get_commands()[0]