Ejemplo n.º 1
0
def test_import_parser():
    for i, source_ucl in enumerate((
            source_unicode_literals1,
            source_unicode_literals2,
            source_unicode_literals3,
            source_unicode_literals4,
            source_unicode_literals5,
            source_unicode_literals6,
            ), 1):
        module = parse(StringIO(source_ucl), 'file_ucl{}.py'.format(i))

        assert Module('file_ucl{}.py'.format(i), _, 1,
                      _, _, None, _, _,
                      _, {'unicode_literals': True}, '') == module
        assert module.future_imports['unicode_literals']

    for i, source_mfi in enumerate((
            source_multiple_future_imports1,
            source_multiple_future_imports2,
            source_multiple_future_imports3,
            source_multiple_future_imports4,
            source_multiple_future_imports5,
            source_multiple_future_imports6,
            source_multiple_future_imports7,
            ), 1):
        module = parse(StringIO(source_mfi), 'file_mfi{}.py'.format(i))
        assert Module('file_mfi{}.py'.format(i), _, 1,
                      _, _, None, _, _,
                      _, {'unicode_literals': True, 'nested_scopes': True},
                      '') == module
        assert module.future_imports['unicode_literals']

    # These are invalid syntax, so there is no need to verify the result
    for i, source_ucli in enumerate((
            source_future_import_invalid1,
            source_future_import_invalid2,
            source_future_import_invalid3,
            source_future_import_invalid4,
            source_future_import_invalid5,
            source_future_import_invalid6,
            source_future_import_invalid7,
            source_future_import_invalid8,
            source_token_error,
            source_invalid_syntax,
            ), 1):
        module = parse(StringIO(source_ucli), 'file_invalid{}.py'.format(i))

        assert Module('file_invalid{}.py'.format(i), _, 1,
                      _, _, None, _, _,
                      _, _, '') == module
Ejemplo n.º 2
0
def test_parser():
    dunder_all = ('a', 'bc')
    module = parse(StringIO(source), 'file.py')
    assert len(list(module)) == 8
    assert Module('file.py', _, 1, len(source.split('\n')),
                  _, '"""Module."""', _, _, dunder_all, {}, '') == \
        module

    function, class_ = module.children
    assert Function('function', _, _, _, _, '"Function."', _,
                    module, '') == function
    assert Class('class_', _, _, _, _, '"""Class."""', _, module, '') == class_

    nested_1, nested_2 = function.children
    assert NestedFunction('nested_1', _, _, _, _,
                          '"""Nested."""', _, function, '') == nested_1
    assert NestedFunction('nested_2', _, _, _, _, None, _,
                          function, '') == nested_2
    assert nested_1.is_public is False

    method_1, method_2 = class_.children
    assert method_1.parent == method_2.parent == class_
    assert Method('method_1', _, _, _, _, '"""Method."""', _,
                  class_, '') == method_1
    assert Method('method_2', _, _, _, _, None, _, class_, '') == method_2

    nested_3, = method_2.children
    assert NestedFunction('nested_3', _, _, _, _,
                          '"""Nested."""', _, method_2, '') == nested_3
    assert nested_3.module == module
    assert nested_3.all == dunder_all

    module = parse(StringIO(source_alt), 'file_alt.py')
    assert Module('file_alt.py', _, 1, len(source_alt.split('\n')),
                  _, None, _, _, dunder_all, {}, '') == module

    module = parse(StringIO(source_alt_nl_at_bracket), 'file_alt_nl.py')
    assert Module('file_alt_nl.py', _, 1,
                  len(source_alt_nl_at_bracket.split('\n')), _, None, _, _,
                  dunder_all, {}, '') == module

    with pytest.raises(AllError):
        parse(StringIO(source_complex_all), 'file_complex_all.py')
Ejemplo n.º 3
0
def _test_module():

    module = Module(source, 'module.py')
    assert module.source == source
    assert module.parent is None
    assert module.name == 'module'
    assert module.docstring == '"""Module docstring."""'
    assert module.is_public

    function, = module.children
    assert function.source.startswith('def function')
    assert function.source.endswith('pass\n')
    assert function.parent is module
    assert function.name == 'function'
    assert function.docstring == '"""Function docstring."""'