Skip to content

Rspec 3 fixes #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ source 'https://rubygems.org'

gemspec

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

Expand Down
62 changes: 56 additions & 6 deletions lib/rspec/collection_matchers/have.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,33 @@ def not_a_collection
"expected #{@collection_name} to be a collection but it does not respond to #length, #size or #count"
end

def failure_message_for_should
def failure_message
"expected #{relative_expectation} #{@collection_name}, got #{@actual}"
end
alias failure_message_for_should failure_message

def failure_message_for_should_not
def failure_message_when_negated
if @relativity == :exactly
return "expected target not to have #{@expected} #{@collection_name}, got #{@actual}"
elsif @relativity == :at_most
return <<-EOF
Isn't life confusing enough?
Instead of having to figure out the meaning of this:
#{Expectations::Syntax.negative_expression("actual", "have_at_most(#{@expected}).#{@collection_name}")}
#{Syntax.negative_expression("actual", "have_at_most(#{@expected}).#{@collection_name}")}
We recommend that you use this instead:
#{Expectations::Syntax.positive_expression("actual", "have_at_least(#{@expected + 1}).#{@collection_name}")}
#{Syntax.positive_expression("actual", "have_at_least(#{@expected + 1}).#{@collection_name}")}
EOF
elsif @relativity == :at_least
return <<-EOF
Isn't life confusing enough?
Instead of having to figure out the meaning of this:
#{Expectations::Syntax.negative_expression("actual", "have_at_least(#{@expected}).#{@collection_name}")}
#{Syntax.negative_expression("actual", "have_at_least(#{@expected}).#{@collection_name}")}
We recommend that you use this instead:
#{Expectations::Syntax.positive_expression("actual", "have_at_most(#{@expected - 1}).#{@collection_name}")}
#{Syntax.positive_expression("actual", "have_at_most(#{@expected - 1}).#{@collection_name}")}
EOF
end
end
alias failure_message_for_should_not failure_message_when_negated

def description
"have #{relative_expectation} #{@collection_name}"
Expand Down Expand Up @@ -118,5 +120,53 @@ def enumerator_class
RUBY_VERSION < '1.9' ? Enumerable::Enumerator : Enumerator
end
end

module Syntax
# @api private
# Generates a positive expectation expression.
def self.positive_expression(target_expression, matcher_expression)
expression_generator.positive_expression(target_expression, matcher_expression)
end

# @api private
# Generates a negative expectation expression.
def self.negative_expression(target_expression, matcher_expression)
expression_generator.negative_expression(target_expression, matcher_expression)
end

# @api private
# Selects which expression generator to use based on the configured syntax.
def self.expression_generator
if RSpec::Expectations::Syntax.expect_enabled?
ExpectExpressionGenerator
else
ShouldExpressionGenerator
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice but what made you go modules and module functions over two constants containing say a hash?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy/paste from rspec-expectations 2.x. That was the simplest given this was already written.


# @api private
# Generates expectation expressions for the `should` syntax.
module ShouldExpressionGenerator
def self.positive_expression(target_expression, matcher_expression)
"#{target_expression}.should #{matcher_expression}"
end

def self.negative_expression(target_expression, matcher_expression)
"#{target_expression}.should_not #{matcher_expression}"
end
end

# @api private
# Generates expectation expressions for the `expect` syntax.
module ExpectExpressionGenerator
def self.positive_expression(target_expression, matcher_expression)
"expect(#{target_expression}).to #{matcher_expression}"
end

def self.negative_expression(target_expression, matcher_expression)
"expect(#{target_expression}).not_to #{matcher_expression}"
end
end
end
end
end
71 changes: 70 additions & 1 deletion spec/rspec/collection_matchers/have_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def items

it "passes block to target" do
target = double("target")
block = lambda { 5 }
block = Proc.new { 5 }
target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
expect(target).to have(3).items("arg1","arg2", block)
end
Expand Down Expand Up @@ -485,3 +485,72 @@ def players
end
end
end

module RSpec
module CollectionMatchers
describe Syntax do
describe "expression generation" do
let(:target) { "foo" }
let(:expectation) { "eq('bar')" }
let(:positive_expect_example) { "expect(foo).to eq('bar')" }
let(:positive_should_example) { "foo.should eq('bar')" }
let(:negative_expect_example) { "expect(foo).not_to eq('bar')" }
let(:negative_should_example) { "foo.should_not eq('bar')" }

def positive_expression
Syntax.positive_expression(target, expectation)
end

def negative_expression
Syntax.negative_expression(target, expectation)
end

context "when only :expect is enabled", :uses_only_expect do
before do
expect(Expectations::Syntax.should_enabled?).to be_falsey
expect(Expectations::Syntax.expect_enabled?).to be_truthy
end

it 'generates a positive expression using the expect syntax' do
expect(positive_expression).to eq(positive_expect_example)
end

it 'generates a negative expression using the expect syntax' do
expect(negative_expression).to eq(negative_expect_example)
end
end

context "when both :should and :expect are enabled", :uses_should do
before do
expect(Expectations::Syntax.should_enabled?).to be_truthy
expect(Expectations::Syntax.expect_enabled?).to be_truthy
end

it 'generates a positive expression using the expect syntax' do
expect(positive_expression).to eq(positive_expect_example)
end

it 'generates a negative expression using the expect syntax' do
expect(negative_expression).to eq(negative_expect_example)
end
end

context "when only :should is enabled", :uses_only_should do
before do
Expectations::Syntax.should_enabled?.should be_truthy
Expectations::Syntax.expect_enabled?.should be_falsey
end

it 'generates a positive expression using the expect syntax' do
positive_expression.should eq(positive_should_example)
end

it 'generates a negative expression using the expect syntax' do
negative_expression.should eq(negative_should_example)
end
end
end

end
end
end
39 changes: 39 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,42 @@

config.order = 'random'
end

shared_context "with #should enabled", :uses_should do
orig_syntax = nil

before(:all) do
orig_syntax = RSpec::Matchers.configuration.syntax
RSpec::Matchers.configuration.syntax = [:expect, :should]
end

after(:all) do
RSpec::Matchers.configuration.syntax = orig_syntax
end
end

shared_context "with #should exclusively enabled", :uses_only_should do
orig_syntax = nil

before(:all) do
orig_syntax = RSpec::Matchers.configuration.syntax
RSpec::Matchers.configuration.syntax = :should
end

after(:all) do
RSpec::Matchers.configuration.syntax = orig_syntax
end
end

shared_context "with #expect exclusively enabled", :uses_only_expect do
orig_syntax = nil

before(:all) do
orig_syntax = RSpec::Matchers.configuration.syntax
RSpec::Matchers.configuration.syntax = :expect
end

after(:all) do
RSpec::Matchers.configuration.syntax = orig_syntax
end
end