Module: Assay::AE::Check

Defined in:
lib/assay/ae/check.rb

Overview

The Ok mixin is a reusable assertion helper that makes it easy to construct parameterized assertions with an elegant syntax.

Defined Under Namespace

Classes: Proc

Constant Summary collapse

TABLE =

Built-in check procedures.

{
  :equality       => Check::Proc.new(:message=>"should be equal"){|h| h.any?{|a,b| b==a}},
  :case_equality  => Check::Proc.new(:message=>"should be equal"){|h| h.any?{|a,b| b===a}}
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define(name, &block) ⇒ Object

Define a univerally available ok/no check.

Assay::AE::Check.define(:palindrome) do |x|
x.reverse == x
end


90
91
92
# File 'lib/assay/ae/check.rb', line 90

def self.define(name, &block)
  table[name] = Check::Proc.new(:name=>name, &block)
end

.table ⇒ Object



80
81
82
# File 'lib/assay/ae/check.rb', line 80

def self.table
  @table ||= TABLE.dup
end

Instance Method Details

#__check__ ⇒ Object

Returns the current check.



147
148
149
# File 'lib/assay/ae/check.rb', line 147

def __check__
  @__check__ || check_table[:equality]
end

#check(name = nil, &block) ⇒ Object

Define an ok/no check procedure. A one-off procedure is defined with a block.

check do |x, y|
x == y
end

ok 1,1
no 1,2

The check method can also be used to define reusable checks.

check(:palindrome) do |x|
x.reverse == x
end

This will also cause the current check to be set. Later in the code, the check procedure can be restored by just passing the symbolic name.

check :palindrome

ok 'abracarba'
no 'foolishness'


124
125
126
127
128
129
130
131
132
133
134
# File 'lib/assay/ae/check.rb', line 124

def check(name=nil, &block)
  if name
    if block
      check_table[name] = Check::Proc.new(:name=>name, &block)
    end
    @__check__ = check_table[name]
  else
    #raise ArgumentError if block.arity == 0
    @__check__ = Check::Proc.new(&block)
  end
end

#check_table ⇒ Object



95
96
97
# File 'lib/assay/ae/check.rb', line 95

def check_table
  Check.table
end

#no(*args) ⇒ Object



142
143
144
# File 'lib/assay/ae/check.rb', line 142

def no(*args)
  __check__.check!(args, true, caller)
end

#ok(*args) ⇒ Object



137
138
139
# File 'lib/assay/ae/check.rb', line 137

def ok(*args)
  __check__.check!(args, false, caller)
end