abstract struct Athena::Validator::Spec::ComparisonConstraintValidatorTestCase

Overview

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

Defined in:

spec.cr

Instance Method Summary

Instance methods inherited from struct Athena::Validator::Spec::ConstraintValidatorTestCase

assert_no_violation(*, file : String = __FILE__, line : Int32 = __LINE__) : Nil assert_no_violation, assert_violation(message : String, code : String, value : _) : Nil
assert_violation(message : String, code : String) : Nil
assert_violation(message : String) : Nil
assert_violation
, build_violation(message : String, code : String, value : _) : AVD::Spec::ConstraintValidatorTestCase::Assertion
build_violation(message : String, code : String) : AVD::Spec::ConstraintValidatorTestCase::Assertion
build_violation(message : String) : AVD::Spec::ConstraintValidatorTestCase::Assertion
build_violation
, constraint_class : AVD::Constraint.class constraint_class, context : AVD::ExecutionContext(String) context, create_validator : AVD::ConstraintValidatorInterface create_validator, new_constraint(**args) : AVD::Constraint new_constraint, validator : AVD::ConstraintValidatorInterface validator, value=(value : Array(String) | String) : Nil value=

Constructor methods inherited from struct Athena::Validator::Spec::ConstraintValidatorTestCase

new new

Instance Method Detail

abstract def error_code : String #

The code for the current constraint.


[View source]
abstract def invalid_comparisons : Tuple #

A Tuple of tuples representing invalid comparisons.


[View source]
def test_invalid_comparisons(actual, expected : T) : Nil forall T #

[View source]
def test_valid_comparisons(actual, expected) : Nil #

[View source]
abstract def valid_comparisons : Tuple #

A Tuple of tuples representing valid comparisons.


[View source]