#! /usr/bin/env python # $Id: test_parser.py 6495 2010-12-07 10:42:53Z milde $ # Author: David Goodger # Copyright: This module has been placed in the public domain. """ Tests for docutils/readers/python/moduleparser.py. """ from __init__ import DocutilsTestSupport def suite(): s = DocutilsTestSupport.PythonModuleParserTestSuite() s.generateTests(totest) return s totest = {} totest['module'] = [ ['''\ ''', '''\ '''], ['''\ """docstring""" ''', '''\ docstring '''], ['''\ u"""Unicode docstring""" ''', '''\ Unicode docstring '''], ['''\ """docstring""" """additional docstring""" ''', '''\ docstring additional docstring '''], ['''\ """docstring""" # comment """additional docstring""" ''', '''\ docstring additional docstring '''], ['''\ """docstring""" 1 """not an additional docstring""" ''', '''\ docstring '''], ] totest['import'] = [ ['''\ import module ''', '''\ module '''], ['''\ import module as local ''', '''\ module local '''], ['''\ import module.name ''', '''\ module.name '''], ['''\ import module.name as local ''', '''\ module.name local '''], ['''\ import module """not documentable""" ''', '''\ module '''], ] totest['from'] = [ ['''\ from module import name ''', '''\ module name '''], ['''\ from module import name as local ''', '''\ module name local '''], ['''\ from module import name1, name2 as local2 ''', '''\ module name1 name2 local2 '''], ['''\ from module.sub import name ''', '''\ module.sub name '''], ['''\ from module.sub import name as local ''', '''\ module.sub name local '''], ['''\ from module import * ''', '''\ module * '''], ['''\ from __future__ import division ''', '''\ __future__ division '''], ] totest['assign'] = [ ['''\ a = 1 ''', '''\ a 1 '''], ['''a = 1''', '''\ a 1 '''], ['''\ a = 1 """a docstring""" ''', #" '''\ a 1 a docstring '''], ['''\ a = 1 """a docstring""" """additional docstring""" ''', #" '''\ a 1 a docstring additional docstring '''], #' ['''\ a = 1 + 2 * 3 / 4 ** 5 ''', '''\ a 1 + 2 * 3 / 4 ** 5 '''], ['''\ a = 1 \\ + 2 ''', '''\ a 1 + 2 '''], ['''\ a = not 1 and 2 or 3 ''', '''\ a not 1 and 2 or 3 '''], ['''\ a = ~ 1 & 2 | 3 ^ 4 ''', '''\ a ~ 1 & 2 | 3 ^ 4 '''], ['''\ a = `1 & 2` ''', '''\ a `1 & 2` '''], ['''\ very_long_name = \\ x ''', '''\ very_long_name x '''], ['''\ very_long_name \\ = x ''', '''\ very_long_name x '''], ['''\ very_long_name = \\ another_long_name = \\ x ''', '''\ very_long_name x another_long_name x '''], ['''\ a = (1 + 2) b = a.b[1 + fn(x, y, z, {'key': (1 + 2 + 3)})][4] c = """first line second line third""" ''', '''\ a (1 + 2) b a.b[1 + fn(x, y, z, {'key': (1 + 2 + 3)})][4] c """first line second line third""" '''], ['''\ a, b, c = range(3) (d, e, f) = a, b, c g, h, i = j = a, b, c k.a, k.b.c, k.d.e.f = a, b, c ''', '''\ a b c range(3) d e f a, b, c g h i a, b, c j a, b, c k.a k.b.c k.d.e.f a, b, c '''], ['''\ a = 1 ; b = 2 print ; c = 3 ''', '''\ a 1 b 2 c 3 '''], ['''\ a.b = 1 """This assignment is noted but ignored unless ``a`` is a function.""" ''', '''\ a.b 1 This assignment is noted but ignored unless ``a`` is a function. '''], ['''\ a[b] = 1 """Subscript assignments are ignored.""" ''', '''\ '''], ['''\ a = foo(b=1) ''', '''\ a foo(b=1) '''], # ['''\ # a = 1 # # """Because of the blank above, this is a module docstring.""" # ''', # '''\ # # # # a # # 1 # # Because of the blank above, this is a module docstring. # '''], ] totest['def'] = [ ['''\ def f(): """Function f docstring""" """Additional docstring""" local = 1 """Not a docstring, since ``local`` is local.""" ''', # " '''\ f Function f docstring Additional docstring '''], # ' ['''\ def f(a, b): local = 1 ''', '''\ f a b '''], ['''\ def f(a=None, b=1): local = 1 ''', '''\ f a None b 1 '''], ['''\ def f(a, (b, c, d)=range(3), e=None): local = 1 ''', '''\ f a b c d range(3) e None '''], ['''\ def f(*args): local = 1 ''', '''\ f args '''], ['''\ def f(**kwargs): local = 1 ''', '''\ f kwargs '''], ['''\ def f(a, b=None, *args, **kwargs): local = 1 ''', '''\ f a b None args kwargs '''], ['''\ def f(): pass f.attrib = 1 """f.attrib docstring""" ''', # " # @@@ When should the attribute move inside the Function? '''\ f f.attrib 1 f.attrib docstring '''], ['''\ def f(): def g(): pass """Not a docstring""" local = 1 ''', '''\ f '''], ] totest['class'] = [ ['''\ class C: """class C docstring""" ''', '''\ C class C docstring '''], ['''\ class C(Super): pass class D(SuperD, package.module.SuperD): pass ''', '''\ C Super D SuperD package.module.SuperD '''], ['''\ class C: class D: pass """Not a docstring""" ''', '''\ C '''], ['''\ class C: def f(self): self.local = 1 local = 1 ''', '''\ C f self '''], ['''\ class C: def __init__(self): self.local = 1 local = 1 ''', '''\ C __init__ self self.local 1 local 1 '''], ['''\ class C: def __init__(self): local = foo(a=1) ''', '''\ C __init__ self local foo(a=1) '''], ] totest['ignore'] = [ ['''\ 1 + 2 ''', '''\ '''], ['''\ del a ''', '''\ '''], ] totest['comments'] = [ # ['''\ # # Comment # ''', # '''\ # # # # Comment # '''], ] # @@@ we don't parse comments yet totest['everything'] = [ ['''\ # comment """Docstring""" """Additional docstring""" __docformat__ = 'reStructuredText' a = 1 """Attribute docstring""" class C(Super): """C docstring""" class_attribute = 1 """class_attribute docstring""" def __init__(self, text=None): """__init__ docstring""" self.instance_attribute = (text * 7 + ' whaddyaknow') """instance_attribute docstring""" def f(x, # parameter x y=a*5, # parameter y *args): # parameter args """f docstring""" return [x + item for item in args] f.function_attribute = 1 """f.function_attribute docstring""" ''', '''\ Docstring Additional docstring __docformat__ 'reStructuredText' a 1 Attribute docstring C Super C docstring class_attribute 1 class_attribute docstring __init__ __init__ docstring self text None self.instance_attribute (text * 7 + ' whaddyaknow') instance_attribute docstring f f docstring x y a * 5 args f.function_attribute 1 f.function_attribute docstring '''], ] """ ['''\ ''', '''\ '''], """ if __name__ == '__main__': import unittest unittest.main(defaultTest='suite')