Sha256: a7bd4cb21f81e541f92d359d8b5598ab6d89478b6f5de471ddd8c337f54fe2a6

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 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, :type, :content

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

    def matches?(content) 
      @content = content 
      match_res = case type
      when :class
        content =~ /def\s+self.#{method}\s*(\(.+\))?(.*?)(def|end)/m
      else
        content =~ /def\s+#{method}\s*(\(.+\))?(.*?)(def|end)/m
      end   
      if block_given? && $2
        ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
        ruby_content << "\nend" if !(ruby_content =~ /\nend$/)
        yield ruby_content         
      end      
      match_res
    end          
  
    def failure_message
      puts "Content: #{content}" if RSpec::Generator.debug?      
      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                                      
      puts "Content: #{content}" if RSpec::Generator.debug?      
      return "Did not expect there to be the class 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

2 entries across 2 versions & 1 rubygems

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