rspec 1.2.9 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/History.rdoc +34 -1
  2. data/License.txt +1 -1
  3. data/Manifest.txt +11 -4
  4. data/README.rdoc +2 -3
  5. data/Rakefile +17 -13
  6. data/Upgrade.rdoc +63 -2
  7. data/features/formatters/nested_formatter.feature +32 -0
  8. data/features/interop/cucumber_stubs_dont_leak.feature +11 -0
  9. data/features/matchers/define_matcher_with_fluent_interface.feature +21 -0
  10. data/features/matchers/define_wrapped_matcher.feature +28 -1
  11. data/features/matchers/match_unless_raises.feature +60 -0
  12. data/features/matchers/match_unless_raises_unexpected_error.feature +39 -0
  13. data/features/mocks/block_local_expectations.feature +62 -0
  14. data/features/step_definitions/running_rspec_steps.rb +9 -0
  15. data/features/step_definitions/stubbing_steps.rb +16 -0
  16. data/features/support/env.rb +1 -0
  17. data/features/support/matchers/smart_match.rb +23 -4
  18. data/geminstaller.yml +28 -0
  19. data/lib/autotest/rspec.rb +14 -7
  20. data/lib/spec/dsl/main.rb +1 -1
  21. data/lib/spec/example/example_methods.rb +4 -0
  22. data/lib/spec/{matchers/extensions → extensions}/instance_exec.rb +0 -0
  23. data/lib/spec/interop/test.rb +1 -1
  24. data/lib/spec/matchers.rb +21 -2
  25. data/lib/spec/matchers/be.rb +167 -128
  26. data/lib/spec/matchers/has.rb +3 -3
  27. data/lib/spec/matchers/have.rb +1 -0
  28. data/lib/spec/matchers/matcher.rb +55 -10
  29. data/lib/spec/matchers/method_missing.rb +2 -2
  30. data/lib/spec/matchers/raise_exception.rb +131 -0
  31. data/lib/spec/matchers/throw_symbol.rb +16 -20
  32. data/lib/spec/mocks/message_expectation.rb +63 -48
  33. data/lib/spec/mocks/methods.rb +13 -8
  34. data/lib/spec/mocks/proxy.rb +43 -22
  35. data/lib/spec/runner/differs/default.rb +1 -1
  36. data/lib/spec/runner/drb_command_line.rb +8 -2
  37. data/lib/spec/runner/example_group_runner.rb +1 -2
  38. data/lib/spec/runner/formatter/nested_text_formatter.rb +6 -3
  39. data/lib/spec/runner/option_parser.rb +2 -0
  40. data/lib/spec/runner/options.rb +6 -1
  41. data/lib/spec/stubs/cucumber.rb +2 -2
  42. data/lib/spec/version.rb +2 -2
  43. data/spec/autotest/autotest_helper.rb +1 -1
  44. data/spec/autotest/discover_spec.rb +2 -2
  45. data/spec/autotest/failed_results_re_spec.rb +2 -2
  46. data/spec/autotest/rspec_spec.rb +21 -6
  47. data/spec/spec/example/example_group_methods_spec.rb +2 -1
  48. data/spec/spec/interop/test/unit/spec_spec.rb +7 -7
  49. data/spec/spec/interop/test/unit/testcase_spec.rb +7 -7
  50. data/spec/spec/interop/test/unit/testsuite_adapter_spec.rb +1 -1
  51. data/spec/spec/matchers/be_spec.rb +159 -10
  52. data/spec/spec/matchers/has_spec.rb +109 -0
  53. data/spec/spec/matchers/matcher_spec.rb +70 -9
  54. data/spec/spec/matchers/raise_exception_spec.rb +345 -0
  55. data/spec/spec/matchers/throw_symbol_spec.rb +83 -58
  56. data/spec/spec/mocks/and_yield_spec.rb +117 -0
  57. data/spec/spec/mocks/mock_spec.rb +2 -2
  58. data/spec/spec/runner/command_line_spec.rb +26 -5
  59. data/spec/spec/runner/drb_command_line_spec.rb +39 -0
  60. data/spec/spec/runner/formatter/nested_text_formatter_spec.rb +35 -11
  61. data/spec/spec/runner/option_parser_spec.rb +12 -6
  62. data/spec/spec/runner/options_spec.rb +7 -0
  63. data/spec/spec/runner/quiet_backtrace_tweaker_spec.rb +23 -5
  64. metadata +17 -10
  65. data/lib/spec/matchers/raise_error.rb +0 -129
  66. data/spec/spec/matchers/matcher_methods_spec.rb +0 -63
  67. data/spec/spec/matchers/raise_error_spec.rb +0 -333
