Sha256: 375f1f4490e49480e5216b79a990b50d0d67692adf0efa0c37f28803a5dfe528

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

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

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

    def matches?(content)
      super
      match_res = (content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)#{end_expr}/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   
      super
      return "Expected there to be a block #{name} with arguments #{args}, but there wasn't"
    end 
    
    def negative_failure_message  
      super
      return "Did not expect there to be a block #{name} with arguments #{args}, but there was"
    end               
    
    protected

    def end_expr
      "(end|# #{name})"
    end

    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

  def have_class_self(name, options={})
    HaveBlock.new('class', :args => '<< self')
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
generator-spec-0.4.5 lib/generator_spec/matchers/content/have_block.rb
generator-spec-0.4.4 lib/generator_spec/matchers/content/have_block.rb