diff options
author | Francis Irving <francis@mysociety.org> | 2009-12-02 17:51:30 +0000 |
---|---|---|
committer | Francis Irving <francis@mysociety.org> | 2009-12-02 17:51:30 +0000 |
commit | 5f3139b538d1ff58b719a72d7c7cf05a5b6136b5 (patch) | |
tree | 13597cf6751df3122bfb1f1e5b1699e5c7ec5f93 /vendor/plugins/rspec/lib/spec/example | |
parent | dcf788cab6268a5c9830178d1bdff606f84132ce (diff) |
Part of upgrade to rails 2.3.2
Diffstat (limited to 'vendor/plugins/rspec/lib/spec/example')
7 files changed, 429 insertions, 0 deletions
diff --git a/vendor/plugins/rspec/lib/spec/example/args_and_options.rb b/vendor/plugins/rspec/lib/spec/example/args_and_options.rb new file mode 100644 index 000000000..b74fddd8e --- /dev/null +++ b/vendor/plugins/rspec/lib/spec/example/args_and_options.rb @@ -0,0 +1,27 @@ +module Spec + module Example + module ArgsAndOptions + def args_and_options(*args) # :nodoc: + options = Hash === args.last ? args.pop : {} + return args, options + end + + def add_options(args, options={}) # :nodoc: + args << {} unless Hash === args.last + args.extend WithOptions + args.options.merge!(options) + args.options + end + + def set_location(options, location) # :nodoc: + options[:location] ||= location + end + + module WithOptions # :nodoc: + def options + last + end + end + end + end +end diff --git a/vendor/plugins/rspec/lib/spec/example/before_and_after_hooks.rb b/vendor/plugins/rspec/lib/spec/example/before_and_after_hooks.rb new file mode 100644 index 000000000..9f5039d1e --- /dev/null +++ b/vendor/plugins/rspec/lib/spec/example/before_and_after_hooks.rb @@ -0,0 +1,93 @@ +module Spec + module Example + module BeforeAndAfterHooks + class << self + def before_suite_parts # :nodoc: + @before_suite_parts ||= [] + end + + def after_suite_parts # :nodoc: + @after_suite_parts ||= [] + end + end + + # Registers a block to be executed before examples. <tt>scope</tt> can be + # <tt>:each</tt> (default), <tt>:all</tt>, or <tt>:suite</tt>. When + # <tt>:each</tt>, the block is executed before each example. When + # <tt>:all</tt>, the block is executed only once before any examples are + # run. + def append_before(scope = :each, &block) + before_parts(scope) << block + end + alias_method :before, :append_before + + # Registers a block to be executed before each example. + # This method prepends +block+ to existing before blocks. + # + # See <tt>append_before</tt> for scoping semantics. + def prepend_before(scope = :each, &block) + before_parts(scope).unshift(block) + end + + # Registers a block to be executed after each example. + # This method prepends +block+ to existing after blocks. + # + # See <tt>append_before</tt> for scoping semantics. + def prepend_after(scope = :each, &block) + after_parts(scope).unshift(block) + end + alias_method :after, :prepend_after + + # Registers a block to be executed after each example. + # This method appends +block+ to existing after blocks. + # + # See <tt>append_before</tt> for scoping semantics. + def append_after(scope = :each, &block) + after_parts(scope) << block + end + + def before_each_parts # :nodoc: + @before_each_parts ||= [] + end + + def after_each_parts # :nodoc: + @after_each_parts ||= [] + end + + def before_all_parts # :nodoc: + @before_all_parts ||= [] + end + + def after_all_parts # :nodoc: + @after_all_parts ||= [] + end + + def before_suite_parts # :nodoc: + BeforeAndAfterHooks.before_suite_parts + end + + def after_suite_parts # :nodoc: + BeforeAndAfterHooks.after_suite_parts + end + + private + + def before_parts(scope) + case scope + when :each; before_each_parts + when :all; before_all_parts + when :suite; before_suite_parts + end + end + + def after_parts(scope) + case scope + when :each; after_each_parts + when :all; after_all_parts + when :suite; after_suite_parts + end + end + + end + end +end diff --git a/vendor/plugins/rspec/lib/spec/example/example_group_hierarchy.rb b/vendor/plugins/rspec/lib/spec/example/example_group_hierarchy.rb new file mode 100644 index 000000000..f2c9fb5cd --- /dev/null +++ b/vendor/plugins/rspec/lib/spec/example/example_group_hierarchy.rb @@ -0,0 +1,53 @@ +module Spec + module Example + class ExampleGroupHierarchy < Array + def initialize(example_group_class) + push example_group_class + if example_group_class.respond_to?(:superclass) && example_group_class.superclass.respond_to?(:example_group_hierarchy) + unshift example_group_class.superclass.example_group_hierarchy + flatten! + end + end + + def run_before_all(example) + example.eval_each_fail_fast(before_all_parts) + end + + def run_before_each(example) + example.eval_each_fail_fast(before_each_parts) + end + + def run_after_each(example) + example.eval_each_fail_slow(after_each_parts) + end + + def run_after_all(example) + example.eval_each_fail_slow(after_all_parts) + end + + def before_all_parts + @before_all_parts ||= collect {|klass| klass.before_all_parts}.flatten + end + + def before_each_parts + @before_each_parts ||= collect {|klass| klass.before_each_parts}.flatten + end + + def after_each_parts + @after_each_parts ||= reverse.collect {|klass| klass.after_each_parts}.flatten + end + + def after_all_parts + @after_all_parts ||= reverse.collect {|klass| klass.after_all_parts}.flatten + end + + def nested_descriptions + @nested_descriptions ||= collect {|eg| nested_description_from(eg) == "" ? nil : nested_description_from(eg) }.compact + end + + def nested_description_from(example_group) + example_group.description_args.join + end + end + end +end diff --git a/vendor/plugins/rspec/lib/spec/example/example_group_proxy.rb b/vendor/plugins/rspec/lib/spec/example/example_group_proxy.rb new file mode 100644 index 000000000..3c258d61f --- /dev/null +++ b/vendor/plugins/rspec/lib/spec/example/example_group_proxy.rb @@ -0,0 +1,61 @@ +module Spec + module Example + # Lightweight proxy for an example group. This is the object that is passed + # to Spec::Runner::Formatter::BaseFormatter#example_group_started + class ExampleGroupProxy + + def initialize(example_group) # :nodoc: + @description = example_group.description + @nested_descriptions = example_group.nested_descriptions + @examples = example_group.example_proxies + @location = example_group.location + @backtrace = example_group.location # deprecated - see the backtrace method below + @options = example_group.options.dup + @options.delete(:location) + @options.delete(:scope) + end + + # Optional hash passed to the example group declaration. Note that RSpec uses + # this hash internally and reserves the keys :location and :scope for its own + # use (and removes them from this hash) + attr_reader :options + + # This is the description passed to the <tt>describe()</tt> method or any + # of its aliases + attr_reader :description + + # Used by Spec::Runner::Formatter::NestedTextFormatter to access the + # description of each example group in a nested group separately. + attr_reader :nested_descriptions + + # A collection of ExampleGroupProxy objects, one for each example + # declared in this group. + attr_reader :examples + + # The file and line number at which the proxied example group + # was declared. This is extracted from <tt>caller</tt>, and is therefore + # formatted as an individual line in a backtrace. + attr_reader :location + + # Deprecated - use location() instead + def backtrace + Spec::deprecate("ExampleGroupProxy#backtrace","ExampleGroupProxy#location") + @backtrace + end + + # Deprecated - just use gsub on the description instead. + def filtered_description(regexp) + Spec::deprecate("ExampleGroupProxy#filtered_description","gsub (or similar) to modify ExampleGroupProxy#description") + ExampleGroupMethods.build_description_from( + *nested_descriptions.collect do |description| + description =~ regexp ? description.gsub($1, "") : description + end + ) + end + + def ==(other) # :nodoc: + other.description == description + end + end + end +end diff --git a/vendor/plugins/rspec/lib/spec/example/example_proxy.rb b/vendor/plugins/rspec/lib/spec/example/example_proxy.rb new file mode 100644 index 000000000..f726d0e70 --- /dev/null +++ b/vendor/plugins/rspec/lib/spec/example/example_proxy.rb @@ -0,0 +1,41 @@ +module Spec + module Example + # Lightweight proxy for an example. This is the object that is passed to + # example-related methods in Spec::Runner::Formatter::BaseFormatter + class ExampleProxy + + def initialize(description=nil, options={}, location=nil) # :nodoc: + @description, @options, @location = description, options, location + end + + # Optional hash passed to the example declaration + attr_reader :options + + # This is the docstring passed to the <tt>it()</tt> method or any + # of its aliases + attr_reader :description + + # The file and line number at which the represented example + # was declared. This is extracted from <tt>caller</tt>, and is therefore + # formatted as an individual line in a backtrace. + attr_reader :location + + # Deprecated - use location() + def backtrace + Spec.deprecate("ExampleProxy#backtrace","ExampleProxy#location") + location + end + + # Convenience method for example group - updates the value of + # <tt>description</tt> and returns self. + def update(description) # :nodoc: + @description = description + self + end + + def ==(other) # :nodoc: + (other.description == description) & (other.location == location) + end + end + end +end diff --git a/vendor/plugins/rspec/lib/spec/example/predicate_matchers.rb b/vendor/plugins/rspec/lib/spec/example/predicate_matchers.rb new file mode 100644 index 000000000..c3c319519 --- /dev/null +++ b/vendor/plugins/rspec/lib/spec/example/predicate_matchers.rb @@ -0,0 +1,46 @@ +module Spec + module Example + module PredicateMatchers + # :call-seq: + # predicate_matchers[matcher_name] = method_on_object + # predicate_matchers[matcher_name] = [method1_on_object, method2_on_object] + # + # Dynamically generates a custom matcher that will match + # a predicate on your class. RSpec provides a couple of these + # out of the box: + # + # exist (for state expectations) + # File.should exist("path/to/file") + # + # an_instance_of (for mock argument matchers) + # mock.should_receive(:message).with(an_instance_of(String)) + # + # == Examples + # + # class Fish + # def can_swim? + # true + # end + # end + # + # describe Fish do + # predicate_matchers[:swim] = :can_swim? + # it "should swim" do + # Fish.new.should swim + # end + # end + def predicate_matchers + @predicate_matchers ||= Spec::HashWithDeprecationNotice.new("predicate_matchers", "the new Matcher DSL") + end + + def define_methods_from_predicate_matchers # :nodoc: + predicate_matchers.each_pair do |matcher_method, method_on_object| + define_method matcher_method do |*args| + eval("be_#{method_on_object.to_s.gsub('?','')}(*args)") + end + end + end + + end + end +end diff --git a/vendor/plugins/rspec/lib/spec/example/subject.rb b/vendor/plugins/rspec/lib/spec/example/subject.rb new file mode 100644 index 000000000..944edca45 --- /dev/null +++ b/vendor/plugins/rspec/lib/spec/example/subject.rb @@ -0,0 +1,108 @@ +module Spec + module Example + module Subject + module ExampleGroupMethods + # Defines an explicit subject for an example group which can then be the + # implicit receiver (through delegation) of calls to +should+. + # + # == Examples + # + # describe CheckingAccount, "with $50" do + # subject { CheckingAccount.new(:amount => 50, :currency => :USD) } + # it { should have_a_balance_of(50, :USD) } + # it { should_not be_overdrawn } + # its(:currency) { should == :USD } + # end + # + # See +ExampleMethods#should+ for more information about this approach. + def subject(&block) + block.nil? ? + explicit_subject || implicit_subject : @explicit_subject_block = block + end + + def its(attribute, &block) + describe(attribute) do + define_method(:subject) { super().send(attribute) } + it(&block) + end + end + + attr_reader :explicit_subject_block # :nodoc: + + private + + def explicit_subject + group = self + while group.respond_to?(:explicit_subject_block) + return group.explicit_subject_block if group.explicit_subject_block + group = group.superclass + end + end + + def implicit_subject + (described_class ? lambda {described_class.new} : lambda {description_args.first}) + end + end + + module ExampleMethods + + alias_method :__should_for_example_group__, :should + alias_method :__should_not_for_example_group__, :should_not + + # Returns the subject defined in ExampleGroupMethods#subject. The + # subject block is only executed once per example, the result of which + # is cached and returned by any subsequent calls to +subject+. + # + # If a class is passed to +describe+ and no subject is explicitly + # declared in the example group, then +subject+ will return a new + # instance of that class. + # + # == Examples + # + # # explicit subject defined by the subject method + # describe Person do + # subject { Person.new(:birthdate => 19.years.ago) } + # it "should be eligible to vote" do + # subject.should be_eligible_to_vote + # end + # end + # + # # implicit subject => { Person.new } + # describe Person do + # it "should be eligible to vote" do + # subject.should be_eligible_to_vote + # end + # end + def subject + @subject ||= instance_eval(&self.class.subject) + end + + # When +should+ is called with no explicit receiver, the call is + # delegated to the object returned by +subject+. Combined with + # an implicit subject (see +subject+), this supports very concise + # expressions. + # + # == Examples + # + # describe Person do + # it { should be_eligible_to_vote } + # end + def should(matcher=nil, message=nil) + self == subject ? self.__should_for_example_group__(matcher) : subject.should(matcher,message) + end + + # Just like +should+, +should_not+ delegates to the subject (implicit or + # explicit) of the example group. + # + # == Examples + # + # describe Person do + # it { should_not be_eligible_to_vote } + # end + def should_not(matcher=nil, message=nil) + self == subject ? self.__should_not_for_example_group__(matcher) : subject.should_not(matcher,message) + end + end + end + end +end |