From 49175e56ac39b22adc8221b7ff104e7228ee1bc5 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 15 Feb 2019 23:05:48 -0700 Subject: [PATCH] Initial code for summary output --- src/spectator/formatters/failure_block.cr | 45 +++++++++++++++++++ src/spectator/formatters/suite_summary.cr | 54 ++++++++++++++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/spectator/formatters/failure_block.cr diff --git a/src/spectator/formatters/failure_block.cr b/src/spectator/formatters/failure_block.cr new file mode 100644 index 0000000..0d58c59 --- /dev/null +++ b/src/spectator/formatters/failure_block.cr @@ -0,0 +1,45 @@ +module Spectator::Formatters + class FailureBlock + def initialize(@index : Int32, @result : FailedResult) + end + + def source(io) + io << @result.example.source_file + io << ':' + io << @result.example.source_line + end + + def to_s(io) + to_s_title(io) + to_s_message(io) + to_s_expected_actual(io) + to_s_source(io) + end + + private def to_s_title(io) + io << " " + io << @index + io << ')' + io << @result.example + io.puts + end + + private def to_s_message(io) + io << " Failure: " + io << @result.error + io.puts + end + + private def to_s_expected_actual(io) + io.puts + io.puts " Expected: TODO" + io.puts " got: TODO" + io.puts + end + + private def to_s_source(io) + io << " # " + source(io) + end + end +end diff --git a/src/spectator/formatters/suite_summary.cr b/src/spectator/formatters/suite_summary.cr index 8ac2b1d..cff2ea0 100644 --- a/src/spectator/formatters/suite_summary.cr +++ b/src/spectator/formatters/suite_summary.cr @@ -10,7 +10,59 @@ module Spectator::Formatters # A block describing each failure is displayed. # At the end, the totals and runtime are printed. def end_suite(report) - raise NotImplementedError.new("SuiteSummary#end_suite") + failures = report.failures.map_with_index { |result, index| FailureBlock.new(index + 1, result) } + puts + puts + display_failures(failures) if failures.any? + display_stats(report) + display_failure_commands(failures) if failures.any? + end + + private def display_failures(failures) + puts "Failures:" + puts + failures.each do |block| + puts block + puts + end + end + + private def display_stats(report) + puts "Finished in #{human_time(report.runtime)}" + puts "#{report.example_count} examples, #{report.failed_count} failures, #{report.error_count} errors, #{report.pending_count} pending" + end + + private def display_failure_commands(failures) + puts + puts "Failed examples:" + puts + failures.each do |block| + print "crystal spec " + block.source(STDOUT) + puts " # TODO" + end + end + + private def human_time(span) + millis = span.total_milliseconds + return "#{(millis * 1000).round.to_i} microseconds" if millis < 1 + + seconds = span.total_seconds + return "#{millis.round(2)} milliseconds" if seconds < 1 + return "#{seconds.round(2)} seconds" if seconds < 60 + + int_seconds = seconds.to_i + minutes = int_seconds / 60 + int_seconds %= 60 + return sprintf("%i:%02i", minutes, int_seconds) if minutes < 60 + + hours = minutes / 60 + minutes %= 60 + return sprintf("%i:%02i:%02i", hours, minutes, int_seconds) if hours < 24 + + days = hours / 24 + hours %= 24 + return sprintf("%i days %i:%02i:%02i", days, hours, minutes, int_seconds) end end end