<commit_before>require 'cucumber/formatter/ansicolor'

module Cucumber
  module Formatter
    module Console
      extend ANSIColor
      FORMATS = Hash.new{|hash, format| hash[format] = method(format).to_proc}

      def format_step(keyword, step_match, status, source_indent)
        comment = if source_indent
          c = (' # ' + step_match.file_colon_line).indent(source_indent)
          format_string(c, :comment)
        else
          ''
        end

        format = format_for(status, :param)
        line = keyword + " " + step_match.format_args(format) + comment
        format_string(line, status)
      end

      def format_string(string, status)
        fmt = format_for(status)
        if Proc === fmt
          fmt.call(string)
        else
          fmt % string
        end
      end

      def print_steps(status)
        print_elements(step_mother.steps(status), status, 'steps')
      end

      def print_elements(elements, status, kind)
        if elements.any?
          @io.puts(format_string("(::) #{status} #{kind} (::)", status))
          @io.puts
          @io.flush
        end

        elements.each_with_index do |element, i|
          if status == :failed
            print_exception(element.exception, status, 0)
          else
            @io.puts(format_string(element.backtrace_line, status))
          end
          @io.puts
          @io.flush
        end
      end

      def print_counts
        @io.print dump_count(step_mother.scenarios.length, "scenario")
        scenario_counts = [:failed, :skipped, :undefined, :pending, :passed].map do |status|
          if step_mother.scenarios(status).any?
            count_string = "#{step_mother.scenarios(status).length} #{status.to_s}"
            format_string(count_string, status)
          end
        end
        @io.puts(" (#{scenario_counts.compact.join(', ')})")

        @io.print dump_count(step_mother.steps.length, "step")
        step_counts = [:failed, :skipped, :undefined, :pending, :passed].map do |status|
          if step_mother.steps(status).any?
            count_string = "#{step_mother.steps(status).length} #{status.to_s}"
            format_string(count_string, status)
          end
        end
        @io.puts(" (#{step_counts.compact.join(', ')})")

        @io.flush
      end

      def print_exception(e, status, indent)
        if @options[:strict] || !(Undefined === e) || e.nested?
          @io.puts(format_string("#{e.message} (#{e.class})\n#{e.backtrace.join("\n")}".indent(indent), status))
        end
      end

      def print_snippets(options)
        return unless options[:snippets]
        undefined = step_mother.steps(:undefined)
        return if undefined.empty?
        snippets = undefined.map do |step|
          step_name = Undefined === step.exception ? step.exception.step_name : step.name
          step_multiline_class = step.multiline_arg ? step.multiline_arg.class : nil
          snippet = @step_mother.snippet_text(step.actual_keyword, step_name, step_multiline_class)
          snippet
        end.compact.uniq

        text = "\nYou can implement step definitions for undefined steps with these snippets:\n\n"
        text += snippets.join("\n\n")

        @io.puts format_string(text, :undefined)
        @io.puts
        @io.flush
      end

      def announce(announcement)
        @io.puts
        @io.puts(format_string(announcement, :tag))
        @io.flush
      end

    private

      def dump_count(count, what, state=nil)
        [count, state, "#{what}#{count == 1 ? '' : 's'}"].compact.join(" ")
      end<commit_msg>remove duplication<commit_after>require 'cucumber/formatter/ansicolor'

module Cucumber
  module Formatter
    module Console
      extend ANSIColor
      FORMATS = Hash.new{|hash, format| hash[format] = method(format).to_proc}

      def format_step(keyword, step_match, status, source_indent)
        comment = if source_indent
          c = (' # ' + step_match.file_colon_line).indent(source_indent)
          format_string(c, :comment)
        else
          ''
        end

        format = format_for(status, :param)
        line = keyword + " " + step_match.format_args(format) + comment
        format_string(line, status)
      end

      def format_string(string, status)
        fmt = format_for(status)
        if Proc === fmt
          fmt.call(string)
        else
          fmt % string
        end
      end

      def print_steps(status)
        print_elements(step_mother.steps(status), status, 'steps')
      end

      def print_elements(elements, status, kind)
        if elements.any?
          @io.puts(format_string("(::) #{status} #{kind} (::)", status))
          @io.puts
          @io.flush
        end

        elements.each_with_index do |element, i|
          if status == :failed
            print_exception(element.exception, status, 0)
          else
            @io.puts(format_string(element.backtrace_line, status))
          end
          @io.puts
          @io.flush
        end
      end

      def print_counts
        @io.print dump_count(step_mother.scenarios.length, "scenario")
        print_status_counts{|status| step_mother.scenarios(status)}

        @io.print dump_count(step_mother.steps.length, "step")
        print_status_counts{|status| step_mother.steps(status)}

        @io.flush
      end
      
      def print_exception(e, status, indent)
        if @options[:strict] || !(Undefined === e) || e.nested?
          @io.puts(format_string("#{e.message} (#{e.class})\n#{e.backtrace.join("\n")}".indent(indent), status))
        end
      end

      def print_snippets(options)
        return unless options[:snippets]
        undefined = step_mother.steps(:undefined)
        return if undefined.empty?
        snippets = undefined.map do |step|
          step_name = Undefined === step.exception ? step.exception.step_name : step.name
          step_multiline_class = step.multiline_arg ? step.multiline_arg.class : nil
          snippet = @step_mother.snippet_text(step.actual_keyword, step_name, step_multiline_class)
          snippet
        end.compact.uniq

        text = "\nYou can implement step definitions for undefined steps with these snippets:\n\n"
        text += snippets.join("\n\n")

        @io.puts format_string(text, :undefined)
        @io.puts
        @io.flush
      end

      def announce(announcement)
        @io.puts
        @io.puts(format_string(announcement, :tag))
        @io.flush
      end

    private

      def print_status_counts
        counts = [:failed, :skipped, :undefined, :pending, :passed].map do |status|
          elements = yield status
          elements.any? ? format_string("#{elements.length} #{status.to_s}", status) : nil
        end
        @io.puts(" (#{counts.compact.join(', ')})")
      end

      def dump_count(count, what, state=nil)
        [count, state, "#{what}#{count == 1 ? '' : 's'}"].compact.join(" ")
      end
{'fraction_non_alphanumeric': 0.11957305639694216, 'fraction_numerical': 0.0005769508149430262, 'mean_word_length': 2.2508204406938583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4990701', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2081, 'n_words': 426}