1
1
module OpalSpec
2
2
class ExpectationNotMetError < StandardError ; end
3
3
4
- module Expectations ; end
4
+ module Expectations
5
+ end
5
6
6
- def self . matcher name , &block
7
+ def self . matcher ( name , &block )
7
8
klass = Class . new ( Matcher , &block )
8
9
9
- klass . define_method ( :matcher_name ) do
10
- name
11
- end
10
+ klass . define_method ( :matcher_name ) { name }
12
11
13
12
Expectations . define_method ( name ) do |*args |
14
13
klass . new ( *args )
15
14
end
16
15
end
17
16
18
17
matcher :be_nil do
19
- def match expected , actual
20
- actual . nil?
18
+ def match?
19
+ @subject . nil?
21
20
end
22
21
23
- def failure_message_for_should
24
- "expected #{ expected . inspect } to be nil."
22
+ def failure_message
23
+ "expected #{ @subject . inspect } to be nil."
24
+ end
25
+
26
+ def negative_failure_message
27
+ "expected #{ @subject . inspect } to not be nil."
25
28
end
26
29
end
27
30
28
31
matcher :be_true do
29
- def match expected , actual
30
- actual == true
32
+ def match?
33
+ @subject == true
31
34
end
32
35
33
- def failure_message_for_should
34
- "expected #{ actual . inspect } to be true."
36
+ def failure_message
37
+ "expected #{ @subject . inspect } to be true."
35
38
end
36
39
end
37
40
38
41
matcher :be_false do
39
- def match expected , actual
40
- actual == false
42
+ def match?
43
+ @subject == false
41
44
end
42
45
43
- def failure_message_for_should
44
- "expected #{ actual . inspect } to be false."
46
+ def failure_message
47
+ "expected #{ @subject . inspect } to be false."
45
48
end
46
49
end
47
50
48
51
matcher :be_kind_of do
49
- def match expected , actual
50
- actual . kind_of? expected
52
+ def initialize ( klass )
53
+ @klass = klass
51
54
end
52
55
53
- def failure_message_for_should
54
- "expected #{ actual . inspect } to be a kind of #{ expected . name } , not #{ actual . class . name } ."
56
+ def match?
57
+ @subject . kind_of? ( @klass )
58
+ end
59
+
60
+ def failure_message
61
+ "expected #{ @subject . inspect } to be a kind of #{ @klass . name } , not #{ @subject . class . name } ."
55
62
end
56
63
end
57
64
58
65
matcher :eq do
59
- def match expected , actual
60
- expected == actual
66
+ def initialize ( expected )
67
+ @ expected = expected
61
68
end
62
69
63
- def failure_message_for_should
64
- "expected #{ expected . inspect } , got: #{ actual . inspect } (using ==)."
70
+ def match?
71
+ @subject == @expected
65
72
end
66
73
67
- def failure_message_for_should_not
68
- "expected #{ expected . inspect } , not to be: #{ actual . inspect } (using ==)."
74
+ def failure_message
75
+ "expected #{ @expected . inspect } , got: #{ @subject . inspect } (using ==)."
76
+ end
77
+
78
+ def negative_failure_message
79
+ "expected #{ @expected . inspect } , not to be: #{ @subject . inspect } (using ==)."
69
80
end
70
81
end
71
82
@@ -84,46 +95,54 @@ def failure_message_for_should_not
84
95
end
85
96
86
97
matcher :raise_error do
87
- def match expected , actual
88
- @expected = expected || Exception
89
- ok = true
98
+ def initialize ( klass )
99
+ @klass = klass
100
+ end
101
+
102
+ def match?
103
+ @klass ||= Exception
104
+ passed = true
90
105
91
106
begin
92
- actual . call
93
- ok = false
107
+ @subject . call
108
+ passed = false
94
109
rescue => e
95
- @expected = @ error = e
110
+ @error = e
96
111
end
97
112
98
- ok
113
+ passed
99
114
end
100
115
101
- def failure_message_for_should
102
- "expected #@expected to be raised, but nothing was."
116
+ def failure_message
117
+ "expected #@klass to be raised, but nothing was."
103
118
end
104
119
105
- def failure_message_for_should_not
106
- "did not expect an error, but #{ @expected . class } was raised"
120
+ def negative_failure_message
121
+ "did not expect an error, but #{ @error . class } was raised"
107
122
end
108
123
end
109
124
110
125
matcher :be_empty do
111
- def match expected , actual
112
- actual . empty?
126
+ def match?
127
+ @subject . empty?
113
128
end
114
129
115
- def failure_message_for_should
116
- "expected #{ actual . inspect } to be empty"
130
+ def failure_message
131
+ "expected #{ @subject . inspect } to be empty"
117
132
end
118
133
end
119
134
120
135
matcher :respond_to do
121
- def match expected , actual
122
- actual . respond_to? expected
136
+ def initialize ( method_name )
137
+ @method_name = method_name
123
138
end
124
139
125
- def failure_message_for_should
126
- "expected #{ actual . inspect } (#{ actual . class } ) to respond to #{ expected } ."
140
+ def match?
141
+ @subject . respond_to? @method_name
142
+ end
143
+
144
+ def failure_message
145
+ "expected #{ @subject . inspect } (#{ @subject . class } ) to respond to #{ @method_name } ."
127
146
end
128
147
end
129
148
end
0 commit comments