parent
b2bc582f92
commit
bbd9e1b523
@ -0,0 +1,27 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
try:
|
||||||
|
import unittest2 as unittest
|
||||||
|
except ImportError:
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from isso.compat import string_types
|
||||||
|
from isso.utils import types
|
||||||
|
|
||||||
|
|
||||||
|
class TestTypes(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_require(self):
|
||||||
|
|
||||||
|
try:
|
||||||
|
types.require("foo", string_types)
|
||||||
|
except TypeError:
|
||||||
|
self.assertTrue(False)
|
||||||
|
|
||||||
|
def test_require_raises(self):
|
||||||
|
|
||||||
|
self.assertRaises(TypeError, types.require, 1, bool)
|
||||||
|
self.assertRaises(TypeError, types.require, 1, str)
|
||||||
|
|
@ -0,0 +1,15 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
|
||||||
|
def _TypeError(expected, val):
|
||||||
|
if isinstance(expected, (list, tuple)):
|
||||||
|
expected = ", ".join(expected.__name__)
|
||||||
|
else:
|
||||||
|
expected = expected.__name__
|
||||||
|
return TypeError("Expected {0}, not {1}".format(
|
||||||
|
expected, val.__class__.__name__))
|
||||||
|
|
||||||
|
|
||||||
|
def require(val, expected):
|
||||||
|
if not isinstance(val, expected):
|
||||||
|
raise _TypeError(expected, val)
|
Loading…
Reference in new issue