module Spec::Example::ExampleGroupMethods
def build_description_from(*args)
def build_description_from(*args) text = args.inject("") do |description, arg| description << " " unless (description == "" || arg.to_s =~ /^(\s|\.|#)/) description << arg.to_s end text == "" ? nil : text end
def describe(*args, &example_group_block)
end
end
lambda{ @stack.pop }.should raise_error
it "should raise on pop" do
end
@stack = Stack.new
before
describe Stack, "with no elements"
class StackSpec < Spec::ExampleGroup
Makes the describe/it syntax available from a class. For example:
def describe(*args, &example_group_block) raise Spec::Example::NoDescriptionError.new("example group", caller(0)[1]) if args.empty? if example_group_block options = add_options(args) set_location(options, caller(0)[1]) if options[:shared] ExampleGroupFactory.create_shared_example_group(*args, &example_group_block) else subclass(*args, &example_group_block) end else set_description(*args) end end
def described_class
def described_class @described_class ||= Class === described_type ? described_type : nil end
def described_type
def described_type @described_type ||= description_parts.reverse.find {|part| part.is_a?(Module)} end
def description
def description @description ||= ExampleGroupMethods.build_description_from(*description_parts) || to_s end
def description_args
def description_args @description_args ||= [] end
def description_parts #:nodoc:
def description_parts #:nodoc: @description_parts ||= example_group_hierarchy.inject([]) do |parts, example_group_class| [parts << example_group_class.description_args].flatten end end
def dry_run(examples, run_options)
def dry_run(examples, run_options) examples.each do |example| run_options.reporter.example_started(example) run_options.reporter.example_finished(example) end end
def example(description=nil, options={}, backtrace=nil, &implementation)
Creates an instance of the current example group class and adds it to
def example(description=nil, options={}, backtrace=nil, &implementation) example_proxy = ExampleProxy.new(description, options, backtrace || caller(0)[1]) example_proxies << example_proxy example_implementations[example_proxy] = implementation || pending_implementation example_proxy end
def example_group_hierarchy
def example_group_hierarchy @example_group_hierarchy ||= ExampleGroupHierarchy.new(self) end
def example_implementations # :nodoc:
def example_implementations # :nodoc: @example_implementations ||= {} end
def example_method?(method_name)
def example_method?(method_name) should_method?(method_name) end
def example_proxies # :nodoc:
def example_proxies # :nodoc: @example_proxies ||= [] end
def examples(run_options=nil) #:nodoc:
def examples(run_options=nil) #:nodoc: (run_options && run_options.reverse) ? example_proxies.reverse : example_proxies end
def examples_to_run(run_options)
def examples_to_run(run_options) return example_proxies unless examples_were_specified?(run_options) example_proxies.reject do |proxy| matcher = ExampleGroupMethods.matcher_class. new(description.to_s, proxy.description) !matcher.matches?(run_options.examples) end end
def examples_were_specified?(run_options)
def examples_were_specified?(run_options) !run_options.examples.empty? end
def include_constants_in(mod)
def include_constants_in(mod) include mod if (Spec::Ruby.version.to_f >= 1.9) & (Module === mod) & !(Class === mod) end
def include_shared_example_group(shared_example_group)
def include_shared_example_group(shared_example_group) case shared_example_group when SharedExampleGroup include shared_example_group else unless example_group = SharedExampleGroup.find(shared_example_group) raise RuntimeError.new("Shared Example Group '#{shared_example_group}' can not be found") end include(example_group) end end
def inherited(klass) # :nodoc:
def inherited(klass) # :nodoc: super ExampleGroupFactory.register_example_group(klass) end
def it_should_behave_like(*shared_example_groups)
def it_should_behave_like(*shared_example_groups) shared_example_groups.each do |group| include_shared_example_group(group) end end
def method_added(name) # :nodoc:
def method_added(name) # :nodoc: example(name.to_s, {}, caller(0)[1]) {__send__ name.to_s} if example_method?(name.to_s) end
def nested_descriptions
def nested_descriptions example_group_hierarchy.nested_descriptions end
def notify(reporter) # :nodoc:
def notify(reporter) # :nodoc: reporter.example_group_started(ExampleGroupProxy.new(self)) end
def number_of_examples #:nodoc:
def number_of_examples #:nodoc: example_proxies.length end
def options # :nodoc:
def options # :nodoc: @options ||= {} end
def pending_implementation
def pending_implementation lambda { raise(Spec::Example::NotYetImplementedError) } end
def run(run_options)
def run(run_options) examples = examples_to_run(run_options) notify(run_options.reporter) unless examples.empty? return true if examples.empty? return dry_run(examples, run_options) if run_options.dry_run? define_methods_from_predicate_matchers success, before_all_instance_variables = run_before_all(run_options) success, after_all_instance_variables = run_examples(success, before_all_instance_variables, examples, run_options) success = run_after_all(success, after_all_instance_variables, run_options) end
def run_after_all(success, instance_variables, run_options)
def run_after_all(success, instance_variables, run_options) return success if example_group_hierarchy.after_all_parts.empty? example_proxy = ExampleProxy.new("after(:all)") after_all = new(example_proxy) after_all.set_instance_variables_from_hash(instance_variables) example_group_hierarchy.run_after_all(after_all) success rescue Exception => e run_options.reporter.example_failed(example_proxy, e) false end
def run_before_all(run_options)
def run_before_all(run_options) return [true,{}] if example_group_hierarchy.before_all_parts.empty? example_proxy = ExampleProxy.new("before(:all)") before_all = new(example_proxy) begin example_group_hierarchy.run_before_all(before_all) return [true, before_all.instance_variable_hash] rescue Exception => e run_options.reporter.example_failed(example_proxy, e) return [false, before_all.instance_variable_hash] end end
def run_examples(success, instance_variables, examples, run_options)
def run_examples(success, instance_variables, examples, run_options) return [success, instance_variables] unless success after_all_instance_variables = instance_variables examples.each do |example| example_group_instance = new(example, &example_implementations[example]) success &= example_group_instance.execute(run_options, instance_variables) after_all_instance_variables = example_group_instance.instance_variable_hash end return [success, after_all_instance_variables] end
def set_description(*args)
def set_description(*args) @description_args, @options = args_and_options(*args) @backtrace = caller(1) @location = File.expand_path(options[:location]) if options[:location] self end
def should_method?(method_name)
def should_method?(method_name) !(method_name =~ /^should(_not)?$/) && method_name =~ /^should/ && instance_method(method_name).arity < 1 end
def subclass(*args, &example_group_block)
def subclass(*args, &example_group_block) @class_count ||= 0 @class_count += 1 klass = const_set("Subclass_#{@class_count}", Class.new(self)) klass.set_description(*args) klass.include_constants_in(args.last[:scope]) klass.module_eval(&example_group_block) klass end
def xexample(description=nil, opts={}, &block)
def xexample(description=nil, opts={}, &block) Kernel.warn("Example disabled: #{description}") end