File: include_lines_matcher.rb

package info (click to toggle)
ruby-test-prof 1.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,448 kB
  • sloc: ruby: 13,093; sh: 4; makefile: 4
file content (47 lines) | stat: -rw-r--r-- 961 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
# frozen_string_literal: true

# Verify that the actual string contains all lines that included the specified
# lines in the provided order.
#
#   Example:
#
#   text = <<~TXT
#     Jingle bells,
#     Jingle bells,
#     Jingle all the way
#   TXT
#
#   # passes
#   expect(text).to include_lines("Jingle bells", "Jingle bells")
#
#   # passes
#   expect(text).to include_lines("Jingle bells", "Jingle all the")
#
#   # do not pass
#   expect(text).to include_lines("Jingle all the way", "Jingle bells")
::RSpec::Matchers.define :include_lines do |*lines|
  match do |actual|
    actual.each_line do |line|
      lines.shift if line.include?(lines.first)
      break if lines.empty?
    end

    lines.empty?
  end

  match_when_negated do |_actual|
    raise "This matcher doesn't support negation"
  end

  failure_message do |actual|
    <<~MSG
      Couldn't find lines:

      #{lines.join("\n")}

      in the output:

      #{actual}"
    MSG
  end
end