Sha256: 588bbe1f222fe57ede06744f91d236ce9d356546012e4e4313fbfb8721257c4a

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

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

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

    def matches?(content)            
      @content = content 
      match_res = (content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)end/m) != nil
      block_content = $1 || $3
      if block_given? && block_content
        ruby_content = block_content.strip.extend(RSpec::RubyContent::Helpers)
        yield ruby_content 
      end
      match_res
    end          
  
    def failure_message   
      puts "Content: #{content}" if RSpec::Generator.debug?      
      return "Expected there to be a block #{name} with arguments #{args}, but there wasn't"
    end 
    
    def negative_failure_message  
      puts "Content: #{content}" if RSpec::Generator.debug?                                          
      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

2 entries across 2 versions & 1 rubygems

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