Exemple #1
0
 def __init__(self, stdout=True, stderr=True):
     self.string_io = io.StringIO()
     
     if stdout:
         self._stdout_temp_setter = \
             TempValueSetter((sys, 'stdout'), self.string_io)
     else: # not stdout
         self._stdout_temp_setter = BlankContextManager()
         
     if stderr:
         self._stderr_temp_setter = \
             TempValueSetter((sys, 'stderr'), self.string_io)
     else: # not stderr
         self._stderr_temp_setter = BlankContextManager()
Exemple #2
0
def test_dict_key():
    '''Test `TempValueSetter` with variable inputted as `(dict, key)`.'''
    a = {1: 2}

    assert a[1] == 2
    with TempValueSetter((a, 1), 'meow'):
        assert a[1] == 'meow'
    assert a[1] == 2

    b = {}

    assert sum not in b
    with TempValueSetter((b, sum), 7):
        assert b[sum] == 7
    assert sum not in b
Exemple #3
0
def test_setter_getter():
    '''Test `TempValueSetter` with variable inputted as `(getter, setter)`.'''
    a = Object()
    a.x = 1
    getter = lambda: getattr(a, 'x')
    setter = lambda value: setattr(a, 'x', value)

    assert a.x == 1
    with TempValueSetter((getter, setter), 2):
        assert a.x == 2
    assert a.x == 1
Exemple #4
0
def test_simple():
    '''
    Test `TempValueSetter` with variable inputted as `(obj, attribute_name)`.
    '''
    a = Object()
    a.x = 1

    assert a.x == 1
    with TempValueSetter((a, 'x'), 2):
        assert a.x == 2
    assert a.x == 1
Exemple #5
0
def test_active():
    a = Object()
    a.x = 1

    assert a.x == 1
    temp_value_setter = TempValueSetter((a, 'x'), 2)
    assert not temp_value_setter.active
    with temp_value_setter:
        assert a.x == 2
        assert temp_value_setter.active
    assert not temp_value_setter.active
    assert a.x == 1
def test_function_in_main():
    '''Test that a function defined in `__main__` is well-described.'''

    ###########################################################################
    # We can't really define a function in `__main__` in this test, so we
    # emulate it:
    with TempValueSetter((globals(), '__name__'), '__main__'):
        def f(x):
            pass
        
        # Accessing `f.__module__` here so PyPy will calculate it:
        assert f.__module__ == '__main__'
        
    assert f.__module__ == '__main__'
    import __main__
    __main__.f = f
    del __main__
    #
    ###########################################################################
    
    assert describe(f) == '__main__.f'
    assert resolve(describe(f)) is f