Sha256: da8c5f3453285a89a8bb60d1f9a59467f0c738d6825ab8ee3d656e2c36facd59

Contents?: true

Size: 1.73 KB

Versions: 41

Compression:

Stored size: 1.73 KB

Contents

module Spec
  module Story
    class Step
      PARAM_PATTERN = /([^\\]|^)(\$(?!\$)\w*)/
      PARAM_OR_GROUP_PATTERN = /(\$(?!\$)\w*)|\(.*?\)/
      
      attr_reader :name
      
      def initialize(name, &block)
        init_name(name)
        init_expression(name)
        block_given? ? init_module(name, &block) : set_pending
      end

      def perform(instance, *args)
        raise Spec::Example::ExamplePendingError.new("Not Yet Implemented") if pending?
        instance.extend(@mod)
        instance.__send__(sanitize(@name), *args)
      end

      def matches?(name)
        !(name.strip =~ @expression).nil?
      end
            
      def parse_args(name)
        name.strip.match(@expression)[1..-1]
      end

      private
      
      def sanitize(a_string_or_regexp)
        return a_string_or_regexp.source if Regexp == a_string_or_regexp
        a_string_or_regexp.to_s
      end

      def init_module(name, &block)
        sanitized_name = sanitize(name)
        @mod = Module.new do
          define_method(sanitized_name, &block)
        end
      end
    
      def set_pending
        @pending = true
      end
      
      def pending?
        @pending == true
      end
      
      def init_name(name)
        @name = name
      end
    
      def init_expression(string_or_regexp)
        if String === string_or_regexp
          expression = string_or_regexp.dup
          %w<? ( ) [ ] { } ^ !>.each {|c| expression.gsub! c, "\\#{c}"}
        elsif Regexp === string_or_regexp
          expression = string_or_regexp.source
        end
        while expression =~ PARAM_PATTERN
          expression.sub!($2, "(.*?)")
        end
        @expression = Regexp.new("\\A#{expression}\\Z", Regexp::MULTILINE)
      end

    end
  end
end

Version data entries

41 entries across 41 versions & 10 rubygems

Version Path
dchelimsky-rspec-1.1.10 lib/spec/story/step.rb
dchelimsky-rspec-1.1.11.1 lib/spec/story/step.rb
dchelimsky-rspec-1.1.11.2 lib/spec/story/step.rb
dchelimsky-rspec-1.1.11.3 lib/spec/story/step.rb
dchelimsky-rspec-1.1.11.4 lib/spec/story/step.rb
dchelimsky-rspec-1.1.11.5 lib/spec/story/step.rb
dchelimsky-rspec-1.1.11.6 lib/spec/story/step.rb
dchelimsky-rspec-1.1.11.7 lib/spec/story/step.rb
dchelimsky-rspec-1.1.11 lib/spec/story/step.rb
dchelimsky-rspec-stories-1.0.0 lib/spec/story/step.rb
jnstq-acts_as_sms-0.1.0 test/vendor/plugins/rspec/lib/spec/story/step.rb
jnstq-acts_as_sms-0.1.1 test/vendor/plugins/rspec/lib/spec/story/step.rb
jnstq-acts_as_sms-0.1.3 test/vendor/plugins/rspec/lib/spec/story/step.rb
jnstq-acts_as_sms-0.1.4 test/vendor/plugins/rspec/lib/spec/story/step.rb
samstokes-rspec-stories-1.0.1 lib/spec/story/step.rb
samstokes-rspec-stories-1.0.2 lib/spec/story/step.rb
merb-core-1.1.3 spec10/public/webrat/test_app/gems/gems/rspec-1.1.11/lib/spec/story/step.rb
merb-core-1.1.2 spec10/public/webrat/test_app/gems/gems/rspec-1.1.11/lib/spec/story/step.rb
merb-core-1.1.1 spec10/public/webrat/test_app/gems/gems/rspec-1.1.11/lib/spec/story/step.rb
merb-core-1.1.0 spec10/public/webrat/test_app/gems/gems/rspec-1.1.11/lib/spec/story/step.rb