|
28 | 28 | lt, |
29 | 29 | matches_re, |
30 | 30 | max_len, |
| 31 | + min_len, |
31 | 32 | optional, |
32 | 33 | provides, |
33 | 34 | ) |
@@ -950,3 +951,74 @@ def test_repr(self): |
950 | 951 | __repr__ is meaningful. |
951 | 952 | """ |
952 | 953 | assert repr(max_len(23)) == "<max_len validator for 23>" |
| 954 | + |
| 955 | + |
| 956 | +class TestMinLen: |
| 957 | + """ |
| 958 | + Tests for `min_len`. |
| 959 | + """ |
| 960 | + |
| 961 | + MIN_LENGTH = 2 |
| 962 | + |
| 963 | + def test_in_all(self): |
| 964 | + """ |
| 965 | + validator is in ``__all__``. |
| 966 | + """ |
| 967 | + assert min_len.__name__ in validator_module.__all__ |
| 968 | + |
| 969 | + def test_retrieve_min_len(self): |
| 970 | + """ |
| 971 | + The configured min. length can be extracted from the Attribute |
| 972 | + """ |
| 973 | + |
| 974 | + @attr.s |
| 975 | + class Tester(object): |
| 976 | + value = attr.ib(validator=min_len(self.MIN_LENGTH)) |
| 977 | + |
| 978 | + assert fields(Tester).value.validator.min_length == self.MIN_LENGTH |
| 979 | + |
| 980 | + @pytest.mark.parametrize( |
| 981 | + "value", |
| 982 | + [ |
| 983 | + "foo", |
| 984 | + "spam", |
| 985 | + list(range(MIN_LENGTH)), |
| 986 | + {"spam": 3, "eggs": 4}, |
| 987 | + ], |
| 988 | + ) |
| 989 | + def test_check_valid(self, value): |
| 990 | + """ |
| 991 | + Silent if len(value) => min_len. |
| 992 | + Values can be strings and other iterables. |
| 993 | + """ |
| 994 | + |
| 995 | + @attr.s |
| 996 | + class Tester(object): |
| 997 | + value = attr.ib(validator=min_len(self.MIN_LENGTH)) |
| 998 | + |
| 999 | + Tester(value) # shouldn't raise exceptions |
| 1000 | + |
| 1001 | + @pytest.mark.parametrize( |
| 1002 | + "value", |
| 1003 | + [ |
| 1004 | + "", |
| 1005 | + list(range(1)), |
| 1006 | + ], |
| 1007 | + ) |
| 1008 | + def test_check_invalid(self, value): |
| 1009 | + """ |
| 1010 | + Raise ValueError if len(value) < min_len. |
| 1011 | + """ |
| 1012 | + |
| 1013 | + @attr.s |
| 1014 | + class Tester(object): |
| 1015 | + value = attr.ib(validator=min_len(self.MIN_LENGTH)) |
| 1016 | + |
| 1017 | + with pytest.raises(ValueError): |
| 1018 | + Tester(value) |
| 1019 | + |
| 1020 | + def test_repr(self): |
| 1021 | + """ |
| 1022 | + __repr__ is meaningful. |
| 1023 | + """ |
| 1024 | + assert repr(min_len(23)) == "<min_len validator for 23>" |
0 commit comments