rspec-expectations 2.8.0 → 2.9.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/.document +5 -0
  2. data/.yardopts +3 -0
  3. data/Changelog.md +176 -0
  4. data/README.md +2 -13
  5. data/features/custom_matchers/access_running_example.feature +1 -1
  6. data/features/step_definitions/additional_cli_steps.rb +4 -4
  7. data/lib/rspec/expectations/fail_with.rb +3 -3
  8. data/lib/rspec/expectations/handler.rb +3 -5
  9. data/lib/rspec/expectations/version.rb +1 -1
  10. data/lib/rspec/matchers.rb +387 -21
  11. data/lib/rspec/matchers/built_in.rb +33 -0
  12. data/lib/rspec/matchers/built_in/base_matcher.rb +58 -0
  13. data/lib/rspec/matchers/built_in/be.rb +183 -0
  14. data/lib/rspec/matchers/built_in/be_instance_of.rb +13 -0
  15. data/lib/rspec/matchers/built_in/be_kind_of.rb +13 -0
  16. data/lib/rspec/matchers/built_in/be_within.rb +39 -0
  17. data/lib/rspec/matchers/built_in/change.rb +132 -0
  18. data/lib/rspec/matchers/built_in/cover.rb +22 -0
  19. data/lib/rspec/matchers/built_in/eq.rb +26 -0
  20. data/lib/rspec/matchers/built_in/eql.rb +25 -0
  21. data/lib/rspec/matchers/built_in/equal.rb +48 -0
  22. data/lib/rspec/matchers/built_in/exist.rb +28 -0
  23. data/lib/rspec/matchers/built_in/has.rb +47 -0
  24. data/lib/rspec/matchers/built_in/have.rb +107 -0
  25. data/lib/rspec/matchers/built_in/include.rb +52 -0
  26. data/lib/rspec/matchers/built_in/match.rb +13 -0
  27. data/lib/rspec/matchers/built_in/match_array.rb +52 -0
  28. data/lib/rspec/matchers/built_in/raise_error.rb +96 -0
  29. data/lib/rspec/matchers/built_in/respond_to.rb +73 -0
  30. data/lib/rspec/matchers/built_in/satisfy.rb +29 -0
  31. data/lib/rspec/matchers/built_in/throw_symbol.rb +93 -0
  32. data/lib/rspec/matchers/dsl.rb +1 -1
  33. data/lib/rspec/matchers/matcher.rb +263 -233
  34. data/lib/rspec/matchers/method_missing.rb +2 -2
  35. data/lib/rspec/matchers/operator_matcher.rb +19 -20
  36. data/spec/rspec/expectations/handler_spec.rb +1 -1
  37. data/spec/rspec/matchers/base_matcher_spec.rb +1 -2
  38. data/spec/rspec/matchers/change_spec.rb +3 -3
  39. data/spec/rspec/matchers/cover_spec.rb +46 -46
  40. data/spec/rspec/matchers/dsl_spec.rb +36 -3
  41. data/spec/rspec/matchers/have_spec.rb +2 -2
  42. data/spec/rspec/matchers/include_spec.rb +1 -1
  43. data/spec/rspec/matchers/matcher_spec.rb +319 -305
  44. data/spec/rspec/matchers/method_missing_spec.rb +1 -0
  45. data/spec/rspec/matchers/operator_matcher_spec.rb +2 -2
  46. data/spec/rspec/matchers/throw_symbol_spec.rb +103 -105
  47. metadata +93 -39
  48. data/lib/rspec/matchers/base_matcher.rb +0 -56
  49. data/lib/rspec/matchers/be.rb +0 -232
  50. data/lib/rspec/matchers/be_instance_of.rb +0 -24
  51. data/lib/rspec/matchers/be_kind_of.rb +0 -24
  52. data/lib/rspec/matchers/be_within.rb +0 -47
  53. data/lib/rspec/matchers/change.rb +0 -197
  54. data/lib/rspec/matchers/cover.rb +0 -36
  55. data/lib/rspec/matchers/eq.rb +0 -36
  56. data/lib/rspec/matchers/eql.rb +0 -35
  57. data/lib/rspec/matchers/equal.rb +0 -58
  58. data/lib/rspec/matchers/errors.rb +0 -5
  59. data/lib/rspec/matchers/exist.rb +0 -34
  60. data/lib/rspec/matchers/has.rb +0 -44
  61. data/lib/rspec/matchers/have.rb +0 -162
  62. data/lib/rspec/matchers/include.rb +0 -66
  63. data/lib/rspec/matchers/match.rb +0 -21
  64. data/lib/rspec/matchers/match_array.rb +0 -65
  65. data/lib/rspec/matchers/raise_error.rb +0 -116
  66. data/lib/rspec/matchers/respond_to.rb +0 -80
  67. data/lib/rspec/matchers/satisfy.rb +0 -46
  68. data/lib/rspec/matchers/throw_symbol.rb +0 -112
