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 "./example_filter"
require "./formatting" require "./formatting"
require "./run_flags"
module Spectator module Spectator
# Provides customization and describes specifics for how Spectator will run and report tests. # Provides customization and describes specifics for how Spectator will run and report tests.
class Config class Config
@formatters : Array(Formatting::Formatter) @formatters : Array(Formatting::Formatter)
# Indicates whether the test should abort on first failure. # Flags indicating how the spec should run.
getter? fail_fast : Bool getter run_flags : RunFlags
# 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
# Seed used for random number generation. # Seed used for random number generation.
getter random_seed : UInt64 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. # Creates a new configuration.
# Properties are pulled from *source*. # Properties are pulled from *source*.
# Typically, *source* is a `ConfigBuilder`. # Typically, *source* is a `ConfigBuilder`.
def initialize(source) def initialize(source)
@formatters = source.formatters @formatters = source.formatters
@fail_fast = source.fail_fast? @run_flags = source.run_flags
@fail_blank = source.fail_blank?
@dry_run = source.dry_run?
@randomize = source.randomize?
@random_seed = source.random_seed @random_seed = source.random_seed
@profile = source.profile?
@example_filter = source.example_filter @example_filter = source.example_filter
end end
@ -47,7 +28,7 @@ module Spectator
# Otherwise, the items are left alone and returned as-is. # Otherwise, the items are left alone and returned as-is.
# The array of *items* is never modified. # The array of *items* is never modified.
def shuffle(items) def shuffle(items)
return items unless randomize? return items unless run_flags.randomize?
items.shuffle(random) items.shuffle(random)
end end
@ -57,7 +38,7 @@ module Spectator
# Otherwise, the items are left alone and returned as-is. # Otherwise, the items are left alone and returned as-is.
# The array of *items* is modified, the items are shuffled in-place. # The array of *items* is modified, the items are shuffled in-place.
def shuffle!(items) def shuffle!(items)
return items unless randomize? return items unless run_flags.randomize?
items.shuffle!(random) items.shuffle!(random)
end end

View File

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