Class: RSpec::RubyContentMatchers::HaveMethod

Inherits:
RSpec::RubyContentMatcher show all
Defined in:
lib/generator_spec/matchers/content/have_method.rb

Instance Attribute Summary collapse

Attributes inherited from RSpec::RubyContentMatcher

#content

Instance Method Summary collapse

Methods inherited from RSpec::RubyContentMatcher

#debug_content

Constructor Details

#initialize(method, type = nil) ⇒ HaveMethod

Returns a new instance of HaveMethod.



12
13
14
15
# File 'lib/generator_spec/matchers/content/have_method.rb', line 12

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

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



10
11
12
# File 'lib/generator_spec/matchers/content/have_method.rb', line 10

def method
  @method
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/generator_spec/matchers/content/have_method.rb', line 10

def type
  @type
end

Instance Method Details

#failure_messageObject



33
34
35
36
37
# File 'lib/generator_spec/matchers/content/have_method.rb', line 33

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

#matches?(content) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/generator_spec/matchers/content/have_method.rb', line 17

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

#negative_failure_messageObject



39
40
41
42
43
# File 'lib/generator_spec/matchers/content/have_method.rb', line 39

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