From 0ed684afbc9d9cddafa41776b481a56846849c4b Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sun, 16 May 2021 13:20:02 -0600 Subject: [PATCH] Integrate report and summary events --- src/spectator/formatting/formatter.cr | 3 +++ src/spectator/formatting/notifications.cr | 8 ++++++++ src/spectator/spec/events.cr | 16 ++++++++++++++++ src/spectator/spec/runner.cr | 21 ++++++++++----------- 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/spectator/formatting/formatter.cr b/src/spectator/formatting/formatter.cr index 0928de3..bcc7c9c 100644 --- a/src/spectator/formatting/formatter.cr +++ b/src/spectator/formatting/formatter.cr @@ -96,17 +96,20 @@ module Spectator::Formatting # Invoked after testing completes with a list of pending examples. # This method will be called with an empty list if there were no pending (skipped) examples. # Called after `#start_dump` and before `#dump_failures`. + # The *notification* will be an `ExampleSummaryNotification` type of object. def dump_pending(_notification) end # Invoked after testing completes with a list of failed examples. # This method will be called with an empty list if there were no failures. # Called after `#dump_pending` and before `#dump_summary`. + # The *notification* will be an `ExampleSummaryNotification` type of object. def dump_failures(_notification) end # Invoked after testing completes with summarized information from the test suite. # Called after `#dump_failures` and before `#dump_profile`. + # The *notification* will be an `SummaryNotification` type of object. def dump_summary(_notification) end diff --git a/src/spectator/formatting/notifications.cr b/src/spectator/formatting/notifications.cr index afe57e2..7fba582 100644 --- a/src/spectator/formatting/notifications.cr +++ b/src/spectator/formatting/notifications.cr @@ -1,7 +1,15 @@ require "../example" module Spectator::Formatting + # Structure indicating the test suite has started. record StartNotification, example_count : Int32 + # Structure indicating an event occurred with an example. record ExampleNotification, example : Example + + # Structure containing a subset of examples from the test suite. + record ExampleSummaryNotification, examples : Enumerable(Example) + + # Structure containing summarized information from the outcome of the test suite. + record SummaryNotification, report : Report end diff --git a/src/spectator/spec/events.cr b/src/spectator/spec/events.cr index e146e1c..7ef1fe3 100644 --- a/src/spectator/spec/events.cr +++ b/src/spectator/spec/events.cr @@ -35,6 +35,22 @@ module Spectator formatter.stop(nil) end + # Triggers the 'dump' events. + private def summarize(report) + formatter.start_dump + + notification = Formatting::ExampleSummaryNotification.new(report.pending) + formatter.dump_pending(notification) + + notification = Formatting::ExampleSummaryNotification.new(report.failures) + formatter.dump_failures(notification) + + notification = Formatting::SummaryNotification.new(report) + formatter.dump_summary(notification) + + formatter.dump_profile(nil) + end + # Triggers the 'close' event. # See `Formatting::Formatter#close` private def close diff --git a/src/spectator/spec/runner.cr b/src/spectator/spec/runner.cr index 3ffe030..adc8a82 100644 --- a/src/spectator/spec/runner.cr +++ b/src/spectator/spec/runner.cr @@ -1,4 +1,5 @@ require "../example" +require "../report" require "../run_flags" require "./events" @@ -26,11 +27,11 @@ module Spectator # or false if there was at least one failure. def run : Bool start - executed = [] of Example - elapsed = Time.measure { executed = run_examples } + elapsed = Time.measure { run_examples } stop - # TODO: Generate a report and pass it along to the formatter. + report = Report.generate(@examples, elapsed, nil) # TODO: Provide random seed. + summarize(report) false # TODO: Report real result @@ -41,15 +42,12 @@ module Spectator # Attempts to run all examples. # Returns a list of examples that ran. private def run_examples - Array(Example).new(example_count).tap do |executed| - @examples.each do |example| - result = run_example(example) - executed << example + @examples.each do |example| + result = run_example(example) - # Bail out if the example failed - # and configured to stop after the first failure. - break fail_fast if fail_fast? && result.fail? - end + # Bail out if the example failed + # and configured to stop after the first failure. + break fail_fast if fail_fast? && result.fail? end end @@ -58,6 +56,7 @@ module Spectator private def run_example(example) example_started(example) result = if dry_run? + # TODO: Pending examples return a pending result instead of pass in RSpec dry-run. dry_run_result else example.run