Class: Spec::Matchers::Be

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Be

Returns a new instance of Be.



5
6
7
8
# File 'lib/spec/matchers/be.rb', line 5

def initialize(*args)
  @expected = args.empty? ? true : set_expected(args.shift)
  @args = args
end

Instance Method Details

#descriptionObject



50
51
52
# File 'lib/spec/matchers/be.rb', line 50

def description
  "#{prefix_to_sentence}#{comparison} #{expected_to_sentence}#{args_to_sentence}".gsub(/\s+/,' ')
end

#failure_message_for_shouldObject



29
30
31
32
33
# File 'lib/spec/matchers/be.rb', line 29

def failure_message_for_should
  handling_predicate? ?
    "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}" :
    "expected #{@comparison_method} #{expected}, got #{@actual.inspect}".gsub('  ',' ')
end

#failure_message_for_should_notObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/spec/matchers/be.rb', line 35

def failure_message_for_should_not
  if handling_predicate?
    "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
  else
    message = <<-MESSAGE
'should_not be #{@comparison_method} #{expected}' not only FAILED,
it reads really poorly.
    MESSAGE
    
    raise message << ([:===,:==].include?(@comparison_method) ?
      "Why don't you try expressing it without the \"be\"?" :
      "Why don't you try expressing it in the positive?")
  end
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/spec/matchers/be.rb', line 10

def matches?(actual)
  @actual = actual
  handling_predicate? ? run_predicate_on(actual) : match_or_compare(actual)
end

#run_predicate_on(actual) ⇒ Object



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

def run_predicate_on(actual)
  begin
    return @result = actual.__send__(predicate, *@args)
  rescue NameError => predicate_missing_error
    "this needs to be here or rcov will not count this branch even though it's executed in a code example"
  end

  begin
    return @result = actual.__send__(present_tense_predicate, *@args)
  rescue NameError
    raise predicate_missing_error
  end
end