Exemplo n.º 1
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'
        ))))
    ]))
Exemplo n.º 2
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'
        ))))
    ]))
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'"),
    }
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'"),
    }