Sha256: 956cf872ca7520e2a9c5f8a485404e95c2ae542b05f92bf223f4aae8b60dfbf2

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 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 HaveMethod
    attr_reader :method

    def initialize(method, type)
      @type = type
      @method = method.to_s
    end

    def matches?(content)      
      case @type
      when :class
        content =~ /def\s+self.#{method}\s*(\(.+\))?(.*?)/m
      else
        content =~ /def\s+#{method}\s*(\(.+\))?(.*?)/m
      end
    end          
  
    def failure_message
      return "Expected there to be the class method #{method}, but there wasn't" if @type == :class
      "Expected there to be the method #{method}, but there wasn't"        
    end 
    
    def negative_failure_message                                      
      return "Did not expect there to be the method #{method}, but there was" if @type == :class
      "Did not expect there to be the method #{method}, but there was"
    end
               
  end
  
  def have_method(method, type = nil)
    HaveMethod.new(method, type)
  end  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
generator-spec-0.3.3 lib/rspec_for_generators/matchers/content/have_method.rb