@@ -4,6 +4,6 @@ describe "TestSuiteAdapter" do
4
4
  include TestUnitSpecHelper
5
5
  it "should pass" do
6
6
  dir = File.dirname(__FILE__)
7
- run_script "#{dir}/resources/testsuite_adapter_spec_with_test_unit.rb"
7
+ run_script "'#{dir}/resources/testsuite_adapter_spec_with_test_unit.rb'"
8
8
  end
9
9
  end
@@ -117,27 +117,143 @@ describe "should_not be_predicate(*args)" do
117
117
  end
118
118
  end
119
119
 
120
+ describe "should be_predicate(&block)" do
121
+ it "should pass when actual returns true for :predicate?(&block)" do
122
+ actual = mock("actual")
123
+ delegate = mock("delegate")
124
+ actual.should_receive(:happy?).and_yield
125
+ delegate.should_receive(:check_happy).and_return(true)
126
+ actual.should be_happy { delegate.check_happy }
127
+ end
128
+
129
+ it "should fail when actual returns false for :predicate?(&block)" do
130
+ actual = mock("actual")
131
+ delegate = mock("delegate")
132
+ actual.should_receive(:happy?).and_yield
133
+ delegate.should_receive(:check_happy).and_return(false)
134
+ lambda {
135
+ actual.should be_happy { delegate.check_happy }
136
+ }.should fail_with("expected happy? to return true, got false")
137
+ end
138
+
139
+ it "should fail when actual does not respond to :predicate?" do
140
+ delegate = mock("delegate", :check_happy => true)
141
+ lambda {
142
+ Object.new.should be_happy { delegate.check_happy }
143
+ }.should raise_error(NameError)
144
+ end
145
+ end
146
+
147
+ describe "should_not be_predicate(&block)" do
148
+ it "should pass when actual returns false for :predicate?(&block)" do
149
+ actual = mock("actual")
150
+ delegate = mock("delegate")
151
+ actual.should_receive(:happy?).and_yield
152
+ delegate.should_receive(:check_happy).and_return(false)
153
+ actual.should_not be_happy { delegate.check_happy }
154
+ end
155
+
156
+ it "should fail when actual returns true for :predicate?(&block)" do
157
+ actual = mock("actual")
158
+ delegate = mock("delegate")
159
+ actual.should_receive(:happy?).and_yield
160
+ delegate.should_receive(:check_happy).and_return(true)
161
+ lambda {
162
+ actual.should_not be_happy { delegate.check_happy }
163
+ }.should fail_with("expected happy? to return false, got true")
164
+ end
165
+
166
+ it "should fail when actual does not respond to :predicate?" do
167
+ delegate = mock("delegate", :check_happy => true)
168
+ lambda {
169
+ Object.new.should_not be_happy { delegate.check_happy }
170
+ }.should raise_error(NameError)
171
+ end
172
+ end
173
+
174
+ describe "should be_predicate(*args, &block)" do
175
+ it "should pass when actual returns true for :predicate?(*args, &block)" do
176
+ actual = mock("actual")
177
+ delegate = mock("delegate")
178
+ actual.should_receive(:older_than?).with(3).and_yield(3)
179
+ delegate.should_receive(:check_older_than).with(3).and_return(true)
180
+ actual.should be_older_than(3) { |age| delegate.check_older_than(age) }
181
+ end
182
+
183
+ it "should fail when actual returns false for :predicate?(*args, &block)" do
184
+ actual = mock("actual")
185
+ delegate = mock("delegate")
186
+ actual.should_receive(:older_than?).with(3).and_yield(3)
187
+ delegate.should_receive(:check_older_than).with(3).and_return(false)
188
+ lambda {
189
+ actual.should be_older_than(3) { |age| delegate.check_older_than(age) }
190
+ }.should fail_with("expected older_than?(3) to return true, got false")
191
+ end
192
+
193
+ it "should fail when actual does not respond to :predicate?" do
194
+ delegate = mock("delegate", :check_older_than => true)
195
+ lambda {
196
+ Object.new.should be_older_than(3) { |age| delegate.check_older_than(age) }
197
+ }.should raise_error(NameError)
198
+ end
199
+ end
200
+
201
+ describe "should_not be_predicate(*args, &block)" do
202
+ it "should pass when actual returns false for :predicate?(*args, &block)" do
203
+ actual = mock("actual")
204
+ delegate = mock("delegate")
205
+ actual.should_receive(:older_than?).with(3).and_yield(3)
206
+ delegate.should_receive(:check_older_than).with(3).and_return(false)
207
+ actual.should_not be_older_than(3) { |age| delegate.check_older_than(age) }
208
+ end
209
+
210
+ it "should fail when actual returns true for :predicate?(*args, &block)" do
211
+ actual = mock("actual")
212
+ delegate = mock("delegate")
213
+ actual.should_receive(:older_than?).with(3).and_yield(3)
214
+ delegate.should_receive(:check_older_than).with(3).and_return(true)
215
+ lambda {
216
+ actual.should_not be_older_than(3) { |age| delegate.check_older_than(age) }
217
+ }.should fail_with("expected older_than?(3) to return false, got true")
218
+ end
219
+
220
+ it "should fail when actual does not respond to :predicate?" do
221
+ delegate = mock("delegate", :check_older_than => true)
222
+ lambda {
223
+ Object.new.should_not be_older_than(3) { |age| delegate.check_older_than(age) }
224
+ }.should raise_error(NameError)
225
+ end
226
+ end
227
+
120
228
  describe "should be_true" do
