/delete_history/pymodules/python2.7/lib/python/ipython-2.2.0-py2.7.egg/IPython/testing/tests/test_decorators.py

https://gitlab.com/pooja043/Globus_Docker_3 · Python · 169 lines · 137 code · 7 blank · 25 comment · 4 complexity · 5a19e9f23aca2a8fc6717ea34d06bb9c MD5 · raw file

  1. """Tests for the decorators we've created for IPython.
  2. """
  3. from __future__ import print_function
  4. # Module imports
  5. # Std lib
  6. import inspect
  7. import sys
  8. # Third party
  9. import nose.tools as nt
  10. # Our own
  11. from IPython.testing import decorators as dec
  12. from IPython.testing.skipdoctest import skip_doctest
  13. #-----------------------------------------------------------------------------
  14. # Utilities
  15. # Note: copied from OInspect, kept here so the testing stuff doesn't create
  16. # circular dependencies and is easier to reuse.
  17. def getargspec(obj):
  18. """Get the names and default values of a function's arguments.
  19. A tuple of four things is returned: (args, varargs, varkw, defaults).
  20. 'args' is a list of the argument names (it may contain nested lists).
  21. 'varargs' and 'varkw' are the names of the * and ** arguments or None.
  22. 'defaults' is an n-tuple of the default values of the last n arguments.
  23. Modified version of inspect.getargspec from the Python Standard
  24. Library."""
  25. if inspect.isfunction(obj):
  26. func_obj = obj
  27. elif inspect.ismethod(obj):
  28. func_obj = obj.__func__
  29. else:
  30. raise TypeError('arg is not a Python function')
  31. args, varargs, varkw = inspect.getargs(func_obj.__code__)
  32. return args, varargs, varkw, func_obj.__defaults__
  33. #-----------------------------------------------------------------------------
  34. # Testing functions
  35. @dec.as_unittest
  36. def trivial():
  37. """A trivial test"""
  38. pass
  39. @dec.skip
  40. def test_deliberately_broken():
  41. """A deliberately broken test - we want to skip this one."""
  42. 1/0
  43. @dec.skip('Testing the skip decorator')
  44. def test_deliberately_broken2():
  45. """Another deliberately broken test - we want to skip this one."""
  46. 1/0
  47. # Verify that we can correctly skip the doctest for a function at will, but
  48. # that the docstring itself is NOT destroyed by the decorator.
  49. @skip_doctest
  50. def doctest_bad(x,y=1,**k):
  51. """A function whose doctest we need to skip.
  52. >>> 1+1
  53. 3
  54. """
  55. print('x:',x)
  56. print('y:',y)
  57. print('k:',k)
  58. def call_doctest_bad():
  59. """Check that we can still call the decorated functions.
  60. >>> doctest_bad(3,y=4)
  61. x: 3
  62. y: 4
  63. k: {}
  64. """
  65. pass
  66. def test_skip_dt_decorator():
  67. """Doctest-skipping decorator should preserve the docstring.
  68. """
  69. # Careful: 'check' must be a *verbatim* copy of the doctest_bad docstring!
  70. check = """A function whose doctest we need to skip.
  71. >>> 1+1
  72. 3
  73. """
  74. # Fetch the docstring from doctest_bad after decoration.
  75. val = doctest_bad.__doc__
  76. nt.assert_equal(check,val,"doctest_bad docstrings don't match")
  77. # Doctest skipping should work for class methods too
  78. class FooClass(object):
  79. """FooClass
  80. Example:
  81. >>> 1+1
  82. 2
  83. """
  84. @skip_doctest
  85. def __init__(self,x):
  86. """Make a FooClass.
  87. Example:
  88. >>> f = FooClass(3)
  89. junk
  90. """
  91. print('Making a FooClass.')
  92. self.x = x
  93. @skip_doctest
  94. def bar(self,y):
  95. """Example:
  96. >>> ff = FooClass(3)
  97. >>> ff.bar(0)
  98. boom!
  99. >>> 1/0
  100. bam!
  101. """
  102. return 1/y
  103. def baz(self,y):
  104. """Example:
  105. >>> ff2 = FooClass(3)
  106. Making a FooClass.
  107. >>> ff2.baz(3)
  108. True
  109. """
  110. return self.x==y
  111. def test_skip_dt_decorator2():
  112. """Doctest-skipping decorator should preserve function signature.
  113. """
  114. # Hardcoded correct answer
  115. dtargs = (['x', 'y'], None, 'k', (1,))
  116. # Introspect out the value
  117. dtargsr = getargspec(doctest_bad)
  118. assert dtargsr==dtargs, \
  119. "Incorrectly reconstructed args for doctest_bad: %s" % (dtargsr,)
  120. @dec.skip_linux
  121. def test_linux():
  122. nt.assert_false(sys.platform.startswith('linux'),"This test can't run under linux")
  123. @dec.skip_win32
  124. def test_win32():
  125. nt.assert_not_equal(sys.platform,'win32',"This test can't run under windows")
  126. @dec.skip_osx
  127. def test_osx():
  128. nt.assert_not_equal(sys.platform,'darwin',"This test can't run under osx")