Sha256: c30f4cb8f3b41c3dd53272417203a5035e1a0ee8067bbaf5fead73f2cb591381

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 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, args = nil, options = {})
      @method = method.to_s
      @args = args
      @dot = options[:dot]
    end

    def matches?(content)
      @content = content
      (content =~ /#{dot_expr}#{method}#{args_expr}/m)
    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]?\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, args = nil)
    HaveCall.new(method, args)
  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.4.7 lib/generator_spec/matchers/content/have_call.rb