121
- it "should pass when actual equal(true)" do
229
+ it "should pass when actual equal?(true)" do
122
230
  true.should be_true
123
231
  end
124
232
 
125
- it "should fail when actual equal(false)" do
233
+ it "should pass when actual is 1" do
234
+ 1.should be_true
235
+ end
236
+
237
+ it "should fail when actual equal?(false)" do
126
238
  lambda {
127
239
  false.should be_true
128
- }.should fail_with("expected true, got false")
240
+ }.should fail_with("expected false to be true")
129
241
  end
130
242
  end
131
243
 
132
244
  describe "should be_false" do
133
- it "should pass when actual equal(false)" do
245
+ it "should pass when actual equal?(false)" do
134
246
  false.should be_false
135
247
  end
136
248
 
137
- it "should fail when actual equal(true)" do
249
+ it "should pass when actual equal?(nil)" do
250
+ nil.should be_false
251
+ end
252
+
253
+ it "should fail when actual equal?(true)" do
138
254
  lambda {
139
255
  true.should be_false
140
- }.should fail_with("expected false, got true")
256
+ }.should fail_with("expected true to be false")
141
257
  end
142
258
  end
143
259
 
@@ -173,6 +289,10 @@ describe "should be <" do
173
289
  it "should fail when < operator returns false" do
174
290
  lambda { 3.should be < 3 }.should fail_with("expected < 3, got 3")
175
291
  end
292
+
293
+ it "should describe itself" do
294
+ be.<(4).description.should == "be < 4"
295
+ end
176
296
  end
177
297
 
178
298
  describe "should be <=" do
@@ -236,17 +356,32 @@ describe "should_not with operators" do
236
356
  end
237
357
 
238
358
  describe "should be" do
239
- it "should pass if actual is true or a set value" do
359
+ it "should pass if actual is truthy" do
240
360
  true.should be
241
361
  1.should be
242
362
  end
243
363
 
244
364
  it "should fail if actual is false" do
245
- lambda {false.should be}.should fail_with("expected true, got false")
365
+ lambda {false.should be}.should fail_with("expected false to evaluate to true")
246
366
  end
247
367
 
248
368
  it "should fail if actual is nil" do
249
- lambda {nil.should be}.should fail_with("expected true, got nil")
369
+ lambda {nil.should be}.should fail_with("expected nil to evaluate to true")
370
+ end
371
+
372
+ it "should describe itself" do
373
+ be.description.should == "be"
374
+ end
375
+ end
376
+
377
+ describe "should_not be" do
378
+ it "should pass if actual is falsy" do
379
+ false.should_not be
380
+ nil.should_not be
381
+ end
382
+
383
+ it "should fail on true" do
384
+ lambda {true.should_not be}.should fail_with("expected true to evaluate to false")
250
385
  end
251
386
  end
252
387
 
@@ -254,9 +389,23 @@ describe "should be(value)" do
254
389
  it "should pass if actual.equal?(value)" do
255
390
  5.should be(5)
256
391
  end
392
+
257
393
  it "should fail if !actual.equal?(value)" do
258
394
  lambda { 5.should be(6) }.should fail_with("expected 6, got 5")
259
395
  end
396
+
397
+ it "should describe itself" do
398
+ be(5).description.should == "be 5"
399
+ end
400
+ end
401
+
402
+ describe "should_not be(value)" do
403
+ it "should pass if !actual.equal?(value)" do
404
+ 5.should_not be(6)
405
+ end
406
+ it "should fail if !actual.equal?(value)" do
407
+ lambda { 5.should_not be(5) }.should fail_with("expected not 5, got 5")
408
+ end
260
409
  end
261
410
 
