Class: Spec::Matchers::Be

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

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(expected = nil, *args) ⇒ Be

Returns a new instance of Be.



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

def initialize(expected=nil, *args)
  @expected = parse_expected(expected)
  @args = args
  @comparison = ""
end

Instance Method Details

#<(expected) ⇒ Object



63
64
65
66
67
68
# File 'lib/spec/matchers/be.rb', line 63

def <(expected)
  @less_than = true
  @comparison = "< "
  @expected = expected
  self
end

#<=(expected) ⇒ Object



70
71
72
73
74
75
# File 'lib/spec/matchers/be.rb', line 70

def <=(expected)
  @less_than_or_equal = true
  @comparison = "<= "
  @expected = expected
  self
end

#>(expected) ⇒ Object



84
85
86
87
88
89
# File 'lib/spec/matchers/be.rb', line 84

def >(expected)
  @greater_than = true
  @comparison = "> "
  @expected = expected
  self
end

#>=(expected) ⇒ Object



77
78
79
80
81
82
# File 'lib/spec/matchers/be.rb', line 77

def >=(expected)
  @greater_than_or_equal = true
  @comparison = ">= "
  @expected = expected
  self
end

#descriptionObject



91
92
93
# File 'lib/spec/matchers/be.rb', line 91

def description
  "be #{@comparison}#{@expected}"
end

#expectedObject



45
46
47
48
49
50
# File 'lib/spec/matchers/be.rb', line 45

def expected
  return true if @expected == :true
  return false if @expected == :false
  return "nil" if @expected == :nil
  return @expected.inspect
end

#failure_messageObject



35
36
37
38
# File 'lib/spec/matchers/be.rb', line 35

def failure_message
  return "expected #{@comparison}#{expected}, got #{@actual.inspect}" unless handling_predicate?
  return "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
end

#match_or_compareObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/spec/matchers/be.rb', line 52

def match_or_compare
  return @actual == true if @expected == :true
  return @actual == false if @expected == :false
  return @actual.nil? if @expected == :nil
  return @actual < @expected if @less_than
  return @actual <= @expected if @less_than_or_equal
  return @actual >= @expected if @greater_than_or_equal
  return @actual > @expected if @greater_than
  return @actual.equal?(@expected)
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spec/matchers/be.rb', line 11

def matches?(actual)
  @actual = actual
  return true if match_or_compare unless handling_predicate?
  if handling_predicate?
    begin
      return @result = actual.__send__(predicate, *@args)
    rescue => predicate_error
      # This clause should be empty, but rcov will not report it as covered
      # unless something (anything) is executed within the clause
      rcov_error_report = "http://eigenclass.org/hiki.rb?rcov-0.8.0"
    end

    # This supports should_exist > target.exists? in the old world.
    # We should consider deprecating that ability as in the new world
    # you can't write "should exist" unless you have your own custom matcher.
    begin
      return @result = actual.__send__(present_tense_predicate, *@args)
    rescue
      raise predicate_error
    end
  end
  return false
end

#negative_failure_messageObject



40
41
42
43
# File 'lib/spec/matchers/be.rb', line 40

def negative_failure_message
  return "expected not #{expected}, got #{@actual.inspect}" unless handling_predicate?
  return "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
end