Sha256: a8c977e62be34e04d2033b8b2d8dbc9ff939b30c6465cf81f38fcd6e1619421d

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

module Coco

  # I format coverages data for console output.
  #
  class ConsoleFormatter

    # Public: Get a colored report, formatted for console output.
    #
    # Returns percent covered and associated filenames as a multilines
    # or a single line String.
    #
    def format
      @config[:single_line_report] ? single_line_message : multilines_message
    end

    # Get the link for the report's index file.
    #
    # Returns String.
    #
    def link
      unless @formatted_output.empty?
        'See file://' +
          File.expand_path(File.join(Coco::HtmlDirectory.new.coverage_dir,
                                     'index.html'))
      end
    end

    # Public: Creates a new ConsoleFormatter.
    #
    # uncovered - An Array list of uncovered files.
    # threshold - The Fixnum percentage threshold.
    # result    - A CoverageResult.
    # config    - A Configuration.
    #
    def initialize(uncovered, threshold, result, config)
      @uncovered = uncovered
      @result = result

      @formatted_output = []
      compute_percentage
      add_percentage_to_uncovered
      @formatted_output.sort!
      @formatted_output.map! do |percentage, filename|
        text = ColoredString.new "#{percentage}% #{filename}"
        if percentage <= 50
          text.red
        elsif percentage >= threshold
          text.green
        else
          text.yellow
        end
      end
      @summary = Summary.new(result, uncovered)
      @config = config
    end

    private

    def compute_percentage
      @result.not_covered_enough.each do |filename, coverage|
        percentage = CoverageStat.coverage_percent(coverage)
        @formatted_output << [percentage, filename]
      end
    end

    def add_percentage_to_uncovered
      @uncovered.each { |filename| @formatted_output << [0, filename] }
    end

    def single_line_message
      ColoredString.new(@summary.to_s).yellow
    end

    def multilines_message
      @formatted_output.join("\n") + "\n" + @summary.to_s + "\n"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
coco-0.15.0 lib/coco/formatter/console_formatter.rb
coco-0.14.0 lib/coco/formatter/console_formatter.rb