Skip to content

abstract struct Athena::Validator::Spec::ComparisonConstraintValidatorTestCase
inherits Athena::Validator::Spec::ConstraintValidatorTestCase #

Extension of AVD::Spec::ConstraintValidatorTestCase used for testing AVD::Constraints::AbstractComparison based constraints.

Example#

Using the spec from AVD::Constraints::EqualTo:

# Makes for a bit less typing when needing to reference the constraint.
private alias CONSTRAINT = AVD::Constraints::EqualTo

# Define our test case inheriting from the abstract `ComparisonConstraintValidatorTestCase`.
struct EqualToValidatorTest < AVD::Spec::ComparisonConstraintValidatorTestCase
  # Returns a Tuple of Tuples representing valid comparisons.
  # The first item  is the actual value and the second item is the expected value.
  def valid_comparisons : Tuple
    {
      {3, 3},
      {'a', 'a'},
      {"a", "a"},
      {Time.utc(2020, 4, 7), Time.utc(2020, 4, 7)},
      {nil, false},
    }
  end

  # Returns a Tuple of Tuples representing invalid comparisons.
  # The first item  is the actual value and the second item is the expected value.
  def invalid_comparisons : Tuple
    {
      {1, 3},
      {'b', 'a'},
      {"b", "a"},
      {Time.utc(2020, 4, 8), Time.utc(2020, 4, 7)},
    }
  end

  # The error code related to the current CONSTRAINT.
  def error_code : String
    CONSTRAINT::NOT_EQUAL_ERROR
  end

  # Implement some abstract defs to return the validator and constraint class.
  def create_validator : AVD::ConstraintValidatorInterface
    CONSTRAINT::Validator.new
  end

  def constraint_class : AVD::Constraint.class
    CONSTRAINT
  end
end

Methods#

abstract #error_code : String#

The code for the current constraint.

View source

abstract #invalid_comparisons : Tuple#

A Tuple of tuples representing invalid comparisons.

View source

#test_invalid_comparisons(actual, expected : T) : Nil forall T#

View source

#test_valid_comparisons(actual, expected) : Nil#

View source

abstract #valid_comparisons : Tuple#

A Tuple of tuples representing valid comparisons.

View source