add more validators

This commit is contained in:
Jakob Ketterl 2021-01-24 21:58:02 +01:00
parent 66dc4e5772
commit 4b03ced1f7
4 changed files with 90 additions and 7 deletions

View File

@ -1,4 +1,6 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from functools import reduce
from operator import or_
class ValidatorException(Exception): class ValidatorException(Exception):
@ -29,19 +31,51 @@ class LambdaValidator(Validator):
return self.callable(value) return self.callable(value)
class NumberValidator(Validator): class TypeValidator(Validator):
def __init__(self, type):
self.type = type
super().__init__()
def isValid(self, value): 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): def isValid(self, value):
return isinstance(value, int) return reduce(
or_,
[v.isValid(value) for v in self.validators],
False
)
class StringValidator(Validator): class NumberValidator(OrValidator):
def isValid(self, value): def __init__(self):
return isinstance(value, str) super().__init__(IntegerValidator(), FloatValidator())
validator_types = { validator_types = {

View File

@ -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()))

View File

@ -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()))

View File

@ -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"))