File: test_helper.rb

package info (click to toggle)
ruby-flexmock 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 836 kB
  • sloc: ruby: 7,572; makefile: 6
file content (108 lines) | stat: -rw-r--r-- 2,969 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
104
105
106
107
108
begin
    require 'simplecov'
    require 'coveralls'
    SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new(
        [SimpleCov::Formatter::HTMLFormatter,
        Coveralls::SimpleCov::Formatter]
    )
    SimpleCov.start do
        add_filter "/test/"
    end
rescue LoadError
end

require 'minitest/autorun'
require 'fileutils'
require 'redirect_error'
require 'flexmock'
require 'flexmock/minitest_integration'


class FlexMock
  module Minitest
    def assertion_failed_error
      FlexMock.framework_adapter.assertion_failed_error
    end

    def check_failed_error
      FlexMock.framework_adapter.check_failed_error
    end

    # Assertion helper used to assert validation failure.  If a
    # message is given, then the error message should match the
    # expected error message.
    def assert_failure(klass, options={}, &block)
      message = options[:message]
      ex = assert_raises(klass) { yield }
      if message
        case message
        when Regexp
          assert_match message, ex.message
        when String
          assert ex.message.index(message), "Error message '#{ex.message}' should contain '#{message}'"
        end
      end
      ex
    end

    # Similar to assert_failure, but assumes that a mock generated
    # error object is return, so additional tests on the backtrace are
    # added.
    def assert_mock_failure(klass, options={}, &block)
      ex = assert_failure(klass, options, &block)
      file = if block.binding.respond_to?(:source_location)
               block.binding.source_location.first
             else
               eval("__FILE__", block.binding)
             end
      assert_matching_line(ex, file, options)
    end

    # Assert that there is a line matching file in the backtrace.
    # Options are:
    #
    #     deep: true -- matching line can be anywhere in backtrace,
    #                   otherwise it must be the first.
    #
    #     line: n    -- Add a line number to the match
    #
    def assert_matching_line(ex, file, options)
      line = options[:line]
      search_all = options[:deep]
      if line
        loc_re = /#{Regexp.quote(file)}:#{line}/
      else
        loc_re = Regexp.compile(Regexp.quote(file))
      end


      if search_all
        bts = ex.backtrace.join("\n")
        assert_with_block("expected a backtrace line to match #{loc_re}\nBACKTRACE:\n#{bts}") {
          ex.backtrace.any? { |bt| loc_re =~ bt }
        }
      else
        assert_match(loc_re, ex.backtrace.first, "BACKTRACE:\n  #{ex.backtrace.join("\n  ")}")
      end

      ex
    end

    def assert_with_block(msg=nil)
      unless yield
        assert(false, msg || "Expected block to yield true")
      end
    end

    def pending(msg="")
      state = "PASSING"
      begin
        yield
      rescue Exception => _
        state = "FAILING"
      end
      where = caller.first.split(/:in/).first
      puts "\n#{state} PENDING TEST (#{msg}) #{where}"
    end
  end
end