Permalink
Browse files

Make rspec/expectations configurable.

rspec-core currently only supports using rspec-expectations to set
expectations and define matchers, but this opens the door for new
expectation frameworks and/or using assertion frameworks like that in
test/unit or minitest without having to load rspec's expectation
framework.

- Closes #118.
  • Loading branch information...
justinko authored and dchelimsky committed Aug 23, 2010
1 parent 3b016d2 commit eefc972b7ac5705b61b9430e1f1b87b1de440e09
View
@@ -58,7 +58,5 @@ def self.clear_remaining_example_groups
end
end
-# TODO - make this configurable with default 'on'
-require 'rspec/expectations'
-
+require 'rspec/core/backward_compatibility'
require 'rspec/monkey'
@@ -17,6 +17,7 @@ def run(err, out)
@options.configure(@configuration)
@configuration.load_spec_files
@configuration.configure_mock_framework
+ @configuration.configure_expectation_framework
@world.announce_inclusion_filter
@world.announce_exclusion_filter
@@ -27,6 +27,7 @@ def self.add_setting(name, opts={})
add_setting :fail_fast, :default => false
add_setting :run_all_when_everything_filtered
add_setting :mock_framework, :default => :rspec
+ add_setting :expectation_framework, :default => :rspec
add_setting :filter
add_setting :exclusion_filter
add_setting :filename_pattern, :default => '**/*_spec.rb'
@@ -125,6 +126,19 @@ def require_mock_framework_adapter
end
end
+ def expect_with(expectation_framework)
+ settings[:expectation_framework] = expectation_framework
+ end
+
+ def require_expectation_framework_adapter
+ require case expectation_framework.to_s
+ when /rspec/i
+ 'rspec/core/expecting/with_rspec'
+ else
+ raise ArgumentError, "#{expectation_framework.inspect} is not supported"
+ end
+ end
+
def full_backtrace=(bool)
settings[:backtrace_clean_patterns] = []
end
@@ -302,6 +316,11 @@ def configure_mock_framework
RSpec::Core::ExampleGroup.send(:include, RSpec::Core::MockFrameworkAdapter)
end
+ def configure_expectation_framework
+ require_expectation_framework_adapter
+ RSpec::Core::ExampleGroup.send(:include, RSpec::Core::ExpectationFrameworkAdapter)
+ end
+
def load_spec_files
files_to_run.map {|f| load File.expand_path(f) }
end
@@ -0,0 +1,11 @@
+require 'rspec/expectations'
+
+module RSpec
+ module Core
+ module ExpectationFrameworkAdapter
+
+ include RSpec::Matchers
+
+ end
+ end
+end
@@ -31,19 +31,30 @@ module RSpec::Core
config_options
end
- let(:command_line) do
- CommandLine.new(config_options, config)
- end
-
let(:config) do
RSpec::Core::Configuration.new
end
- let(:err) { ::StringIO.new }
+ let(:world) do
+ RSpec::Core::World.new
+ end
+
+ let(:command_line) do
+ CommandLine.new(config_options, config, world)
+ end
+
let(:out) { ::StringIO.new }
+ let(:err) { ::StringIO.new }
before do
config.stub(:run_hook)
+
+ config.should_receive(:load_spec_files)
+ config.should_receive(:configure_mock_framework)
+ config.should_receive(:configure_expectation_framework)
+
+ world.should_receive(:announce_inclusion_filter)
+ world.should_receive(:announce_exclusion_filter)
end
it "configures streams before command line options" do
@@ -14,7 +14,7 @@ module RSpec::Core
end
end
- describe "#mock_framework_class" do
+ describe "mock_framework" do
before(:each) do
config.stub(:require)
end
@@ -46,6 +46,34 @@ module RSpec::Core
end
+ describe "expectation_framework" do
+
+ it "defaults to :rspec" do
+ config.should_receive(:require).with('rspec/core/expecting/with_rspec')
+ config.require_expectation_framework_adapter
+ end
+
+ [:rspec].each do |framework|
+ it "uses #{framework.inspect} framework when set explicitly" do
+ config.should_receive(:require).with("rspec/core/expecting/with_#{framework}")
+ config.mock_framework = framework
+ config.require_expectation_framework_adapter
+ end
+ end
+
+ it "supports expect_with for backward compatibility with rspec-1.x" do
+ config.should_receive(:require).with('rspec/core/expecting/with_rspec')
+ config.mock_with :rspec
+ config.require_expectation_framework_adapter
+ end
+
+ it "raises ArgumentError if framework is not supported" do
+ config.expectation_framework = :not_supported
+ expect { config.require_expectation_framework_adapter }.to raise_error(ArgumentError)
+ end
+
+ end
+
context "setting the files to run" do
it "loads files not following pattern if named explicitly" do
@@ -370,7 +398,7 @@ def that_thing
config.filter_run :focus => true
config.filter.should eq({:focus => true})
end
-
+
it "warns if :line_number is already a filter" do
config.filter_run :line_number => 100
config.should_receive(:warn).with(
@@ -379,7 +407,7 @@ def that_thing
)
config.filter_run :focus => true
end
-
+
it "warns if :full_description is already a filter" do
config.filter_run :full_description => 'foo'
config.should_receive(:warn).with(
@@ -399,7 +427,7 @@ def that_thing
describe "line_number=" do
before { config.stub(:warn) }
-
+
it "sets the line number" do
config.line_number = '37'
config.filter.should == {:line_number => 37}
View
@@ -3,20 +3,6 @@
Dir['./spec/support/**/*.rb'].map {|f| require f}
-module RSpec
- module Core
- module Matchers
- def fail
- raise_error(::RSpec::Expectations::ExpectationNotMetError)
- end
-
- def fail_with(message)
- raise_error(::RSpec::Expectations::ExpectationNotMetError, message)
- end
- end
- end
-end
-
class NullObject
def method_missing(method, *args, &block)
# ignore
View
@@ -1,3 +1,5 @@
+require 'rspec/matchers'
+
RSpec::Matchers.define :map_specs do |specs|
match do |autotest|
@specs = specs

0 comments on commit eefc972

Please sign in to comment.