def test_custom_error_attributes(self): """Test the properties of ValueUnsupportedError and its children.""" with self.assertRaises(ValueUnsupportedError) as catcher: validate_int("a") self.assertEqual(catcher.exception.value_type, "input") self.assertEqual(catcher.exception.actual_value, "a") self.assertEqual(catcher.exception.expected_value, "integer") self.assertEqual(str(catcher.exception), "Unsupported value 'a' for input - expected integer") with self.assertRaises(ValueTooLowError) as catcher: non_negative_int(-1) self.assertEqual(catcher.exception.value_type, "input") self.assertEqual(catcher.exception.actual_value, -1) self.assertEqual(catcher.exception.expected_value, 0) self.assertEqual( str(catcher.exception), "Value '-1' for input is too low - must be at least 0") with self.assertRaises(ValueTooHighError) as catcher: validate_int("100", maximum=10, label="score") self.assertEqual(catcher.exception.value_type, "score") self.assertEqual(catcher.exception.actual_value, 100) self.assertEqual(catcher.exception.expected_value, 10) self.assertEqual( str(catcher.exception), "Value '100' for score is too high - must be at most 10")
def serial_ports(self, value): try: value = int(value) except ValueError: raise InvalidInputError("serial_ports value must be an integer") non_negative_int(value, label="serial_ports") self.ui.validate_value(self.vm.platform.validate_serial_count, value) self._serial_ports = value
def nics(self, value): try: value = int(value) except ValueError: raise InvalidInputError("nics value must be an integer") non_negative_int(value, label="nics") self.ui.validate_value(self.vm.platform.validate_nic_count, value) self._nics = value
def test_non_negative_int(self): """Test the non_negative_int() validator.""" self.assertEqual(non_negative_int("10"), 10) self.assertEqual(non_negative_int("0"), 0) self.assertRaises(ValueTooLowError, non_negative_int, "-1")