[validators] fix fontLibValidator doctests; make errors more verbose

This commit is contained in:
Cosimo Lupo 2017-10-19 18:29:05 +02:00
parent 25c84dcb84
commit 9c72e09e18

View File

@ -943,7 +943,7 @@ def fontLibValidator(value):
>>> fontLibValidator(lib) >>> fontLibValidator(lib)
(True, None) (True, None)
>>> lib = {"public.glyphOrder" : [b"A", b"C", b"B"]} >>> lib = {"public.glyphOrder" : ["A", "C", "B"]}
>>> fontLibValidator(lib) >>> fontLibValidator(lib)
(True, None) (True, None)
@ -951,28 +951,41 @@ def fontLibValidator(value):
>>> fontLibValidator(lib) >>> fontLibValidator(lib)
(True, None) (True, None)
>>> lib = "hello"
>>> fontLibValidator(lib)
(False, 'The lib data is not in the correct format: expected a dictionary, found str')
>>> lib = {1: "hello"}
>>> fontLibValidator(lib)
(False, 'The lib key is not properly formatted: expected basestring, found int: 1')
>>> lib = {"public.glyphOrder" : "hello"} >>> lib = {"public.glyphOrder" : "hello"}
>>> fontLibValidator(lib) >>> fontLibValidator(lib)
(False, 'public.glyphOrder is not properly formatted.') (False, 'public.glyphOrder is not properly formatted: expected list or tuple, found str')
>>> lib = {"public.glyphOrder" : ["A", 1, "B"]} >>> lib = {"public.glyphOrder" : ["A", 1, "B"]}
>>> fontLibValidator(lib) >>> fontLibValidator(lib)
(False, 'public.glyphOrder is not properly formatted.') (False, 'public.glyphOrder is not properly formatted: expected basestring, found int')
""" """
bogusFormatMessage = "The lib data is not in the correct format." bogusFormatMessage = "The lib data is not in the correct format: %s"
if not isDictEnough(value): if not isDictEnough(value):
return False, bogusFormatMessage reason = "expected a dictionary, found %s" % type(value).__name__
return False, bogusFormatMessage % reason
for key, value in list(value.items()): for key, value in list(value.items()):
if not isinstance(key, basestring): if not isinstance(key, basestring):
return False, bogusFormatMessage return False, (
"The lib key is not properly formatted: expected basestring, found %s: %r" %
(type(key).__name__, key))
# public.glyphOrder # public.glyphOrder
if key == "public.glyphOrder": if key == "public.glyphOrder":
bogusGlyphOrderMessage = "public.glyphOrder is not properly formatted." bogusGlyphOrderMessage = "public.glyphOrder is not properly formatted: %s"
if not isinstance(value, (list, tuple)): if not isinstance(value, (list, tuple)):
return False, bogusGlyphOrderMessage reason = "expected list or tuple, found %s" % type(value).__name__
return False, bogusGlyphOrderMessage % reason
for glyphName in value: for glyphName in value:
if not isinstance(glyphName, basestring): if not isinstance(glyphName, basestring):
return False, bogusGlyphOrderMessage reason = "expected basestring, found %s" % type(glyphName).__name__
return False, bogusGlyphOrderMessage % reason
return True, None return True, None
# -------- # --------