def test_cast(self): self.assertEqual(cast("1", int), 1) self.assertEqual(cast("-2", int), -2) self.assertEqual(cast("3", float), float(3)) self.assertEqual(cast("-4", float), float(-4)) self.assertEqual(cast("5.6", float), 5.6) self.assertEqual(cast("-7.8", float), -7.8)
def test_cast(self): self.assertEqual(cast('1', int), 1) self.assertEqual(cast('-2', int), -2) self.assertEqual(cast('3', float), float(3)) self.assertEqual(cast('-4', float), float(-4)) self.assertEqual(cast('5.6', float), 5.6) self.assertEqual(cast('-7.8', float), -7.8)
def fractional_range_check(conversion, valuestr, limit): if valuestr is None: return None if '.' in valuestr: castfunc = partial(_cast_to_fractional_component, conversion) else: castfunc = int value = cast(valuestr, castfunc, thrownmessage=limit.casterrorstring) if type(value) is FractionalComponent: tocheck = float(valuestr) else: tocheck = int(valuestr) if limit.min is not None and tocheck < limit.min: raise limit.rangeexception(limit.rangeerrorstring) if limit.max is not None and tocheck > limit.max: raise limit.rangeexception(limit.rangeerrorstring) return value
def test_cast_thrownexception(self): with self.assertRaises(RuntimeError): cast("asdf", int, thrownexception=RuntimeError)
def test_cast_caughtexception(self): def tester(value): raise RuntimeError with self.assertRaises(ISOFormatError): cast("asdf", tester, caughtexceptions=(RuntimeError, ))
def test_cast_exception(self): with self.assertRaises(ISOFormatError): cast("asdf", int) with self.assertRaises(ISOFormatError): cast("asdf", float)