Sha256: c34a2e37f45fc254739257475bb1360ab47ffde6762263978f76b77649c0eab1

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require "minitest"
require_relative "error"
require_relative "failure_group"

module AggregateAssertions
  # Main entry-point for the interacting with the state of aggregation.
  module AssertionAggregator
    class << self
      def active?
        !store.nil? && !store.empty?
      end

      def add_error(error)
        store.last.add_error(error)
      end

      def open_failure_group(label = nil)
        initialize_store << FailureGroup.new(label: label, location: location)
      end

      def close_failure_group
        store.pop
      end

      def wrap(label = nil)
        AssertionAggregator.open_failure_group(label)

        begin
          yield
        rescue Minitest::Assertion, StandardError => e
          AssertionAggregator.add_error(e)
        ensure
          failure_group = AssertionAggregator.close_failure_group
        end

        return if failure_group.success?

        raise failure_group.error unless AssertionAggregator.active?

        AssertionAggregator.add_error(failure_group.error)
      end

      private

      def store
        Thread.current[:__mt_aggregate]
      end

      def initialize_store
        Thread.current[:__mt_aggregate] ||= []
      end

      def location
        caller(3, 1).first.sub(/:in .*$/, "")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aggregate_assertions-0.2.0 lib/aggregate_assertions/assertion_aggregator.rb