mirror of
https://gitea.invidious.io/iv-org/shard-spectator.git
synced 2024-08-15 00:53:35 +00:00
Clean up example randomization
This commit is contained in:
parent
b2bf980685
commit
225e1a52ba
3 changed files with 35 additions and 20 deletions
|
@ -20,7 +20,7 @@ module Spectator
|
||||||
getter? randomize : Bool
|
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.
|
# Indicates whether timing information should be displayed.
|
||||||
getter? profile : Bool
|
getter? profile : Bool
|
||||||
|
@ -29,22 +29,50 @@ module Spectator
|
||||||
getter example_filter : ExampleFilter
|
getter example_filter : ExampleFilter
|
||||||
|
|
||||||
# Creates a new configuration.
|
# Creates a new configuration.
|
||||||
|
# Properties are pulled from *source*.
|
||||||
|
# Typically, *source* is a `ConfigBuilder`.
|
||||||
def initialize(builder)
|
def initialize(builder)
|
||||||
@formatters = builder.formatters
|
@formatters = builder.formatters
|
||||||
@fail_fast = builder.fail_fast?
|
@fail_fast = builder.fail_fast?
|
||||||
@fail_blank = builder.fail_blank?
|
@fail_blank = builder.fail_blank?
|
||||||
@dry_run = builder.dry_run?
|
@dry_run = builder.dry_run?
|
||||||
@randomize = builder.randomize?
|
@randomize = builder.randomize?
|
||||||
@random_seed = builder.seed?
|
@random_seed = builder.seed
|
||||||
@profile = builder.profile?
|
@profile = builder.profile?
|
||||||
@example_filter = builder.example_filter
|
@example_filter = builder.example_filter
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
# Yields each formatter that should be reported to.
|
# Yields each formatter that should be reported to.
|
||||||
def each_formatter
|
def each_formatter
|
||||||
@formatters.each do |formatter|
|
@formatters.each do |formatter|
|
||||||
yield formatter
|
yield formatter
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# 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
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,6 +13,9 @@ module Spectator
|
||||||
new.build
|
new.build
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Seed used for random number generation.
|
||||||
|
property seed : UInt64 = Random.rand(UInt64)
|
||||||
|
|
||||||
@primary_formatter : Formatting::Formatter?
|
@primary_formatter : Formatting::Formatter?
|
||||||
@additional_formatters = [] of Formatting::Formatter
|
@additional_formatters = [] of Formatting::Formatter
|
||||||
@fail_fast = false
|
@fail_fast = false
|
||||||
|
@ -97,14 +100,6 @@ module Spectator
|
||||||
@dry_run
|
@dry_run
|
||||||
end
|
end
|
||||||
|
|
||||||
# Seed used for random number generation.
|
|
||||||
getter! seed : UInt64?
|
|
||||||
|
|
||||||
# Sets the seed for the random number generator.
|
|
||||||
def seed=(seed)
|
|
||||||
@seed = seed
|
|
||||||
end
|
|
||||||
|
|
||||||
# Randomizes test execution order.
|
# Randomizes test execution order.
|
||||||
def randomize
|
def randomize
|
||||||
self.randomize = true
|
self.randomize = true
|
||||||
|
|
|
@ -16,16 +16,8 @@ module Spectator
|
||||||
# Generates a list of examples to run.
|
# Generates a list of examples to run.
|
||||||
# The order of the examples are also sorted based on the configuration.
|
# The order of the examples are also sorted based on the configuration.
|
||||||
private def examples
|
private def examples
|
||||||
ExampleIterator.new(@root).to_a.tap do |examples|
|
examples = ExampleIterator.new(@root).to_a
|
||||||
if @config.randomize?
|
@config.shuffle!(examples)
|
||||||
random = if (seed = @config.random_seed)
|
|
||||||
Random.new(seed)
|
|
||||||
else
|
|
||||||
Random.new
|
|
||||||
end
|
|
||||||
examples.shuffle!(random)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private struct Runner
|
private struct Runner
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue