Skip to content

Commit

Permalink
Feat/#16/background (#47)
Browse files Browse the repository at this point in the history
* Support background for each scenario

* Fix linter

* generate
  • Loading branch information
hedhyw authored Jun 25, 2022
1 parent 2e6d375 commit b392309
Show file tree
Hide file tree
Showing 11 changed files with 634 additions and 68 deletions.
25 changes: 18 additions & 7 deletions internal/assets/std.struct.v1.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import (

{{ define "Background" }}
{{- $Background := . -}}
f.{{ $Background.PluginData.GoName }}({{ $Background.PluginData.GoValue }}, func(t *testing.T, f *bdd.Feature) {
background := func(t *testing.T, f *bdd.Feature) interface{} {
/* TODO: Feel free to modify return value(s). */
{{- range $Background.Steps }}
f.{{ .PluginData.GoName }}({{.PluginData.GoValue}}, func() {

})
{{- end }}
})

return nil
}

{{ end }}

{{ define "Scenario" }}
Expand Down Expand Up @@ -45,6 +49,10 @@ import (
}

f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
{{- if $Scenario.PluginData.GoHasBackground }}
_ = background(t, f)
{{- end -}}

{{- range $Scenario.Steps }}
f.{{ .PluginData.GoName }}({{ .PluginData.GoValue }}, func() {

Expand All @@ -54,7 +62,10 @@ import (
{{- else }}
{{- range $Scenario.Steps }}
f.{{ .PluginData.GoName }}({{.PluginData.GoValue}}, func() {

{{- if $Scenario.PluginData.GoHasBackground }}
_ = background(t, f)
{{- end }}

})
{{- end -}}
{{ end }}
Expand All @@ -66,13 +77,13 @@ import (
f.{{ $Rule.PluginData.GoName }}({{ $Rule.PluginData.GoValue }}, func(_ *testing.T, f *bdd.Feature) {
{{- range $Rule.Children -}}

{{- if .Background -}}
{{- if .Background }}
{{ template "Background" .Background }}
{{- end -}}
{{- end }}

{{- if .Scenario -}}
{{- if .Scenario }}
{{- template "Scenario" .Scenario -}}
{{- end -}}
{{- end }}

{{- end -}}
})
Expand Down
59 changes: 55 additions & 4 deletions internal/docplugin/goplugin/goplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
const maxRecursionDepth = 10

const (
dataFieldGoType = "GoType"
dataFieldGoValue = "GoValue"
dataFieldGoName = "GoName"
dataFieldGoComment = "GoComment"
dataFieldGoType = "GoType"
dataFieldGoValue = "GoValue"
dataFieldGoName = "GoName"
dataFieldGoComment = "GoComment"
dataFieldGoBackground = "GoHasBackground"
)

// GoPlugin injects golang specific information: go types, aliases.
Expand Down Expand Up @@ -149,10 +150,12 @@ func (p GoPlugin) handleStruct(
val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Name)
val.PluginData[dataFieldGoType] = string(goTypeString)
val.PluginData[dataFieldGoComment] = p.prepareFeatureDescription(val.Description)
p.processFeatureBackground(val)
case model.Rule:
val.PluginData[dataFieldGoName] = p.aliaser.NameAlias(val.Keyword)
val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Name)
val.PluginData[dataFieldGoType] = string(goTypeString)
p.processRuleBackground(val)
case model.Scenario:
val.PluginData[dataFieldGoName] = p.aliaser.NameAlias(val.Keyword)
val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Name)
Expand All @@ -175,6 +178,54 @@ func (p GoPlugin) handleStruct(
return nil
}

func (p GoPlugin) processFeatureBackground(f model.Feature) {
var hasBackground bool

for _, ch := range f.Children {
if ch.Background != nil {
hasBackground = true

break
}
}

if !hasBackground {
return
}

for _, ch := range f.Children {
if ch.Scenario != nil {
ch.Scenario.PluginData[dataFieldGoBackground] = true
}

if ch.Rule != nil {
ch.Rule.PluginData[dataFieldGoBackground] = true
}
}
}

func (p GoPlugin) processRuleBackground(f model.Rule) {
var hasBackground bool

for _, ch := range f.Children {
if ch.Background != nil {
hasBackground = true

break
}
}

if !hasBackground {
return
}

for _, ch := range f.Children {
if ch.Scenario != nil {
ch.Scenario.PluginData[dataFieldGoBackground] = true
}
}
}

func (p GoPlugin) prepareFeatureDescription(descr string) string {
lines := strings.Split(descr, "\n")

Expand Down
Loading

0 comments on commit b392309

Please sign in to comment.