Skip to content

Commit 352b398

Browse files
committed
Merge pull request #11 from rspec/rspec-3-fixes
Rspec 3 fixes
2 parents fd61699 + fa16df9 commit 352b398

File tree

4 files changed

+167
-9
lines changed

4 files changed

+167
-9
lines changed

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ source 'https://rubygems.org'
22

33
gemspec
44

5-
%w[rspec rspec-core rspec-expectations rspec-mocks].each do |lib|
5+
%w[rspec rspec-core rspec-expectations rspec-mocks rspec-support].each do |lib|
66
library_path = File.expand_path("../../#{lib}", __FILE__)
77
if File.exist?(library_path)
88
gem lib, :path => library_path
99
else
10-
gem lib, :git => "git://github.com/rspec/#{lib}.git", :branch => "2-99-maintenance"
10+
gem lib, :git => "git://github.com/rspec/#{lib}.git", :branch => "2-99-maintenance" unless lib == 'rspec-support'
1111
end
1212
end
1313

lib/rspec/collection_matchers/have.rb

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,33 @@ def not_a_collection
6464
"expected #{@collection_name} to be a collection but it does not respond to #length, #size or #count"
6565
end
6666

67-
def failure_message_for_should
67+
def failure_message
6868
"expected #{relative_expectation} #{@collection_name}, got #{@actual}"
6969
end
70+
alias failure_message_for_should failure_message
7071

71-
def failure_message_for_should_not
72+
def failure_message_when_negated
7273
if @relativity == :exactly
7374
return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}"
7475
elsif @relativity == :at_most
7576
return <<-EOF
7677
Isn't life confusing enough?
7778
Instead of having to figure out the meaning of this:
78-
#{Expectations::Syntax.negative_expression("actual", "have_at_most(#{@expected}).#{@collection_name}")}
79+
#{Syntax.negative_expression("actual", "have_at_most(#{@expected}).#{@collection_name}")}
7980
We recommend that you use this instead:
80-
#{Expectations::Syntax.positive_expression("actual", "have_at_least(#{@expected + 1}).#{@collection_name}")}
81+
#{Syntax.positive_expression("actual", "have_at_least(#{@expected + 1}).#{@collection_name}")}
8182
EOF
8283
elsif @relativity == :at_least
8384
return <<-EOF
8485
Isn't life confusing enough?
8586
Instead of having to figure out the meaning of this:
86-
#{Expectations::Syntax.negative_expression("actual", "have_at_least(#{@expected}).#{@collection_name}")}
87+
#{Syntax.negative_expression("actual", "have_at_least(#{@expected}).#{@collection_name}")}
8788
We recommend that you use this instead:
88-
#{Expectations::Syntax.positive_expression("actual", "have_at_most(#{@expected - 1}).#{@collection_name}")}
89+
#{Syntax.positive_expression("actual", "have_at_most(#{@expected - 1}).#{@collection_name}")}
8990
EOF
9091
end
9192
end
93+
alias failure_message_for_should_not failure_message_when_negated
9294

9395
def description
9496
"have #{relative_expectation} #{@collection_name}"
@@ -118,5 +120,53 @@ def enumerator_class
118120
RUBY_VERSION < '1.9' ? Enumerable::Enumerator : Enumerator
119121
end
120122
end
123+
124+
module Syntax
125+
# @api private
126+
# Generates a positive expectation expression.
127+
def self.positive_expression(target_expression, matcher_expression)
128+
expression_generator.positive_expression(target_expression, matcher_expression)
129+
end
130+
131+
# @api private
132+
# Generates a negative expectation expression.
133+
def self.negative_expression(target_expression, matcher_expression)
134+
expression_generator.negative_expression(target_expression, matcher_expression)
135+
end
136+
137+
# @api private
138+
# Selects which expression generator to use based on the configured syntax.
139+
def self.expression_generator
140+
if RSpec::Expectations::Syntax.expect_enabled?
141+
ExpectExpressionGenerator
142+
else
143+
ShouldExpressionGenerator
144+
end
145+
end
146+
147+
# @api private
148+
# Generates expectation expressions for the `should` syntax.
149+
module ShouldExpressionGenerator
150+
def self.positive_expression(target_expression, matcher_expression)
151+
"#{target_expression}.should #{matcher_expression}"
152+
end
153+
154+
def self.negative_expression(target_expression, matcher_expression)
155+
"#{target_expression}.should_not #{matcher_expression}"
156+
end
157+
end
158+
159+
# @api private
160+
# Generates expectation expressions for the `expect` syntax.
161+
module ExpectExpressionGenerator
162+
def self.positive_expression(target_expression, matcher_expression)
163+
"expect(#{target_expression}).to #{matcher_expression}"
164+
end
165+
166+
def self.negative_expression(target_expression, matcher_expression)
167+
"expect(#{target_expression}).not_to #{matcher_expression}"
168+
end
169+
end
170+
end
121171
end
122172
end

