Skip to content

Commit 8eae430

Browse files
committed
Remove indirection.
1 parent cfa287c commit 8eae430

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

jsonschema/tests/test_validators.py

+25-24
Original file line numberDiff line numberDiff line change
@@ -187,31 +187,31 @@ def test_invalid_format_default_message(self):
187187

188188

189189
class TestErrorReprStr(unittest.TestCase):
190-
191-
message = "hello"
192-
193-
def setUp(self):
194-
self.error = ValidationError(
195-
message=self.message,
190+
def make_error(self, **kwargs):
191+
defaults = dict(
192+
message=u"hello",
196193
validator=u"type",
197194
validator_value=u"string",
198195
instance=5,
199-
schema={u"type" : u"string"},
196+
schema={u"type": u"string"},
200197
)
198+
defaults.update(kwargs)
199+
return ValidationError(**defaults)
201200

202-
def assertShows(self, message):
201+
def assertShows(self, expected, **kwargs):
203202
if PY3:
204-
message = message.replace("u'", "'")
205-
message = textwrap.dedent(message).rstrip("\n")
203+
expected = expected.replace("u'", "'")
204+
expected = textwrap.dedent(expected).rstrip("\n")
206205

207-
message_line, _, rest = str(self.error).partition("\n")
208-
self.assertEqual(message_line, self.message)
209-
self.assertEqual(rest, message)
206+
error = self.make_error(**kwargs)
207+
message_line, _, rest = str(error).partition("\n")
208+
self.assertEqual(message_line, error.message)
209+
self.assertEqual(rest, expected)
210210

211211
def test_repr(self):
212212
self.assertEqual(
213-
repr(self.error),
214-
"<ValidationError: %r>" % self.message,
213+
repr(ValidationError(message="Hello!")),
214+
"<ValidationError: %r>" % "Hello!",
215215
)
216216

217217
def test_unset_error(self):
@@ -232,46 +232,47 @@ def test_unset_error(self):
232232
self.assertEqual(str(error), "message")
233233

234234
def test_empty_paths(self):
235-
self.error.path = self.error.schema_path = []
236235
self.assertShows(
237236
"""
238237
Failed validating u'type' in schema:
239238
{u'type': u'string'}
240239
241240
On instance:
242241
5
243-
"""
242+
""",
243+
path=[],
244+
schema_path=[],
244245
)
245246

246247
def test_one_item_paths(self):
247-
self.error.path = [0]
248-
self.error.schema_path = ["items"]
249248
self.assertShows(
250249
"""
251250
Failed validating u'type' in schema:
252251
{u'type': u'string'}
253252
254253
On instance[0]:
255254
5
256-
"""
255+
""",
256+
path=[0],
257+
schema_path=["items"],
257258
)
258259

259260
def test_multiple_item_paths(self):
260-
self.error.path = [0, u"a"]
261-
self.error.schema_path = [u"items", 0, 1]
262261
self.assertShows(
263262
"""
264263
Failed validating u'type' in schema[u'items'][0]:
265264
{u'type': u'string'}
266265
267266
On instance[0][u'a']:
268267
5
269-
"""
268+
""",
269+
path=[0, u"a"],
270+
schema_path=[u"items", 0, 1],
270271
)
271272

272273
def test_uses_pprint(self):
273274
with mock.patch("pprint.pformat") as pformat:
274-
str(self.error)
275+
str(self.make_error())
275276
self.assertEqual(pformat.call_count, 2) # schema + instance
276277

277278

0 commit comments

Comments
 (0)