Module: AE::Check

Included in:
World
Defined in:
lib/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.

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


82
83
84
# File 'lib/ae/check.rb', line 82

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

.tableObject



72
73
74
# File 'lib/ae/check.rb', line 72

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

Instance Method Details

#__check__Object

Returns the current check.



139
140
141
# File 'lib/ae/check.rb', line 139

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'


116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ae/check.rb', line 116

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_tableObject



87
88
89
# File 'lib/ae/check.rb', line 87

def check_table
  Check.table
end

#no(*args) ⇒ Object



134
135
136
# File 'lib/ae/check.rb', line 134

def no(*args)
  __check__.no!(*args)
end

#ok(*args) ⇒ Object



129
130
131
# File 'lib/ae/check.rb', line 129

def ok(*args)
  __check__.ok!(*args)
end