Class: Assay::RSpec::Expect

Inherits:
Object show all
Defined in:
lib/assay/rspec/expect.rb

Overview

Wraps the target of an expectation.

Examples:

expect(something) # => Expect

# used with `to`
expect(actual).to eq(3)

# with `to_not`
expect(actual).to_not eq(3)

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Expect

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Expect.



17
18
19
# File 'lib/assay/rspec/expect.rb', line 17

def initialize(target)
  @target = target
end

Instance Method Details

#check_message(msg) ⇒ Object (private)



145
146
147
# File 'lib/assay/rspec/expect.rb', line 145

def check_message(msg)
  ::Kernel.warn message_must_be_string(msg) unless msg.nil? || msg.is_a?(String)
end

#handle_negative_matcher(actual, matcher, message = nil, &block) ⇒ Object (private)



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/assay/rspec/expect.rb', line 108

def handle_negative_matcher(actual, matcher, message=nil, &block)
  check_message(message)

  #::RSpec::Matchers.last_should = :should_not
  #::RSpec::Matchers.last_matcher = matcher

  if block
    actual = block.call(actual)
  end

  #return ::RSpec::Matchers::BuiltIn::NegativeOperatorMatcher.new(actual) if matcher.nil?

  return Operatics.new(actual, true) if matcher.nil?

  matcher !~ actual

  #match = matcher.respond_to?(:does_not_match?) ?
  #        !matcher.does_not_match?(actual, &block) :
  #        matcher.matches?(actual, &block)
  #return match unless match

  #message ||= matcher.respond_to?(:failure_message_for_should_not) ?
  #            matcher.failure_message_for_should_not :
  #            matcher.negative_failure_message

  #if matcher.respond_to?(:diffable?) && matcher.diffable?
  #  ::RSpec::Expectations.fail_with message, matcher.expected, matcher.actual
  #else
  #  ::RSpec::Expectations.fail_with message
  #end
end

#handle_positive_matcher(actual, matcher, message = nil, &block) ⇒ Object (private)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/assay/rspec/expect.rb', line 75

def handle_positive_matcher(actual, matcher, message=nil, &block)
  check_message(message)

  #::RSpec::Matchers.last_should = :should
  #::RSpec::Matchers.last_matcher = matcher

  if block
    actual = block.call(actual)
  end

  #return ::RSpec::Matchers::BuiltIn::PositiveOperatorMatcher.new(actual) if matcher.nil?

  return Operatics.new(actual) if matcher.nil?

  matcher =~ actual

  #match = matcher.matches?(actual, &block)
  #return match if match

  #message ||= matcher.respond_to?(:failure_message_for_should) ?
  #            matcher.failure_message_for_should :
  #            matcher.failure_message

  #if matcher.respond_to?(:diffable?) && matcher.diffable?
  #  ::RSpec::Expectations.fail_with message, matcher.expected, matcher.actual
  #else
  #  ::RSpec::Expectations.fail_with message
  #end
end

#message_must_be_string(msg) ⇒ Object (private)



140
141
142
143
# File 'lib/assay/rspec/expect.rb', line 140

def message_must_be_string(msg)
  "WARNING: ignoring the provided expectation message argument " +
  "(#{msg.inspect}) since it is not a string."
end

#to(matcher = nil, message = nil, &block) ⇒ Boolean

Runs the given expectation, passing if matcher returns true.

Examples:

expect(value).to eq(5)
expect { perform }.to raise_error

Parameters:

  • matcher (Matcher) (defaults to: nil)
  • message (String) (defaults to: nil) —

    optional message to display when the expectation fails

Returns:

  • (Boolean) —

    true if the expectation succeeds (else raises)

See Also:



36
37
38
39
# File 'lib/assay/rspec/expect.rb', line 36

def to(matcher=nil, message=nil, &block)
  #prevent_operator_matchers(:to, matcher)
  handle_positive_matcher(@target, matcher, message, &block)
end

#to_not(matcher = nil, message = nil, &block) ⇒ Boolean Also known as: not_to

Runs the given expectation, passing if matcher returns false.

Examples:

expect(value).to_not eq(5)
expect(value).not_to eq(5)

Parameters:

  • matcher (Matcher) (defaults to: nil)
  • message (String) (defaults to: nil) —

    optional message to display when the expectation fails

Returns:

  • (Boolean) —

    false if the negative expectation succeeds (else raises)

See Also:



56
57
58
59
# File 'lib/assay/rspec/expect.rb', line 56

def to_not(matcher=nil, message=nil, &block)
  #prevent_operator_matchers(:to_not, matcher)
  handle_negative_matcher(@target, matcher, message, &block)
end