Sha256: 0fd392b4ee5e081f30d998d0cb6014a8f1c239487662b1bcc0458b1a42dec844

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

require "petitest/texts/base_text"

module Petitest
  module Texts
    class TestCountsText < ::Petitest::Texts::BaseText
      # @return [Array<Petitest::Test>]
      attr_reader :tests

      # @param tests [Array<Petitest::Test>]
      def initialize(tests:)
        @tests = tests
      end

      # @note Override
      def to_s
        [
          heading,
          indent(body, 2),
        ].join("\n\n")
      end

      private

      # @return [String]
      def body
        [
          text_of_count_of_tests,
          text_of_count_of_passed_tests,
          text_of_count_of_failed_tests,
          text_of_count_of_skipped_tests,
        ].join("\n")
      end

      # @return [Integer]
      def count_of_failed_tests
        tests.map(&:runner).select(&:failed?).length
      end

      # @return [Integer]
      def count_of_passed_tests
        tests.map(&:runner).select(&:passed?).length
      end

      # @return [Integer]
      def count_of_skipped_tests
        tests.map(&:runner).select(&:skipped?).length
      end

      # @return [Integer]
      def count_of_tests
        tests.length
      end

      # @return [String]
      def heading
        "Counts:"
      end

      # @return [Integer]
      def max_digits_length
        @max_digits_length ||= count_of_tests.to_s.length
      end

      # @return [String]
      def text_of_count_of_failed_tests
        colorize("%#{max_digits_length}d failures" % count_of_failed_tests, :error)
      end

      # @return [String]
      def text_of_count_of_passed_tests
        colorize("%#{max_digits_length}d passes" % count_of_passed_tests, :pass)
      end

      # @return [String]
      def text_of_count_of_skipped_tests
        colorize("%#{max_digits_length}d skips" % count_of_skipped_tests, :skip)
      end

      # @return [String]
      def text_of_count_of_tests
        "%#{max_digits_length}d tests" % count_of_tests
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
petitest-0.3.1 lib/petitest/texts/test_counts_text.rb
petitest-0.3.0 lib/petitest/texts/test_counts_text.rb