lib/cucumber/formatter/console.rb in cucumber-2.99.0 vs lib/cucumber/formatter/console.rb in cucumber-3.0.0.pre.1

- old
+ new

@@ -1,8 +1,9 @@ +# frozen_string_literal: true require 'cucumber/formatter/ansicolor' require 'cucumber/formatter/duration' -require 'cucumber/formatter/summary' +require 'cucumber/gherkin/i18n' module Cucumber module Formatter # This module contains helper methods that are used by formatters that @@ -26,11 +27,10 @@ # which returns the formatted (e.g. colored) string. module Console extend ANSIColor include Duration - include Summary def format_step(keyword, step_match, status, source_indent) comment = if source_indent c = ('# ' + step_match.location.to_s).indent(source_indent) format_string(c, :comment) @@ -57,79 +57,57 @@ def print_steps(status) print_elements(runtime.steps(status), status, 'steps') end def print_elements(elements, status, kind) - if elements.any? + return if elements.empty? + + element_messages = element_messages(elements, status) + print_element_messages(element_messages, status, kind) + end + + def print_element_messages(element_messages, status, kind) + if element_messages.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 - message = linebreaks(element.backtrace_line, ENV['CUCUMBER_TRUNCATE_OUTPUT'].to_i) - @io.puts(format_string(message, status)) - end + element_messages.each do |message| + @io.puts(format_string(message, status)) @io.puts @io.flush end end - def print_stats(features, options) - duration = features ? features.duration : nil - print_statistics(duration, options) - end - - def print_statistics(duration, options) - failures = collect_failing_scenarios(runtime) - if !failures.empty? - print_failing_scenarios(failures, options.custom_profiles, options[:source]) + def print_statistics(duration, config, counts, issues) + if issues.any? + @io.puts issues.to_s + @io.puts end - @io.puts scenario_summary(runtime) {|status_count, status| format_string(status_count, status)} - @io.puts step_summary(runtime) {|status_count, status| format_string(status_count, status)} - @io.puts(format_duration(duration)) if duration && options[:duration] + @io.puts counts.to_s + @io.puts(format_duration(duration)) if duration && config.duration? - if runtime.configuration.randomize? + if config.randomize? @io.puts - @io.puts "Randomized with seed #{runtime.configuration.seed}" + @io.puts "Randomized with seed #{config.seed}" end @io.flush end - def collect_failing_scenarios(runtime) - # TODO: brittle - stop coupling to types - scenario_class = LegacyApi::Ast::Scenario - example_table_class = Core::Ast::ExamplesTable - - runtime.scenarios(:failed).select do |s| - [scenario_class, example_table_class].include?(s.class) - end.map do |s| - s.is_a?(example_table_class)? s.scenario_outline : s - end + def print_exception(e, status, indent) + string = exception_message_string(e, indent) + @io.puts(format_string(string, status)) end - def print_failing_scenarios(failures, custom_profiles, given_source) - @io.puts format_string("Failing Scenarios:", :failed) - failures.each do |failure| - profiles_string = custom_profiles.empty? ? '' : (custom_profiles.map{|profile| "-p #{profile}" }).join(' ') + ' ' - source = given_source ? format_string(" # " + failure.name, :comment) : '' - @io.puts format_string("cucumber #{profiles_string}" + failure.location, :failed) + source - end - @io.puts - end - - def print_exception(e, status, indent) - message = "#{e.message} (#{e.class})".force_encoding("UTF-8") + def exception_message_string(e, indent) + message = "#{e.message} (#{e.class})".dup.force_encoding("UTF-8") message = linebreaks(message, ENV['CUCUMBER_TRUNCATE_OUTPUT'].to_i) string = "#{message}\n#{e.backtrace.join("\n")}".indent(indent) - @io.puts(format_string(string, status)) end # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/10655 def linebreaks(s, max) return s unless max && max > 0 @@ -149,12 +127,19 @@ def print_snippets(options) return unless options[:snippets] return if runtime.steps(:undefined).empty? + snippet_text_proc = lambda { |step_keyword, step_name, multiline_arg| + runtime.snippet_text(step_keyword, step_name, multiline_arg) + } + do_print_snippets(snippet_text_proc) + end + + def do_print_snippets(snippet_text_proc) snippets = @snippets_input.map do |data| - @runtime.snippet_text(data.actual_keyword, data.step.name, data.step.multiline_arg) + snippet_text_proc.call(data.actual_keyword, data.step.name, data.step.multiline_arg) end.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) @@ -163,14 +148,18 @@ @io.flush end def print_passing_wip(options) return unless options[:wip] - passed = runtime.scenarios(:passed) - if passed.any? + passed_messages = element_messages(runtime.scenarios(:passed), :passed) + do_print_passing_wip(passed_messages) + end + + def do_print_passing_wip(passed_messages) + if passed_messages.any? @io.puts format_string("\nThe --wip switch was used, so I didn't expect anything to pass. These scenarios passed:", :failed) - print_elements(passed, :passed, "scenarios") + print_element_messages(passed_messages, :passed, "scenarios") else @io.puts format_string("\nThe --wip switch was used, so the failures were expected. All is good.\n", :passed) end end @@ -215,11 +204,14 @@ @delayed_messages = [] end def print_profile_information return if @options[:skip_profile_information] || @options[:profiles].nil? || @options[:profiles].empty? - profiles = @options[:profiles] + do_print_profile_information(@options[:profiles]) + end + + def do_print_profile_information(profiles) profiles_sentence = '' profiles_sentence = profiles.size == 1 ? profiles.first : "#{profiles[0...-1].join(', ')} and #{profiles.last}" @io.puts "Using the #{profiles_sentence} profile#{'s' if profiles.size> 1}..." @@ -236,9 +228,26 @@ fmt end def hook?(test_step) not test_step.source.last.respond_to?(:actual_keyword) + end + + def element_messages(elements, status) + element_messages = elements.map do |element| + if status == :failed + message = exception_message_string(element.exception, 0) + else + message = linebreaks(element.backtrace_line, ENV['CUCUMBER_TRUNCATE_OUTPUT'].to_i) + end + end + end + + def snippet_text(step_keyword, step_name, multiline_arg) + keyword = Cucumber::Gherkin::I18n.code_keyword_for(step_keyword).strip + config.snippet_generators.map { |generator| + generator.call(keyword, step_name, multiline_arg, config.snippet_type) + }.join("\n") end class SnippetData attr_reader :actual_keyword, :step def initialize(actual_keyword, step)