diff --git a/src/spectator/formatting/profile_block.cr b/src/spectator/formatting/profile_block.cr new file mode 100644 index 0000000..d17cf86 --- /dev/null +++ b/src/spectator/formatting/profile_block.cr @@ -0,0 +1,28 @@ +module Spectator::Formatting + # Contents of a profile block. + private struct ProfileBlock + # Creates the block. + def initialize(@profile : Profile) + end + + # Appends the block to the output. + def to_s(io) + io.puts(ProfileSummary.new(@profile)) + + indent = Indent.new(io) + indent.increase do + @profile.each do |result| + entry(indent, result) + end + end + end + + # Adds a result entry to the output. + private def entry(indent, result) + indent.line(result.example) + indent.increase do + indent.line(SourceTiming.new(result.elapsed, result.example.source)) + end + end + end +end diff --git a/src/spectator/formatting/profile_summary.cr b/src/spectator/formatting/profile_summary.cr new file mode 100644 index 0000000..9d6a05c --- /dev/null +++ b/src/spectator/formatting/profile_summary.cr @@ -0,0 +1,29 @@ +module Spectator::Formatting + # Top line of a profile block which gives a summary. + private struct ProfileSummary + # Creates the summary line. + def initialize(@profile : Profile) + end + + # Appends the summary to the output. + def to_s(io) + io << "Top " + io << @profile.size + io << " slowest examples (" + io << human_time + io << ", " + io.printf("%.2f", percentage) + io << "% of total time):" + end + + # Creates a human-friendly string for the total time. + private def human_time + HumanTime.new(@profile.total_time) + end + + # Percentage (0 to 100) of total time. + private def percentage + @profile.percentage * 100 + end + end +end diff --git a/src/spectator/formatting/source_timing.cr b/src/spectator/formatting/source_timing.cr new file mode 100644 index 0000000..527fa17 --- /dev/null +++ b/src/spectator/formatting/source_timing.cr @@ -0,0 +1,16 @@ +module Spectator::Formatting + # Produces the timing line in a profile block. + # This contains the length of time, and the example's source. + private struct SourceTiming + # Creates the source timing line. + def initialize(@span : Time::Span, @source : Source) + end + + # Appends the source timing information to the output. + def to_s(io) + io << HumanTime.new(@span).colorize.bold + io << ' ' + io << @source + end + end +end diff --git a/src/spectator/formatting/suite_summary.cr b/src/spectator/formatting/suite_summary.cr index be900f5..3d2dcfd 100644 --- a/src/spectator/formatting/suite_summary.cr +++ b/src/spectator/formatting/suite_summary.cr @@ -40,7 +40,7 @@ module Spectator::Formatting # Produces the profiling section of the summary. private def profile(profile) - raise NotImplementedError.new("profile") + @io.puts ProfileBlock.new(profile) end # Produces the statistical section of the summary.