MCPcopy
hub / github.com/python-attrs/attrs / test_validator

Method test_validator

tests/test_setattr.py:76–104  ·  view source on GitHub ↗

Validators are run and they don't alter the value.

(self, on_setattr)

Source from the content-addressed store, hash-verified

74 [setters.validate, [setters.validate], setters.pipe(setters.validate)],
75 )
76 def test_validator(self, on_setattr):
77 """
78 Validators are run and they don't alter the value.
79 """
80
81 @attr.s(on_setattr=on_setattr)
82 class ValidatedAttribute:
83 x = attr.ib()
84 y = attr.ib(validator=[instance_of(str), matches_re("foo.*qux")])
85
86 va = ValidatedAttribute(42, "foobarqux")
87
88 with pytest.raises(TypeError) as ei:
89 va.y = 42
90
91 assert "foobarqux" == va.y
92
93 assert ei.value.args[0].startswith("'y' must be <")
94
95 with pytest.raises(ValueError) as ei:
96 va.y = "quxbarfoo"
97
98 assert ei.value.args[0].startswith("'y' must match regex '")
99
100 assert "foobarqux" == va.y
101
102 va.y = "foobazqux"
103
104 assert "foobazqux" == va.y
105
106 def test_pipe(self):
107 """

Callers

nothing calls this directly

Calls 1

ValidatedAttributeClass · 0.85

Tested by

no test coverage detected