コード例 #1
0
ファイル: test_guard.py プロジェクト: Luftzig/pypatterns
def test_has_attribute_predicate():
    def f(x):
        return x.count(2)
    g = guard(Any.has('count'))(f)
    with pytest.raises(NoMatch):
        g({})
    with pytest.raises(NoMatch):
        g(1)
    assert g((1, 2, 2, 3)) == 2
コード例 #2
0
ファイル: test_guard.py プロジェクト: Luftzig/pypatterns
def test_has_with_predicate():
    X = type('X', (object, ), {'value': None})

    def f(x):
        return x.value
    g = guard(Any.has(value=Any(int)))(f)
    with pytest.raises(NoMatch):
        g('123')
    with pytest.raises(NoMatch):
        x = X()
        x.value = '321'
        g(x)
    x = X()
    x.value = 321
    assert g(x) == 321