diff options
author | francis <francis> | 2008-09-04 06:10:25 +0000 |
---|---|---|
committer | francis <francis> | 2008-09-04 06:10:25 +0000 |
commit | 5bde1025dc4d43ea53f63107b88711ebf8942408 (patch) | |
tree | 962c8b1fb32186fbd1ab15050ede8e560d9a63f6 /vendor/plugins/rspec/lib/spec/example | |
parent | ce2cf5ed73d81180e9f88d590daaa23989ee9472 (diff) |
rspec for rails 2.1
Diffstat (limited to 'vendor/plugins/rspec/lib/spec/example')
6 files changed, 130 insertions, 47 deletions
diff --git a/vendor/plugins/rspec/lib/spec/example/configuration.rb b/vendor/plugins/rspec/lib/spec/example/configuration.rb index 674184727..cd3f46909 100644 --- a/vendor/plugins/rspec/lib/spec/example/configuration.rb +++ b/vendor/plugins/rspec/lib/spec/example/configuration.rb @@ -7,14 +7,20 @@ module Spec # config.mock_with :rspec, :mocha, :flexmock, or :rr # end # - # To use any other mock framework, you'll have to provide - # your own adapter. This is simply a module that responds to - # setup_mocks_for_rspec, verify_mocks_for_rspec and teardown_mocks_for_rspec. + # To use any other mock framework, you'll have to provide your own + # adapter. This is simply a module that responds to the following + # methods: + # + # setup_mocks_for_rspec + # verify_mocks_for_rspec + # teardown_mocks_for_rspec. + # # These are your hooks into the lifecycle of a given example. RSpec will - # call setup_mocks_for_rspec before running anything else in each Example. - # After executing the #after methods, RSpec will then call verify_mocks_for_rspec - # and teardown_mocks_for_rspec (this is guaranteed to run even if there are - # failures in verify_mocks_for_rspec). + # call setup_mocks_for_rspec before running anything else in each + # Example. After executing the #after methods, RSpec will then call + # verify_mocks_for_rspec and teardown_mocks_for_rspec (this is + # guaranteed to run even if there are failures in + # verify_mocks_for_rspec). # # Once you've defined this module, you can pass that to mock_with: # @@ -35,12 +41,18 @@ module Spec @mock_framework ||= mock_framework_path("rspec") end - # Declares modules to be included in all example groups (<tt>describe</tt> blocks). - # - # config.include(My::Bottle, My::Cup) - # - # If you want to restrict the inclusion to a subset of all the example groups then - # specify this in a Hash as the last argument: + # :call-seq: + # include(Some::Helpers) + # include(Some::Helpers, More::Helpers) + # include(My::Helpers, :type => :key) + # + # Declares modules to be included in multiple example groups + # (<tt>describe</tt> blocks). With no :type, the modules listed will be + # included in all example groups. Use :type to restrict the inclusion to + # a subset of example groups. The value assigned to :type should be a + # key that maps to a class that is either a subclass of + # Spec::Example::ExampleGroup or extends Spec::Example::ExampleGroupMethods + # and includes Spec::Example::ExampleMethods # # config.include(My::Pony, My::Horse, :type => :farm) # @@ -70,7 +82,7 @@ module Spec # # This makes it possible to say: # - # person.should swim # passes if person.should_swim? returns true + # person.should swim # passes if person.can_swim? returns true # def predicate_matchers @predicate_matchers ||= {} @@ -85,10 +97,11 @@ module Spec ) example_group.prepend_before(scope, &proc) end + # Appends a global <tt>before</tt> block to all example groups. # - # If you want to restrict the block to a subset of all the example groups then - # specify this in a Hash as the last argument: + # If you want to restrict the block to a subset of all the example + # groups then specify this in a Hash as the last argument: # # config.prepend_before(:all, :type => :farm) # @@ -115,6 +128,7 @@ module Spec example_group.prepend_after(scope, &proc) end alias_method :after, :prepend_after + # Appends a global <tt>after</tt> block to all example groups. # See #append_before for filtering semantics. def append_after(*args, &proc) diff --git a/vendor/plugins/rspec/lib/spec/example/example_group.rb b/vendor/plugins/rspec/lib/spec/example/example_group.rb index d6e156f93..35997f0c4 100644 --- a/vendor/plugins/rspec/lib/spec/example/example_group.rb +++ b/vendor/plugins/rspec/lib/spec/example/example_group.rb @@ -1,6 +1,7 @@ module Spec module Example - # The superclass for all regular RSpec examples. + # Base class for customized example groups. Use this if you + # want to make a custom example group. class ExampleGroup extend Spec::Example::ExampleGroupMethods include Spec::Example::ExampleMethods diff --git a/vendor/plugins/rspec/lib/spec/example/example_group_factory.rb b/vendor/plugins/rspec/lib/spec/example/example_group_factory.rb index 0414a3b96..c5caf4c9c 100644 --- a/vendor/plugins/rspec/lib/spec/example/example_group_factory.rb +++ b/vendor/plugins/rspec/lib/spec/example/example_group_factory.rb @@ -7,52 +7,54 @@ module Spec default(ExampleGroup) end - # Registers an example group class +klass+ with the symbol - # +type+. For example: + # Registers an example group class +klass+ with the symbol +type+. For + # example: # - # Spec::Example::ExampleGroupFactory.register(:farm, Spec::Farm::Example::FarmExampleGroup) + # Spec::Example::ExampleGroupFactory.register(:farm, FarmExampleGroup) # - # This will cause Main#describe from a file living in - # <tt>spec/farm</tt> to create example group instances of type - # Spec::Farm::Example::FarmExampleGroup. - def register(id, example_group_class) - @example_group_types[id] = example_group_class + # With that you can append a hash with :type => :farm to the describe + # method and it will load an instance of FarmExampleGroup. + # + # describe Pig, :type => :farm do + # ... + # + # If you don't use the hash explicitly, <tt>describe</tt> will + # implicitly use an instance of FarmExampleGroup for any file loaded + # from the <tt>./spec/farm</tt> directory. + def register(key, example_group_class) + @example_group_types[key] = example_group_class end # Sets the default ExampleGroup class def default(example_group_class) old = @example_group_types @example_group_types = Hash.new(example_group_class) - @example_group_types.merge(old) if old + @example_group_types.merge!(old) if old end - def get(id=nil) - if @example_group_types.values.include?(id) - id + def get(key=nil) + if @example_group_types.values.include?(key) + key else - @example_group_types[id] + @example_group_types[key] end end def create_example_group(*args, &block) opts = Hash === args.last ? args.last : {} - if opts[:shared] - SharedExampleGroup.new(*args, &block) - else - superclass = determine_superclass(opts) - superclass.describe(*args, &block) - end + superclass = determine_superclass(opts) + superclass.describe(*args, &block) end protected def determine_superclass(opts) - id = if opts[:type] + key = if opts[:type] opts[:type] elsif opts[:spec_path] =~ /spec(\\|\/)(#{@example_group_types.keys.join('|')})/ $2 == '' ? nil : $2.to_sym end - get(id) + get(key) end end diff --git a/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb b/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb index 8f73a9853..64e90cdb2 100644 --- a/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb +++ b/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb @@ -11,13 +11,14 @@ module Spec end end - attr_reader :description_text, :description_args, :description_options, :spec_path + attr_reader :description_text, :description_args, :description_options, :spec_path, :registration_binding_block def inherited(klass) super - klass.register + klass.register {} + Spec::Runner.register_at_exit_hook end - + # Makes the describe/it syntax available from a class. For example: # # class StackSpec < Spec::ExampleGroup @@ -33,10 +34,17 @@ module Spec # end # def describe(*args, &example_group_block) + args << {} unless Hash === args.last if example_group_block - self.subclass("Subclass") do - describe(*args) - module_eval(&example_group_block) + params = args.last + params[:spec_path] = eval("caller(0)[1]", example_group_block) unless params[:spec_path] + if params[:shared] + SharedExampleGroup.new(*args, &example_group_block) + else + self.subclass("Subclass") do + describe(*args) + module_eval(&example_group_block) + end end else set_description(*args) @@ -44,6 +52,7 @@ module Spec self end end + alias :context :describe # Use this to pull in examples from shared example groups. # See Spec::Runner for information about shared example groups. @@ -106,11 +115,12 @@ module Spec def xit(description=nil, opts={}, &block) Kernel.warn("Example disabled: #{description}") end + alias_method :xspecify, :xit def run examples = examples_to_run - return true if examples.empty? reporter.add_example_group(self) + return true if examples.empty? return dry_run(examples) if dry_run? plugin_mock_framework @@ -149,10 +159,12 @@ module Spec @description_text = ExampleGroupMethods.description_text(*args) @spec_path = File.expand_path(options[:spec_path]) if options[:spec_path] if described_type.class == Module - include described_type + @described_module = described_type end self end + + attr_reader :described_module def examples #:nodoc: examples = example_objects.dup @@ -236,7 +248,8 @@ module Spec @after_each_parts = nil end - def register + def register(®istration_binding_block) + @registration_binding_block = registration_binding_block rspec_options.add_example_group self end @@ -244,6 +257,10 @@ module Spec rspec_options.remove_example_group self end + def registration_backtrace + eval("caller", registration_binding_block.binding) + end + def run_before_each(example) execute_in_class_hierarchy do |example_group| example.eval_each_fail_fast(example_group.before_each_parts) @@ -378,6 +395,7 @@ module Spec case scope when :each; before_each_parts when :all; before_all_parts + when :suite; rspec_options.before_suite_parts end end @@ -385,6 +403,7 @@ module Spec case scope when :each; after_each_parts when :all; after_all_parts + when :suite; rspec_options.after_suite_parts end end diff --git a/vendor/plugins/rspec/lib/spec/example/example_methods.rb b/vendor/plugins/rspec/lib/spec/example/example_methods.rb index 6dd4c9c72..d4d716c2d 100644 --- a/vendor/plugins/rspec/lib/spec/example/example_methods.rb +++ b/vendor/plugins/rspec/lib/spec/example/example_methods.rb @@ -3,6 +3,8 @@ module Spec module ExampleMethods extend ExampleGroupMethods extend ModuleReopeningFix + include ModuleInclusionWarnings + PENDING_EXAMPLE_BLOCK = lambda { raise Spec::Example::ExamplePendingError.new("Not Yet Implemented") @@ -63,6 +65,10 @@ module Spec def description @_defined_description || @_matcher_description || "NO NAME" end + + def __full_description + "#{self.class.description} #{self.description}" + end def set_instance_variables_from_hash(ivars) ivars.each do |variable_name, value| @@ -81,6 +87,10 @@ module Spec Spec::Matchers.clear_generated_description end end + + def implementation_backtrace + eval("caller", @_implementation) + end protected include Matchers diff --git a/vendor/plugins/rspec/lib/spec/example/module_inclusion_warnings.rb b/vendor/plugins/rspec/lib/spec/example/module_inclusion_warnings.rb new file mode 100644 index 000000000..c65faf2dd --- /dev/null +++ b/vendor/plugins/rspec/lib/spec/example/module_inclusion_warnings.rb @@ -0,0 +1,37 @@ +module Spec + module Example + # In the future, modules will no longer be automatically included + # in the Example Group (based on the description name); when that + # time comes, this code should be removed. + module ModuleInclusionWarnings + # Thanks, Francis Hwang. + class MethodDispatcher + def initialize(mod, target=nil) + @mod = mod + @target = target + end + + def respond_to?(sym) + @mod && @mod.instance_methods.include?(sym.to_s) + end + + def call(sym, *args, &blk) + Kernel.warn("Modules will no longer be automatically included in RSpec version 1.1.4. Called from #{caller[2]}") + @target.extend @mod + @target.send(sym, *args, &blk) + end + end + + def respond_to?(sym) + MethodDispatcher.new(self.class.described_module).respond_to?(sym) ? true : super + end + + private + + def method_missing(sym, *args, &blk) + md = MethodDispatcher.new(self.class.described_module, self) + self.respond_to?(sym) ? md.call(sym, *args, &blk) : super + end + end + end +end |