Пример #1
0
def test_pretty_print_any():
    a = Any()
    assert repr(a) == "Any()"
    assert str(a) == "Any()"
    b = Any(str)
    assert repr(b) == "Any(<type 'str'>)" or repr(b) == "Any(<class 'str'>)"
    assert str(b) == "Any(<type 'str'>)" or str(b) == "Any(<class 'str'>)"
Пример #2
0
def test_when_with_any():
    mock_fn = Mock()
    when(mock_fn).called_with(Any()).then(sentinel.result1)
    when(mock_fn).called_with(sentinel.arg1, Any()).then(sentinel.result2)
    when(mock_fn).called_with(sentinel.arg2, Any(list)).then(sentinel.result3)
    when(mock_fn).called_with(sentinel.arg2, Any(str)).then(sentinel.result4)

    assert mock_fn(sentinel.arg1) == sentinel.result1
    assert mock_fn(sentinel.arg2) == sentinel.result1
    assert mock_fn("hello") == sentinel.result1
    assert mock_fn(100) == sentinel.result1
    assert mock_fn(sentinel.arg1, "hello") == sentinel.result2
    assert mock_fn(sentinel.arg1, "world") == sentinel.result2
    assert mock_fn(sentinel.arg2, []) == sentinel.result3
    assert mock_fn(sentinel.arg2, [1, 2, 3]) == sentinel.result3
    assert mock_fn(sentinel.arg2, ["hello", "world"]) == sentinel.result3
    assert mock_fn(sentinel.arg2, "world") == sentinel.result4
Пример #3
0
def test_most_general_last():
    mock = Mock()
    when(mock).called_with(100, 200).then("monkey")
    when(mock).called_with(100, Any()).then("hello")

    assert 'monkey' == mock(100, 200)
    assert 'hello' == mock(100, 300)
    assert 'hello' == mock(100, "monkey")
    assert 'hello' == mock(100, {"key": 1000})
Пример #4
0
def test_pretty_print_any_such_that():
    def my_predicate(x):
        return True

    a = Any()
    aa = a.such_that(my_predicate).such_that(lambda: True)
    assert repr(aa) == repr(a) + ".such_that(my_predicate).such_that(<lambda>)"
    assert str(aa) == str(a) + ".such_that(my_predicate).such_that(<lambda>)"

    class Predicate(object):
        @classmethod
        def __call__(cls, x):
            return True

    b = Any(str)
    bb = b.such_that(Predicate)
    assert repr(bb) == repr(b) + ".such_that(Predicate)"
    assert str(bb) == str(b) + ".such_that(Predicate)"
Пример #5
0
def test_any_such_that_equality(predicates, matches, not_matches):
    matcher = functools.reduce(lambda m, p: m.such_that(p), predicates, Any())

    for match in matches:
        assert matcher == match
        assert match == matcher

    for not_match in not_matches:
        assert matcher != not_match
        assert not_match != matcher
Пример #6
0
def test_any_such_that_not_mutated():
    """Ensure that `such_that` operates on a new copy of the matcher instead
    of mutating the old one.
    """
    a = Any(str).such_that(lambda s: s[0] == "a")
    b = a.such_that(lambda s: s[1] == "b")
    c = a.such_that(lambda s: s[1] == "c")

    assert b == "able"
    assert "able" == b
    assert b is not a

    assert c == "act"
    assert "act" == c
    assert c is not a
Пример #7
0
def test_stub_arg_matching():
    mock_fn = Mock()
    mock_fn.side_effect = stub(
        (call(), sentinel.res0), (call(sentinel.arg1), sentinel.res1),
        (call(datetime(1978, 2, 2, 12, 34, 56)), sentinel.res2),
        (call(Any()), sentinel.res3),
        (call(x=sentinel.argx, y=sentinel.argy), sentinel.res4),
        (call(x=sentinel.argx, y=datetime(1978, 2, 2, 12, 34, 56)),
         sentinel.res5), (call(x=sentinel.argx, y=Any()), sentinel.res6))

    assert mock_fn() == sentinel.res0
    assert mock_fn(sentinel.arg1) == sentinel.res1
    assert mock_fn(Any()) == sentinel.res1
    assert mock_fn(Any(datetime)) == sentinel.res2
    assert mock_fn(datetime(1978, 2, 2, 12, 34, 56)) == sentinel.res2
    assert mock_fn(datetime(1978, 2, 2, 12, 45, 00)) == sentinel.res3
    assert mock_fn(sentinel.meh) == sentinel.res3
    assert mock_fn(x=sentinel.argx, y=sentinel.argy) == sentinel.res4
    assert mock_fn(x=sentinel.argx, y=datetime(1978, 2, 2, 12, 34,
                                               56)) == sentinel.res5
    assert mock_fn(x=sentinel.argx, y=Any()) == sentinel.res4
    assert mock_fn(x=sentinel.argx, y=Any(datetime)) == sentinel.res5
    assert mock_fn(x=sentinel.argx, y=sentinel.meh) == sentinel.res6
Пример #8
0
def test_any_such_that_equality_typed():
    assert Any(str).such_that(lambda x: len(x) > 3) == "hello"
    assert "hello" == Any(str).such_that(lambda x: len(x) > 3)

    assert Any(str).such_that(lambda x: len(x) > 3) != ["h", "e", "l", "l", "o"]
    assert ["h", "e", "l", "l", "o"] != Any(str).such_that(lambda x: len(x) > 3)
Пример #9
0
def test_any_equality():
    assert Any() == "hello"
    assert "hello" == Any()

    assert Any() == []
    assert [] == Any()

    assert Any() == 100
    assert 100 == Any()

    assert Any() == []
    assert [] == Any()

    assert Any() == 100
    assert 100 == Any()

    assert Any(str) == "hello"
    assert "hello" == Any(str)

    assert Any(list) != "hello"
    assert "hello" != Any(list)

    assert Any(list) == []
    assert [] == Any(list)