示例#1
0
    def test_call_none_is_no_result(self):
        def m1():
            return 1

        def m2():
            return None

        res = _MultiCall([m1, m2], {}, firstresult=True).execute()
        assert res == 1
        res = _MultiCall([m1, m2], {}).execute()
        assert res == [1]
示例#2
0
    def test_call_none_is_no_result(self):
        def m1():
            return 1

        def m2():
            return None

        res = _MultiCall([m1, m2], {}, firstresult=True).execute()
        assert res == 1
        res = _MultiCall([m1, m2], {}).execute()
        assert res == [1]
示例#3
0
    def test_hookwrapper_exception(self, exc):
        l = []

        @hookimpl_opts(hookwrapper=True)
        def m1():
            l.append("m1 init")
            yield None
            l.append("m1 finish")

        def m2():
            raise exc

        with pytest.raises(exc):
            _MultiCall([m2, m1], {}).execute()
        assert l == ["m1 init", "m1 finish"]
示例#4
0
    def test_hookwrapper_exception(self, exc):
        l = []

        @hookimpl_opts(hookwrapper=True)
        def m1():
            l.append("m1 init")
            yield None
            l.append("m1 finish")

        def m2():
            raise exc

        with pytest.raises(exc):
            _MultiCall([m2, m1], {}).execute()
        assert l == ["m1 init", "m1 finish"]
示例#5
0
 def test_uses_copy_of_methods(self):
     l = [lambda: 42]
     mc = _MultiCall(l, {})
     repr(mc)
     l[:] = []
     res = mc.execute()
     return res == 42
示例#6
0
 def test_uses_copy_of_methods(self):
     l = [lambda: 42]
     mc = _MultiCall(l, {})
     repr(mc)
     l[:] = []
     res = mc.execute()
     return res == 42
示例#7
0
    def test_hookwrapper_not_yield(self):
        @hookimpl_opts(hookwrapper=True)
        def m1():
            pass

        mc = _MultiCall([m1], {})
        with pytest.raises(TypeError):
            mc.execute()
示例#8
0
    def test_hookwrapper_not_yield(self):
        @hookimpl_opts(hookwrapper=True)
        def m1():
            pass

        mc = _MultiCall([m1], {})
        with pytest.raises(TypeError):
            mc.execute()
示例#9
0
    def test_hookwrapper_too_many_yield(self):
        @hookimpl_opts(hookwrapper=True)
        def m1():
            yield 1
            yield 2

        mc = _MultiCall([m1], {})
        with pytest.raises(RuntimeError) as ex:
            mc.execute()
        assert "m1" in str(ex.value)
        assert "test_pluggy.py:" in str(ex.value)
示例#10
0
    def test_hookwrapper(self):
        l = []

        @hookimpl_opts(hookwrapper=True)
        def m1():
            l.append("m1 init")
            yield None
            l.append("m1 finish")

        def m2():
            l.append("m2")
            return 2

        res = _MultiCall([m2, m1], {}).execute()
        assert res == [2]
        assert l == ["m1 init", "m2", "m1 finish"]
        l[:] = []
        res = _MultiCall([m2, m1], {}, firstresult=True).execute()
        assert res == 2
        assert l == ["m1 init", "m2", "m1 finish"]
示例#11
0
    def test_hookwrapper(self):
        l = []

        @hookimpl_opts(hookwrapper=True)
        def m1():
            l.append("m1 init")
            yield None
            l.append("m1 finish")

        def m2():
            l.append("m2")
            return 2

        res = _MultiCall([m2, m1], {}).execute()
        assert res == [2]
        assert l == ["m1 init", "m2", "m1 finish"]
        l[:] = []
        res = _MultiCall([m2, m1], {}, firstresult=True).execute()
        assert res == 2
        assert l == ["m1 init", "m2", "m1 finish"]
示例#12
0
    def test_call_subexecute(self):
        def m(__multicall__):
            subresult = __multicall__.execute()
            return subresult + 1

        def n():
            return 1

        call = _MultiCall([n, m], {}, firstresult=True)
        res = call.execute()
        assert res == 2
示例#13
0
    def test_call_subexecute(self):
        def m(__multicall__):
            subresult = __multicall__.execute()
            return subresult + 1

        def n():
            return 1

        call = _MultiCall([n, m], {}, firstresult=True)
        res = call.execute()
        assert res == 2
示例#14
0
    def test_hookwrapper_too_many_yield(self):
        @hookimpl_opts(hookwrapper=True)
        def m1():
            yield 1
            yield 2

        mc = _MultiCall([m1], {})
        with pytest.raises(RuntimeError) as ex:
            mc.execute()
        assert "m1" in str(ex.value)
        assert "test_pluggy.py:" in str(ex.value)
