def test_is_of_type_throws_error_on_different_type(): """ Tests that the `is_of_type()` method throws an ArgumentError when it is supplied with a type that is different to the specified type. """ # Arrange obj = TypeExample() validator = ObjectValidator(obj, 'obj') # Assert with pytest.raises(ArgumentError): # Act validator.is_of_type(TypeExample2)
def test_is_of_type_throws_error_on_no_supplied_type_class(): """ Tests that the `is_of_type()` method does not throw a TypeError when it is supplied with a type that is initialized rather than a class type. """ # Arrange actual = type('type', (object,), {})() expected = type('type', (object,), {})() validator = ObjectValidator(actual, 'actual') # Assert with pytest.raises(TypeError): # Act validator.is_of_type(expected)
def test_is_of_type_skips_check_if_value_is_none(): """ Tests that the `is_of_type()` skips the type check if the specified value is `None`. """ # Arrange obj = None validator = ObjectValidator(obj, 'obj') # Act try: validator.is_of_type(TypeExample) # Assert except ArgumentError: pytest.fail(f'Type `{obj.__class__.__name__}` should match type `{TypeExample.__name__}`, but an error occurred.')
def test_is_of_type_accepts_equivalent_type(): """ Tests that the `is_of_type()` method does not throw an ArgumentError when it is supplied with a type that is equivalent to the specified type. """ # Arrange obj = TypeExample() validator = ObjectValidator(obj, 'obj') # Act try: validator.is_of_type(TypeExample) # Assert except ArgumentError: pytest.fail(f'Type `{obj.__class__.__name__}` should match type `{TypeExample.__name__}`, but an error occurred.')
def test_is_of_type_returns_validator_self(): """ Tests if the `is_of_type()` validator method returns itself after the validation is performed, so that additional validations can be performed. """ # Arrange obj = TypeExample() validator = ObjectValidator(obj, 'obj') # Act validator_returned = validator.is_of_type(TypeExample) # Assert assert validator_returned is validator