Sha256: 48c453a6f7fe129cf81712d053adf7007d3ea7cbd4c0ae5a736d7c21d149a2e6

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 < RSpec::RubyContentMatcher
    attr_reader :method, :type

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

    def matches?(content) 
      super
      match_res = case type
      when :class
        content =~ /def\s+self.#{method}#{args_expr}(.*?)#{end_expr}/m
      else
        content =~ /def\s+#{method}#{args_expr}(.*?)#{end_expr}/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
      super
      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                                      
      super
      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

    protected

    def args_expr
      '\s*(\(.+\))?'
    end
    
    def end_expr
      "(def|end|# #{method})"
    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.5 lib/generator_spec/matchers/content/have_method.rb
generator-spec-0.4.4 lib/generator_spec/matchers/content/have_method.rb