Class: RSpec::RubyContentMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/code_spec/matchers/content_matcher.rb

Constant Summary collapse

Q_ANY_GROUP =
'(.*?)'
ANY_GROUP =
'(.*)'
SPACES =
'\s+'
OPT_SPACES =
'\s*'
LPAR =
'(\()'
RPAR =
'(\))'
OPT_ARGS =
'(\(.+\))?'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ RubyContentMatcher

Returns a new instance of RubyContentMatcher.



5
6
7
# File 'lib/code_spec/matchers/content_matcher.rb', line 5

def initialize name
  @end_option = name
end

Instance Attribute Details

#alt_endObject (readonly)

Returns the value of attribute alt_end.



3
4
5
# File 'lib/code_spec/matchers/content_matcher.rb', line 3

def alt_end
  @alt_end
end

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'lib/code_spec/matchers/content_matcher.rb', line 3

def content
  @content
end

#content_matchesObject (readonly)

Returns the value of attribute content_matches.



3
4
5
# File 'lib/code_spec/matchers/content_matcher.rb', line 3

def content_matches
  @content_matches
end

#end_optionObject (readonly)

Returns the value of attribute end_option.



3
4
5
# File 'lib/code_spec/matchers/content_matcher.rb', line 3

def end_option
  @end_option
end

Instance Method Details

#any_args_exprObject



74
75
76
# File 'lib/code_spec/matchers/content_matcher.rb', line 74

def any_args_expr
  OPT_SPACES + OPT_ARGS
end

#args_exprObject



78
79
80
# File 'lib/code_spec/matchers/content_matcher.rb', line 78

def args_expr    
  args ? OPT_SPACES + opt(LPAR) + OPT_SPACES + "#{args}" + OPT_SPACES + opt(RPAR) : ''
end

#args_msgObject



70
71
72
# File 'lib/code_spec/matchers/content_matcher.rb', line 70

def args_msg
  args ? " with arguments #{args}" : ''
end

#comment_endObject



90
91
92
# File 'lib/code_spec/matchers/content_matcher.rb', line 90

def comment_end 
  alt_end != nil ? '#' + SPACES + "(#{end_option}|#{alt_end})" : '#' + SPACES + "#{end_option}"
end

#debug(msg) ⇒ Object



106
107
108
# File 'lib/code_spec/matchers/content_matcher.rb', line 106

def debug msg
  puts msg if debug?
end

#debug?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/code_spec/matchers/content_matcher.rb', line 17

def debug?
  false
end

#debug_contentObject



102
103
104
# File 'lib/code_spec/matchers/content_matcher.rb', line 102

def debug_content
  debug "Content: #{content}"
end

#end_exprObject



86
87
88
# File 'lib/code_spec/matchers/content_matcher.rb', line 86

def end_expr
  "$"
end

#failure_messageObject



94
95
96
# File 'lib/code_spec/matchers/content_matcher.rb', line 94

def failure_message
  debug_content
end

#get_expr(content) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/code_spec/matchers/content_matcher.rb', line 48

def get_expr content
  expr = /#{main_expr}#{comment_end}/m            
  if (content =~ expr).nil?                             
    /#{main_expr}#{end_expr}/m  
  else
    expr  
  end        
end

#handle_result(content, match, &block) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/code_spec/matchers/content_matcher.rb', line 40

def handle_result content, match, &block 
  if block && match && content
    ruby_content = content.strip #.extend(RSpec::RubyContent::Helpers)
    yield ruby_content        
  end
  match
end

#indexObject



9
10
11
# File 'lib/code_spec/matchers/content_matcher.rb', line 9

def index
  0
end

#indexesObject



13
14
15
# File 'lib/code_spec/matchers/content_matcher.rb', line 13

def indexes
  nil
end

#is_match?(content) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
# File 'lib/code_spec/matchers/content_matcher.rb', line 32

def is_match? content
  expr = get_expr(content) 
  debug "match expression: #{expr}"
  match = (content =~ expr)
  @content_matches = [$1, $2, $3]
  match
end

#main_exprObject



82
83
84
# File 'lib/code_spec/matchers/content_matcher.rb', line 82

def main_expr
  raise "Must override main_expr"
end

#matches?(content, &block) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
# File 'lib/code_spec/matchers/content_matcher.rb', line 21

def matches? content, &block
  @content = content
  match = is_match? content                                                        
  content_to_yield = if indexes
    content_matches[indexes.last] || content_matches[indexes.first]
  else
    content_matches[index]
  end          
  handle_result(content_to_yield, match, &block)
end

#negative_failure_messageObject



98
99
100
# File 'lib/code_spec/matchers/content_matcher.rb', line 98

def negative_failure_message
  debug_content
end

#opt(expr) ⇒ Object



66
67
68
# File 'lib/code_spec/matchers/content_matcher.rb', line 66

def opt expr
  "#{expr}?"
end