Sha256: abc7921245c47ec93f7e31a2567f87a110956efc423c3177c5444c214574e66b

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

require_relative "error"

module AggregateAssertions
  # This class is used to aggregates the failures/errors for an aggregate block.
  class FailureGroup
    attr_reader :label, :location

    def initialize(location:, label: nil)
      @location = location
      @label = label
      @failures = []
      @other_errors = []
    end

    def add_error(error)
      if error.is_a?(Minitest::Assertion)
        @failures << error
      else
        @other_errors << error
      end
    end

    def error
      case @failures.size + @other_errors.size
      when 0
        nil
      when 1
        @failures.first || @other_errors.first
      else
        group_error
      end
    end

    def success?
      @failures.empty? && @other_errors.empty?
    end

    private

    def group_error
      Minitest::MultipleAssertionError.new(message_for_errors,
                                           location: location,
                                           result_label: result_label)
    end

    def message_for_errors
      errors = @failures + @other_errors
      "There were #{errors.size} errors#{group_label}:\n" +
        errors.map.with_index do |error, index|
          if error.is_a?(Minitest::Assertion)
            "    #{index + 1}) #{error.location}:\n#{indent(error.message)}\n"
          else
            "    #{index + 1}) #{error.class}: #{error.message}\n#{indent(error.backtrace.first)}\n"
          end
        end.join("\n")
    end

    def group_label
      label ? " in group #{label.inspect}" : ""
    end

    def result_label
      @other_errors.any? ? "Error" : nil
    end

    def indent(string, size: 6)
      prefix = (" " * size)
      string.split("\n").map { |str| "#{prefix}#{str}" }.join("\n")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aggregate_assertions-0.2.0 lib/aggregate_assertions/failure_group.rb
aggregate_assertions-0.1.1 lib/aggregate_assertions/failure_group.rb
aggregate_assertions-0.1.0 lib/aggregate_assertions/failure_group.rb