Path: blob/main/vendor/github.com/sclevine/spec/options.go
2875 views
package spec12import (3"io"4"testing"5)67// An Option controls the behavior of a suite, group, or spec.8// Options are inherited by subgroups and subspecs.9//10// Example:11// If the top-level Run group is specified as Random(), each subgroup will12// inherit the Random() order. This means that each group will be randomized13// individually, unless another ordering is specified on any of the subgroups.14// If the Run group is also passed Global(), then all specs inside Run will run15// in completely random order, regardless of any ordering specified on the16// subgroups.17type Option func(*config)1819// Report specifies a Reporter for a suite.20//21// Valid Option for:22// New, Run, Focus, Pend23func Report(r Reporter) Option {24return func(c *config) {25c.report = r26}27}2829// Seed specifies the random seed used for any randomized specs in a Run block.30// The random seed is always displayed before specs are run.31// If not specified, the current time is used.32//33// Valid Option for:34// New, Run, Focus, Pend35func Seed(s int64) Option {36return func(c *config) {37c.seed = s38}39}4041// Sequential indicates that a group of specs should be run in order.42// This is the default behavior.43//44// Valid Option for:45// New, Run, Focus, Pend, Suite, Suite.Focus, Suite.Pend, G, G.Focus, G.Pend46func Sequential() Option {47return func(c *config) {48c.order = orderSequential49}50}5152// Random indicates that a group of specs should be run in random order.53// Randomization is per group, such that all groupings are maintained.54//55// Valid Option for:56// New, Run, Focus, Pend, Suite, Suite.Focus, Suite.Pend, G, G.Focus, G.Pend57func Random() Option {58return func(c *config) {59c.order = orderRandom60}61}6263// Reverse indicates that a group of specs should be run in reverse order.64//65// Valid Option for:66// New, Run, Focus, Pend, Suite, Suite.Focus, Suite.Pend, G, G.Focus, G.Pend67func Reverse() Option {68return func(c *config) {69c.order = orderReverse70}71}7273// Parallel indicates that a spec or group of specs should be run in parallel.74// This Option is equivalent to t.Parallel().75//76// Valid Option for:77// New, Run, Focus, Pend, Suite, Suite.Focus, Suite.Pend, G, G.Focus, G.Pend, S78func Parallel() Option {79return func(c *config) {80c.order = orderParallel81}82}8384// Local indicates that the test order applies to each subgroup individually.85// For example, a group with Random() and Local() will run all subgroups and86// specs in random order, and each subgroup will be randomized, but specs in87// different subgroups will not be interleaved.88// This is the default behavior.89//90// Valid Option for:91// New, Run, Focus, Pend, Suite, Suite.Focus, Suite.Pend, G, G.Focus, G.Pend92func Local() Option {93return func(c *config) {94c.scope = scopeLocal95}96}9798// Global indicates that test order applies globally to all descendant specs.99// For example, a group with Random() and Global() will run all descendant100// specs in random order, regardless of subgroup. Specs in different subgroups101// may be interleaved.102//103// Valid Option for:104// New, Run, Focus, Pend, Suite, Suite.Focus, Suite.Pend, G, G.Focus, G.Pend105func Global() Option {106return func(c *config) {107c.scope = scopeGlobal108}109}110111// Flat indicates that a parent subtest should not be created for the group.112// This is the default behavior.113//114// Valid Option for:115// New, Run, Focus, Pend, Suite, Suite.Focus, Suite.Pend, G, G.Focus, G.Pend116func Flat() Option {117return func(c *config) {118c.nest = nestOff119}120}121122// Nested indicates that a parent subtest should be created for the group.123// This allows for more control over parallelism.124//125// Valid Option for:126// New, Run, Focus, Pend, Suite, Suite.Focus, Suite.Pend, G, G.Focus, G.Pend127func Nested() Option {128return func(c *config) {129c.nest = nestOn130}131}132133type order int134135const (136orderInherit order = iota137orderSequential138orderParallel139orderRandom140orderReverse141)142143func (o order) or(last order) order {144return order(defaultZero(int(o), int(last)))145}146147type scope int148149const (150scopeInherit scope = iota151scopeLocal152scopeGlobal153)154155func (s scope) or(last scope) scope {156return scope(defaultZero(int(s), int(last)))157}158159type nest int160161const (162nestInherit nest = iota163nestOff164nestOn165)166167func (n nest) or(last nest) nest {168return nest(defaultZero(int(n), int(last)))169}170171func defaultZero(next, last int) int {172if next == 0 {173return last174}175return next176}177178func defaultZero64(next, last int64) int64 {179if next == 0 {180return last181}182return next183}184185type config struct {186seed int64187order order188scope scope189nest nest190pend bool191focus bool192before bool193after bool194t *testing.T195out func(io.Writer)196report Reporter197}198199type options []Option200201func (o options) apply() *config {202cfg := &config{}203for _, opt := range o {204opt(cfg)205}206return cfg207}208209210