sphinx-autoapi/tests/python/test_parser.py

135 lines
3.7 KiB
Python
Raw Normal View History

# 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.parser import Parser
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)
def test_parses_basic_file(self):
source = """
def foo(bar):
pass
"""
2017-09-12 17:59:21 +00:00
data = self.parse(source)[0]
2019-01-27 05:20:45 +00:00
self.assertEqual(data["name"], "foo")
self.assertEqual(data["type"], "function")
def test_parses_all(self):
source = """
__all__ = ['Foo', 5.0]
"""
2017-09-12 17:59:21 +00:00
data = self.parse(source)[0]
2019-01-27 05:20:45 +00:00
self.assertEqual(data["name"], "__all__")
self.assertEqual(data["value"], ["Foo", 5.0])
def test_parses_all_multiline(self):
source = """
__all__ = [
'foo',
'bar',
]
"""
2017-09-12 17:59:21 +00:00
data = self.parse(source)[0]
2019-01-27 05:20:45 +00:00
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]
2019-01-27 05:20:45 +00:00
self.assertEqual(data["name"], name)
self.assertEqual(data["value"], value)
2017-09-12 17:59:21 +00:00
def test_parses_nested_list(self):
name = "__all__"
value = [[1, 2], [3, 4]]
source = "{} = {}".format(name, value)
data = self.parse(source)[0]
2019-01-27 05:20:45 +00:00
self.assertEqual(data["name"], name)
self.assertEqual(data["value"], value)
2017-09-12 17:59:21 +00:00
def test_arguments(self):
"""Argument parsing of source"""
2019-01-27 05:20:45 +00:00
source = (
"def foobar(self, bar, baz=42, foo=True,\n"
" *args, **kwargs):\n"
2017-09-12 17:59:21 +00:00
' "This is a docstring"\n'
2019-01-27 05:20:45 +00:00
" return True\n"
2017-09-12 17:59:21 +00:00
)
data = self.parse(source)[0]
2019-01-27 05:20:45 +00:00
self.assertEqual(data["args"], "self, bar, baz=42, foo=True, *args, **kwargs")
2017-09-12 17:59:21 +00:00
def test_advanced_arguments(self):
"""Advanced argument parsing"""
2019-01-27 05:20:45 +00:00
source = (
2017-09-12 17:59:21 +00:00
'def foobar(self, a, b, c=42, d="string", e=(1,2),\n'
' f={"a": True}, g=None, h=[1,2,3,4],\n'
2019-01-27 05:20:45 +00:00
" i=dict(a=True), j=False, *args, **kwargs):\n"
2017-09-12 17:59:21 +00:00
' "This is a docstring"\n'
2019-01-27 05:20:45 +00:00
" return True\n"
2017-09-12 17:59:21 +00:00
)
data = self.parse(source)[0]
self.assertEqual(
2019-01-27 05:20:45 +00:00
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",
]
),
2017-09-12 17:59:21 +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]
2019-01-27 05:20:45 +00:00
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]
2019-01-27 05:20:45 +00:00
self.assertEqual(data["name"], "COLOUR")