def test_tripwire(): # Test tripwire object silly_module_name = TripWire('We do not have silly_module_name') npt.assert_raises(TripWireError, getattr, silly_module_name, 'do_silly_thing') npt.assert_raises(TripWireError, silly_module_name) # Check AttributeError can be checked too try: silly_module_name.__wrapped__ except TripWireError as err: assert_true(isinstance(err, AttributeError)) else: raise RuntimeError("No error raised, but expected")
def test_skipper(): def f(): pass docstring = \ """ Header >>> something # skip if not HAVE_AMODULE >>> something + else >>> a = 1 # skip if not HAVE_BMODULE >>> something2 # skip if HAVE_AMODULE """ f.__doc__ = docstring f2 = doctest_skip_parser(f) assert_true(f is f2) npt.assert_equal( f2.__doc__, """ Header >>> something # doctest: +SKIP >>> something + else >>> a = 1 >>> something2 """) global HAVE_AMODULE, HAVE_BMODULE HAVE_AMODULE = True HAVE_BMODULE = False f.__doc__ = docstring f2 = doctest_skip_parser(f) assert_true(f is f2) npt.assert_equal( f2.__doc__, """ Header >>> something >>> something + else >>> a = 1 # doctest: +SKIP >>> something2 # doctest: +SKIP """) del HAVE_AMODULE f.__doc__ = docstring npt.assert_raises(NameError, doctest_skip_parser, f)
def test_deprecate_with_version(): def func_no_doc(): pass def func_doc(i): "A docstring" def func_doc_long(i, j): "A docstring\n\n Some text" class CustomError(Exception): """ Custom error class for testing expired deprecation errors """ my_mod = sys.modules[__name__] dec = deprecate_with_version func = dec('foo')(func_no_doc) with clear_and_catch_warnings(modules=[my_mod]) as w: warnings.simplefilter('always') npt.assert_equal(func(), None) npt.assert_equal(len(w), 1) assert_true(w[0].category is DeprecationWarning) npt.assert_equal(func.__doc__, 'foo\n') func = dec('foo')(func_doc) with clear_and_catch_warnings(modules=[my_mod]) as w: warnings.simplefilter('always') npt.assert_equal(func(1), None) npt.assert_equal(len(w), 1) npt.assert_equal(func.__doc__, 'A docstring\n\nfoo\n') func = dec('foo')(func_doc_long) with clear_and_catch_warnings(modules=[my_mod]) as w: warnings.simplefilter('always') npt.assert_equal(func(1, 2), None) npt.assert_equal(len(w), 1) npt.assert_equal(func.__doc__, 'A docstring\n \n foo\n \n Some text\n') # Try some since and until versions func = dec('foo', '0.2')(func_no_doc) npt.assert_equal(func.__doc__, 'foo\n\n* deprecated from version: 0.2\n') with clear_and_catch_warnings(modules=[my_mod]) as w: warnings.simplefilter('always') npt.assert_equal(func(), None) npt.assert_equal(len(w), 1) func = dec('foo', until='0.6')(func_no_doc) with clear_and_catch_warnings(modules=[my_mod]) as w: warnings.simplefilter('always') npt.assert_equal(func(), None) npt.assert_equal(len(w), 1) npt.assert_equal(func.__doc__, 'foo\n\n* Will raise {} as of version: 0.6\n' .format(ExpiredDeprecationError)) func = dec('foo', until='0.3')(func_no_doc) npt.assert_raises(ExpiredDeprecationError, func) npt.assert_equal(func.__doc__, 'foo\n\n* Raises {} as of version: 0.3\n' .format(ExpiredDeprecationError)) func = dec('foo', '0.2', '0.3')(func_no_doc) npt.assert_raises(ExpiredDeprecationError, func) npt.assert_equal(func.__doc__, 'foo\n\n* deprecated from version: 0.2\n' '* Raises {} as of version: 0.3\n' .format(ExpiredDeprecationError)) func = dec('foo', '0.2', '0.3')(func_doc_long) npt.assert_equal(func.__doc__, 'A docstring\n \n foo\n \n' ' * deprecated from version: 0.2\n' ' * Raises {} as of version: 0.3\n \n' ' Some text\n' .format(ExpiredDeprecationError)) npt.assert_raises(ExpiredDeprecationError, func) # Check different warnings and errors func = dec('foo', warn_class=UserWarning)(func_no_doc) with clear_and_catch_warnings(modules=[my_mod]) as w: warnings.simplefilter('always') npt.assert_equal(func(), None) npt.assert_equal(len(w), 1) assert_true(w[0].category is UserWarning) func = dec('foo', error_class=CustomError)(func_no_doc) with clear_and_catch_warnings(modules=[my_mod]) as w: warnings.simplefilter('always') npt.assert_equal(func(), None) npt.assert_equal(len(w), 1) assert_true(w[0].category is DeprecationWarning) func = dec('foo', until='0.3', error_class=CustomError)(func_no_doc) npt.assert_raises(CustomError, func)
def test_is_tripwire(): assert_false(is_tripwire(object())) assert_true(is_tripwire(TripWire('some message'))) assert_false(is_tripwire(ValueError('some message')))