Sha256: 89ffe92826358742de4e17d74e4760a12c475ae82c3825de08650a39313cd40c

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

module RSpec::RubyContentMatchers
  class HaveBlock
    attr_reader :name, :args, :block_args

    def initialize(name, options={})
      @name = name
      @args = options[:args]
      @block_args = options[:block_args]      
    end

    def matches?(content)            
      match_res = match(content)
      if block_given? && $1
        ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
        yield ruby_content 
      else
        match_res
      end
    end          
  
    def failure_message
      return "Expected there to be a block #{name} with arguments #{args}, but there wasn't"
    end 
    
    def negative_failure_message                                      
      return "Did not expect there to be a block #{name} with arguments #{args}, but there was"
    end               
    
    protected

    def match content
      content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)end/m
    end

    def args_expr
      args ? "\(?\s*#{args}\s*\)?\s+" : ''
    end      

    def block_args_expr
      args ? "\|\s*#{block_args}\s*\|" : ''
    end    
  end
  
  def have_block(name, options={})
    HaveBlock.new(name, options)
  end    
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
generator-spec-0.3.3 lib/rspec_for_generators/matchers/content/have_block.rb