module Cucumber::StepMother
def After(*tag_names, &proc)
def After(*tag_names, &proc) register_hook(:after, tag_names, proc) end
def Before(*tag_names, &proc)
Registers a Before proc. You can call this method as many times as you
def Before(*tag_names, &proc) register_hook(:before, tag_names, proc) end
def World(*world_modules, &proc)
World(MyModule)
end
MyClass.new
World do
From Cucumber 0.3 the recommended way to do this is:
end
world.extend(MyModule)
World do |world| # NOT SUPPORTED FROM 0.3
end
MyClass.new
World do |world| # NOT SUPPORTED FROM 0.3
to the next +proc+. Example:
calling the method several times). The result of each +proc+ would be yielded
any +world_modules+. Instead you would register several Proc objects (by
In earlier versions of Cucumber (before 0.3) you could not register
Cucumber will not yield anything to the +proc+ (like it used to do before v0.3).
one Proc you will get an error.
like (to register more modules), but if you try to register more than
features/support. You can call this method as many times as you
This method is typically called from one or more Ruby scripts under
will be mixed into this Object (via Object#extend).
Object that the scenario's steps will run within. Any +world_modules+
The +proc+ will be executed once before each scenario to create an
Registers any number of +world_modules+ (Ruby Modules) and/or a Proc.
def World(*world_modules, &proc) if(proc) raise MultipleWorld.new(@world_proc, proc) if @world_proc @world_proc = proc end @world_modules ||= [] @world_modules += world_modules end
def after(scenario)
def after(scenario) execute_after(scenario) nil_world! end
def alias_adverb(adverb)
def alias_adverb(adverb) adverb = adverb.gsub(/\s/, '') alias_method adverb, :register_step_definition end
def before(scenario)
def before(scenario) unless current_world new_world! execute_before(scenario) end end
def before_and_after(scenario, skip=false)
def before_and_after(scenario, skip=false) before(scenario) unless skip yield after(scenario) unless skip scenario_visited(scenario) end
def best_matches(step_name, step_matches)
def best_matches(step_name, step_matches) max_arg_length = step_matches.map {|step_match| step_match.args.length }.max top_groups = step_matches.select {|step_match| step_match.args.length == max_arg_length } if top_groups.length > 1 shortest_capture_length = top_groups.map {|step_match| step_match.args.inject(0) {|sum, c| sum + c.length } }.min top_groups.select {|step_match| step_match.args.inject(0) {|sum, c| sum + c.length } == shortest_capture_length } else top_groups end end
def check_nil(o, proc)
def check_nil(o, proc) if o.nil? begin raise NilWorld.new rescue NilWorld => e e.backtrace.clear e.backtrace.push(proc.backtrace_line("World")) raise e end else o end end
def connect_world
def connect_world @current_world.__cucumber_step_mother = self @current_world.__cucumber_visitor = @visitor end
def create_world!
def create_world! if(@world_proc) @current_world = @world_proc.call check_nil(@current_world, @world_proc) else @current_world = Object.new end end
def current_world
def current_world @current_world end
def execute_after(scenario)
def execute_after(scenario) return if options[:dry_run] hooks_for(:after, scenario).each do |hook| hook.execute_in(@current_world, scenario, 'After') end end
def execute_before(scenario)
def execute_before(scenario) return if options[:dry_run] hooks_for(:before, scenario).each do |hook| hook.execute_in(@current_world, scenario, 'Before') end end
def extend_world
def extend_world @current_world.extend(World) @current_world.extend(::Spec::Matchers) if defined?(::Spec::Matchers) (@world_modules || []).each do |mod| @current_world.extend(mod) end end
def hooks
def hooks @hooks ||= Hash.new {|hash, phase| hash[phase] = []} end
def hooks_for(phase, scenario)
def hooks_for(phase, scenario) hooks[phase].select{|hook| scenario.accept_hook?(hook)} end
def max_step_definition_length
def max_step_definition_length @max_step_definition_length ||= step_definitions.map{|step_definition| step_definition.text_length}.max end
def new_world!
def new_world! return if options[:dry_run] create_world! extend_world connect_world @current_world end
def nil_world!
def nil_world! @current_world = nil end
def options
def options @options || {} end
def options
def options @options || {} end
def register_hook(phase, tags, proc)
def register_hook(phase, tags, proc) hook = Hook.new(tags, proc) hooks[phase] << hook hook end
def register_step_definition(regexp, &proc)
object is created for each scenario and is shared across
object, which is defined by #World. A new world
The +&proc+ gets executed in the context of a world
create your own aliases.
See Cucumber#alias_steps for details on how to
to Given, When and Then.
Registers a new StepDefinition. This method is aliased
def register_step_definition(regexp, &proc) step_definition = StepDefinition.new(regexp, &proc) step_definitions.each do |already| raise Redundant.new(already, step_definition) if already.match(regexp) end step_definitions << step_definition step_definition end
def scenario_visited(scenario)
def scenario_visited(scenario) scenarios << scenario unless scenarios.index(scenario) end
def scenarios(status = nil)
def scenarios(status = nil) @scenarios ||= [] if(status) @scenarios.select{|scenario| scenario.status == status} else @scenarios end end
def snippet_text(step_keyword, step_name, multiline_arg_class)
def snippet_text(step_keyword, step_name, multiline_arg_class) @snippet_generator.snippet_text(step_keyword, step_name, multiline_arg_class) end
def step_definitions
def step_definitions @step_definitions ||= [] end
def step_match(step_name, formatted_step_name=nil)
def step_match(step_name, formatted_step_name=nil) matches = step_definitions.map { |d| d.step_match(step_name, formatted_step_name) }.compact raise Undefined.new(step_name) if matches.empty? matches = best_matches(step_name, matches) if matches.size > 1 && options[:guess] raise Ambiguous.new(step_name, matches, options[:guess]) if matches.size > 1 matches[0] end
def step_visited(step)
def step_visited(step) steps << step unless steps.index(step) end
def steps(status = nil)
def steps(status = nil) @steps ||= [] if(status) @steps.select{|step| step.status == status} else @steps end end