Beispiel #1
0
def test_weave_str_target():
    with aspectlib.weave('test_pkg1.test_pkg2.test_mod.target', mock('foobar')):
        from test_pkg1.test_pkg2.test_mod import target
        assert target() == 'foobar'

    from test_pkg1.test_pkg2.test_mod import target
    assert target() is None
def test_weave_str_target():
    with aspectlib.weave("test_pkg1.test_pkg2.test_mod.target", mock("foobar")):
        from test_pkg1.test_pkg2.test_mod import target

        assert target() == "foobar"

    from test_pkg1.test_pkg2.test_mod import target

    assert target() is None
Beispiel #3
0
def test_weave_module(strmod=None):
    calls = []
    from test_pkg1.test_pkg2 import test_mod
    with aspectlib.weave(strmod or test_mod, record(calls=calls,
                                                    extended=True)):
        test_mod.target()
        obj = test_mod.Stuff()
        obj.meth()
    assert calls == [(None, 'test_pkg1.test_pkg2.test_mod.target', (), {}),
                     (obj, 'test_pkg1.test_pkg2.test_mod.meth', (), {})]
def test_weave_module(strmod=None):
    calls = []
    from test_pkg1.test_pkg2 import test_mod
    with aspectlib.weave(strmod or test_mod, record(calls=calls, extended=True)):
        test_mod.target()
        obj = test_mod.Stuff()
        obj.meth()
    assert calls == [
        (None, 'test_pkg1.test_pkg2.test_mod.target', (), {}),
        (obj, 'test_pkg1.test_pkg2.test_mod.meth', (), {})
    ]
Beispiel #5
0
def test_story_full_play_noproxy_dump():
    with Story(test_mod) as story:
        test_mod.target(123) == 'foobar'
        test_mod.target(1234) ** ValueError

    with story.replay(recurse_lock=True, proxy=False, strict=False, dump=True) as replay:
        raises(AssertionError, test_mod.target)
        assert test_mod.target(123) == 'foobar'
        raises(ValueError, test_mod.target, 1234)

    assert replay.unexpected == ""
