Class: TarkaMatchers::Matchers::Regex::MatchSections

Inherits:
Object
  • Object
show all
Includes:
Helpers::Utility
Defined in:
lib/tarka_matchers/matchers/regex/match_sections.rb

Instance Attribute Summary

Attributes included from Helpers::Utility

#description, #failure_message, #failure_message_when_negated

Instance Method Summary collapse

Methods included from Helpers::Utility

#difference, #fail_default, #fail_with_message, #negated_default, #pass_default, #pass_with_message, #selected

Constructor Details

#initialize(expected) ⇒ MatchSections

Returns a new instance of MatchSections.



13
14
15
16
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 13

def initialize expected
	@expected = expected
	@li = @expected.length
end

Instance Method Details

#extracts_listObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 78

def extracts_list
	list = ''
	@expected.each_with_index do |v,i|
		if i == @li - 2
			divider = ' and '
		elsif i != @li - 1
			divider = ','
		end
		list << "'#{v}'#{divider}"
	end
	list
end

#indexes_listObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 63

def indexes_list
	list = ''
	@expected.each_with_index do |v,i|
		if i.even?
			divider = ' to '
		elsif i == @li - 3
			divider = ' and '
		elsif i != @li - 1
			divider = ','
		end
		list << "'#{v}'#{divider}"
	end
	list
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


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
				extracts = @matches.map{ |v| v[1] }.flatten
				if @expected == extracts
					pass_with_message "contain the pattern, '#{@actual}' and match: #{extracts_list}"
				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

#when_used_on(string) ⇒ Object



18
19
20
21
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 18

def when_used_on string
	@string = string
	self
end