moduleSpecmoduleExamplemoduleExampleGroupMethodsclass<<selfattr_accessor:matcher_classdefbuild_description_from(*args)text=args.inject("")do|description,arg|description<<" "unless(description==""||arg.to_s=~/^(\s|\.|#)/)description<<arg.to_sendtext==""?nil:textendendincludeSpec::Example::BeforeAndAfterHooksincludeSpec::Example::Subject::ExampleGroupMethodsincludeSpec::Example::PredicateMatchersincludeSpec::Example::ArgsAndOptionsattr_reader:locationdefoptions# :nodoc:@options||={}enddefinherited(klass)# :nodoc:superExampleGroupFactory.register_example_group(klass)end# Makes the describe/it syntax available from a class. For example:## class StackSpec < Spec::ExampleGroup# describe Stack, "with no elements"## before# @stack = Stack.new# end## it "should raise on pop" do# lambda{ @stack.pop }.should raise_error# end# end#defdescribe(*args,&example_group_block)raiseSpec::Example::NoDescriptionError.new("example group",caller(0)[1])ifargs.empty?ifexample_group_blockoptions=add_options(args)set_location(options,caller(0)[1])ifoptions[:shared]ExampleGroupFactory.create_shared_example_group(*args,&example_group_block)elsesubclass(*args,&example_group_block)endelseset_description(*args)endendalias:context:describe# Use this to pull in examples from shared example groups.defit_should_behave_like(*shared_example_groups)shared_example_groups.eachdo|group|include_shared_example_group(group)endend# Creates an instance of the current example group class and adds it to# a collection of examples of the current example group.defexample(description=nil,options={},backtrace=nil,&implementation)example_proxy=ExampleProxy.new(description,options,backtrace||caller(0)[1])example_proxies<<example_proxyexample_implementations[example_proxy]=implementation||pending_implementationexample_proxyenddefpending_implementationlambda{raise(Spec::Example::NotYetImplementedError)}endalias_method:it,:examplealias_method:specify,:example# Use this to temporarily disable an example.defxexample(description=nil,opts={},&block)Kernel.warn("Example disabled: #{description}")endalias_method:xit,:xexamplealias_method:xspecify,:xexampledefrun(run_options)examples=examples_to_run(run_options)notify(run_options.reporter)unlessexamples.empty?returntrueifexamples.empty?returndry_run(examples,run_options)ifrun_options.dry_run?define_methods_from_predicate_matcherssuccess,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)enddefset_description(*args)@description_args,@options=args_and_options(*args)@backtrace=caller(1)@location=File.expand_path(options[:location])ifoptions[:location]selfenddefnotify(reporter)# :nodoc:reporter.example_group_started(ExampleGroupProxy.new(self))enddefdescription@description||=ExampleGroupMethods.build_description_from(*description_parts)||to_senddefdescribed_type@described_type||=description_parts.reverse.find{|part|part.is_a?(Module)}enddefdescribed_class@described_class||=Class===described_type?described_type:nilenddefdescription_args@description_args||=[]enddefdescription_parts#:nodoc:@description_parts||=example_group_hierarchy.inject([])do|parts,example_group_class|[parts<<example_group_class.description_args].flattenendenddefexample_proxies# :nodoc:@example_proxies||=[]enddefexample_implementations# :nodoc:@example_implementations||={}enddefexamples(run_options=nil)#:nodoc:(run_options&&run_options.reverse)?example_proxies.reverse:example_proxiesenddefnumber_of_examples#:nodoc:example_proxies.lengthenddefexample_group_hierarchy@example_group_hierarchy||=ExampleGroupHierarchy.new(self)enddefnested_descriptionsexample_group_hierarchy.nested_descriptionsenddefinclude_constants_in(mod)includemodif(Spec::Ruby.version.to_f>=1.9)&(Module===mod)&!(Class===mod)endprivatedefsubclass(*args,&example_group_block)@class_count||=0@class_count+=1klass=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)klassenddefdry_run(examples,run_options)examples.eachdo|example|run_options.reporter.example_started(example)run_options.reporter.example_finished(example)endenddefrun_before_all(run_options)return[true,{}]ifexample_group_hierarchy.before_all_parts.empty?example_proxy=ExampleProxy.new("before(:all)")before_all=new(example_proxy)beginexample_group_hierarchy.run_before_all(before_all)return[true,before_all.instance_variable_hash]rescueException=>erun_options.reporter.example_failed(example_proxy,e)return[false,before_all.instance_variable_hash]endenddefrun_examples(success,instance_variables,examples,run_options)return[success,instance_variables]unlesssuccessafter_all_instance_variables=instance_variablesexamples.eachdo|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_hashendreturn[success,after_all_instance_variables]enddefrun_after_all(success,instance_variables,run_options)returnsuccessifexample_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)successrescueException=>erun_options.reporter.example_failed(example_proxy,e)falseenddefexamples_to_run(run_options)returnexample_proxiesunlessexamples_were_specified?(run_options)example_proxies.rejectdo|proxy|matcher=ExampleGroupMethods.matcher_class.new(description.to_s,proxy.description)!matcher.matches?(run_options.examples)endenddefexamples_were_specified?(run_options)!run_options.examples.empty?enddefmethod_added(name)# :nodoc:example(name.to_s,{},caller(0)[1]){__send__name.to_s}ifexample_method?(name.to_s)enddefexample_method?(method_name)should_method?(method_name)enddefshould_method?(method_name)!(method_name=~/^should(_not)?$/)&&method_name=~/^should/&&instance_method(method_name).arity<1enddefinclude_shared_example_group(shared_example_group)caseshared_example_groupwhenSharedExampleGroupincludeshared_example_groupelseunlessexample_group=SharedExampleGroup.find(shared_example_group)raiseRuntimeError.new("Shared Example Group '#{shared_example_group}' can not be found")endinclude(example_group)endendendendend