/
object.rb
92 lines (82 loc) · 2.95 KB
/
object.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
module Micronaut
module Expectations
class InvalidMatcherError < ArgumentError; end
module ObjectExpectations
# :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
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
# :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
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
end
end
end
class Object
include Micronaut::Expectations::ObjectExpectations
end