Ejemplo n.º 1
0
    def test_multiple_replace(self):

        def test_y(self):
            return 'test y'

        def test_z():
            return 'test z'

        compare(sample1.z(),'original z')
        compare(sample1.X().y(),'original y')

        @replace('testfixtures.tests.sample1.z',test_z)
        @replace('testfixtures.tests.sample1.X.y',test_y)
        def test_something(passed_test_y,passed_test_z):
            compare(test_z,passed_test_z)
            compare(test_y,passed_test_y)
            compare(sample1.z(),'test z')
            compare(sample1.X().y(),'test y')

        compare(sample1.z(),'original z')
        compare(sample1.X().y(),'original y')

        test_something()

        compare(sample1.z(),'original z')
        compare(sample1.X().y(),'original y')
Ejemplo n.º 2
0
    def test_multiple_replace(self):
        def test_y(self):
            return 'test y'

        def test_z():
            return 'test z'

        compare(sample1.z(), 'original z')
        compare(sample1.X().y(), 'original y')

        @replace('testfixtures.tests.sample1.z', test_z)
        @replace('testfixtures.tests.sample1.X.y', test_y)
        def test_something(passed_test_y, passed_test_z):
            compare(test_z, passed_test_z)
            compare(test_y, passed_test_y)
            compare(sample1.z(), 'test z')
            compare(sample1.X().y(), 'test y')

        compare(sample1.z(), 'original z')
        compare(sample1.X().y(), 'original y')

        test_something()

        compare(sample1.z(), 'original z')
        compare(sample1.X().y(), 'original y')
Ejemplo n.º 3
0
    def test_replace_context_manager(self):
        def test_z():
            return 'replacement z'

        compare(sample1.z(), 'original z')

        with Replace('testfixtures.tests.sample1.z', test_z) as z:
            compare(z(), 'replacement z')
            compare(sample1.z(), 'replacement z')

        compare(sample1.z(), 'original z')
Ejemplo n.º 4
0
    def test_replace_context_manager(self):
        def test_z():
            return 'replacement z'

        compare(sample1.z(), 'original z')

        with Replace('testfixtures.tests.sample1.z', test_z) as z:
            compare(z(), 'replacement z')
            compare(sample1.z(), 'replacement z')

        compare(sample1.z(), 'original z')
Ejemplo n.º 5
0
    def test_with_statement(self):
        from testfixtures.tests import sample1
        assert sample1.z() == 'original z'

        def test_z():
          return 'replacement z'

        with Replacer() as r:
            r.replace('testfixtures.tests.sample1.z',test_z)
            assert sample1.z() == 'replacement z'

        assert sample1.z() == 'original z'
Ejemplo n.º 6
0
    def test_remove_called_twice(self):
        from testfixtures.tests import sample1

        def test_z(): pass

        r = Replacer()
        r.replace('testfixtures.tests.sample1.z',test_z)

        r.restore()
        assert sample1.z() == 'original z'

        r.restore()
        assert sample1.z() == 'original z'
Ejemplo n.º 7
0
    def test_use_as_cleanup(self):
        def test_z():
            return 'replacement z'

        compare(sample1.z(), 'original z')
        replace = Replacer()
        compare(sample1.z(), 'original z')
        replace('testfixtures.tests.sample1.z', test_z)
        cleanup = replace.restore
        try:
            compare(sample1.z(), 'replacement z')
        finally:
            cleanup()
        compare(sample1.z(), 'original z')
Ejemplo n.º 8
0
    def test_use_as_cleanup(self):
        def test_z():
            return 'replacement z'

        compare(sample1.z(), 'original z')
        replace = Replacer()
        compare(sample1.z(), 'original z')
        replace('testfixtures.tests.sample1.z', test_z)
        cleanup = replace.restore
        try:
            compare(sample1.z(), 'replacement z')
        finally:
            cleanup()
        compare(sample1.z(), 'original z')
Ejemplo n.º 9
0
    def test_function(self):

        def test_z():
            return 'replacement z'

        compare(sample1.z(), 'original z')

        @replace('testfixtures.tests.sample1.z', test_z)
        def test_something():
            compare(sample1.z(), 'replacement z')

        compare(sample1.z(), 'original z')
        test_something()
        compare(sample1.z(), 'original z')
Ejemplo n.º 10
0
    def test_function(self):
        from testfixtures.tests import sample1
        assert sample1.z() == 'original z'

        def test_z():
          return 'replacement z'

        r = Replacer()
        r.replace('testfixtures.tests.sample1.z',test_z)

        assert sample1.z() == 'replacement z'

        r.restore()

        assert sample1.z() == 'original z'
Ejemplo n.º 11
0
    def test_function(self):

        def test_z():
            return 'replacement z'

        compare(sample1.z(), 'original z')

        @replace('testfixtures.tests.sample1.z',test_z)
        def test_something():
            compare(sample1.z(), 'replacement z')

        compare(sample1.z(), 'original z')

        test_something()

        compare(sample1.z(), 'original z')
Ejemplo n.º 12
0
    def test_raises(self):

        def test_z():
            return 'replacement z'

        compare(sample1.z(), 'original z')

        @replace('testfixtures.tests.sample1.z', test_z)
        def test_something():
            compare(sample1.z(), 'replacement z')
            raise Exception()

        compare(sample1.z(), 'original z')
        with ShouldRaise():
            test_something()
        compare(sample1.z(), 'original z')
Ejemplo n.º 13
0
    def test_raises(self):

        def test_z():
            return 'replacement z'

        compare(sample1.z(),'original z')

        @replace('testfixtures.tests.sample1.z',test_z)
        def test_something():
            compare(sample1.z(),'replacement z')
            raise Exception()

        compare(sample1.z(),'original z')

        should_raise(test_something)()

        compare(sample1.z(),'original z')
