Use enum flags for run modes

This commit is contained in:
Michael Miller 2021-05-08 22:51:54 -06:00
parent e8848d6855
commit 83d7657b18
No known key found for this signature in database
GPG Key ID: F9A0C5C65B162436
3 changed files with 64 additions and 45 deletions

View File

@ -1,44 +1,25 @@
require "./example_filter"
require "./formatting"
require "./run_flags"
module Spectator
# Provides customization and describes specifics for how Spectator will run and report tests.
class Config
@formatters : Array(Formatting::Formatter)
# Indicates whether the test should abort on first failure.
getter? fail_fast : Bool
# Indicates whether the test should fail if there are no examples.
getter? fail_blank : Bool
# Indicates whether the test should be done as a dry-run.
# Examples won't run, but the output will show that they did.
getter? dry_run : Bool
# Indicates whether examples run in a random order.
getter? randomize : Bool
# Flags indicating how the spec should run.
getter run_flags : RunFlags
# Seed used for random number generation.
getter random_seed : UInt64
# Indicates whether timing information should be displayed.
getter? profile : Bool
# Filter determining examples to run.
getter example_filter : ExampleFilter
# Creates a new configuration.
# Properties are pulled from *source*.
# Typically, *source* is a `ConfigBuilder`.
def initialize(source)
@formatters = source.formatters
@fail_fast = source.fail_fast?
@fail_blank = source.fail_blank?
@dry_run = source.dry_run?
@randomize = source.randomize?
@run_flags = source.run_flags
@random_seed = source.random_seed
@profile = source.profile?
@example_filter = source.example_filter
end
@ -47,7 +28,7 @@ module Spectator
# Otherwise, the items are left alone and returned as-is.
# The array of *items* is never modified.
def shuffle(items)
return items unless randomize?
return items unless run_flags.randomize?
items.shuffle(random)
end
@ -57,7 +38,7 @@ module Spectator
# Otherwise, the items are left alone and returned as-is.
# The array of *items* is modified, the items are shuffled in-place.
def shuffle!(items)
return items unless randomize?
return items unless run_flags.randomize?
items.shuffle!(random)
end

View File

@ -2,6 +2,7 @@ require "./composite_example_filter"
require "./config"
require "./example_filter"
require "./null_example_filter"
require "./run_flags"
module Spectator
# Mutable configuration used to produce a final configuration.
@ -18,11 +19,7 @@ module Spectator
@primary_formatter : Formatting::Formatter?
@additional_formatters = [] of Formatting::Formatter
@fail_fast = false
@fail_blank = false
@dry_run = false
@randomize = false
@profile = false
@run_flags = RunFlags::None
@filters = [] of ExampleFilter
# Creates a configuration.
@ -55,79 +52,99 @@ module Spectator
# Enables fail-fast mode.
def fail_fast
self.fail_fast = true
@run_flags |= RunFlags::FailFast
end
# Sets the fail-fast flag.
def fail_fast=(flag)
@fail_fast = flag
if flag
@run_flags |= RunFlags::FailFast
else
@run_flags &= ~RunFlags::FailFast
end
end
# Indicates whether fail-fast mode is enabled.
protected def fail_fast?
@fail_fast
@run_flags.fail_fast?
end
# Enables fail-blank mode (fail on no tests).
def fail_blank
self.fail_blank = true
@run_flags |= RunFlags::FailBlank
end
# Enables or disables fail-blank mode.
def fail_blank=(flag)
@fail_blank = flag
if flag
@run_flags |= RunFlags::FailBlank
else
@run_flags &= ~RunFlags::FailBlank
end
end
# Indicates whether fail-fast mode is enabled.
# That is, it is a failure if there are no tests.
protected def fail_blank?
@fail_blank
@run_flags.fail_blank?
end
# Enables dry-run mode.
def dry_run
self.dry_run = true
@run_flags |= RunFlags::DryRun
end
# Enables or disables dry-run mode.
def dry_run=(flag)
@dry_run = flag
if flag
@run_flags |= RunFlags::DryRun
else
@run_flags &= ~RunFlags::DryRun
end
end
# Indicates whether dry-run mode is enabled.
# In this mode, no tests are run, but output acts like they were.
protected def dry_run?
@dry_run
@run_flags.dry_run?
end
# Randomizes test execution order.
def randomize
self.randomize = true
@run_flags |= RunFlags::Randomize
end
# Enables or disables running tests in a random order.
def randomize=(flag)
@randomize = flag
if flag
@run_flags |= RunFlags::Randomize
else
@run_flags &= ~RunFlags::Randomize
end
end
# Indicates whether tests are run in a random order.
protected def randomize?
@randomize
@run_flags.randomize?
end
# Displays profiling information
def profile
self.profile = true
@run_flags |= RunFlags::Profile
end
# Enables or disables displaying profiling information.
def profile=(flag)
@profile = flag
if flag
@run_flags |= RunFlags::Profile
else
@run_flags &= ~RunFlags::Profile
end
end
# Indicates whether profiling information should be displayed.
protected def profile?
@profile
@run_flags.profile?
end
# Adds a filter to determine which examples can run.

View File

@ -0,0 +1,21 @@
module Spectator
# Toggles indicating how the test spec should execute.
@[Flags]
enum RunFlags
# Indicates whether the test should abort on first failure.
FailFast
# Indicates whether the test should fail if there are no examples.
FailBlank
# Indicates whether the test should be done as a dry-run.
# Examples won't run, but the output will show that they did.
DryRun
# Indicates whether examples run in a random order.
Randomize
# Indicates whether timing information should be generated.
Profile
end
end