Skip to content

Commit

Permalink
GIT-94: enable extended Feature API to provide named setup and teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
harshanarayana committed Feb 21, 2022
1 parent 319e09e commit 353abeb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
21 changes: 15 additions & 6 deletions pkg/features/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,29 @@ func (b *FeatureBuilder) WithStep(name string, level Level, fn Func) *FeatureBui

// Setup adds a new setup step that will be applied prior to feature test.
func (b *FeatureBuilder) Setup(fn Func) *FeatureBuilder {
b.feat.steps = append(b.feat.steps, newStep(fmt.Sprintf("%s-setup", b.feat.name), types.LevelSetup, fn))
return b
return b.WithSetup(fmt.Sprintf("%s-setup", b.feat.name), fn)
}

// WithSetup adds a new setup step with a pre-defined setup name instead of automating
// the setup name generation. This can make tests more readable.
func (b *FeatureBuilder) WithSetup(name string, fn Func) *FeatureBuilder {
return b.WithStep(name, types.LevelSetup, fn)
}

// Teardown adds a new teardown step that will be applied after feature test.
func (b *FeatureBuilder) Teardown(fn Func) *FeatureBuilder {
b.feat.steps = append(b.feat.steps, newStep(fmt.Sprintf("%s-teardown", b.feat.name), types.LevelTeardown, fn))
return b
return b.WithTeardown(fmt.Sprintf("%s-teardown", b.feat.name), fn)
}

// WithTeardown adds a new teardown step with a pre-defined name instead of an
// auto-generated one
func (b *FeatureBuilder) WithTeardown(name string, fn Func) *FeatureBuilder {
return b.WithStep(name, types.LevelTeardown, fn)
}

// Assess adds an assessment step to the feature test.
func (b *FeatureBuilder) Assess(desc string, fn Func) *FeatureBuilder {
b.feat.steps = append(b.feat.steps, newStep(desc, types.LevelAssess, fn))
return b
return b.WithStep(desc, types.LevelAssess, fn)
}

// Feature returns a feature configured by builder.
Expand Down
36 changes: 33 additions & 3 deletions pkg/features/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ func TestFeatureBuilder(t *testing.T) {
}
},
},
{ // nolint
name: "named setups",
setup: func(t *testing.T) types.Feature {
return New("test").WithSetup("setup-test", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
return ctx
}).Feature()
},
eval: func(t *testing.T, f types.Feature) {
ft := f.(*defaultFeature) //nolint
setups := GetStepsByLevel(ft.Steps(), types.LevelSetup)
if setups[0].Name() != "setup-test" {
t.Errorf("unexpected setup name: %s", setups[0].Name())
}
},
},
{
name: "one teardown",
setup: func(t *testing.T) types.Feature {
Expand Down Expand Up @@ -149,6 +164,21 @@ func TestFeatureBuilder(t *testing.T) {
}
},
},
{ // nolint
name: "named teardowns",
setup: func(t *testing.T) types.Feature {
return New("test").WithTeardown("teardown-test", func(ctx context.Context, t *testing.T, config *envconf.Config) context.Context {
return ctx
}).Feature()
},
eval: func(t *testing.T, f types.Feature) {
ft := f.(*defaultFeature) //nolint
setups := GetStepsByLevel(ft.Steps(), types.LevelTeardown)
if setups[0].Name() != "teardown-test" {
t.Errorf("unexpected teardown name: %s", setups[0].Name())
}
},
},
{
name: "single assessment",
setup: func(t *testing.T) types.Feature {
Expand All @@ -174,7 +204,7 @@ func TestFeatureBuilder(t *testing.T) {
return New("test").Assess("some test", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
// test
return ctx
}).Assess("some tets 2", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
}).Assess("some tests 2", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
// test
return ctx
}).Feature()
Expand All @@ -196,10 +226,10 @@ func TestFeatureBuilder(t *testing.T) {
return New("test").Setup(func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
// test
return ctx
}).Assess("some tets 2", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
}).Assess("some tests 2", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
// test
return ctx
}).Assess("some tets 3", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
}).Assess("some tests 3", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
// test
return ctx
}).Teardown(func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context {
Expand Down

0 comments on commit 353abeb

Please sign in to comment.