262
411
  describe "'should be' with operator" do
@@ -308,4 +457,4 @@ describe "be_an_instance_of" do
308
457
  it "fails when class is higher up hierarchy" do
309
458
  5.should_not be_an_instance_of(Numeric)
310
459
  end
311
- end
460
+ end
@@ -70,6 +70,115 @@ describe "should_not have_sym(*args)" do
70
70
  end
71
71
  end
72
72
 
73
+ describe "should have_sym(&block)" do
74
+ it "should pass when actual returns true for :has_sym?(&block)" do
75
+ actual = mock("actual")
76
+ delegate = mock("delegate")
77
+ actual.should_receive(:has_foo?).and_yield
78
+ delegate.should_receive(:check_has_foo).and_return(true)
79
+ actual.should have_foo { delegate.check_has_foo }
80
+ end
81
+
82
+ it "should fail when actual returns false for :has_sym?(&block)" do
83
+ actual = mock("actual")
84
+ delegate = mock("delegate")
85
+ actual.should_receive(:has_foo?).and_yield
86
+ delegate.should_receive(:check_has_foo).and_return(false)
87
+ lambda {
88
+ actual.should have_foo { delegate.check_has_foo }
89
+ }.should fail_with("expected #has_foo?(nil) to return true, got false")
90
+ end
91
+
92
+ it "should fail when actual does not respond to :has_sym?" do
93
+ delegate = mock("delegate", :check_has_foo => true)
94
+ lambda {
95
+ Object.new.should have_foo { delegate.check_has_foo }
96
+ }.should raise_error(NameError)
97
+ end
98
+ end
99
+
100
+ describe "should_not have_sym(&block)" do
101
+ it "should pass when actual returns false for :has_sym?(&block)" do
102
+ actual = mock("actual")
103
+ delegate = mock("delegate")
104
+ actual.should_receive(:has_foo?).and_yield
105
+ delegate.should_receive(:check_has_foo).and_return(false)
106
+ actual.should_not have_foo { delegate.check_has_foo }
107
+ end
108
+
109
+ it "should fail when actual returns true for :has_sym?(&block)" do
110
+ actual = mock("actual")
111
+ delegate = mock("delegate")
112
+ actual.should_receive(:has_foo?).and_yield
113
+ delegate.should_receive(:check_has_foo).and_return(true)
114
+ lambda {
115
+ actual.should_not have_foo { delegate.check_has_foo }
116
+ }.should fail_with("expected #has_foo?(nil) to return false, got true")
117
+ end
118
+
119
+ it "should fail when actual does not respond to :has_sym?" do
120
+ delegate = mock("delegate", :check_has_foo => true)
121
+ lambda {
122
+ Object.new.should_not have_foo { delegate.check_has_foo }
123
+ }.should raise_error(NameError)
124
+ end
125
+ end
126
+
127
+ describe "should have_sym(*args, &block)" do
128
+ it "should pass when actual returns true for :has_sym?(*args, &block)" do
129
+ actual = mock("actual")
130
+ delegate = mock("delegate")
131
+ actual.should_receive(:has_foo?).with(:a).and_yield(:a)
132
+ delegate.should_receive(:check_has_foo).with(:a).and_return(true)
133
+ actual.should have_foo(:a) { |foo| delegate.check_has_foo(foo) }
134
+ end
135
+
136
+ it "should fail when actual returns false for :has_sym?(*args, &block)" do
137
+ actual = mock("actual")
138
+ delegate = mock("delegate")
139
+ actual.should_receive(:has_foo?).with(:a).and_yield(:a)
140
+ delegate.should_receive(:check_has_foo).with(:a).and_return(false)
141
+ lambda {
142
+ actual.should have_foo(:a) { |foo| delegate.check_has_foo(foo) }
143
+ }.should fail_with("expected #has_foo?(:a) to return true, got false")
144
+ end
145
+
146
+ it "should fail when actual does not respond to :has_sym?" do
147
+ delegate = mock("delegate", :check_has_foo => true)
148
+ lambda {
149
+ Object.new.should have_foo(:a) { |foo| delegate.check_has_foo(foo) }
150
+ }.should raise_error(NameError)
151
+ end
152
+ end
153
+
154
+ describe "should_not have_sym(*args, &block)" do
155
+ it "should pass when actual returns false for :has_sym?(*args, &block)" do
156
+ actual = mock("actual")
157
+ delegate = mock("delegate")
158
+ actual.should_receive(:has_foo?).with(:a).and_yield(:a)
159
+ delegate.should_receive(:check_has_foo).with(:a).and_return(false)
160
+ actual.should_not have_foo(:a) { |foo| delegate.check_has_foo(foo) }
161
+ end
162
+
163
+ it "should fail when actual returns true for :has_sym?(*args, &block)" do
164
+ actual = mock("actual")
165
+ delegate = mock("delegate")
166
+ actual.should_receive(:has_foo?).with(:a).and_yield(:a)
167
+ delegate.should_receive(:check_has_foo).with(:a).and_return(true)
168
+ lambda {
169
+ actual.should_not have_foo(:a) { |foo| delegate.check_has_foo(foo) }
170
+ }.should fail_with("expected #has_foo?(:a) to return false, got true")
171
+ end
172
+
173
+ it "should fail when actual does not respond to :has_sym?" do
174
+ delegate = mock("delegate", :check_has_foo => true)
175
+ lambda {
176
+ Object.new.should_not have_foo(:a) { |foo| delegate.check_has_foo(foo) }
177
+ }.should raise_error(NameError)
178
+ end
179
+ end
180
+
181
+
73
182
  describe "has" do
