Beispiel #1
0
def test_is_none_or():
    assert Optionally is IsNoneOr
    assert [None, 1] == All(Optionally(1))
    assert [None, 1] == [IsNoneOr(1), IsNoneOr(LessThan(0), EqualTo(1))]
    assert not [2] == [IsNoneOr(1)]

    assert str([IsNoneOr('1')]) == "[IsNoneOr('1')]"
Beispiel #2
0
def test_dict_contains_all_of():
    test_input = {'a': [1, 2, 3], 'b': 4, 'c': 'extra'}

    def dict_to_match() -> Dict[Any, Any]:
        return {
            'a': And(HasLength(3), All(IsInstance(int))),
            'b': 4,
            'd': NotPresent
        }

    assert not test_input == dict_to_match()
    assert test_input == DictContainsAllOf(dict_to_match())

    assert not test_input == DictContainsAllOf({'a': [1]})
    assert not test_input == DictContainsAllOf({
        'a': [1, 2, 3],
        'b': 4,
        'c': 'extra',
        'e': 'not there'
    })

    assert (
        str(
            DictContainsAllOf({
                'a': And(HasLength(3), All(IsInstance(int))),
                'b': 4,
                'd': NotPresent
            })) ==
        "DictContainsAllOf({'a': And(HasLength(3), All(IsInstance(int))), 'b': 4, 'd': NotPresent})"
    )
Beispiel #3
0
def test_last():
    assert 'abcdef' == Last(3)('def')
    assert [1, 2, 3, 'a', 'b', 'c'] == Last(3)(All(IsInstance(str)))
    assert [1, 2, 3, 'a', 'b', 'c'] != Last(4)(All(IsInstance(str)))
    assert [1, 2, 3] == Last()(3)

    with pytest.raises(AssertionError):
        assert [1, 2, 3] == Last(2)

    with pytest.raises(TypeError) as te_info:
        assert [1, 2, 3] == Last(2)([0, 1])([2])  # type: ignore[operator]
    assert str(te_info.value) == "'_Last' object is not callable"

    with pytest.raises(AssertionError) as exc_info:
        assert [1, 2, 3] == Last(2)([1, 2])
    assert str(exc_info.value).split(
        '\n')[0] == 'assert [1, 2, 3] == Last(2)([1, 2])[FAILED for [1, 2, 3]]'
Beispiel #4
0
def test_slice():
    assert 'abcdef' == Slice[3:](EqualTo('def'))
    assert 'abcdef' == Slice[:3](All(LessThan('d')))
    assert [1, 2, 3] == Slice[1](EqualTo(2))

    with pytest.raises(AssertionError):
        assert [1, 2, 3] == Slice[2]

    with pytest.raises(TypeError) as te_info:
        assert [1, 2, 3] == Slice[2:3]([0, 1])([2])  # type: ignore[operator]
    assert str(te_info.value) == "'_Slice' object is not callable"

    with pytest.raises(AssertionError) as exc_info:
        assert [1, 2, 3] == Slice[0](EqualTo(0))
    assert (
        str(exc_info.value).split('\n')[0] ==
        'assert [1, 2, 3] == Slice[0](EqualTo(0)[FAILED for 1])[FAILED for [1, 2, 3]]'
    )
Beispiel #5
0
def test_is_falsy():
    assert [0, False, [], set(), {}, ()] == All(IsFalsy())
    assert [1, True, [0], {0}, {'': None}, (0, )] == All(Not(IsFalsy()))

    assert str(All(Not(IsFalsy()))) == 'All(Not(IsFalsy()))'
Beispiel #6
0
def test_all():
    assert {'a': [1, 2, 3]} == {'a': All(IsInstance(int), GreaterThan(0))}
    assert not [-1, 1, 2, 3] == All(GreaterThan(0))

    assert str(All(GreaterThan(0))) == 'All(GreaterThan(0))'
Beispiel #7
0
 def dict_to_match() -> Dict[Any, Any]:
     return {
         'a': And(HasLength(3), All(IsInstance(int))),
         'b': 4,
         'd': NotPresent
     }