Exemplo n.º 1
0
    def test_default_integer_argument(self):
        integer = IntegerArgument()

        validation_status = integer.validate("45")

        self.assertTrue(validation_status)
        self.assertEqual(validation_status.value(), 45)
        self.assertEqual(validation_status.message(), "")


        validation_status = integer.validate("-45")

        self.assertTrue(validation_status)
        self.assertEqual(validation_status.value(), -45)


        validation_status = integer.validate("45 ")

        self.assertFalse(validation_status)
        self.assertNotEqual(validation_status.message(), "")
        self.assertIsNone(validation_status.value())

        validation_status = integer.validate("gx")

        self.assertFalse(validation_status)
        self.assertNotEqual(validation_status.message(), "")
Exemplo n.º 2
0
    def test_integer_range_argument(self):
        from_value = 10
        to_value = 20
        integer = IntegerArgument(from_value=from_value, to_value=to_value)

        validation_status = integer.validate("%d" % to_value)
        self.assertTrue(validation_status)

        validation_status = integer.validate("%d" % from_value)
        self.assertTrue(validation_status)

        validation_status = integer.validate("%d" % 15)
        self.assertTrue(validation_status)

        value = 9
        validation_status = integer.validate("%d" % value)
        self.assertFalse(validation_status)

        range_string = "%d <= %d <= %d" % (from_value, value, to_value)
        self.assertEqual(validation_status.message(), IntegerArgument.NOT_IN_RANGE % range_string)

        value = 21
        validation_status = integer.validate("%d" % value)
        self.assertFalse(validation_status)

        range_string = "%d <= %d <= %d" % (from_value, value, to_value)
        self.assertEqual(validation_status.message(), IntegerArgument.NOT_IN_RANGE % range_string)
Exemplo n.º 3
0
    def test_integer_range_argument(self):
        from_value = 10
        to_value = 20
        integer = IntegerArgument(from_value=from_value, to_value=to_value)

        validation_status = integer.validate("%d" % to_value)
        self.assertTrue(validation_status)

        validation_status = integer.validate("%d" % from_value)
        self.assertTrue(validation_status)

        validation_status = integer.validate("%d" % 15)
        self.assertTrue(validation_status)

        value = 9
        validation_status = integer.validate("%d" % value)
        self.assertFalse(validation_status)

        range_string = "%d <= %d <= %d" % (from_value, value, to_value)
        self.assertEqual(validation_status.message(),
                         IntegerArgument.NOT_IN_RANGE % range_string)

        value = 21
        validation_status = integer.validate("%d" % value)
        self.assertFalse(validation_status)

        range_string = "%d <= %d <= %d" % (from_value, value, to_value)
        self.assertEqual(validation_status.message(),
                         IntegerArgument.NOT_IN_RANGE % range_string)
Exemplo n.º 4
0
    def test_integer_range_argument_to(self):
        to_value = 99
        integer = IntegerArgument(to_value=to_value)

        validation_status = integer.validate("%d" % to_value)
        self.assertTrue(validation_status)

        value = 100
        validation_status = integer.validate("%d" % value)
        self.assertFalse(validation_status)

        range_string = "%d <= %d" % (value, to_value)
        self.assertEqual(validation_status.message(), IntegerArgument.NOT_IN_RANGE % range_string)
Exemplo n.º 5
0
    def test_integer_range_argument_to(self):
        to_value = 99
        integer = IntegerArgument(to_value=to_value)

        validation_status = integer.validate("%d" % to_value)
        self.assertTrue(validation_status)

        value = 100
        validation_status = integer.validate("%d" % value)
        self.assertFalse(validation_status)

        range_string = "%d <= %d" % (value, to_value)
        self.assertEqual(validation_status.message(),
                         IntegerArgument.NOT_IN_RANGE % range_string)
Exemplo n.º 6
0
    def test_default_integer_argument(self):
        integer = IntegerArgument()

        validation_status = integer.validate("45")

        self.assertTrue(validation_status)
        self.assertEqual(validation_status.value(), 45)
        self.assertEqual(validation_status.message(), "")

        validation_status = integer.validate("-45")

        self.assertTrue(validation_status)
        self.assertEqual(validation_status.value(), -45)

        validation_status = integer.validate("45 ")

        self.assertFalse(validation_status)
        self.assertNotEqual(validation_status.message(), "")
        self.assertIsNone(validation_status.value())

        validation_status = integer.validate("gx")

        self.assertFalse(validation_status)
        self.assertNotEqual(validation_status.message(), "")
Exemplo n.º 7
0
def valid_iter_num(user_input):
    validator = IntegerArgument(from_value=0)
    validated = validator.validate(user_input)
    if validated.failed():
        strip_error_message_and_raise_exception(validated)
    return user_input