Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: rspec/rspec-core
base: v2.11.0
...
head fork: rspec/rspec-core
compare: classes_and_methods
  • 7 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 23, 2011
@dchelimsky dchelimsky clean up the expectation framework configuration cd5bbec
@dchelimsky dchelimsky add feature with RSpec::TestCase and test_ methods cea5d2f
@dchelimsky dchelimsky move example group subclassing to inherited hook to support defining …
…an example group with a class declaration
fcd564e
@dchelimsky dchelimsky add support for defining examples using def test_xxx d523321
@dchelimsky dchelimsky strip Test, Spec, or Describe from class name c1b2b00
@dchelimsky dchelimsky configure stdlib in feature
- we'll make rspec + stdlib the default in rspec-3, but automatically
  including stdlib would be a potentially breaking change in rspec-2
450a3b1
@dchelimsky dchelimsky Merge branch 'master' into classes_and_methods
* master:
  ci: bundle exec rake (to ensure compatibility w/ downstream deps)
  dev: remove unnecessary monkey patch of Cucumber
  remove accidental file
d51eb83
View
27 features/example_groups/classes_and_methods.feature
@@ -0,0 +1,27 @@
+Feature: define example groups with classes and methods
+
+ You can use familiar constructs like Ruby classes and methods if you prefer
+ them over the `describe`/`it` DSL.
+
+ To get test/unit or minitest assertions you need to configure RSpec as shown
+ in the following scenario:
+
+ Scenario: declare example group by subclassing RSpec::ExampleGroup
+ Given a file named "array_spec.rb" with:
+ """
+ RSpec.configure {|c| c.expect_with :stdlib}
+
+ class ArrayTest < RSpec::TestCase
+ def test_is_empty_when_created
+ assert Array.new.empty?
+ end
+ end
+ """
+ When I run `rspec array_spec.rb --format doc`
+ Then the output should contain:
+ """
+ Array
+ is_empty_when_created
+ """
+ And the examples should all pass
+
View
2  lib/rspec/core.rb
@@ -31,6 +31,8 @@
module RSpec
autoload :Matchers, 'rspec/matchers'
+ ExampleGroup = Core::ExampleGroup
+ TestCase = Core::ExampleGroup
SharedContext = Core::SharedContext
module Core
View
52 lib/rspec/core/configuration.rb
@@ -37,7 +37,8 @@ def self.add_setting(name, opts={})
add_setting :backtrace_clean_patterns
add_setting :tty
add_setting :treat_symbols_as_metadata_keys_with_true_values, :default => false
- add_setting :expecting_with_rspec
+ add_setting :expectation_frameworks
+
CONDITIONAL_FILTERS = {
:if => lambda { |value, metadata| metadata.has_key?(:if) && !value },
@@ -52,12 +53,15 @@ def self.add_setting(name, opts={})
/lib\/rspec\/(core|expectations|matchers|mocks)/
]
+ DEFAULT_EXPECTATION_FRAMEWORKS = [:rspec]
+
def initialize
@color_enabled = false
self.include_or_extend_modules = []
self.files_to_run = []
self.backtrace_clean_patterns = DEFAULT_BACKTRACE_PATTERNS.dup
self.exclusion_filter = CONDITIONAL_FILTERS.dup
+ self.expectation_frameworks = DEFAULT_EXPECTATION_FRAMEWORKS.dup
end
def reset
@@ -179,43 +183,24 @@ def mock_framework=(framework)
end
end
- # Returns the configured expectation framework adapter module(s)
- def expectation_frameworks
- expect_with :rspec unless settings[:expectation_frameworks]
- settings[:expectation_frameworks]
- end
-
- # Delegates to expect_with([framework])
- def expectation_framework=(framework)
- expect_with([framework])
- end
-
# Sets the expectation framework module(s).
#
- # +frameworks+ can be :rspec, :stdlib, or both
+ # +frameworks+ can be :rspec, :stdlib, or both
#
# Given :rspec, configures rspec/expectations.
# Given :stdlib, configures test/unit/assertions
# Given both, configures both
def expect_with(*frameworks)
- settings[:expectation_frameworks] = []
+ expectation_frameworks.clear
frameworks.each do |framework|
- case framework
- when Symbol
- case framework
- when :rspec
- require 'rspec/core/expecting/with_rspec'
- self.expecting_with_rspec = true
- when :stdlib
- require 'rspec/core/expecting/with_stdlib'
- else
- raise ArgumentError, "#{framework.inspect} is not supported"
- end
- settings[:expectation_frameworks] << RSpec::Core::ExpectationFrameworkAdapter
- end
+ settings[:expectation_frameworks] << framework
end
end
+ def expecting_with_rspec?
+ expectation_frameworks.include?(:rspec)
+ end
+
def full_backtrace=(true_or_false)
settings[:backtrace_clean_patterns] = true_or_false ? [] : DEFAULT_BACKTRACE_PATTERNS
end
@@ -360,6 +345,7 @@ def inclusion_filter=(filter)
def inclusion_filter
settings[:inclusion_filter] || {}
end
+
def filter_run_including(*args)
force_overwrite = if args.last.is_a?(Hash) || args.last.is_a?(Symbol)
false
@@ -411,7 +397,17 @@ def configure_mock_framework
def configure_expectation_framework
expectation_frameworks.each do |framework|
- RSpec::Core::ExampleGroup.send(:include, framework)
+ case framework
+ when :rspec
+ require 'rspec/core/expecting/with_rspec'
+ when :stdlib
+ require 'rspec/core/expecting/with_stdlib'
+ else
+ raise ArgumentError, "#{framework.inspect} is not supported"
+ end
+ end
+ unless expectation_frameworks.empty?
+ RSpec::Core::ExampleGroup.send(:include, RSpec::Core::ExpectationFrameworkAdapter)
end
end
View
39 lib/rspec/core/example_group.rb
@@ -118,29 +118,22 @@ def self.superclass_metadata
end
def self.describe(*args, &example_group_block)
- @_subclass_count ||= 0
- @_subclass_count += 1
- args << {} unless args.last.is_a?(Hash)
- args.last.update(:example_group_block => example_group_block)
-
- # TODO 2010-05-05: Because we don't know if const_set is thread-safe
- child = const_set(
- "Nested_#{@_subclass_count}",
- subclass(self, args, &example_group_block)
- )
- children << child
+ $initialization_args = args
+ child = Class.new(self)
+ child.module_eval(&example_group_block) if example_group_block
child
+ ensure
+ $initialization_args = nil
end
- class << self
- alias_method :context, :describe
+ def self.inherited(child)
+ child.register if child.top_level?
+ child.set_it_up(*$initialization_args || [subject_for(child.name)])
+ children << child
end
- def self.subclass(parent, args, &example_group_block)
- subclass = Class.new(parent)
- subclass.set_it_up(*args)
- subclass.module_eval(&example_group_block) if example_group_block
- subclass
+ class << self
+ alias_method :context, :describe
end
def self.children
@@ -315,6 +308,16 @@ def self.set_ivars(instance, ivars)
ivars.each {|name, value| instance.instance_variable_set(name, value)}
end
+ def self.method_added(m)
+ if m.to_s =~ /test_(.*)/
+ example($1) { method(m).call }
+ end
+ end
+
+ def self.subject_for(name)
+ name.gsub(/(^Describe|Spec|Test)/,'')
+ end
+
def described_class
self.class.described_class
end
View
2  lib/rspec/core/extensions/object.rb
@@ -2,7 +2,7 @@ module RSpec
module Core
module ObjectExtensions
def describe(*args, &example_group_block)
- RSpec::Core::ExampleGroup.describe(*args, &example_group_block).register
+ RSpec::Core::ExampleGroup.describe(*args, &example_group_block)
end
end
end
View
18 spec/rspec/core/configuration_spec.rb
@@ -82,15 +82,9 @@ module RSpec::Core
describe "#expectation_framework" do
it "defaults to :rspec" do
+ config.expectation_frameworks = [:rspec]
config.should_receive(:require).with('rspec/core/expecting/with_rspec')
- config.expectation_frameworks
- end
- end
-
- describe "#expectation_framework=" do
- it "delegates to expect_with=" do
- config.should_receive(:expect_with).with([:rspec])
- config.expectation_framework = :rspec
+ config.configure_expectation_framework
end
end
@@ -98,8 +92,9 @@ module RSpec::Core
[:rspec, :stdlib].each do |framework|
context "with #{framework}" do
it "requires the adapter for #{framework.inspect}" do
- config.should_receive(:require).with("rspec/core/expecting/with_#{framework}")
config.expect_with framework
+ config.should_receive(:require).with("rspec/core/expecting/with_#{framework}")
+ config.configure_expectation_framework
end
end
end
@@ -107,6 +102,7 @@ module RSpec::Core
it "raises ArgumentError if framework is not supported" do
expect do
config.expect_with :not_supported
+ config.configure_expectation_framework
end.to raise_error(ArgumentError)
end
end
@@ -118,8 +114,8 @@ module RSpec::Core
config.stub(:require)
end
- it "returns false by default" do
- config.should_not be_expecting_with_rspec
+ it "returns true by default" do
+ config.should be_expecting_with_rspec
end
it "returns true when `expect_with :rspec` has been configured" do
View
23 spec/rspec/core/example_group_spec.rb
@@ -906,5 +906,28 @@ def foo; 'foo'; end
end
end
+ describe "a method starting with test_" do
+ it "is generates an an example" do
+ group = ExampleGroup.describe do
+ def test_something_that_fails
+ 1.should eq(2)
+ end
+ end
+
+ group.examples.length.should eq(1)
+ group.examples.first.description.should eq('something_that_fails')
+ group.run.should be_false, "expected a failure"
+ end
+ end
+
+ describe "#subject_for" do
+ it "removes Test or Spec, or Describe from the name" do
+ group = ExampleGroup.describe
+ group.subject_for('FooTest').should eq('Foo')
+ group.subject_for('FooSpec').should eq('Foo')
+ group.subject_for('DescribeFoo').should eq('Foo')
+ end
+ end
+
end
end

No commit comments for this range

Something went wrong with that request. Please try again.