Ejemplo n.º 14
0
    def test_gotcha(self):

        def test_z():
            return 'test z'

        compare(sample1.z(), 'original z')
        compare(sample2.z(), 'original z')

        @replace('testfixtures.tests.sample1.z', test_z)
        def test_something():
            compare(sample1.z(), 'test z')
            compare(sample2.z(), 'original z')

        compare(sample1.z(), 'original z')
        compare(sample2.z(), 'original z')
        test_something()
        compare(sample1.z(), 'original z')
        compare(sample2.z(), 'original z')
Ejemplo n.º 15
0
    def test_gotcha(self):
        # Just because you replace an object in one context:

        from testfixtures.tests import sample1
        from testfixtures.tests import sample2
        assert sample1.z() == 'original z'

        def test_z():
          return 'replacement z'

        r = Replacer()
        r.replace('testfixtures.tests.sample1.z',test_z)

        assert sample1.z() == 'replacement z'

        # Doesn't meant that it's replaced in all contexts:

        assert sample2.z() == 'original z'

        r.restore()
Ejemplo n.º 16
0
    def test_multiple_replace(self):
        from testfixtures.tests import sample1
        assert sample1.z() == 'original z'
        assert sample1.X().y() == 'original y'

        def test_y(self):
          return self.__class__.__name__
        def test_z():
          return 'replacement z'

        r = Replacer()
        r.replace('testfixtures.tests.sample1.z',test_z)
        r.replace('testfixtures.tests.sample1.X.y',test_y)

        assert sample1.z() == 'replacement z'
        assert sample1.X().y() == 'X'

        r.restore()

        assert sample1.z() == 'original z'
        assert sample1.X().y() == 'original y'
Ejemplo n.º 17
0
    def test_multiple_context_managers(self):
        def test_y(self):
            return 'test y'

        def test_z():
            return 'test z'

        compare(sample1.z(), 'original z')
        compare(sample1.X().y(), 'original y')

        with Replacer() as replace:
            z = replace('testfixtures.tests.sample1.z', test_z)
            y = replace('testfixtures.tests.sample1.X.y', test_y)
            compare(z(), 'test z')
            if PY3:
                compare(y, sample1.X.y)
            compare(sample1.X().y(), 'test y')
            compare(sample1.z(), 'test z')
            compare(sample1.X().y(), 'test y')

        compare(sample1.z(), 'original z')
        compare(sample1.X().y(), 'original y')
Ejemplo n.º 18
0
    def test_gotcha(self):
        # Just because you replace an object in one context,
        # doesn't meant that it's replaced in all contexts!

        def test_z():
            return 'test z'

        compare(sample1.z(), 'original z')
        compare(sample2.z(), 'original z')

        @replace('testfixtures.tests.sample1.z', test_z)
        def test_something():
            compare(sample1.z(), 'test z')
            compare(sample2.z(), 'original z')

        compare(sample1.z(), 'original z')
        compare(sample2.z(), 'original z')

        test_something()

        compare(sample1.z(), 'original z')
        compare(sample2.z(), 'original z')
Ejemplo n.º 19
0
    def test_gotcha(self):
        # Just because you replace an object in one context,
        # doesn't meant that it's replaced in all contexts!

        def test_z():
            return 'test z'
        
        compare(sample1.z(),'original z')
        compare(sample2.z(),'original z')
        
        @replace('testfixtures.tests.sample1.z',test_z)
        def test_something():
            compare(sample1.z(),'test z')
            compare(sample2.z(),'original z')
            
        compare(sample1.z(),'original z')
        compare(sample2.z(),'original z')
    
        test_something()

        compare(sample1.z(),'original z')
        compare(sample2.z(),'original z')
Ejemplo n.º 20
0
    def test_multiple_context_managers(self):

        def test_y(self):
            return 'test y'

        def test_z():
            return 'test z'

        compare(sample1.z(), 'original z')
        compare(sample1.X().y(), 'original y')

        with Replacer() as replace:
            z = replace('testfixtures.tests.sample1.z', test_z)
            y = replace('testfixtures.tests.sample1.X.y', test_y)
            compare(z(), 'test z')
            if PY3:
                compare(y, sample1.X.y)
            compare(sample1.X().y(), 'test y')
            compare(sample1.z(), 'test z')
            compare(sample1.X().y(), 'test y')

        compare(sample1.z(), 'original z')
        compare(sample1.X().y(), 'original y')
Ejemplo n.º 21
0
 def test_something():
     compare(sample1.z(), 'replacement z')
     raise Exception()
Ejemplo n.º 22
0
 def test_something():
     compare(sample1.z(), 'test z')
     compare(sample2.z(), 'original z')
Ejemplo n.º 23
0
 def test_something(passed_test_y,passed_test_z):
     compare(test_z,passed_test_z)
     compare(test_y,passed_test_y)
     compare(sample1.z(),'test z')
     compare(sample1.X().y(),'test y')
Ejemplo n.º 24
0
 def test_something():
     compare(sample1.z(),'test z')
     compare(sample2.z(),'original z')
Ejemplo n.º 25
0
 def test_something():
     compare(sample1.z(), 'replacement z')
Ejemplo n.º 26
0
 def test_something():
     compare(sample1.z(),'replacement z')
     raise Exception()
Ejemplo n.º 27
0
 def test_something(passed_test_y, passed_test_z):
     compare(test_z, passed_test_z)
     compare(test_y, passed_test_y)
     compare(sample1.z(), 'test z')
     compare(sample1.X().y(), 'test y')
Ejemplo n.º 28
0
 def test_something():
     compare(sample1.z(), 'replacement z')