abstract struct Athena::Validator::Spec::ConstraintValidatorTestCase
- Athena::Validator::Spec::ConstraintValidatorTestCase
- Athena::Spec::TestCase
- Struct
- Value
- Object
Overview
Test case designed to make testing AVD::ConstraintValidatorInterface
easier.
Example
Using the spec from AVD::Constraints::NotNil
:
# Makes for a bit less typing when needing to reference the constraint.
private alias CONSTRAINT = AVD::Constraints::NotNil
# Define our test case inheriting from the abstract ConstraintValidatorTestCase.
struct NotNilValidatorTest < AVD::Spec::ConstraintValidatorTestCase
@[DataProvider("valid_values")]
def test_valid_values(value : _) : Nil
# Validate the value against a new instance of the constraint.
self.validator.validate value, self.new_constraint
# Assert no violations were added to the context.
self.assert_no_violation
end
# Use data providers to reduce duplication.
def valid_values : NamedTuple
{
string: {""},
bool_false: {false},
bool_true: {true},
zero: {0},
null_pointer: {Pointer(Void).null},
}
end
def test_nil_is_invalid
# Validate an invalid value against a new instance of the constraint with a custom message.
self.validator.validate nil, self.new_constraint message: "my_message"
# Asssert a violation with the expected message, code, and value parameter is added to the context.
self
.build_violation("my_message", CONSTRAINT::IS_NULL_ERROR, nil)
.assert_violation
end
# Implement some abstract defs to return the validator and constraint class.
private def create_validator : AVD::ConstraintValidatorInterface
CONSTRAINT::Validator.new
end
private def constraint_class : AVD::Constraint.class
CONSTRAINT
end
end
This type is an extension of ASPEC::TestCase
, see that type for more information on this testing approach.
This approach also allows using ASPEC::TestCase::DataProvider
s for reducing duplication withing your test.
Direct Known Subclasses
Defined in:
spec/constraint_validator_test_case.crConstructors
Instance Method Summary
-
#assert_no_violation(*, file : String = __FILE__, line : Int32 = __LINE__) : Nil
Asserts that no violations were added to the context.
-
#assert_violation(message : String, code : String, value : _) : Nil
Asserts a violation with the provided message, code, and value parameter was added to the context.
-
#assert_violation(message : String, code : String) : Nil
Asserts a violation with the provided provided message, and code was added to the context.
-
#assert_violation(message : String) : Nil
Asserts a violation with the provided message was added to the context.
-
#build_violation(message : String, code : String, value : _) : AVD::Spec::ConstraintValidatorTestCase::Assertion
Returns an
AVD::Spec::ConstraintValidatorTestCase::Assertion
with the provided message, code, and value parameter preset. -
#build_violation(message : String, code : String) : AVD::Spec::ConstraintValidatorTestCase::Assertion
Returns an
AVD::Spec::ConstraintValidatorTestCase::Assertion
with the provided message, and code preset. -
#build_violation(message : String) : AVD::Spec::ConstraintValidatorTestCase::Assertion
Returns an
AVD::Spec::ConstraintValidatorTestCase::Assertion
with the provided message preset. -
#constraint_class : AVD::Constraint.class
Returns the class of the constraint being tested.
-
#context : AVD::ExecutionContext(String)
Returns a reference to the context used for the current test.
-
#create_validator : AVD::ConstraintValidatorInterface
Returns a new validator instance for the constraint being tested.
-
#new_constraint(**args) : AVD::Constraint
Returns a new constraint instance based on
#constraint_class
and the provided args. -
#validator : AVD::ConstraintValidatorInterface
Returns the validator instance returned via
#create_validator
. -
#value=(value : Array(String) | String) : Nil
Overrides the value/node currently being validated.
Constructor Detail
Instance Method Detail
Asserts that no violations were added to the context.
Asserts a violation with the provided message, code, and value parameter was added to the context.
Asserts a violation with the provided provided message, and code was added to the context.
Asserts a violation with the provided message was added to the context.
Returns an AVD::Spec::ConstraintValidatorTestCase::Assertion
with the provided message, code, and value parameter preset.
Returns an AVD::Spec::ConstraintValidatorTestCase::Assertion
with the provided message, and code preset.
Returns an AVD::Spec::ConstraintValidatorTestCase::Assertion
with the provided message preset.
Returns the class of the constraint being tested.
Returns a reference to the context used for the current test.
Returns a new validator instance for the constraint being tested.
Returns a new constraint instance based on #constraint_class
and the provided args.
Returns the validator instance returned via #create_validator
.
Overrides the value/node currently being validated.