Class: Spec::Matchers::Be

Inherits:
Object show all
Includes:
Pretty
Defined in:
lib/vendor/plugins/rspec/lib/spec/matchers/be.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Pretty

#_pretty_print, #split_words, #to_sentence

Constructor Details

#initialize(*args) ⇒ Be

Returns a new instance of Be.



7
8
9
10
11
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/be.rb', line 7

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

Instance Method Details

#descriptionObject



63
64
65
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/be.rb', line 63

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

#failure_message_for_shouldObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/be.rb', line 32

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

#failure_message_for_should_notObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/be.rb', line 44

def failure_message_for_should_not
  if handling_predicate?
    if predicate == :nil?
      "expected not nil, got nil"
    else
    "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
    end
  else
    message = <<-MESSAGE
'should_not be #{@comparison_method} #{expected}' not only FAILED,
it is a bit confusing.
    MESSAGE
    
    raise message << ([:===,:==].include?(@comparison_method) ?
      "It might be more clearly expressed without the \"be\"?" :
      "It might be more clearly expressed in the positive?")
  end
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/be.rb', line 13

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

#run_predicate_on(actual) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vendor/plugins/rspec/lib/spec/matchers/be.rb', line 18

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