Class: RSpec::RubyContentMatchers::HaveCall

Inherits:
RSpec::RubyContentMatcher show all
Defined in:
lib/code_spec/matchers/have_call.rb

Direct Known Subclasses

HaveCalls

Constant Summary

Constants inherited from RSpec::RubyContentMatcher

RSpec::RubyContentMatcher::ANY_GROUP, RSpec::RubyContentMatcher::LPAR, RSpec::RubyContentMatcher::OPT_ARGS, RSpec::RubyContentMatcher::OPT_SPACES, RSpec::RubyContentMatcher::Q_ANY_GROUP, RSpec::RubyContentMatcher::RPAR, RSpec::RubyContentMatcher::SPACES

Instance Attribute Summary collapse

Attributes inherited from RSpec::RubyContentMatcher

#alt_end, #content_matches, #end_option

Instance Method Summary collapse

Methods inherited from RSpec::RubyContentMatcher

#any_args_expr, #args_expr, #args_msg, #comment_end, #debug, #debug?, #debug_content, #end_expr, #get_expr, #handle_result, #index, #indexes, #is_match?, #main_expr, #opt

Constructor Details

#initialize(method_name, options = {}) ⇒ HaveCall

Returns a new instance of HaveCall.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/code_spec/matchers/have_call.rb', line 12

def initialize(method_name, options = {})
  self.method_name = method_name.to_s
  self.args = case options
  when Hash         
    self.dot = options[:dot]
    options[:args] 
  else 
    (options == {}) ? nil : options
  end
  
  self.args = ":#{args}" if args.kind_of? Symbol      
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



10
11
12
# File 'lib/code_spec/matchers/have_call.rb', line 10

def args
  @args
end

#contentObject

Returns the value of attribute content.



10
11
12
# File 'lib/code_spec/matchers/have_call.rb', line 10

def content
  @content
end

#dotObject

Returns the value of attribute dot.



10
11
12
# File 'lib/code_spec/matchers/have_call.rb', line 10

def dot
  @dot
end

#method_nameObject

Returns the value of attribute method_name.



10
11
12
# File 'lib/code_spec/matchers/have_call.rb', line 10

def method_name
  @method_name
end

Instance Method Details

#failure_messageObject



54
55
56
57
# File 'lib/code_spec/matchers/have_call.rb', line 54

def failure_message
  super
  "Expected there to be a call to #{method_name}#{args_msg}, but there wasn't. Regexp: #{@expr} did not match\n#{@content}"
end

#matches?(content) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/code_spec/matchers/have_call.rb', line 25

def matches?(content)
  @content = content 
  def_pos = (content =~ /def(.*)#{method_name}/) || 999
  call_pos = (content =~ /[^def]?#{method_name}/) || 999

  arguments_expr = case args
  when String
    args_expr
  when Array 
    args.flatten.inject("") do |res, arg|
      arg_val = (arg.kind_of?(String) && arg[0] == '#') ? arg[1..-1] : arg.inspect
      res << '(\s|,|\w|:)*' + arg_val
    end
  else
    return nil
  end

  expr = if (def_pos < call_pos) || dot == :form
    /#{dot_expr}#{method_name}#{arguments_expr}/m        
  else
    /#{dot_expr}?#{method_name}#{arguments_expr}/m
  end
  @expr = expr   
  debug "expr = #{expr}"
  debug "content = %{#{content}}"
  debug "content =~ #{expr}"      
  (content =~ expr)
end

#negative_failure_messageObject



59
60
61
62
# File 'lib/code_spec/matchers/have_call.rb', line 59

def negative_failure_message
  super
  "Did not expect there to be a call to #{method_name}#{args_msg}, but there was"
end