2017-04-07 18:46:57 +00:00
|
|
|
# coding=utf8
|
|
|
|
|
|
|
|
"""Test Python parser"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
from textwrap import dedent
|
|
|
|
|
2017-09-12 17:59:21 +00:00
|
|
|
import astroid
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from autoapi.mappers.python import Parser
|
2017-04-07 18:46:57 +00:00
|
|
|
|
|
|
|
if sys.version_info < (3, 0):
|
|
|
|
from StringIO import StringIO
|
|
|
|
else:
|
|
|
|
from io import StringIO
|
|
|
|
|
|
|
|
|
|
|
|
class PythonParserTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def parse(self, source):
|
2017-09-12 17:59:21 +00:00
|
|
|
node = astroid.extract_node(source)
|
|
|
|
return Parser().parse(node)
|
2017-04-07 18:46:57 +00:00
|
|
|
|
|
|
|
def test_parses_basic_file(self):
|
|
|
|
source = """
|
|
|
|
def foo(bar):
|
|
|
|
pass
|
|
|
|
"""
|
2017-09-12 17:59:21 +00:00
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(data['name'], 'foo')
|
|
|
|
self.assertEqual(data['type'], 'function')
|
2017-04-07 18:46:57 +00:00
|
|
|
|
|
|
|
def test_parses_all(self):
|
|
|
|
source = """
|
|
|
|
__all__ = ['Foo', 5.0]
|
|
|
|
"""
|
2017-09-12 17:59:21 +00:00
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(data['name'], '__all__')
|
|
|
|
self.assertEqual(data['value'], ['Foo', 5.0])
|
2017-04-07 18:46:57 +00:00
|
|
|
|
2017-09-12 17:59:21 +00:00
|
|
|
@pytest.mark.xfail(reason="Cannot parse list additions")
|
2017-04-07 18:46:57 +00:00
|
|
|
def test_parses_all_with_list_addition(self):
|
|
|
|
source = """
|
|
|
|
__all__ = ['Foo'] + []
|
|
|
|
"""
|
2017-09-12 17:59:21 +00:00
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(data['name'], '__all__')
|
|
|
|
self.assertEqual(data['value'], ['Foo'])
|
2017-04-07 18:46:57 +00:00
|
|
|
|
2017-09-12 17:59:21 +00:00
|
|
|
@pytest.mark.xfail(reason="Cannot parse list additions")
|
2017-04-07 18:46:57 +00:00
|
|
|
def test_parses_all_with_name_addtion(self):
|
|
|
|
source = """
|
|
|
|
__all__ = ['Foo'] + bar.__all__
|
|
|
|
"""
|
2017-09-12 17:59:21 +00:00
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(data['name'], '__all__')
|
|
|
|
self.assertEqual(data['value'], ['Foo'])
|
2017-04-07 18:46:57 +00:00
|
|
|
|
2017-09-12 17:59:21 +00:00
|
|
|
@pytest.mark.xfail(reason="Cannot parse list additions")
|
2017-04-07 18:46:57 +00:00
|
|
|
def test_parses_all_with_multiple_name_addtions(self):
|
|
|
|
source = """
|
|
|
|
__all__ = foo + bar
|
|
|
|
__all__ += boop
|
|
|
|
__all__ += ['foo']
|
|
|
|
"""
|
2017-09-12 17:59:21 +00:00
|
|
|
data = self.parse(source)
|
|
|
|
self.assertEqual(data['name'], '__all__')
|
|
|
|
self.assertEqual(data['value'], ['foo'])
|
2017-04-07 18:46:57 +00:00
|
|
|
source = """
|
|
|
|
__all__ = ['foo']
|
|
|
|
__all__ = foo
|
|
|
|
"""
|
2017-09-12 17:59:21 +00:00
|
|
|
data = self.parse(source)
|
|
|
|
self.assertEqual(data['name'], '__all__')
|
|
|
|
self.assertEqual(data['value'], [])
|
2017-04-07 18:46:57 +00:00
|
|
|
|
|
|
|
def test_parses_all_multiline(self):
|
|
|
|
source = """
|
|
|
|
__all__ = [
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
]
|
|
|
|
"""
|
2017-09-12 17:59:21 +00:00
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(data['value'], ['foo', 'bar'])
|
2017-04-07 22:25:34 +00:00
|
|
|
|
2017-09-12 17:59:21 +00:00
|
|
|
@pytest.mark.xfail(reason="Cannot parse list additions")
|
2017-04-07 22:25:34 +00:00
|
|
|
def test_parses_all_generator(self):
|
|
|
|
source = """
|
|
|
|
__all__ = [x for x in dir(token) if x[0] != '_'] + ['foo', 'bar']
|
|
|
|
"""
|
2017-09-12 17:59:21 +00:00
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(data['value'], ['foo', 'bar'])
|
2017-04-07 22:25:34 +00:00
|
|
|
|
|
|
|
def test_parses_name(self):
|
|
|
|
source = "foo.bar"
|
2017-09-12 17:59:21 +00:00
|
|
|
self.assertEqual(self.parse(source), {})
|
2017-04-07 22:25:34 +00:00
|
|
|
|
|
|
|
def test_parses_list(self):
|
2017-09-12 17:59:21 +00:00
|
|
|
name = "__all__"
|
|
|
|
value = [1, 2, 3, 4]
|
|
|
|
source = "{} = {}".format(name, value)
|
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(data['name'], name)
|
|
|
|
self.assertEqual(data['value'], value)
|
|
|
|
|
|
|
|
def test_parses_nested_list(self):
|
|
|
|
name = "__all__"
|
|
|
|
value = [[1, 2], [3, 4]]
|
|
|
|
source = "{} = {}".format(name, value)
|
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(data['name'], name)
|
|
|
|
self.assertEqual(data['value'], value)
|
|
|
|
|
|
|
|
def test_arguments(self):
|
|
|
|
"""Argument parsing of source"""
|
|
|
|
source=(
|
|
|
|
'def foobar(self, bar, baz=42, foo=True,\n'
|
|
|
|
' *args, **kwargs):\n'
|
|
|
|
' "This is a docstring"\n'
|
|
|
|
' return True\n'
|
|
|
|
)
|
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(
|
|
|
|
data['args'],
|
|
|
|
'self, bar, baz=42, foo=True, *args, **kwargs'
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_advanced_arguments(self):
|
|
|
|
"""Advanced argument parsing"""
|
|
|
|
source=(
|
|
|
|
'def foobar(self, a, b, c=42, d="string", e=(1,2),\n'
|
|
|
|
' f={"a": True}, g=None, h=[1,2,3,4],\n'
|
|
|
|
' i=dict(a=True), j=False, *args, **kwargs):\n'
|
|
|
|
' "This is a docstring"\n'
|
|
|
|
' return True\n'
|
|
|
|
)
|
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(
|
|
|
|
data['args'],
|
|
|
|
', '.join([
|
|
|
|
'self',
|
|
|
|
'a',
|
|
|
|
'b',
|
|
|
|
'c=42',
|
|
|
|
'd=\'string\'',
|
|
|
|
'e=(1, 2)',
|
|
|
|
'f={\'a\': True}',
|
|
|
|
'g=None',
|
|
|
|
'h=[1, 2, 3, 4]',
|
|
|
|
'i=dict(a=True)',
|
|
|
|
'j=False',
|
|
|
|
'*args',
|
|
|
|
'**kwargs',
|
|
|
|
])
|
|
|
|
)
|
2018-08-02 03:13:50 +00:00
|
|
|
|
|
|
|
def test_dict_key_assignment(self):
|
|
|
|
"""Ignore assignment to dictionary entries."""
|
|
|
|
source = """
|
|
|
|
MY_DICT = {} #@
|
|
|
|
if condition:
|
|
|
|
MY_DICT['key'] = 'value'
|
|
|
|
MY_DICT['key2'] = 'value2'
|
|
|
|
"""
|
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(data['name'], 'MY_DICT')
|
|
|
|
|
|
|
|
def test_list_index_assignment(self):
|
|
|
|
"""Ignore assignment to indexes."""
|
|
|
|
source = """
|
|
|
|
COLOUR = [255, 128, 0] #@
|
|
|
|
if condition:
|
|
|
|
COLOUR[1] = 255
|
|
|
|
COLOUR[2] = 255
|
|
|
|
"""
|
|
|
|
data = self.parse(source)[0]
|
|
|
|
self.assertEqual(data['name'], 'COLOUR')
|