Class: TarkaMatchers::Matchers::Regex::MatchSections
- Defined in:
- lib/tarka_matchers/matchers/regex/match_sections.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#failure_message ⇒ Object
readonly
Returns the value of attribute failure_message.
Instance Method Summary collapse
- #fail_with_message(message = "The string, '#{@string}', does not contain the pattern, '#{@actual}':#{TarkaMatchers::Formatters::Selected.selected(@string, @matches.map{ |v| [v[0], v[2]] }.flatten)}") ⇒ Object
- #indexes_list ⇒ Object
-
#initialize(expected) ⇒ MatchSections
constructor
A new instance of MatchSections.
- #matches?(actual) ⇒ Boolean
- #pass_with_message(message = "contain the pattern, '#{@actual}' at positions #{indexes_list}.") ⇒ Object
- #when_used_on(string) ⇒ Object
Constructor Details
#initialize(expected) ⇒ MatchSections
Returns a new instance of MatchSections.
12 13 14 |
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 12 def initialize expected @expected = expected end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
11 12 13 |
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 11 def description @description end |
#failure_message ⇒ Object (readonly)
Returns the value of attribute failure_message.
11 12 13 |
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 11 def end |
Instance Method Details
#fail_with_message(message = "The string, '#{@string}', does not contain the pattern, '#{@actual}':#{TarkaMatchers::Formatters::Selected.selected(@string, @matches.map{ |v| [v[0], v[2]] }.flatten)}") ⇒ Object
61 62 63 64 |
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 61 def ="The string, '#{@string}', does not contain the pattern, '#{@actual}':#{TarkaMatchers::Formatters::Selected.selected(@string, @matches.map{ |v| [v[0], v[2]] }.flatten)}" = false end |
#indexes_list ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 66 def indexes_list list = '' li = @expected.length @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
21 22 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 |
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 21 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) if indexes.empty? else if strings extracts = @matches.map{ |v| v[1] }.flatten else indexes = @matches.map{ |v| [v[0], v[2]] }.flatten if @expected.count.odd? "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 "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}'." elsif @expected.count > indexes.count "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}'." elsif @expected == indexes else end end end else "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 |
#pass_with_message(message = "contain the pattern, '#{@actual}' at positions #{indexes_list}.") ⇒ Object
56 57 58 59 |
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 56 def ="contain the pattern, '#{@actual}' at positions #{indexes_list}." @description = true end |
#when_used_on(string) ⇒ Object
16 17 18 19 |
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 16 def when_used_on string @string = string self end |