def testNonFloat(self): decoder = option_decoders.FloatDecoder(option=_OPTION) with self.assertRaises(errors.Config.InvalidValue) as cm: decoder.Decode('5', _COMPONENT, _FLAGS) self.assertEqual(str(cm.exception), ( 'Invalid test_component.test_option value: "5" (of type "str"). ' 'Value must be one of the following types: float, int.'))
def testZeroMaxOrMin(self): decoder = option_decoders.FloatDecoder(max=0, min=0, option=_OPTION) with self.assertRaises(errors.Config.InvalidValue): decoder.Decode(-1, _COMPONENT, _FLAGS) with self.assertRaises(errors.Config.InvalidValue): decoder.Decode(1, _COMPONENT, _FLAGS) self.assertEqual(decoder.Decode(0, _COMPONENT, _FLAGS), 0)
def testMaxInt(self): MAX = 2 decoder = option_decoders.FloatDecoder(max=MAX, option=_OPTION) with self.assertRaises(errors.Config.InvalidValue) as cm: decoder.Decode(2.01, _COMPONENT, _FLAGS) self.assertEqual(str(cm.exception), ( 'Invalid test_component.test_option value: "2.01". Value must be at ' 'most %s.' % MAX)) self.assertIs(decoder.Decode(MAX, _COMPONENT, _FLAGS), MAX) self.assertIs(decoder.Decode(2.0, _COMPONENT, _FLAGS), 2.0) self.assertIs(decoder.Decode(1, _COMPONENT, _FLAGS), 1)
def testMinInt(self): MIN = 2 decoder = option_decoders.FloatDecoder(min=MIN, option=_OPTION) with self.assertRaises(errors.Config.InvalidValue) as cm: decoder.Decode(0, _COMPONENT, _FLAGS) self.assertEqual(str(cm.exception), ( 'Invalid test_component.test_option value: "0". Value must be at ' 'least %s.' % MIN)) self.assertIs(decoder.Decode(MIN, _COMPONENT, _FLAGS), MIN) self.assertIs(decoder.Decode(2.0, _COMPONENT, _FLAGS), 2.0) self.assertIs(decoder.Decode(5, _COMPONENT, _FLAGS), 5)
def testValidFloatAsInt(self): decoder = option_decoders.FloatDecoder(option=_OPTION) self.assertEqual(decoder.Decode(2, _COMPONENT, _FLAGS), 2)
def testNone(self): decoder = option_decoders.FloatDecoder(none_ok=True, option=_OPTION) self.assertIsNone(decoder.Decode(None, _COMPONENT, _FLAGS)) decoder = option_decoders.IntDecoder(option=_OPTION) with self.assertRaises(errors.Config.InvalidValue): decoder.Decode(None, _COMPONENT, _FLAGS)
def testDefault(self): decoder = option_decoders.FloatDecoder(default=2.5, option=_OPTION) self.assertIs(decoder.required, False) self.assertIs(decoder.default, 2.5)