Beispiel #1
0
def test_story_half_play_proxy_class():
    assert test_mod.Stuff(1, 2).mix(3, 4) == (1, 2, 3, 4)

    with Story(test_mod) as story:
        obj = test_mod.Stuff(1, 2)
        obj.mix(3, 4) == (1, 2, 3, 4)

    with story.replay(recurse_lock=True, proxy=True, strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        assert obj.mix(3, 4) == (1, 2, 3, 4)
        assert obj.meth() is None

        raises(TypeError, obj.meth, 123)

        obj = test_mod.Stuff(0, 1)
        assert obj.mix('a', 'b') == (0, 1, 'a', 'b')
        assert obj.mix(3, 4) == (0, 1, 3, 4)

        raises(TypeError, obj.meth, 123)
    assert replay.unexpected == format_calls(OrderedDict([
        (('stuff_1', 'meth', '', ''), _Returns('None')),
        (('stuff_1', 'meth', '123', ''), _Raises(repr_ex(TypeError(
            'meth() takes exactly 1 argument (2 given)' if PY2 else
            'meth() takes 1 positional argument but 2 were given'
        )))),
        ((None, 'test_pkg1.test_pkg2.test_mod.Stuff', '0, 1', ''), _Binds("stuff_2")),
        (('stuff_2', 'mix', "'a', 'b'", ''), _Returns("(0, 1, 'a', 'b')")),
        (('stuff_2', 'mix',  '3, 4', ''), _Returns('(0, 1, 3, 4)')),
        (('stuff_2', 'meth', '123', ''), _Raises(repr_ex(TypeError(
            'meth() takes exactly 1 argument (2 given)' if PY2 else
            'meth() takes 1 positional argument but 2 were given'
        ))))
    ]))
Beispiel #2
0
def test_story_empty_play_proxy_class():
    assert test_mod.Stuff(1, 2).mix(3, 4) == (1, 2, 3, 4)

    with Story(test_mod).replay(recurse_lock=True, proxy=True, strict=False) as replay:
        obj = test_mod.Stuff(1, 2)
        assert obj.mix(3, 4) == (1, 2, 3, 4)
        assert obj.mix('a', 'b') == (1, 2, 'a', 'b')

        raises(TypeError, obj.meth, 123)

        obj = test_mod.Stuff(0, 1)
        assert obj.mix('a', 'b') == (0, 1, 'a', 'b')
        assert obj.mix(3, 4) == (0, 1, 3, 4)

        raises(TypeError, obj.meth, 123)

    assert format_calls(replay._actual) == format_calls(OrderedDict([
        ((None, 'test_pkg1.test_pkg2.test_mod.Stuff', "1, 2", ''), _Binds('stuff_1')),
        (('stuff_1', 'mix', "3, 4", ''), _Returns("(1, 2, 3, 4)")),
        (('stuff_1', 'mix', "'a', 'b'", ''), _Returns("(1, 2, 'a', 'b')")),
        (('stuff_1', 'meth', "123", ''), _Raises(repr_ex(TypeError(
            'meth() takes exactly 1 argument (2 given)' if PY2 else
            'meth() takes 1 positional argument but 2 were given'
        )))),
        ((None, 'test_pkg1.test_pkg2.test_mod.Stuff', "0, 1", ''), _Binds('stuff_2')),
        (('stuff_2', 'mix', "'a', 'b'", ''), _Returns("(0, 1, 'a', 'b')")),
        (('stuff_2', 'mix', "3, 4", ''), _Returns("(0, 1, 3, 4)")),
        (('stuff_2', 'meth', "123", ''), _Raises(repr_ex(TypeError(
            'meth() takes exactly 1 argument (2 given)' if PY2 else
            'meth() takes 1 positional argument but 2 were given'
        ))))
    ]))
Beispiel #3
0
def test_story_empty_play_error_on_init():
    with Story(test_mod).replay(strict=False) as replay:
        raises(ValueError, test_mod.Stuff, "error")
        print(replay._actual)
    assert replay._actual == OrderedDict([
        ((None, 'test_pkg1.test_pkg2.test_mod.Stuff', "'error'", ''), _Raises('ValueError()'))
    ])
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 #5
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_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 #7
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',)
        )))
    ]))