2021-05-09 05:37:33 +00:00
|
|
|
require "./config/*"
|
2021-08-08 18:13:59 +00:00
|
|
|
require "./node_filter"
|
2021-05-09 05:37:33 +00:00
|
|
|
require "./example_group"
|
|
|
|
require "./example_iterator"
|
|
|
|
require "./formatting/formatter"
|
2021-05-09 04:51:54 +00:00
|
|
|
require "./run_flags"
|
2020-09-12 21:58:54 +00:00
|
|
|
|
2018-12-12 18:48:08 +00:00
|
|
|
module Spectator
|
|
|
|
# Provides customization and describes specifics for how Spectator will run and report tests.
|
|
|
|
class Config
|
2021-05-09 05:37:33 +00:00
|
|
|
# Primary formatter all events will be sent to.
|
|
|
|
getter formatter : Formatting::Formatter
|
2018-12-13 21:16:49 +00:00
|
|
|
|
2021-05-09 04:51:54 +00:00
|
|
|
# Flags indicating how the spec should run.
|
|
|
|
getter run_flags : RunFlags
|
2019-03-24 01:43:41 +00:00
|
|
|
|
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
|
|
|
|
2021-05-13 03:41:34 +00:00
|
|
|
# Filter used to select which examples to run.
|
2021-08-08 18:13:59 +00:00
|
|
|
getter node_filter : NodeFilter
|
2021-05-13 03:41:34 +00:00
|
|
|
|
2021-07-17 21:20:58 +00:00
|
|
|
# List of hooks to run before all examples in the test suite.
|
2021-08-08 03:45:49 +00:00
|
|
|
protected getter before_suite_hooks : Deque(ExampleGroupHook)
|
2021-07-17 21:20:58 +00:00
|
|
|
|
|
|
|
# List of hooks to run before each top-level example group.
|
2021-08-08 03:45:49 +00:00
|
|
|
protected getter before_all_hooks : Deque(ExampleGroupHook)
|
2021-07-17 21:20:58 +00:00
|
|
|
|
|
|
|
# List of hooks to run before every example.
|
2021-08-08 03:45:49 +00:00
|
|
|
protected getter before_each_hooks : Deque(ExampleHook)
|
2021-07-17 21:20:58 +00:00
|
|
|
|
|
|
|
# List of hooks to run after all examples in the test suite.
|
2021-08-08 03:45:49 +00:00
|
|
|
protected getter after_suite_hooks : Deque(ExampleGroupHook)
|
2021-07-17 21:20:58 +00:00
|
|
|
|
|
|
|
# List of hooks to run after each top-level example group.
|
2021-08-08 03:45:49 +00:00
|
|
|
protected getter after_all_hooks : Deque(ExampleGroupHook)
|
2021-07-17 21:20:58 +00:00
|
|
|
|
|
|
|
# List of hooks to run after every example.
|
2021-08-08 03:45:49 +00:00
|
|
|
protected getter after_each_hooks : Deque(ExampleHook)
|
2021-07-17 21:20:58 +00:00
|
|
|
|
|
|
|
# List of hooks to run around every example.
|
2021-08-08 03:45:49 +00:00
|
|
|
protected getter around_each_hooks : Deque(ExampleProcsyHook)
|
2021-07-17 21:20:58 +00:00
|
|
|
|
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*.
|
2021-05-09 05:37:33 +00:00
|
|
|
# Typically, *source* is a `Config::Builder`.
|
2020-10-18 04:12:47 +00:00
|
|
|
def initialize(source)
|
2021-05-09 05:37:33 +00:00
|
|
|
@formatter = source.formatter
|
2021-05-09 04:51:54 +00:00
|
|
|
@run_flags = source.run_flags
|
2020-10-18 04:12:47 +00:00
|
|
|
@random_seed = source.random_seed
|
2021-08-08 18:13:59 +00:00
|
|
|
@node_filter = source.node_filter
|
2021-07-17 21:20:58 +00:00
|
|
|
|
|
|
|
@before_suite_hooks = source.before_suite_hooks
|
|
|
|
@before_all_hooks = source.before_all_hooks
|
|
|
|
@before_each_hooks = source.before_each_hooks
|
|
|
|
@after_suite_hooks = source.after_suite_hooks
|
|
|
|
@after_all_hooks = source.after_all_hooks
|
|
|
|
@after_each_hooks = source.after_each_hooks
|
|
|
|
@around_each_hooks = source.around_each_hooks
|
2018-12-13 21:16:49 +00:00
|
|
|
end
|
2019-03-23 23:00:21 +00:00
|
|
|
|
2021-05-09 05:37:33 +00:00
|
|
|
# Produces the default configuration.
|
|
|
|
def self.default : self
|
|
|
|
Builder.new.build
|
|
|
|
end
|
|
|
|
|
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)
|
2021-05-09 04:51:54 +00:00
|
|
|
return items unless run_flags.randomize?
|
2020-10-18 04:11:04 +00:00
|
|
|
|
|
|
|
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)
|
2021-05-09 04:51:54 +00:00
|
|
|
return items unless run_flags.randomize?
|
2020-10-18 04:11:04 +00:00
|
|
|
|
|
|
|
items.shuffle!(random)
|
|
|
|
end
|
|
|
|
|
2021-05-09 05:37:33 +00:00
|
|
|
# Creates an iterator configured to select the filtered examples.
|
|
|
|
def iterator(group : ExampleGroup)
|
2021-08-08 18:13:59 +00:00
|
|
|
ExampleIterator.new(group).select(@node_filter)
|
2019-03-23 23:00:21 +00:00
|
|
|
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.
|
2021-07-17 19:05:03 +00:00
|
|
|
def random
|
2020-10-18 04:11:04 +00:00
|
|
|
Random.new(random_seed)
|
|
|
|
end
|
2018-12-12 18:48:08 +00:00
|
|
|
end
|
|
|
|
end
|