Sha256: 7e5c1940c12f027e9235e62101322039ad091411c555d52f792ff7098f6e007a

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

# This method tries to see if a specific method is contained in the generated file.
# It can operate (should) on either a file name or the raw content 
#
#   generated_file_name.should have_method "hello" # 'my/path/say_hello.rb'.should have_method "hello"
#
#   say_hello_file_content.should have_method "hello"
#
module RSpec::RubyContentMatchers
  class HaveCall < RSpec::RubyContentMatcher
    attr_reader :method, :args, :dot

    def initialize(method, options = {})
      @method = method.to_s
      @args = options[:args] || options
      @dot = options[:dot]
    end

    def matches?(content)
      @content = content                        
      has_def = (content =~ /def(.*)#{method}/)
      expr = if has_def
        /#{dot_expr}#{method}#{args_expr}/m        
      else
        /#{dot_expr}?#{method}#{args_expr}/m
      end
      (content =~ expr)
    end          
  
    def failure_message
      super
      "Expected there to be a call to #{method}#{args_msg}, but there wasn't"
    end 
    
    def negative_failure_message
      super
      "Did not expect there to be a call to #{method}#{args_msg}, but there was"
    end
             
    protected

    def not_def
      '[^def\s]'
    end

    def dot_expr
      return Regexp.escape(dot) if dot.kind_of?(String)
      dot == true ? "#{not_def}\." : not_def
    end
                   
  end
  
  def have_call(method, options = {})
    HaveCall.new(method, options)
  end  

  def have_dot_call(method, options = {})
    HaveCall.new(method, options[:args], :dot => options[:dot] || true)
  end  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
generator-spec-0.5.0 lib/generator_spec/matchers/content/have_call.rb