Permalink
Browse files

add ExampleGroup.create, which decouples creation of an ExampleGroup

class from registration in the world (making spec'ing much easier).
  • Loading branch information...
dchelimsky committed Feb 10, 2010
1 parent 5089a32 commit 11d053f21919b911a4e2a914564bc5c2fed167b9
@@ -58,8 +58,12 @@ def self.examples_to_run
@_examples_to_run ||= []
end
+ def self.superclass_metadata
+ self.superclass.respond_to?(:metadata) ? self.superclass.metadata : nil
+ end
+
def self.set_it_up(*args)
- @metadata = Rspec::Core::Metadata.process(self.superclass.metadata, *args)
+ @metadata = Rspec::Core::Metadata.process(superclass_metadata, *args)
Rspec::Core.configuration.find_modules(self).each do |include_or_extend, mod, opts|
if include_or_extend == :extend
@@ -97,15 +101,24 @@ def self.file_path
def self.describe(*args, &example_group_block)
raise(ArgumentError, "No arguments given. You must a least supply a type or description") if args.empty?
raise(ArgumentError, "You must supply a block when calling describe") if example_group_block.nil?
+ @_subclass_count ||= 0
+ @_subclass_count += 1
+ const_set(
+ "NestedLevel_#{@_subclass_count}",
+ _build(Class.new(self), caller, args, &example_group_block)
+ )
+ end
- # TODO: Pull out the below into a metadata object, that we can defer the subclassing if necessary
- # describe 'foo', :shared => true will need this to be completed first
- subclass('NestedLevel') do
- args << {} unless args.last.is_a?(Hash)
- args.last.update(:example_group_block => example_group_block, :caller => caller)
- set_it_up(*args)
- module_eval(&example_group_block)
- end
+ def self.create(*args, &example_group_block)
+ _build(dup, caller, args, &example_group_block)
+ end
+
+ def self._build(klass, given_caller, args, &example_group_block)
+ args << {} unless args.last.is_a?(Hash)
+ args.last.update(:example_group_block => example_group_block, :caller => given_caller)
+ klass.set_it_up(*args)
+ klass.module_eval(&example_group_block) if example_group_block
+ klass
end
class << self
@@ -137,7 +150,9 @@ def self.before_all_ivars
end
def self.eval_before_alls(running_example)
- superclass.before_all_ivars.each { |ivar, val| running_example.instance_variable_set(ivar, val) }
+ if superclass.respond_to?(:before_all_ivars)
+ superclass.before_all_ivars.each { |ivar, val| running_example.instance_variable_set(ivar, val) }
+ end
Rspec::Core.configuration.find_advice(:before, :all, self).each { |blk| running_example.instance_eval(&blk) }
before_alls.each { |blk| running_example.instance_eval(&blk) }
@@ -180,16 +195,6 @@ def self.run_examples(example_world, reporter)
end.all?
end
- def self.subclass(base_name, &body) # :nodoc:
- @_subclass_count ||= 0
- @_subclass_count += 1
- klass = Class.new(self)
- class_name = "#{base_name}_#{@_subclass_count}"
- const_set(class_name, klass)
- klass.instance_eval(&body)
- klass
- end
-
def self.to_s
self == Rspec::Core::ExampleGroup ? 'Rspec::Core::ExampleGroup' : name
end
@@ -133,7 +133,7 @@ def you_call_this_a_blt?
it "should include the given module into each matching example group" do
Rspec::Core.configuration.include(InstanceLevelMethods, :magic_key => :include)
- group = isolated_example_group('does like, stuff and junk', :magic_key => :include) { }
+ group = Rspec::Core::ExampleGroup.create('does like, stuff and junk', :magic_key => :include) { }
group.should_not respond_to(:you_call_this_a_blt?)
group.new.you_call_this_a_blt?.should == "egad man, where's the mayo?!?!?"
end
@@ -151,10 +151,8 @@ def that_thing
it "should extend the given module into each matching example group" do
Rspec::Core.configuration.extend(ThatThingISentYou, :magic_key => :extend)
- group = Rspec::Core::ExampleGroup.describe(ThatThingISentYou, :magic_key => :extend) { }
-
+ group = Rspec::Core::ExampleGroup.create(ThatThingISentYou, :magic_key => :extend) { }
group.should respond_to(:that_thing)
- remove_last_example_group_from_world
end
end
Oops, something went wrong.

0 comments on commit 11d053f

Please sign in to comment.