From 4b03ced1f778ff7f475b635c97ef15ac54ded2c9 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sun, 24 Jan 2021 21:58:02 +0100 Subject: [PATCH] add more validators --- owrx/property/validators.py | 48 ++++++++++++++++--- .../validators/test_bool_validator.py | 17 +++++++ .../validators/test_float_validator.py | 15 ++++++ test/property/validators/test_or_validator.py | 17 +++++++ 4 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 test/property/validators/test_bool_validator.py create mode 100644 test/property/validators/test_float_validator.py create mode 100644 test/property/validators/test_or_validator.py diff --git a/owrx/property/validators.py b/owrx/property/validators.py index 1812fb2..a893b79 100644 --- a/owrx/property/validators.py +++ b/owrx/property/validators.py @@ -1,4 +1,6 @@ from abc import ABC, abstractmethod +from functools import reduce +from operator import or_ class ValidatorException(Exception): @@ -29,19 +31,51 @@ class LambdaValidator(Validator): return self.callable(value) -class NumberValidator(Validator): +class TypeValidator(Validator): + def __init__(self, type): + self.type = type + super().__init__() + def isValid(self, value): - return isinstance(value, int) or isinstance(value, float) + return isinstance(value, self.type) -class IntegerValidator(Validator): +class IntegerValidator(TypeValidator): + def __init__(self): + super().__init__(int) + + +class FloatValidator(TypeValidator): + def __init__(self): + super().__init__(float) + + +class StringValidator(TypeValidator): + def __init__(self): + super().__init__(str) + + +class BoolValidator(TypeValidator): + def __init__(self): + super().__init__(bool) + + +class OrValidator(Validator): + def __init__(self, *validators): + self.validators = validators + super().__init__() + def isValid(self, value): - return isinstance(value, int) + return reduce( + or_, + [v.isValid(value) for v in self.validators], + False + ) -class StringValidator(Validator): - def isValid(self, value): - return isinstance(value, str) +class NumberValidator(OrValidator): + def __init__(self): + super().__init__(IntegerValidator(), FloatValidator()) validator_types = { diff --git a/test/property/validators/test_bool_validator.py b/test/property/validators/test_bool_validator.py new file mode 100644 index 0000000..08cfea6 --- /dev/null +++ b/test/property/validators/test_bool_validator.py @@ -0,0 +1,17 @@ +from unittest import TestCase +from owrx.property.validators import BoolValidator + + +class NumberValidatorTest(TestCase): + def testPassesNumbers(self): + validator = BoolValidator() + self.assertTrue(validator.isValid(True)) + self.assertTrue(validator.isValid(False)) + + def testDoesntPassOthers(self): + validator = BoolValidator() + self.assertFalse(validator.isValid(123)) + self.assertFalse(validator.isValid(-2)) + self.assertFalse(validator.isValid(.5)) + self.assertFalse(validator.isValid("text")) + self.assertFalse(validator.isValid(object())) diff --git a/test/property/validators/test_float_validator.py b/test/property/validators/test_float_validator.py new file mode 100644 index 0000000..f4e43ec --- /dev/null +++ b/test/property/validators/test_float_validator.py @@ -0,0 +1,15 @@ +from unittest import TestCase +from owrx.property.validators import FloatValidator + + +class FloatValidatorTest(TestCase): + def testPassesNumbers(self): + validator = FloatValidator() + self.assertTrue(validator.isValid(.5)) + + def testDoesntPassOthers(self): + validator = FloatValidator() + self.assertFalse(validator.isValid(123)) + self.assertFalse(validator.isValid(-2)) + self.assertFalse(validator.isValid("text")) + self.assertFalse(validator.isValid(object())) diff --git a/test/property/validators/test_or_validator.py b/test/property/validators/test_or_validator.py new file mode 100644 index 0000000..0f7f79d --- /dev/null +++ b/test/property/validators/test_or_validator.py @@ -0,0 +1,17 @@ +from unittest import TestCase +from owrx.property.validators import OrValidator, IntegerValidator, StringValidator + + +class OrValidatorTest(TestCase): + def testPassesAnyValidators(self): + validator = OrValidator(IntegerValidator(), StringValidator()) + self.assertTrue(validator.isValid(42)) + self.assertTrue(validator.isValid("text")) + + def testRejectsOtherTypes(self): + validator = OrValidator(IntegerValidator(), StringValidator()) + self.assertFalse(validator.isValid(.5)) + + def testRejectsIfNoValidator(self): + validator = OrValidator() + self.assertFalse(validator.isValid("any value"))