Class: Be::Assertor

Inherits:
Object show all
Defined in:
lib/be/assertor.rb

Overview

An assertor is what RSpec calls a matcher.

Instance Method Summary collapse

Constructor Details

#initialize(messages, criteria = {}) ⇒ Assertor

Returns a new instance of Assertor.



8
9
10
11
12
# File 'lib/be/assertor.rb', line 8

def initialize(messages, criteria={})
  @messages   = messages
  @criteria   = criteria
  @comparator = criteria[:compare] || :==
end

Instance Method Details

#assert!(target) ⇒ Object Also known as: =~, ===



34
35
36
# File 'lib/be/assertor.rb', line 34

def assert!(target)
  handle(pass?(target), target)
end

#fail?(target) ⇒ Boolean Also known as: !=, does_not_match?

Returns:

  • (Boolean)


29
30
31
# File 'lib/be/assertor.rb', line 29

def fail?(target)
  ! pass?(target)
end

#pass?(target) ⇒ Boolean Also known as: ==, matches?

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/be/assertor.rb', line 15

def pass?(target)
  result = target
  @messages.each do |op, args, blk|
    result = result.__send__(op, *args, &blk)
  end

  if @criteria.key?(:measure)
    @criteria[:measure].__send__(@comparator, result)
  else
    result
  end
end

#refute!(target) ⇒ Object Also known as: !~



39
40
41
# File 'lib/be/assertor.rb', line 39

def refute!(target)
  handle(fail?(target), target)
end