Get config, spec, builder, and runner working together

This commit is contained in:
Michael Miller 2021-05-12 21:41:34 -06:00
parent 83c4b01e84
commit 81f509c083
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
6 changed files with 52 additions and 36 deletions

View file

@ -1,3 +1,4 @@
require "colorize"
require "log" require "log"
require "./spectator/includes" require "./spectator/includes"
@ -67,7 +68,7 @@ module Spectator
# Build the spec and run it. # Build the spec and run it.
DSL::Builder.config = config DSL::Builder.config = config
spec = DSL::Builder.build spec = DSL::Builder.build
spec.run(config.example_filter) spec.run
rescue ex rescue ex
# Catch all unhandled exceptions here. # Catch all unhandled exceptions here.
# Examples are already wrapped, so any exceptions they throw are caught. # Examples are already wrapped, so any exceptions they throw are caught.

View file

@ -17,6 +17,9 @@ module Spectator
# Seed used for random number generation. # Seed used for random number generation.
getter random_seed : UInt64 getter random_seed : UInt64
# Filter used to select which examples to run.
getter example_filter : ExampleFilter
# Creates a new configuration. # Creates a new configuration.
# Properties are pulled from *source*. # Properties are pulled from *source*.
# Typically, *source* is a `Config::Builder`. # Typically, *source* is a `Config::Builder`.

View file

@ -13,9 +13,11 @@ module Spectator
# Seed used for random number generation. # Seed used for random number generation.
property random_seed : UInt64 = Random.rand(UInt64) property random_seed : UInt64 = Random.rand(UInt64)
# Toggles indicating how the test spec should execute.
property run_flags = RunFlags::None
@primary_formatter : Formatting::Formatter? @primary_formatter : Formatting::Formatter?
@additional_formatters = [] of Formatting::Formatter @additional_formatters = [] of Formatting::Formatter
@run_flags = RunFlags::None
@filters = [] of ExampleFilter @filters = [] of ExampleFilter
# Creates a configuration. # Creates a configuration.

View file

@ -1,3 +1,4 @@
require "colorize"
require "option_parser" require "option_parser"
require "../formatting" require "../formatting"
require "../line_example_filter" require "../line_example_filter"

View file

@ -14,7 +14,7 @@ module Spectator
# Runs all selected examples and returns the results. # Runs all selected examples and returns the results.
def run def run
Runner.new(examples, @config).run Runner.new(examples, @config.run_flags).run
end end
# Selects and shuffles the examples that should run. # Selects and shuffles the examples that should run.

View file

@ -1,4 +1,5 @@
require "../example" require "../example"
require "../run_flags"
module Spectator module Spectator
class Spec class Spec
@ -7,7 +8,7 @@ module Spectator
# Creates the runner. # Creates the runner.
# The collection of *examples* should be pre-filtered and shuffled. # The collection of *examples* should be pre-filtered and shuffled.
# This runner will run each example in the order provided. # This runner will run each example in the order provided.
def initialize(@examples : Enumerable(Example), @config : Config) def initialize(@examples : Enumerable(Example), @run_flags = RunFlags::None)
end end
# Runs the spec. # Runs the spec.
@ -16,32 +17,25 @@ module Spectator
# True will be returned if the spec ran successfully, # True will be returned if the spec ran successfully,
# or false if there was at least one failure. # or false if there was at least one failure.
def run : Bool def run : Bool
# Indicate the suite is starting. executed = [] of Example
@config.each_formatter(&.start_suite(@suite)) elapsed = Time.measure { executed = run_examples }
# Run all examples and capture the results. # TODO: Generate a report and pass it along to the formatter.
examples = Array(Example).new(@suite.size)
elapsed = Time.measure do
collect_results(examples)
end
# Generate a report and pass it along to the formatter. false # TODO: Report real result
remaining = @suite.size - examples.size
seed = (@config.random_seed if @config.randomize?)
report = Report.new(examples, elapsed, remaining, @config.fail_blank?, seed)
@config.each_formatter(&.end_suite(report, profile(report)))
!report.failed?
end end
# Runs all examples and adds them to a list. # Attempts to run all examples.
private def collect_results(examples) # Returns a list of examples that ran.
example_order.each do |example| private def run_examples
result = run_example(example) Array(Example).new(example_count).tap do |executed|
examples << example @examples.each do |example|
if @config.fail_fast? && result.is_a?(FailResult) result = run_example(example)
example.group.call_once_after_all executed << example
break
# Bail out if the example failed
# and configured to stop after the first failure.
break fail_fast if fail_fast? && result.fail?
end end
end end
end end
@ -49,18 +43,15 @@ module Spectator
# Runs a single example and returns the result. # Runs a single example and returns the result.
# The formatter is given the example and result information. # The formatter is given the example and result information.
private def run_example(example) private def run_example(example)
@config.each_formatter(&.start_example(example)) if dry_run?
result = if @config.dry_run? dry_run_result
dry_run_result(example) else
else example.run
example.run end
end
@config.each_formatter(&.end_example(example))
result
end end
# Creates a fake result for an example. # Creates a fake result.
private def dry_run_result(example) private def dry_run_result
expectations = [] of Expectation expectations = [] of Expectation
PassResult.new(Time::Span.zero, expectations) PassResult.new(Time::Span.zero, expectations)
end end
@ -69,6 +60,24 @@ module Spectator
private def profile(report) private def profile(report)
Profile.generate(report) if @config.profile? Profile.generate(report) if @config.profile?
end end
# Indicates whether examples should be simulated, but not run.
private def dry_run?
@run_flags.dry_run?
end
# Indicates whether test execution should stop after the first failure.
private def fail_fast?
@run_flags.fail_fast?
end
private def fail_fast : Nil
end
# Number of examples configured to run.
private def example_count
@examples.size
end
end end
end end
end end