File: indicate_that_tests_were_run_matcher.rb

package info (click to toggle)
ruby-shoulda-matchers 7.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: ruby: 34,046; sh: 280; makefile: 9
file content (103 lines) | stat: -rw-r--r-- 2,563 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require_relative '../helpers/array_helpers'
require_relative '../helpers/pluralization_helpers'
require_relative '../helpers/rails_version_helpers'

module AcceptanceTests
  module Matchers
    def indicate_that_tests_were_run(series)
      IndicateThatTestsWereRunMatcher.new(series)
    end

    class IndicateThatTestsWereRunMatcher
      include ArrayHelpers
      include PluralizationHelpers
      include RailsVersionHelpers

      def initialize(args)
        @args = args
        @series = args.values
      end

      def matches?(runner)
        @runner = runner
        !matching_expected_output.nil?
      end

      def failure_message
        "Expected output to indicate that #{some_tests_were_run}.\n" +
          "#{formatted_expected_output}\n" +
          "#{formatted_actual_output}\n"
      end

      protected

      attr_reader :args, :series, :runner

      private

      def expected_outputs
        [
          expected_output_for_rails_3,
          expected_output_for_turn,
          expected_output_for_rails_4,
        ]
      end

      def matching_expected_output
        @_matching_expected_output ||=
          expected_outputs.detect do |expected_output|
            actual_output =~ expected_output
          end
      end

      def expected_output_for_rails_3
        full_report = series.map do |number|
          "#{number} tests, #{number} assertions, 0 failures, 0 errors(, 0 skips)?"
        end.join('.+')

        Regexp.new(full_report, Regexp::MULTILINE)
      end

      def expected_output_for_turn
        full_report = series.map do |number|
          "pass: #{number},  fail: 0,  error: 0"
        end.join('.+')

        Regexp.new(full_report, Regexp::MULTILINE)
      end

      def expected_output_for_rails_4
        total = series.inject(:+)
        /#{total} (tests|runs), #{total} assertions, 0 failures, 0 errors(, 0 skips)?/
      end

      def formatted_expected_output
        if matching_expected_output
          "Expected output:\n#{matching_actual_output}"
        else
          'Expected output: (n/a)'
        end
      end

      def actual_output
        runner.output
      end

      def formatted_actual_output
        if actual_output.empty?
          'Actual output: (empty)'
        else
          "Actual output:\n#{actual_output}"
        end
      end

      def some_tests_were_run
        clauses = args.map do |type, number|
          pluralize(number, "#{type} test was run", "#{type} tests were run")
        end

        to_sentence(clauses)
      end
    end
  end
end