Sha256: d4016a143a7644e7906720cd65623941c7910ca17053128a73f7e17e6ebcc265

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

require 'cucumber/tree/top_down_visitor'
require 'cucumber/core_ext/proc'

module Cucumber
  class Pending < StandardError
  end

  class Duplicate < StandardError
  end

  class Multiple < StandardError
  end

  class StepMother
    PENDING = lambda do |*_| 
      raise Pending
    end
    PENDING.extend(CoreExt::CallIn)
    PENDING.name = "PENDING"

    def initialize
      @step_procs = Hash.new(PENDING)
    end

    def register_step_proc(key, &proc)
      regexp = case(key)
      when String
        # Replace the $foo and $bar style parameters
        pattern = key.gsub(/\$\w+/, '(.*)')
        Regexp.new("^#{pattern}$")
      when Regexp
        key
      else
        raise "Step patterns must be Regexp or String, but was: #{key.inspect}"
      end
      proc.extend(CoreExt::CallIn)
      proc.name = key.inspect

      if @step_procs.has_key?(regexp)
        first_proc = @step_procs[regexp]
        message = %{Duplicate step definitions:

#{first_proc.to_backtrace_line}
#{proc.to_backtrace_line}

}
        raise Duplicate.new(message)
      end

      @step_procs[regexp] = proc
    end

    def regexp_args_proc(step_name)
      candidates = @step_procs.map do |regexp, proc|
        if step_name =~ regexp
          [regexp, $~.captures, proc]
        end
      end.compact
      
      case(candidates.length)
      when 0
        [nil, [], PENDING]
      when 1
        candidates[0]
      else
        message = %{Multiple step definitions match #{step_name.inspect}:

#{candidates.map{|regexp, args, proc| proc.to_backtrace_line}.join("\n")}

}
        raise Multiple.new(message)
      end
     end
    
    def proc_for(regexp)
      @step_procs[regexp]
    end
    
    # TODO - move execute here?
    def execute(step)
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
elight-cucumber-0.1.9 lib/cucumber/step_mother.rb
cucumber-0.1.7 lib/cucumber/step_mother.rb
cucumber-0.1.8 lib/cucumber/step_mother.rb