Skip to content

abstract struct Athena::Validator::Spec::CompoundConstraintTestCase(T)
inherits Athena::Spec::TestCase #

The AVD::Constraints::Compound constraint allows grouping other constraints into a single reusable constraint. Such as for defining requirements of a user's password.

This type may be used to more easily test compound constraints. For example, using the AVD::Constraints::ValidPassword constraint in the usage docs for the Compound constraint:

# The generic should be set to the type(s) that the compound constraint supports.
struct ValidPasswordTest < AVD::Spec::CompoundConstraintTestCase(String?)
  protected def create_compound : AVD::Constraints::Compound
    AVD::Constraints::ValidPassword.new
  end

  def test_valid_password : Nil
    self.validate_value "1VeryStr0ngP4$$wOrD"

    self.assert_no_violation
  end

  @[TestWith(
    nil: {nil, AVD::Constraints::NotBlank.new},
    too_short: {"123", AVD::Constraints::Size.new(12..)},
    letter_first: {"abc12345qwerty", AVD::Constraints::Regex.new(/^\d.*/)},
  )]
  def test_invalid_password(password : String?, expected_failing_constraint : AVD::Constraint) : Nil
    self.validate_value password

    self.assert_violations_raised_by_compound expected_failing_constraint
  end
end

Methods#

#assert_no_violation : Nil#

Asserts there are no violations after calling #validate_value.

#assert_violation_count(count : Int) : Nil#

Asserts there are count violations after calling #validate_value.

#assert_violations_raised_by_compound(*constraints : AVD::Constraint) : Nil#

Asserts that each of the provided constraints were properly raised.

abstract #create_compound : AVD::Constraints::Compound#

Returns the compound constraint instance to be tested.

#validate_value(value : T) : Nil#

Validates the provided value, populating the data required for further assertions.