shard-spectator/src/spectator/config.cr

79 lines
2.4 KiB
Crystal
Raw Normal View History

2020-09-12 21:58:54 +00:00
require "./example_filter"
require "./formatting"
module Spectator
# Provides customization and describes specifics for how Spectator will run and report tests.
class Config
2019-03-23 23:00:21 +00:00
@formatters : Array(Formatting::Formatter)
2018-12-13 21:16:49 +00:00
2019-03-22 05:40:00 +00:00
# Indicates whether the test should abort on first failure.
getter? fail_fast : Bool
2019-03-22 19:21:37 +00:00
# Indicates whether the test should fail if there are no examples.
getter? fail_blank : Bool
2019-03-22 20:05:48 +00:00
# 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
2020-10-18 03:39:41 +00:00
# Indicates whether examples run in a random order.
2019-03-24 01:43:41 +00:00
getter? randomize : Bool
2020-10-18 03:39:41 +00:00
# Seed used for random number generation.
2020-10-18 04:11:04 +00:00
getter random_seed : UInt64
2020-07-27 16:36:53 +00:00
2020-10-18 03:39:41 +00:00
# Indicates whether timing information should be displayed.
2019-03-25 18:26:45 +00:00
getter? profile : Bool
2020-10-18 03:39:41 +00:00
# Filter determining examples to run.
2019-03-25 17:17:53 +00:00
getter example_filter : ExampleFilter
2018-12-13 21:16:49 +00:00
# Creates a new configuration.
2020-10-18 04:11:04 +00:00
# Properties are pulled from *source*.
# Typically, *source* is a `ConfigBuilder`.
def initialize(builder)
2019-03-23 23:00:21 +00:00
@formatters = builder.formatters
2019-03-22 05:40:00 +00:00
@fail_fast = builder.fail_fast?
2019-03-22 19:21:37 +00:00
@fail_blank = builder.fail_blank?
2019-03-22 20:05:48 +00:00
@dry_run = builder.dry_run?
2019-03-24 01:43:41 +00:00
@randomize = builder.randomize?
2020-10-18 04:11:04 +00:00
@random_seed = builder.seed
2019-03-25 18:26:45 +00:00
@profile = builder.profile?
2019-03-25 17:17:53 +00:00
@example_filter = builder.example_filter
2018-12-13 21:16:49 +00:00
end
2019-03-23 23:00:21 +00:00
2020-10-18 04:11:04 +00:00
# Shuffles the items in an array using the configured random settings.
# If `#randomize?` is true, the *items* are shuffled and returned as a new array.
# Otherwise, the items are left alone and returned as-is.
# The array of *items* is never modified.
def shuffle(items)
return items unless randomize?
items.shuffle(random)
end
# Shuffles the items in an array using the configured random settings.
# If `#randomize?` is true, the *items* are shuffled and returned.
# 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?
items.shuffle!(random)
end
2019-03-23 23:00:21 +00:00
# Yields each formatter that should be reported to.
def each_formatter
@formatters.each do |formatter|
yield formatter
end
end
2020-10-18 04:11:04 +00:00
# Retrieves the configured random number generator.
# This will produce the same generator with the same seed every time.
private def random
Random.new(random_seed)
end
end
end