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 "./spectator/includes"
@ -67,7 +68,7 @@ module Spectator
# Build the spec and run it.
DSL::Builder.config = config
spec = DSL::Builder.build
spec.run(config.example_filter)
spec.run
rescue ex
# Catch all unhandled exceptions here.
# 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.
getter random_seed : UInt64
# Filter used to select which examples to run.
getter example_filter : ExampleFilter
# Creates a new configuration.
# Properties are pulled from *source*.
# Typically, *source* is a `Config::Builder`.

View file

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

View file

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

View file

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

View file

@ -1,4 +1,5 @@
require "../example"
require "../run_flags"
module Spectator
class Spec
@ -7,7 +8,7 @@ module Spectator
# Creates the runner.
# The collection of *examples* should be pre-filtered and shuffled.
# 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
# Runs the spec.
@ -16,32 +17,25 @@ module Spectator
# True will be returned if the spec ran successfully,
# or false if there was at least one failure.
def run : Bool
# Indicate the suite is starting.
@config.each_formatter(&.start_suite(@suite))
executed = [] of Example
elapsed = Time.measure { executed = run_examples }
# Run all examples and capture the results.
examples = Array(Example).new(@suite.size)
elapsed = Time.measure do
collect_results(examples)
end
# TODO: Generate a report and pass it along to the formatter.
# Generate a report and pass it along to the formatter.
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?
false # TODO: Report real result
end
# Runs all examples and adds them to a list.
private def collect_results(examples)
example_order.each do |example|
result = run_example(example)
examples << example
if @config.fail_fast? && result.is_a?(FailResult)
example.group.call_once_after_all
break
# 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
# 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
@ -49,18 +43,15 @@ module Spectator
# Runs a single example and returns the result.
# The formatter is given the example and result information.
private def run_example(example)
@config.each_formatter(&.start_example(example))
result = if @config.dry_run?
dry_run_result(example)
else
example.run
end
@config.each_formatter(&.end_example(example))
result
if dry_run?
dry_run_result
else
example.run
end
end
# Creates a fake result for an example.
private def dry_run_result(example)
# Creates a fake result.
private def dry_run_result
expectations = [] of Expectation
PassResult.new(Time::Span.zero, expectations)
end
@ -69,6 +60,24 @@ module Spectator
private def profile(report)
Profile.generate(report) if @config.profile?
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