23
24
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
53
54
55
56
57
58
59
60
61
|
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 23
def matches? actual
@actual = actual
integers = @expected.all?{ |v| v.is_a?(Integer) }
strings = @expected.all?{ |v| v.is_a?(String) }
if integers || strings
@matches = indexes = Helpers::SGR::StyledCapture.indexes_of(@string, @actual)
pass_default "contain the pattern, '#{@actual}' at positions #{indexes_list}"
fail_default "The string, '#{@string}', does not contain the pattern, '#{@actual}':#{selected(@string, @matches.map{ |v| [v[0], v[2]] }.flatten)}"
if indexes.empty?
fail_with_message
else
if strings
= @matches.map{ |v| v[1] }.flatten
if @expected ==
pass_with_message "contain the pattern, '#{@actual}' and match: #{}"
else
fail
end
else
indexes = @matches.map{ |v| [v[0], v[2]] }.flatten
if @expected.count.odd?
fail_with_message "The indexes provided, '#{@expected}', are of an odd number. Please provide the start and end index pairs of all sections of '#{@string}' that should be selected by '#{@actual}'"
elsif @expected.count < indexes.count
fail_with_message "The index pairs provided, '#{@expected}', are less than the number of matches found in the string. Please provide the start and end index pairs of all sections of '#{@string}' that should be selected by '#{@actual}':#{selected(@string, @matches.map{ |v| [v[0], v[2]] }.flatten)}"
elsif @expected.count > indexes.count
fail_with_message "The index pairs provided, '#{@expected}', are more than the number of matches found in the string. Please provide the start and end index pairs of all sections of '#{@string}' that should be selected by '#{@actual}':#{selected(@string, @matches.map{ |v| [v[0], v[2]] }.flatten)}"
elsif @expected == indexes
pass_with_message "contain the pattern, '#{@actual}' at positions #{indexes_list}"
elsif @expected != indexes
fail
end
end
end
else
fail_with_message "Provided a wrongly formatted argument to 'match_sections'. 'match_sections' expects an argument sequence consisting exclusively of either the start and end indexes of all expected sections of the provided string selected by the match, or an example of the actual text that is selected"
end
end
|