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



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

def any_args_expr
  OPT_SPACES + OPT_ARGS
end

#args_exprObject



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

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

#args_msgObject



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

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

#comment_endObject



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

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

#debug(msg) ⇒ Object



115
116
117
# File 'lib/code_spec/matchers/content_matcher.rb', line 115

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



111
112
113
# File 'lib/code_spec/matchers/content_matcher.rb', line 111

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

#display(msg) ⇒ Object



25
26
27
# File 'lib/code_spec/matchers/content_matcher.rb', line 25

def display msg
  "#{msg}#{display_content}\nRegexp: #{@expr}\n#{@special_error}"
end

#display_contentObject



21
22
23
# File 'lib/code_spec/matchers/content_matcher.rb', line 21

def display_content
  "\ncontent:#{content}"
end

#end_exprObject



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

def end_expr
  "$"
end

#failure_messageObject



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

def failure_message
  debug_content
end

#get_expr(content) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/code_spec/matchers/content_matcher.rb', line 57

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



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

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)


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

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

#main_exprObject



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

def main_expr
  raise "Must override main_expr"
end

#matches?(content, &block) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

def negative_failure_message
  debug_content
end

#opt(expr) ⇒ Object



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

def opt expr
  "#{expr}?"
end