File: adapter.rb

package info (click to toggle)
ruby-mocha 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,400 kB
  • ctags: 2,016
  • sloc: ruby: 10,921; makefile: 12
file content (51 lines) | stat: -rw-r--r-- 1,342 bytes parent folder | download | duplicates (2)
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
require 'mocha/api'
require 'mocha/integration/assertion_counter'
require 'mocha/expectation_error'

module Mocha
  module Integration
    module TestUnit

      # Integrates Mocha into recent versions of Test::Unit.
      #
      # See the source code for an example of how to integrate Mocha into a test library.
      module Adapter
        include Mocha::API

        # @private
        def self.applicable_to?(test_unit_version, ruby_version = nil)
          Gem::Requirement.new('>= 2.5.1').satisfied_by?(test_unit_version)
        end

        # @private
        def self.description
          "adapter for Test::Unit gem >= v2.5.1"
        end

        # @private
        def self.included(mod)
          mod.setup :mocha_setup, :before => :prepend

          mod.exception_handler(:handle_mocha_expectation_error)

          mod.cleanup :after => :append do
            assertion_counter = Integration::AssertionCounter.new(self)
            mocha_verify(assertion_counter)
          end

          mod.teardown :mocha_teardown, :after => :append
        end

        private

        # @private
        def handle_mocha_expectation_error(e)
          return false unless e.is_a?(Mocha::ExpectationError)
          problem_occurred
          add_failure(e.message, e.backtrace)
          true
        end
      end
    end
  end
end