File: minitest_integration.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 (115 lines) | stat: -rw-r--r-- 2,634 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
109
110
111
112
113
114
115
#!/usr/bin/env ruby

#---
# Copyright 2003-2013 by Jim Weirich (jim.weirich@gmail.com).
# All rights reserved.

# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#+++

begin
  require 'minitest/assertions'
rescue LoadError
  require 'minitest/unit'
end

require 'flexmock/base'
require 'flexmock/test_unit_assert_spy_called'

class FlexMock

  # Minitest::Test Integration.
  #
  # Include this module in any Test subclass (in test-style minitest) or or
  # describe block (in spec-style minitest) to get integration with FlexMock.
  # When this module is included, the mock container methods (e.g. flexmock(),
  # flexstub()) will be available.
  #
  module Minitest
    include ArgumentTypes
    include MockContainer
    include TestUnitAssertions

    # Teardown the test case, verifying any mocks that might have been
    # defined in this test case.
    def before_teardown
      super
      @flexmock_teardown_failure = nil
      if respond_to?(:capture_exceptions)
        capture_exceptions do
          flexmock_teardown
        end
      else
        begin
          flexmock_teardown
        rescue Exception => e
          @flexmock_teardown_failure = e
        end
      end
    end

    def after_teardown
      if @flexmock_teardown_failure
        raise @flexmock_teardown_failure
      end
    end
  end

  class CheckFailedError < RuntimeError; end

  # Adapter for adapting FlexMock to the Test::Unit framework.
  #
  class MinitestFrameworkAdapter
    if defined?(::Minitest)
      include ::Minitest::Assertions
    else
      include MiniTest::Assertions
    end

    attr_accessor :assertions

    def initialize
      @assertions = 0
    end

    def filtered_backtrace(bt = caller)
      flexmock_dir = File.expand_path(File.dirname(__FILE__))
      while bt.first.start_with?(flexmock_dir)
          bt.shift
      end
      bt
    end

    def make_assertion(msg, backtrace = caller, &block)
      backtrace = filtered_backtrace(backtrace)
      assert(yield, msg)
    rescue Exception => e
      e.set_backtrace backtrace
      raise e
    end

    def check(msg, &block)
      unless yield
        msg = msg.call if msg.is_a?(Proc)
        raise CheckFailedError, msg, filtered_backtrace
      end
    end

    def assertion_failed_error
      if defined?(::Minitest)
        ::Minitest::Assertion
      else
        MiniTest::Assertion
      end
    end

    def check_failed_error
      CheckFailedError
    end
  end

  @framework_adapter = MinitestFrameworkAdapter.new
end