Beispiel #6
0
def test_xxx():
    with Story(test_mod) as story:
        obj = test_mod.Stuff(1, 2)
        test_mod.target(1) == 2
        test_mod.target(2) == 3
        test_mod.target(3) ** ValueError
        other = test_mod.Stuff(2, 2)
        obj.other('a') == other
        obj.meth('a') == 'x'
        obj = test_mod.Stuff(2, 3)
        obj.meth() ** ValueError('crappo')
        obj.meth('c') == 'x'

    with story.replay(recurse_lock=True, strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        obj.meth('a')
        test_mod.target(1)
        obj.meth()
        test_mod.func(5)

        obj = test_mod.Stuff(4, 4)
        obj.meth()

    for k, v in story._calls.items():
        print(k, "=>", v)
    print("############## UNEXPECTED ##############")
    for k, v in replay._actual.items():
        print(k, "=>", v)
Beispiel #7
0
def test_story_empty_play_proxy():
    assert test_mod.target() is None
    raises(TypeError, test_mod.target, 123)

    with Story(test_mod).replay(recurse_lock=True, proxy=True, strict=False) as replay:
        assert test_mod.target() is None
        raises(TypeError, test_mod.target, 123)

    assert format_calls(replay._actual) == format_calls(OrderedDict([
        ((None, 'test_pkg1.test_pkg2.test_mod.target', '', ''), _Returns("None")),
        ((None, 'test_pkg1.test_pkg2.test_mod.target', '123', ''), _Raises(repr_ex(TypeError(
            'target() takes no arguments (1 given)' if PY2 else
            'target() takes 0 positional arguments but 1 was given',
        ))))
    ]))
def test_story_empty_play_proxy_class_missing_report():
    with Story(test_mod).replay(recurse_lock=True, proxy=True,
                                strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        obj.mix(3, 4)
        obj.mix('a', 'b')
        raises(ValueError, obj.raises, 123)
        obj = test_mod.Stuff(0, 1)
        obj.mix('a', 'b')
        obj.mix(3, 4)
        test_mod.target()
        raises(ValueError, test_mod.raises, 'badarg')
        raises(ValueError, obj.raises, 123)
        test_mod.ThatLONGStuf(1).mix(2)
        test_mod.ThatLONGStuf(3).mix(4)
        obj = test_mod.ThatLONGStuf(2)
        obj.mix()
        obj.meth()
        obj.mix(10)

    print(repr(replay.diff))

    if PY26:
        assert replay.diff == """--- expected """ """
+++ actual """ """
@@ -1,0 +1,18 @@
+stuff_1 = test_pkg1.test_pkg2.test_mod.Stuff(1, 2)
+stuff_1.mix(3, 4) == (1, 2, 3, 4)  # returns
+stuff_1.mix('a', 'b') == (1, 2, 'a', 'b')  # returns
+stuff_1.raises(123) ** ValueError((123,),)  # raises
+stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(0, 1)
+stuff_2.mix('a', 'b') == (0, 1, 'a', 'b')  # returns
+stuff_2.mix(3, 4) == (0, 1, 3, 4)  # returns
+test_pkg1.test_pkg2.test_mod.target() == None  # returns
+test_pkg1.test_pkg2.test_mod.raises('badarg') ** ValueError(('badarg',),)  # raises
+stuff_2.raises(123) ** ValueError((123,),)  # raises
+that_long_stuf_1 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(1)
+that_long_stuf_1.mix(2) == (1, 2)  # returns
+that_long_stuf_2 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(3)
+that_long_stuf_2.mix(4) == (3, 4)  # returns
+that_long_stuf_3 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(2)
+that_long_stuf_3.mix() == (2,)  # returns
+that_long_stuf_3.meth() == None  # returns
+that_long_stuf_3.mix(10) == (2, 10)  # returns
"""
    else:
        assert replay.diff == """--- expected
def test_story_empty_play_proxy_class_missing_report():
    with Story(test_mod).replay(recurse_lock=True, proxy=True, strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        obj.mix(3, 4)
        obj.mix('a', 'b')
        raises(ValueError, obj.raises, 123)
        obj = test_mod.Stuff(0, 1)
        obj.mix('a', 'b')
        obj.mix(3, 4)
        test_mod.target()
        raises(ValueError, test_mod.raises, 'badarg')
        raises(ValueError, obj.raises, 123)
        test_mod.ThatLONGStuf(1).mix(2)
        test_mod.ThatLONGStuf(3).mix(4)
        obj = test_mod.ThatLONGStuf(2)
        obj.mix()
        obj.meth()
        obj.mix(10)

    print(repr(replay.diff))

    if PY26:
        assert replay.diff == """--- expected """ """
+++ actual """ """
@@ -1,0 +1,18 @@
+stuff_1 = test_pkg1.test_pkg2.test_mod.Stuff(1, 2)
+stuff_1.mix(3, 4) == (1, 2, 3, 4)  # returns
+stuff_1.mix('a', 'b') == (1, 2, 'a', 'b')  # returns
+stuff_1.raises(123) ** ValueError((123,),)  # raises
+stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(0, 1)
+stuff_2.mix('a', 'b') == (0, 1, 'a', 'b')  # returns
+stuff_2.mix(3, 4) == (0, 1, 3, 4)  # returns
+test_pkg1.test_pkg2.test_mod.target() == None  # returns
+test_pkg1.test_pkg2.test_mod.raises('badarg') ** ValueError(('badarg',),)  # raises
+stuff_2.raises(123) ** ValueError((123,),)  # raises
+that_long_stuf_1 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(1)
+that_long_stuf_1.mix(2) == (1, 2)  # returns
+that_long_stuf_2 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(3)
+that_long_stuf_2.mix(4) == (3, 4)  # returns
+that_long_stuf_3 = test_pkg1.test_pkg2.test_mod.ThatLONGStuf(2)
+that_long_stuf_3.mix() == (2,)  # returns
+that_long_stuf_3.meth() == None  # returns
+that_long_stuf_3.mix(10) == (2, 10)  # returns
"""
    else:
        assert replay.diff == """--- expected
def test_story_create():
    with Story(test_mod) as story:
        test_mod.target('a', 'b', 'c') == 'abc'
        test_mod.target()**Exception
        test_mod.target(1, 2, 3) == 'foobar'
        obj = test_mod.Stuff('stuff')
        assert isinstance(obj, test_mod.Stuff)
        obj.meth('other', 1, 2) == 123
        obj.mix('other') == 'mixymix'
    #from pprint import pprint as print
    #print (dict(story._calls))
    assert dict(story._calls) == {
        (None, 'test_pkg1.test_pkg2.test_mod.Stuff', "'stuff'", ''):
        _Binds('stuff_1'),
        ('stuff_1', 'meth', "'other', 1, 2", ''):
        _Returns("123"),
        ('stuff_1', 'mix', "'other'", ''):
        _Returns("'mixymix'"),
        (None, 'test_pkg1.test_pkg2.test_mod.target', '', ''):
        _Raises("Exception"),
        (None, 'test_pkg1.test_pkg2.test_mod.target', "1, 2, 3", ''):
        _Returns("'foobar'"),
        (None, 'test_pkg1.test_pkg2.test_mod.target', "'a', 'b', 'c'", ''):
        _Returns("'abc'"),
    }
Beispiel #11
0
def test_story_text_helpers():
    with Story(test_mod) as story:
        obj = test_mod.Stuff(1, 2)
        obj.meth('a') == 'x'
        obj.meth('b') == 'y'
        obj = test_mod.Stuff(2, 3)
        obj.meth('c') == 'z'
        test_mod.target(1) == 2
        test_mod.target(2) == 3

    with story.replay(recurse_lock=True, strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        obj.meth('a')
        obj.meth()
        obj = test_mod.Stuff(4, 4)
        obj.meth()
        test_mod.func(5)
        test_mod.target(1)

    print (replay.missing)
    assert replay.missing == """stuff_1.meth('b') == 'y'  # returns
stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(2, 3)
stuff_2.meth('c') == 'z'  # returns
test_pkg1.test_pkg2.test_mod.target(2) == 3  # returns
"""
    print (replay.unexpected)
    assert replay.unexpected == """stuff_1.meth() == None  # returns
stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(4, 4)
stuff_2.meth() == None  # returns
test_pkg1.test_pkg2.test_mod.func(5) == None  # returns
"""
    print (replay.diff)
    if PY26:
        assert replay.diff == """--- expected """ """
+++ actual """ """
@@ -1,7 +1,7 @@
 stuff_1 = test_pkg1.test_pkg2.test_mod.Stuff(1, 2)
 stuff_1.meth('a') == 'x'  # returns
-stuff_1.meth('b') == 'y'  # returns
-stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(2, 3)
-stuff_2.meth('c') == 'z'  # returns
+stuff_1.meth() == None  # returns
+stuff_2 = test_pkg1.test_pkg2.test_mod.Stuff(4, 4)
+stuff_2.meth() == None  # returns
+test_pkg1.test_pkg2.test_mod.func(5) == None  # returns
 test_pkg1.test_pkg2.test_mod.target(1) == 2  # returns
-test_pkg1.test_pkg2.test_mod.target(2) == 3  # returns
"""
    else:
        assert replay.diff == """--- expected
def test_story_create():
    with Story(test_mod) as story:
        test_mod.target('a', 'b', 'c') == 'abc'
        test_mod.target() ** Exception
        test_mod.target(1, 2, 3) == 'foobar'
        obj = test_mod.Stuff('stuff')
        assert isinstance(obj, test_mod.Stuff)
        obj.meth('other', 1, 2) == 123
        obj.mix('other') == 'mixymix'
    #from pprint import pprint as print
    #print (dict(story._calls))
    assert dict(story._calls) == {
        (None, 'test_pkg1.test_pkg2.test_mod.Stuff',  "'stuff'", ''): _Binds('stuff_1'),
        ('stuff_1', 'meth', "'other', 1, 2", ''): _Returns("123"),
        ('stuff_1', 'mix', "'other'", ''): _Returns("'mixymix'"),
        (None, 'test_pkg1.test_pkg2.test_mod.target', '', ''): _Raises("Exception"),
        (None, 'test_pkg1.test_pkg2.test_mod.target', "1, 2, 3", ''): _Returns("'foobar'"),
        (None, 'test_pkg1.test_pkg2.test_mod.target', "'a', 'b', 'c'", ''): _Returns("'abc'"),
    }
Beispiel #13
0
def test_story_full_play_proxy():
    with Story(test_mod) as story:
        test_mod.target(123) == 'foobar'
        test_mod.target(1234) ** ValueError

    with story.replay(recurse_lock=True, proxy=True, strict=False) as replay:
        assert test_mod.target() is None
        assert test_mod.target(123) == 'foobar'
        raises(ValueError, test_mod.target, 1234)
        raises(TypeError, test_mod.target, 'asdf')

    assert replay.unexpected == format_calls(OrderedDict([
        ((None, 'test_pkg1.test_pkg2.test_mod.target', '', ''), _Returns("None")),
        ((None, 'test_pkg1.test_pkg2.test_mod.target', "'asdf'", ''), _Raises(repr_ex(TypeError(
            'target() takes no arguments (1 given)'
            if PY2
            else 'target() takes 0 positional arguments but 1 was given',)
        )))
    ]))