require 'mocha_standalone'
require 'mocha/expectation_error'
require 'test/unit/assertionfailederror'

test_unit_version = begin
  load 'test/unit/version.rb'
  Gem::Version.new(Test::Unit::VERSION)
rescue LoadError
  Gem::Version.new('1.x')
end

unless Gem::Requirement.new('>= 2.5.1').satisfied_by?(test_unit_version)
  raise "Mocha::Adapters::TestUnit requires Test::Unit version 2.5.1 or higher."
end

module Mocha
  module Adapters
    module TestUnit

      class AssertionCounter
        def initialize(test_case)
          @test_case = test_case
        end

        def increment
          @test_case.assert(true)
        end
      end

      include Mocha::API

      def self.included(mod)
        Mocha::ExpectationErrorFactory.exception_class = Test::Unit::AssertionFailedError

        mod.setup :mocha_setup

        mod.cleanup do
          assertion_counter = AssertionCounter.new(self)
          mocha_verify(assertion_counter)
        end

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


require 'mocha_standalone'
require 'mocha/expectation_error'

test_unit_version = begin
  load 'test/unit/version.rb'
  Gem::Version.new(Test::Unit::VERSION)
rescue LoadError
  Gem::Version.new('1.x')
end

unless Gem::Requirement.new('>= 2.5.1').satisfied_by?(test_unit_version)
  raise "Mocha::Adapters::TestUnit requires Test::Unit version 2.5.1 or higher."
end

module Mocha
  module Adapters
    module TestUnit

      class AssertionCounter
        def initialize(test_case)
          @test_case = test_case
        end

        def increment
          @test_case.assert(true)
        end
      end

      include Mocha::API

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

      def self.included(mod)
        mod.setup :mocha_setup

        mod.exception_handler(:handle_mocha_expectation_error)

        mod.cleanup do
          assertion_counter = AssertionCounter.new(self)
          mocha_verify(assertion_counter)
        end

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


Use a custom exception handler in the new Test::Unit adapter.
Use a custom exception handler in the new Test::Unit adapter.

* It turns out that Test::Unit::AssertionFailedError is derived from
  StandardError; whereas MiniTest::Assertion is derived from Exception.
* I had been telling Mocha::ExpectationErrorFactory to use
  Test::Unit::AssertionFailedError in the Test::Unit adapter, but since
  this inherits from StandardError, this would mean a regression on #15.
* So instead I've introduced a custom exception handler in the new
  Test::Unit adapter to rescue Mocha::ExpectationError as if it were a
  Test::Unit::AssertionFailedError, even though the former is derived
  from Exception.
* Unfortunately I have had to call some private methods in Test::Unit
  from within the exception handler method. I'm going to ask the Test::Unit
  developers whether there is an alternative to doing this.
