Skip to content

Commit

Permalink
Conditionalize nested test for py2
Browse files Browse the repository at this point in the history
__qualname__ didn't exist before python 3.3. Skip checks that depend on it if running in earlier Python versions
  • Loading branch information
jvansanten authored and stefanseefeld committed Sep 18, 2024
1 parent 301256c commit 95e5301
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions test/nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
>>> X.__name__
'X'
>>> X.Y
>>> X.Y # doctest: +py2
<class 'nested_ext.Y'>
>>> X.Y # doctest: +py3
<class 'nested_ext.X.Y'>
>>> X.Y.__module__
Expand All @@ -22,16 +25,22 @@
>>> X.Y.__name__
'Y'
>>> X.color.__qualname__
>>> getattr(X.color, "__qualname__", None) # doctest: +py3
'X.color'
>>> repr(X.color.red)
>>> repr(X.color.red) # doctest: +py2
'nested_ext.color.red'
>>> repr(X.color.red) # doctest: +py3
'nested_ext.X.color.red'
>>> repr(X.color(1))
>>> repr(X.color(1)) # doctest: +py2
'nested_ext.color(1)'
>>> repr(X.color(1)) # doctest: +py3
'nested_ext.X.color(1)'
>>> test_function.__doc__.strip().split('\\n')[0]
>>> test_function.__doc__.strip().split('\\n')[0] # doctest: +py3
'test_function( (X)arg1, (X.Y)arg2) -> None :'
'''
Expand All @@ -42,7 +51,23 @@ def run(args = None):

if args is not None:
sys.argv = args
return doctest.testmod(sys.modules.get(__name__))

py2 = doctest.register_optionflag("py2")
py3 = doctest.register_optionflag("py3")

class ConditionalChecker(doctest.OutputChecker):
def check_output(self, want, got, optionflags):
if (optionflags & py3) and (sys.version_info[0] < 3):
return True
if (optionflags & py2) and (sys.version_info[0] >= 3):
return True
return doctest.OutputChecker.check_output(self, want, got, optionflags)

runner = doctest.DocTestRunner(ConditionalChecker())
for test in doctest.DocTestFinder().find(sys.modules.get(__name__)):
runner.run(test)

return doctest.TestResults(runner.failures, runner.tries)

if __name__ == '__main__':
print("running...")
Expand Down

0 comments on commit 95e5301

Please sign in to comment.