74
183
  it "should work when the target implements #send" do
75
184
  o = {:a => "A"}
@@ -1,5 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class UnexpectedError < StandardError; end
4
+
3
5
  module Spec
4
6
  module Matchers
5
7
  describe Matcher do
@@ -52,6 +54,26 @@ module Spec
52
54
  matcher.actual.should == 'actual string'
53
55
  end
54
56
 
57
+ context "wrapping another expectation (should == ...)" do
58
+ it "returns true if the wrapped expectation passes" do
59
+ matcher = Spec::Matchers::Matcher.new(:name, 'value') do |expected|
60
+ match do |actual|
61
+ actual.should == expected
62
+ end
63
+ end
64
+ matcher.matches?('value').should be_true
65
+ end
66
+
67
+ it "returns false if the wrapped expectation fails" do
68
+ matcher = Spec::Matchers::Matcher.new(:name, 'value') do |expected|
69
+ match do |actual|
70
+ actual.should == expected
71
+ end
72
+ end
73
+ matcher.matches?('other value').should be_false
74
+ end
75
+ end
76
+
55
77
  context "with overrides" do
56
78
  before(:each) do
57
79
  @matcher = Spec::Matchers::Matcher.new(:be_boolean, true) do |boolean|
@@ -207,22 +229,61 @@ module Spec
207
229
  matcher.matches?(8).should be_true
208
230
  end
209
231
 
210
- it "lets you override the actual() in messages" do
211
- matcher = Spec::Matchers::Matcher.new(:be_foo) do
212
- match do |actual|
213
- @submitted = actual
214
- false
232
+ describe "#match_unless_raises" do
233
+ context "with a passing assertion" do
234
+ let(:mod) do
235
+ Module.new do
236
+ def assert_equal(a,b)
237
+ a == b ? nil : (raise UnexpectedError.new("#{a} does not equal #{b}"))
238
+ end
239
+ end
240
+ end
241
+ let(:matcher) do
242
+ m = mod
243
+ Spec::Matchers::Matcher.new :equal, 4 do |expected|
244
+ extend m
245
+ match_unless_raises UnexpectedError do
246
+ assert_equal expected, actual
247
+ end
248
+ end
249
+ end
250
+ it "passes as you would expect" do
251
+ matcher.matches?(4).should be_true
215
252
  end
253
+ it "fails as you would expect" do
254
+ matcher.matches?(5).should be_false
255
+ end
256
+ end
216
257
 
217
- def actual
218
- "replaced"
258
+ context "with an unexpected error" do
259
+ let(:matcher) do
260
+ Spec::Matchers::Matcher.new :foo, :bar do |expected|
261
+ match_unless_raises SyntaxError do |actual|
262
+ raise "unexpected exception"
263
+ end
264
+ end
265
+ end
266
+
267
+ it "raises the error" do
268
+ expect do
269
+ matcher.matches?(:bar)
270
+ end.to raise_error("unexpected exception")
219
271
  end
220
272
  end
221
273
 
222
- matcher.matches?("foo")
223
- matcher.failure_message_for_should.should =~ /replaced/
224
274
  end
225
275
 
276
+ it "can define chainable methods" do
277
+ matcher = Spec::Matchers::Matcher.new(:name) do
278
+ chain(:expecting) do |expected_value|
279
+ @expected_value = expected_value
280
+ end
281
+ match { |actual| actual == @expected_value }
282
+ end
283
+
284
+ matcher.expecting('value').matches?('value').should be_true
285
+ matcher.expecting('value').matches?('other value').should be_false
286
+ end
226
287
  end
227
288
  end
228
289
  end