示例#15
0
    def test_keyword_args(self):
        def f(x):
            return x + 1

        class A:
            def f(self, x, y):
                return x + y

        multicall = _MultiCall([f, A().f], dict(x=23, y=24))
        assert "'x': 23" in repr(multicall)
        assert "'y': 24" in repr(multicall)
        reslist = multicall.execute()
        assert reslist == [24 + 23, 24]
        assert "2 results" in repr(multicall)
示例#16
0
    def test_keyword_args(self):
        def f(x):
            return x + 1

        class A:
            def f(self, x, y):
                return x + y

        multicall = _MultiCall([f, A().f], dict(x=23, y=24))
        assert "'x': 23" in repr(multicall)
        assert "'y': 24" in repr(multicall)
        reslist = multicall.execute()
        assert reslist == [24 + 23, 24]
        assert "2 results" in repr(multicall)
示例#17
0
    def test_hookwrapper_order(self):
        l = []

        @hookimpl_opts(hookwrapper=True)
        def m1():
            l.append("m1 init")
            yield 1
            l.append("m1 finish")

        @hookimpl_opts(hookwrapper=True)
        def m2():
            l.append("m2 init")
            yield 2
            l.append("m2 finish")

        res = _MultiCall([m2, m1], {}).execute()
        assert res == []
        assert l == ["m1 init", "m2 init", "m2 finish", "m1 finish"]
示例#18
0
    def test_hookwrapper_order(self):
        l = []

        @hookimpl_opts(hookwrapper=True)
        def m1():
            l.append("m1 init")
            yield 1
            l.append("m1 finish")

        @hookimpl_opts(hookwrapper=True)
        def m2():
            l.append("m2 init")
            yield 2
            l.append("m2 finish")

        res = _MultiCall([m2, m1], {}).execute()
        assert res == []
        assert l == ["m1 init", "m2 init", "m2 finish", "m1 finish"]
示例#19
0
    def test_call_passing(self):
        class P1:
            def m(self, __multicall__, x):
                assert len(__multicall__.results) == 1
                assert not __multicall__.methods
                return 17

        class P2:
            def m(self, __multicall__, x):
                assert __multicall__.results == []
                assert __multicall__.methods
                return 23

        p1 = P1()
        p2 = P2()
        multicall = _MultiCall([p1.m, p2.m], {'x': 23})
        assert "23" in repr(multicall)
        reslist = multicall.execute()
        assert len(reslist) == 2
        # ensure reversed order
        assert reslist == [23, 17]
示例#20
0
    def test_call_passing(self):
        class P1:
            def m(self, __multicall__, x):
                assert len(__multicall__.results) == 1
                assert not __multicall__.methods
                return 17

        class P2:
            def m(self, __multicall__, x):
                assert __multicall__.results == []
                assert __multicall__.methods
                return 23

        p1 = P1()
        p2 = P2()
        multicall = _MultiCall([p1.m, p2.m], {'x': 23})
        assert "23" in repr(multicall)
        reslist = multicall.execute()
        assert len(reslist) == 2
        # ensure reversed order
        assert reslist == [23, 17]
示例#21
0
 def MC(self, methods, kwargs, firstresult=False):
     hookfuncs = []
     for method in methods:
         f = HookImpl(None, "<temp>", method, method.example_impl)
         hookfuncs.append(f)
     return _MultiCall(hookfuncs, kwargs, {"firstresult": firstresult})
示例#22
0
 def test_tags_call_error(self):
     multicall = _MultiCall([lambda x: x], {})
     pytest.raises(KeyError, multicall.execute)
示例#23
0
    def test_keyword_args_with_defaultargs(self):
        def f(x, z=1):
            return x + z

        reslist = _MultiCall([f], dict(x=23, y=24)).execute()
        assert reslist == [24]
示例#24
0
 def test_tags_call_error(self):
     multicall = _MultiCall([lambda x: x], {})
     pytest.raises(KeyError, multicall.execute)
示例#25
0
 def MC(self, methods, kwargs, firstresult=False):
     hookfuncs = []
     for method in methods:
         f = HookImpl(None, "<temp>", method, method.example_impl)
         hookfuncs.append(f)
     return _MultiCall(hookfuncs, kwargs, {"firstresult": firstresult})
示例#26
0
 def test_keyword_args_with_defaultargs(self):
     def f(x, z=1):
         return x + z
     reslist = _MultiCall([f], dict(x=23, y=24)).execute()
     assert reslist == [24]