@@ -1,232 +0,0 @@
1
- require 'rspec/matchers/dsl'
2
-
3
- module RSpec
4
- module Matchers
5
- class BeTrue
6
- include BaseMatcher
7
-
8
- def matches?(actual)
9
- super(actual)
10
- end
11
- end
12
-
13
- # Passes if actual is truthy (anything but false or nil)
14
- def be_true
15
- BeTrue.new
16
- end
17
-
18
- class BeFalse
19
- include BaseMatcher
20
-
21
- def matches?(actual)
22
- !super(actual)
23
- end
24
- end
25
-
26
- # Passes if actual is falsy (false or nil)
27
- def be_false
28
- BeFalse.new
29
- end
30
-
31
- class BeNil
32
- include BaseMatcher
33
-
34
- def matches?(actual)
35
- super(actual).nil?
36
- end
37
-
38
- def failure_message_for_should
39
- "expected: nil\n got: #{actual.inspect}"
40
- end
41
-
42
- def failure_message_for_should_not
43
- "expected: not nil\n got: nil"
44
- end
45
- end
46
-
47
- # Passes if actual is nil
48
- def be_nil
49
- BeNil.new
50
- end
51
-
52
- class Be
53
- include RSpec::Matchers::Pretty
54
-
55
- def initialize(*args, &block)
56
- @args = args
57
- end
58
-
59
- def matches?(actual)
60
- @actual = actual
61
- !!@actual
62
- end
63
-
64
- def failure_message_for_should
65
- "expected #{@actual.inspect} to evaluate to true"
66
- end
67
-
68
- def failure_message_for_should_not
69
- "expected #{@actual.inspect} to evaluate to false"
70
- end
71
-
72
- def description
73
- "be"
74
- end
75
-
76
- [:==, :<, :<=, :>=, :>, :===].each do |operator|
77
- define_method operator do |operand|
78
- BeComparedTo.new(operand, operator)
79
- end
80
- end
81
-
82
- private
83
-
84
- def args_to_s
85
- @args.empty? ? "" : parenthesize(inspected_args.join(', '))
86
- end
87
-
88
- def parenthesize(string)
89
- "(#{string})"
90
- end
91
-
92
- def inspected_args
93
- @args.collect{|a| a.inspect}
94
- end
95
-
96
- def expected_to_sentence
97
- split_words(@expected)
98
- end
99
-
100
- def args_to_sentence
101
- to_sentence(@args)
102
- end
103
-
104
- end
105
-
106
- class BeComparedTo < Be
107
-
108
- def initialize(operand, operator)
109
- @expected, @operator = operand, operator
110
- @args = []
111
- end
112
-
113
- def matches?(actual)
114
- @actual = actual
115
- @actual.__send__(@operator, @expected)
116
- end
117
-
118
- def failure_message_for_should
119
- "expected: #{@operator} #{@expected.inspect}\n got: #{@operator.to_s.gsub(/./, ' ')} #{@actual.inspect}"
120
- end
121
-
122
- def failure_message_for_should_not
123
- message = <<-MESSAGE
124
- 'should_not be #{@operator} #{@expected}' not only FAILED,
125
- it is a bit confusing.
126
- MESSAGE
127
-
128
- raise message << ([:===,:==].include?(@operator) ?
129
- "It might be more clearly expressed without the \"be\"?" :
130
- "It might be more clearly expressed in the positive?")
131
- end
132
-
133
- def description
134
- "be #{@operator} #{expected_to_sentence}#{args_to_sentence}"
135
- end
136
-
137
- end
138
-
139
- class BePredicate < Be
140
-
141
- def initialize(*args, &block)
142
- @expected = parse_expected(args.shift)
143
- @args = args
144
- @block = block
145
- end
146
-
147
- def matches?(actual)
148
- @actual = actual
149
- begin
150
- return @result = actual.__send__(predicate, *@args, &@block)
151
- rescue NameError => predicate_missing_error
152
- "this needs to be here or rcov will not count this branch even though it's executed in a code example"
153
- end
154
-
155
- begin
156
- return @result = actual.__send__(present_tense_predicate, *@args, &@block)
157
- rescue NameError
158
- raise predicate_missing_error
159
- end
160
- end
161
-
162
- def failure_message_for_should
163
- "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
164
- end
165
-
166
- def failure_message_for_should_not
167
- "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
168
- end
169
-
170
- def description
171
- "#{prefix_to_sentence}#{expected_to_sentence}#{args_to_sentence}"
172
- end
173
-
174
- private
175
-
176
- def predicate
177
- "#{@expected}?".to_sym
178
- end
179
-
180
- def present_tense_predicate
181
- "#{@expected}s?".to_sym
182
- end
183
-
184
- def parse_expected(expected)
185
- @prefix, expected = prefix_and_expected(expected)
186
- expected
187
- end
188
-
189
- def prefix_and_expected(symbol)
190
- symbol.to_s =~ /^(be_(an?_)?)(.*)/
191
- return $1, $3
192
- end
193
-
194
- def prefix_to_sentence
195
- split_words(@prefix)
196
- end
197
-
198
- end
199
-
200
- # @example
201
- # actual.should be_true
202
- # actual.should be_false
203
- # actual.should be_nil
204
- # actual.should be_[arbitrary_predicate](*args)
205
- # actual.should_not be_nil
206
- # actual.should_not be_[arbitrary_predicate](*args)
207
- #
208
- # Given true, false, or nil, will pass if actual value is true, false or
209
- # nil (respectively). Given no args means the caller should satisfy an if
210
- # condition (to be or not to be).
211
- #
212
- # Predicates are any Ruby method that ends in a "?" and returns true or
213
- # false. Given be_ followed by arbitrary_predicate (without the "?"),
214
- # RSpec will match convert that into a query against the target object.
215
- #
216
- # The arbitrary_predicate feature will handle any predicate prefixed with
217
- # "be_an_" (e.g. be_an_instance_of), "be_a_" (e.g. be_a_kind_of) or "be_"
218
- # (e.g. be_empty), letting you choose the prefix that best suits the
219
- # predicate.
220
- def be(*args)
221
- args.empty? ?
222
- Matchers::Be.new : equal(*args)
223
- end
224
-
225
- # passes if target.kind_of?(klass)
226
- def be_a(klass)
227
- be_a_kind_of(klass)
228
- end
229
-
230
- alias_method :be_an, :be_a
231
- end
232
- end
@@ -1,24 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class BeAnInstanceOf
4
- include BaseMatcher
5
-
6
- def matches?(actual)
7
- super(actual).instance_of?(expected)
8
- end
9
- end
10
-
11
- # Passes if actual.instance_of?(expected)
12
- #
13
- # @example
14
- #
15
- # 5.should be_instance_of(Fixnum)
16
- # 5.should_not be_instance_of(Numeric)
17
- # 5.should_not be_instance_of(Float)
18
- def be_an_instance_of(expected)
19
- BeAnInstanceOf.new(expected)
20
- end
21
-
22
- alias_method :be_instance_of, :be_an_instance_of
23
- end
24
- end
@@ -1,24 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class BeAKindOf
4
- include BaseMatcher
5
-
6
- def matches?(actual)
7
- super(actual).kind_of?(expected)
8
- end
9
- end
10
-
11
- # Passes if actual.kind_of?(expected)
12
- #
13
- # @example
14
- #
15
- # 5.should be_kind_of(Fixnum)
16
- # 5.should be_kind_of(Numeric)
17
- # 5.should_not be_kind_of(Float)
18
- def be_a_kind_of(expected)
19
- BeAKindOf.new(expected)
20
- end
21
-
22
- alias_method :be_kind_of, :be_a_kind_of
23
- end
24
- end
@@ -1,47 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class BeWithin
4
- include BaseMatcher
5
-
6
- attr_reader :delta
7
-
8
- def initialize(delta)
9
- @delta = delta
10
- end
11
-
12
- def matches?(actual)
13
- unless defined?(@expected)
14
- raise ArgumentError.new("You must set an expected value using #of: be_within(#{delta}).of(expected_value)")
15
- end
16
- (super(actual) - expected).abs < delta
17
- end
18
-
19
- def of(expected)
20
- @expected = expected
21
- self
22
- end
23
-
24
- def failure_message_for_should
25
- "expected #{actual} to #{description}"
26
- end
27
-
28
- def failure_message_for_should_not
29
- "expected #{actual} not to #{description}"
30
- end
31
-
32
- def description
33
- "be within #{delta} of #{expected}"
34
- end
35
- end
36
-
37
- # Passes if actual == expected +/- delta
38
- #
39
- # @example
40
- #
41
- # result.should be_within(0.5).of(3.0)
42
- # result.should_not be_within(0.5).of(3.0)
43
- def be_within(delta)
44
- BeWithin.new(delta)
45
- end
46
- end
47
- end
@@ -1,197 +0,0 @@
1
- module RSpec
2
- module Matchers
3
- class Change
4
- def initialize(receiver=nil, message=nil, &block)
5
- @message = message
6
- @value_proc = block || lambda {receiver.__send__(message)}
7
- @expected_after = @expected_before = @minimum = @maximum = @expected_delta = nil
8
- @eval_before = @eval_after = false
9
- end
10
-
11
- def matches?(event_proc)
12
- raise_block_syntax_error if block_given?
13
-
14
- @actual_before = evaluate_value_proc
15
- event_proc.call
16
- @actual_after = evaluate_value_proc
17
-
18
- (!change_expected? || changed?) && matches_before? && matches_after? && matches_expected_delta? && matches_min? && matches_max?
19
- end
20
-
21
- def raise_block_syntax_error
22
- raise MatcherError.new(<<-MESSAGE
23
- block passed to should or should_not change must use {} instead of do/end
24
- MESSAGE
25
- )
26
- end
27
-
28
- def evaluate_value_proc
29
- case val = @value_proc.call
30
- when Enumerable
31
- val.dup
32
- else
33
- val
34
- end
35
- end
36
-
37
- def failure_message_for_should
38
- if @eval_before && !expected_matches_actual?(@expected_before, @actual_before)
39
- "#{message} should have initially been #{@expected_before.inspect}, but was #{@actual_before.inspect}"
40
- elsif @eval_after && !expected_matches_actual?(@expected_after, @actual_after)
41
- "#{message} should have been changed to #{@expected_after.inspect}, but is now #{@actual_after.inspect}"
42
- elsif @expected_delta
43
- "#{message} should have been changed by #{@expected_delta.inspect}, but was changed by #{actual_delta.inspect}"
44
- elsif @minimum
45
- "#{message} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}"
46
- elsif @maximum
47
- "#{message} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}"
48
- else
49
- "#{message} should have changed, but is still #{@actual_before.inspect}"
50
- end
51
- end
52
-
53
- def actual_delta
54
- @actual_after - @actual_before
55
- end
56
-
57
- def failure_message_for_should_not
58
- "#{message} should not have changed, but did change from #{@actual_before.inspect} to #{@actual_after.inspect}"
59
- end
60
-
61
- def by(expected_delta)
62
- @expected_delta = expected_delta
63
- self
64
- end
65
-
66
- def by_at_least(minimum)
67
- @minimum = minimum
68
- self
69
- end
70
-
71
- def by_at_most(maximum)
72
- @maximum = maximum
73
- self
74
- end
75
-
76
- def to(to)
77
- @eval_after = true
78
- @expected_after = to
79
- self
80
- end
81
-
82
- def from (before)
83
- @eval_before = true
84
- @expected_before = before
85
- self
86
- end
87
-
88
- def description
89
- "change ##{message}"
90
- end
91
-
92
- private
93
-
94
- def message
95
- @message || "result"
96
- end
97
-
98
- def change_expected?
99
- @expected_delta != 0
100
- end
101
-
102
- def changed?
103
- @actual_before != @actual_after
104
- end
105
-
106
- def matches_before?
107
- @eval_before ? expected_matches_actual?(@expected_before, @actual_before) : true
108
- end
109
-
110
- def matches_after?
111
- @eval_after ? expected_matches_actual?(@expected_after, @actual_after) : true
112
- end
113
-
114
- def matches_expected_delta?
115
- @expected_delta ? (@actual_before + @expected_delta == @actual_after) : true
116
- end
117
-
118
- def matches_min?
119
- @minimum ? (@actual_after - @actual_before >= @minimum) : true
120
- end
121
-
122
- def matches_max?
123
- @maximum ? (@actual_after - @actual_before <= @maximum) : true
124
- end
125
-
126
- def expected_matches_actual?(expected, actual)
127
- expected === actual
128
- end
129
- end
130
-
131
- # Applied to a proc, specifies that its execution will cause some value to
132
- # change.
133
- #
134
- # @param [Object] receiver
135
- # @param [Symbol] message the message to send the receiver
136
- #
137
- # You can either pass <tt>receiver</tt> and <tt>message</tt>, or a block,
138
- # but not both.
139
- #
140
- # When passing a block, it must use the <tt>{ ... }</tt> format, not
141
- # do/end, as <tt>{ ... }</tt> binds to the +change+ method, whereas do/end
142
- # would errantly bind to the +should+ or +should_not+ method.
143
- #
144
- # @example
145
- #
146
- # lambda {
147
- # team.add_player(player)
148
- # }.should change(roster, :count)
149
- #
150
- # lambda {
151
- # team.add_player(player)
152
- # }.should change(roster, :count).by(1)
153
- #
154
- # lambda {
155
- # team.add_player(player)
156
- # }.should change(roster, :count).by_at_least(1)
157
- #
158
- # lambda {
159
- # team.add_player(player)
160
- # }.should change(roster, :count).by_at_most(1)
161
- #
162
- # string = "string"
163
- # lambda {
164
- # string.reverse!
165
- # }.should change { string }.from("string").to("gnirts")
166
- #
167
- # lambda {
168
- # person.happy_birthday
169
- # }.should change(person, :birthday).from(32).to(33)
170
- #
171
- # lambda {
172
- # employee.develop_great_new_social_networking_app
173
- # }.should change(employee, :title).from("Mail Clerk").to("CEO")
174
- #
175
- # lambda {
176
- # doctor.leave_office
177
- # }.should change(doctor, :sign).from(/is in/).to(/is out/)
178
- #
179
- # user = User.new(:type => "admin")
180
- # lambda {
181
- # user.symbolize_type
182
- # }.should change(user, :type).from(String).to(Symbol)
183
- #
184
- # == Notes
185
- #
186
- # Evaluates <tt>receiver.message</tt> or <tt>block</tt> before and after it
187
- # evaluates the proc object (generated by the lambdas in the examples
188
- # above).
189
- #
190
- # <tt>should_not change</tt> only supports the form with no subsequent
191
- # calls to <tt>by</tt>, <tt>by_at_least</tt>, <tt>by_at_most</tt>,
192
- # <tt>to</tt> or <tt>from</tt>.
193
- def change(receiver=nil, message=nil, &block)
194
- Matchers::Change.new(receiver, message, &block)
195
- end
196
- end
197
- end