Module: Micronaut::Expectations::ObjectExpectations

Included in:
Object
Defined in:
lib/micronaut/expectations/extensions/object.rb

Instance Method Summary collapse

Instance Method Details

#should(matcher = nil, &block) ⇒ Object

:call-seq:

should(matcher)
should == expected
should === expected
should =~ expected

receiver.should(matcher)
  => Passes if matcher.matches?(receiver)

receiver.should == expected #any value
  => Passes if (receiver == expected)

receiver.should === expected #any value
  => Passes if (receiver === expected)

receiver.should =~ regexp
  => Passes if (receiver =~ regexp)

See Micronaut::Matchers for more information about matchers

Warning

NOTE that this does NOT support receiver.should != expected. Instead, use receiver.should_not == expected



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/micronaut/expectations/extensions/object.rb', line 29

def should(matcher=nil, &block)
  ::Micronaut::Matchers.last_should = "should"
  return ::Micronaut::Matchers::PositiveOperatorMatcher.new(self) if matcher.nil?

  unless matcher.respond_to?(:matches?)
    raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
  end

  match_found = matcher.matches?(self, &block)
  ::Micronaut::Matchers.last_matcher = matcher
  
  ::Micronaut::Expectations.fail_with(matcher.failure_message) unless match_found
  match_found
end

#should_not(matcher = nil, &block) ⇒ Object

:call-seq:

should_not(matcher)
should_not == expected
should_not === expected
should_not =~ expected

receiver.should_not(matcher)
  => Passes unless matcher.matches?(receiver)

receiver.should_not == expected
  => Passes unless (receiver == expected)

receiver.should_not === expected
  => Passes unless (receiver === expected)

receiver.should_not =~ regexp
  => Passes unless (receiver =~ regexp)

See Micronaut::Matchers for more information about matchers



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/micronaut/expectations/extensions/object.rb', line 63

def should_not(matcher=nil, &block)
  ::Micronaut::Matchers.last_should = "should not"
  return ::Micronaut::Matchers::NegativeOperatorMatcher.new(self) if matcher.nil?

  unless matcher.respond_to?(:matches?)
    raise InvalidMatcherError, "Expected a matcher, got #{matcher.inspect}."
  end

  unless matcher.respond_to?(:negative_failure_message)
    ::Micronaut::Expectations.fail_with(
    <<-EOF
    Matcher does not support should_not.
    See Micronaut::Matchers for more information about matchers.
    EOF
    )
  end
  match_found = matcher.matches?(self, &block)
  ::Micronaut::Matchers.last_matcher = matcher
  
  ::Micronaut::Expectations.fail_with(matcher.negative_failure_message) if match_found
  match_found
end