|
| 1 | +# Copyright 2009-2011 Ram Rachum. |
| 2 | +# This program is distributed under the LGPL2.1 license. |
| 3 | + |
| 4 | +'''Testing pacakge for `SysModulesUnchangedAssertor`.''' |
| 5 | + |
| 6 | +from __future__ import with_statement |
| 7 | + |
| 8 | +import sys |
| 9 | +import uuid |
| 10 | + |
| 11 | +import nose |
| 12 | + |
| 13 | +from garlicsim.general_misc import cute_testing |
| 14 | +from garlicsim.general_misc import module_tasting |
| 15 | +from garlicsim.general_misc.module_tasting.sys_modules_unchanged_assertor \ |
| 16 | + import SysModulesUnchangedAssertor |
| 17 | + |
| 18 | +class MyException(Exception): |
| 19 | + pass |
| 20 | + |
| 21 | + |
| 22 | +def test_without_change(): |
| 23 | + ''' |
| 24 | + Test that `SysModulesUnchangedAssertor` doesn't raise exception needlessly. |
| 25 | + ''' |
| 26 | + with SysModulesUnchangedAssertor(): |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +def test_with_change(): |
| 31 | + '''Test `SysModulesUnchangedAssertor` raises exception when needed.''' |
| 32 | + with SysModulesUnchangedAssertor(): |
| 33 | + random_key = str(uuid.uuid4()) |
| 34 | + with cute_testing.RaiseAssertor(exception_type=RuntimeError, |
| 35 | + text=' we expected `sys.modules` to ' |
| 36 | + 'be unchanged', |
| 37 | + assert_exact_type=True): |
| 38 | + with SysModulesUnchangedAssertor(): |
| 39 | + sys.modules[uuid] = None |
| 40 | + |
| 41 | + assert uuid in sys.modules |
| 42 | + del sys.modules[uuid] |
| 43 | + |
| 44 | + |
| 45 | +def test_error_propagation_without_change(): |
| 46 | + ''' |
| 47 | + Test that `SysModulesUnchangedAssertor` propagates any exception. |
| 48 | + |
| 49 | + This tests when `sys.modules` was not changed. |
| 50 | + ''' |
| 51 | + with cute_testing.RaiseAssertor(exception_type=MyException, |
| 52 | + text='meow', |
| 53 | + assert_exact_type=True): |
| 54 | + with SysModulesUnchangedAssertor(): |
| 55 | + raise MyException('meow') |
| 56 | + |
| 57 | + |
| 58 | +def test_error_propagation_with_change(): |
| 59 | + ''' |
| 60 | + Test that `SysModulesUnchangedAssertor` propagates any exception. |
| 61 | + |
| 62 | + This tests when `sys.modules` was changed. |
| 63 | + ''' |
| 64 | + with SysModulesUnchangedAssertor(): |
| 65 | + random_key = str(uuid.uuid4()) |
| 66 | + with cute_testing.RaiseAssertor(exception_type=MyException, |
| 67 | + text='frrr', |
| 68 | + assert_exact_type=True): |
| 69 | + with SysModulesUnchangedAssertor(): |
| 70 | + sys.modules[uuid] = None |
| 71 | + raise MyException('frrr') |
| 72 | + |
| 73 | + |
| 74 | + assert uuid in sys.modules |
| 75 | + del sys.modules[uuid] |
| 76 | + |
0 commit comments