1"""Testing file unit test.""" 2 3import sys 4import warnings 5import numpy as np 6 7import dipy.testing as dt 8import numpy.testing as npt 9 10 11def test_assert(): 12 npt.assert_raises(AssertionError, dt.assert_false, True) 13 npt.assert_raises(AssertionError, dt.assert_true, False) 14 npt.assert_raises(AssertionError, dt.assert_less, 2, 1) 15 npt.assert_raises(AssertionError, dt.assert_less_equal, 2, 1) 16 npt.assert_raises(AssertionError, dt.assert_greater, 1, 2) 17 npt.assert_raises(AssertionError, dt.assert_greater_equal, 1, 2) 18 npt.assert_raises(AssertionError, dt.assert_not_equal, 5, 5) 19 npt.assert_raises(AssertionError, dt.assert_operator, 2, 1) 20 21 arr = [np.arange(k) for k in range(2, 12, 3)] 22 arr2 = [np.arange(k) for k in range(2, 12, 4)] 23 npt.assert_raises(AssertionError, dt.assert_arrays_equal, arr, arr2) 24 25 26def assert_warn_len_equal(mod, n_in_context): 27 mod_warns = mod.__warningregistry__ 28 # Python 3 appears to clear any pre-existing warnings of the same type, 29 # when raising warnings inside a catch_warnings block. So, there is a 30 # warning generated by the tests within the context manager, but no 31 # previous warnings. 32 if 'version' in mod_warns: 33 # including 'version' 34 npt.assert_equal(len(mod_warns), 2) 35 else: 36 npt.assert_equal(len(mod_warns), n_in_context) 37 38 39def test_clear_and_catch_warnings(): 40 warnings.simplefilter("default", category=UserWarning) 41 # Initial state of module, no warnings 42 my_mod = sys.modules[__name__] 43 try: 44 my_mod.__warningregistry__.clear() 45 except AttributeError: 46 pass 47 48 npt.assert_equal(getattr(my_mod, '__warningregistry__', {}), {}) 49 with dt.clear_and_catch_warnings(modules=[my_mod]): 50 warnings.simplefilter('ignore') 51 warnings.warn('Some warning') 52 npt.assert_equal(my_mod.__warningregistry__, {}) 53 # Without specified modules, don't clear warnings during context 54 with dt.clear_and_catch_warnings(): 55 warnings.warn('Some warning') 56 assert_warn_len_equal(my_mod, 1) 57 # Confirm that specifying module keeps old warning, does not add new 58 with dt.clear_and_catch_warnings(modules=[my_mod]): 59 warnings.warn('Another warning') 60 assert_warn_len_equal(my_mod, 1) 61 # Another warning, no module spec does add to warnings dict, except on 62 # Python 3 (see comments in `assert_warn_len_equal`) 63 with dt.clear_and_catch_warnings(): 64 warnings.warn('Another warning') 65 assert_warn_len_equal(my_mod, 2) 66 warnings.simplefilter("always", category=UserWarning) 67 68 69if __name__ == '__main__': 70 test_clear_and_catch_warnings() 71 test_assert() 72