abstract struct Athena::Validator::Spec::ConstraintValidatorTestCase

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::DataProviders for reducing duplication withing your test.

Direct Known Subclasses

Defined in:

spec/constraint_validator_test_case.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def assert_no_violation(*, file : String = __FILE__, line : Int32 = __LINE__) : Nil #

Asserts that no violations were added to the context.


[View source]
def assert_violation(message : String, code : String, value : _) : Nil #

Asserts a violation with the provided message, code, and value parameter was added to the context.


[View source]
def assert_violation(message : String, code : String) : Nil #

Asserts a violation with the provided provided message, and code was added to the context.


[View source]
def assert_violation(message : String) : Nil #

Asserts a violation with the provided message was added to the context.


[View source]
def 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.


[View source]
def build_violation(message : String, code : String) : AVD::Spec::ConstraintValidatorTestCase::Assertion #

Returns an AVD::Spec::ConstraintValidatorTestCase::Assertion with the provided message, and code preset.


[View source]
def build_violation(message : String) : AVD::Spec::ConstraintValidatorTestCase::Assertion #

Returns an AVD::Spec::ConstraintValidatorTestCase::Assertion with the provided message preset.


[View source]
abstract def constraint_class : AVD::Constraint.class #

Returns the class of the constraint being tested.


[View source]
def context : AVD::ExecutionContext(String) #

Returns a reference to the context used for the current test.


[View source]
abstract def create_validator : AVD::ConstraintValidatorInterface #

Returns a new validator instance for the constraint being tested.


[View source]
def new_constraint(**args) : AVD::Constraint #

Returns a new constraint instance based on #constraint_class and the provided args.


[View source]

Returns the validator instance returned via #create_validator.


[View source]
def value=(value : Array(String) | String) : Nil #

Overrides the value/node currently being validated.


[View source]