spec/rspec/collection_matchers/have_spec.rb

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def items
333333

334334
it "passes block to target" do
335335
target = double("target")
336-
block = lambda { 5 }
336+
block = Proc.new { 5 }
337337
target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
338338
expect(target).to have(3).items("arg1","arg2", block)
339339
end
@@ -485,3 +485,72 @@ def players
485485
end
486486
end
487487
end
488+
489+
module RSpec
490+
module CollectionMatchers
491+
describe Syntax do
492+
describe "expression generation" do
493+
let(:target) { "foo" }
494+
let(:expectation) { "eq('bar')" }
495+
let(:positive_expect_example) { "expect(foo).to eq('bar')" }
496+
let(:positive_should_example) { "foo.should eq('bar')" }
497+
let(:negative_expect_example) { "expect(foo).not_to eq('bar')" }
498+
let(:negative_should_example) { "foo.should_not eq('bar')" }
499+
500+
def positive_expression
501+
Syntax.positive_expression(target, expectation)
502+
end
503+
504+
def negative_expression
505+
Syntax.negative_expression(target, expectation)
506+
end
507+
508+
context "when only :expect is enabled", :uses_only_expect do
509+
before do
510+
expect(Expectations::Syntax.should_enabled?).to be_falsey
511+
expect(Expectations::Syntax.expect_enabled?).to be_truthy
512+
end
513+
514+
it 'generates a positive expression using the expect syntax' do
515+
expect(positive_expression).to eq(positive_expect_example)
516+
end
517+
518+
it 'generates a negative expression using the expect syntax' do
519+
expect(negative_expression).to eq(negative_expect_example)
520+
end
521+
end
522+
523+
context "when both :should and :expect are enabled", :uses_should do
524+
before do
525+
expect(Expectations::Syntax.should_enabled?).to be_truthy
526+
expect(Expectations::Syntax.expect_enabled?).to be_truthy
527+
end
528+
529+
it 'generates a positive expression using the expect syntax' do
530+
expect(positive_expression).to eq(positive_expect_example)
531+
end
532+
533+
it 'generates a negative expression using the expect syntax' do
534+
expect(negative_expression).to eq(negative_expect_example)
535+
end
536+
end
537+
538+
context "when only :should is enabled", :uses_only_should do
539+
before do
540+
Expectations::Syntax.should_enabled?.should be_truthy
541+
Expectations::Syntax.expect_enabled?.should be_falsey
542+
end
543+
544+
it 'generates a positive expression using the expect syntax' do
545+
positive_expression.should eq(positive_should_example)
546+
end
547+
548+
it 'generates a negative expression using the expect syntax' do
549+
negative_expression.should eq(negative_should_example)
550+
end
551+
end
552+
end
553+
554+
end
555+
end
556+
end

spec/spec_helper.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,42 @@
99

1010
config.order = 'random'
1111
end
12+
13+
shared_context "with #should enabled", :uses_should do
14+
orig_syntax = nil
15+
16+
before(:all) do
17+
orig_syntax = RSpec::Matchers.configuration.syntax
18+
RSpec::Matchers.configuration.syntax = [:expect, :should]
19+
end
20+
21+
after(:all) do
22+
RSpec::Matchers.configuration.syntax = orig_syntax
23+
end
24+
end
25+
26+
shared_context "with #should exclusively enabled", :uses_only_should do
27+
orig_syntax = nil
28+
29+
before(:all) do
30+
orig_syntax = RSpec::Matchers.configuration.syntax
31+
RSpec::Matchers.configuration.syntax = :should
32+
end
33+
34+
after(:all) do
35+
RSpec::Matchers.configuration.syntax = orig_syntax
36+
end
37+
end
38+
39+
shared_context "with #expect exclusively enabled", :uses_only_expect do
40+
orig_syntax = nil
41+
42+
before(:all) do
43+
orig_syntax = RSpec::Matchers.configuration.syntax
44+
RSpec::Matchers.configuration.syntax = :expect
45+
end
46+
47+
after(:all) do
48+
RSpec::Matchers.configuration.syntax = orig_syntax
49+
end
50+
end

0 commit comments

Comments
 (0)