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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#descriptionObject (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_messageObject (readonly)

Returns the value of attribute failure_message.



11
12
13
# File 'lib/tarka_matchers/matchers/regex/match_sections.rb', line 11

def failure_message
  @failure_message
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 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)}"
  @failure_message = message
  false
end

#indexes_listObject



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

Returns:

  • (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)
    pass_with_message
    fail_with_message
    if indexes.empty?
      fail_with_message
    else
      if strings
        extracts = @matches.map{ |v| v[1] }.flatten
        pass_with_message
      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}'."
        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}'."
        elsif @expected == indexes
          pass_with_message
        else
          fail_with_message
        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

#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 pass_with_message message="contain the pattern, '#{@actual}' at positions #{indexes_list}." 
  @description = message
  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