Sha256: 1fa909a6fc79b370143e92f7da09fc92e2d38713f4150909c7b422389768a764

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 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 = (content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)end/m)
      block_content = $1 || $3
      if block_given? && block_content
        ruby_content = block_content.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 args_expr
      args ? '(\()?\s*' + args + '\s*(\))?\s+' : ''
    end      

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
generator-spec-0.4.0 lib/rspec_for_generators/matchers/content/have_block.rb
generator-spec-0.3.5 lib/rspec_for_generators/matchers/content/have_block.rb
generator-spec-0.3.4 lib/rspec_for_generators/matchers/content/have_block.rb