diff --git a/internal/assets/std.struct.v1.go.tmpl b/internal/assets/std.struct.v1.go.tmpl index 55f5f38..8572f44 100644 --- a/internal/assets/std.struct.v1.go.tmpl +++ b/internal/assets/std.struct.v1.go.tmpl @@ -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" }} @@ -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() { @@ -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 }} @@ -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 -}} }) diff --git a/internal/docplugin/goplugin/goplugin.go b/internal/docplugin/goplugin/goplugin.go index 5755757..56f4540 100644 --- a/internal/docplugin/goplugin/goplugin.go +++ b/internal/docplugin/goplugin/goplugin.go @@ -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. @@ -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) @@ -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") diff --git a/internal/docplugin/goplugin/goplugin_test.go b/internal/docplugin/goplugin/goplugin_test.go index ed86107..14c34aa 100644 --- a/internal/docplugin/goplugin/goplugin_test.go +++ b/internal/docplugin/goplugin/goplugin_test.go @@ -10,138 +10,129 @@ import ( "github.com/stretchr/testify/assert" ) -// nolint: gocognit,cyclop,maintidx // Unit test. func TestGoPluginProcess(t *testing.T) { t.Parallel() ctx := context.Background() - t.Run("Background", func(t *testing.T) { + t.Run("TableCell", func(t *testing.T) { t.Parallel() p := goplugin.New() doc := getExampleDocument() if assert.NoError(t, p.Process(ctx, doc)) { - pd := doc.Feature.Children[0].Background.PluginData - assert.Equal(t, "\"Name\"", pd["GoValue"]) - assert.Equal(t, "Keyword", pd["GoName"]) - assert.Equal(t, "string", pd["GoType"]) + pd := doc.Feature.Children[0].Scenario.Examples[0].TableHeader.Cells[0].PluginData + assert.Equal(t, "Title", pd["GoName"]) + assert.Equal(t, "\"\"", pd["GoValue"]) + + pd = doc.Feature.Children[0].Scenario.Examples[0].TableHeader.Cells[0].PluginData + assert.Equal(t, "int", pd["GoType"]) } }) - t.Run("Examples", func(t *testing.T) { + t.Run("Feature", func(t *testing.T) { t.Parallel() p := goplugin.New() doc := getExampleDocument() if assert.NoError(t, p.Process(ctx, doc)) { - pd := doc.Feature.Children[0].Scenario.Examples[0].PluginData - assert.Equal(t, "\"Keyword\"", pd["GoValue"]) + pd := doc.Feature.PluginData + assert.Equal(t, "\"Name\"", pd["GoValue"]) assert.Equal(t, "Name", pd["GoName"]) } }) - t.Run("Examples_EmptyTableBody_NoError", func(t *testing.T) { - t.Parallel() - - p := goplugin.New() - - doc := getExampleDocument() - doc.Feature.Children[0].Scenario.Examples[0].TableBody = nil - assert.NoError(t, p.Process(ctx, doc)) - }) - - t.Run("Examples_TableBody_TableHeader_mismatch", func(t *testing.T) { + t.Run("Rule", func(t *testing.T) { t.Parallel() p := goplugin.New() doc := getExampleDocument() - doc.Feature.Children[0].Scenario.Examples[0].TableHeader.Cells = nil - assert.Error(t, p.Process(ctx, doc)) + if assert.NoError(t, p.Process(ctx, doc)) { + pd := doc.Feature.Children[0].Rule.PluginData + assert.Equal(t, "\"Name\"", pd["GoValue"]) + assert.Equal(t, "Keyword", pd["GoName"]) + } }) - t.Run("Examples_underscore", func(t *testing.T) { + t.Run("Scenario", func(t *testing.T) { t.Parallel() p := goplugin.New() - // It tests https://github.com/hedhyw/gherkingen/v2/issues/26. - doc := getExampleDocument() if assert.NoError(t, p.Process(ctx, doc)) { - pd := doc.Feature.Children[0].Scenario.Examples[1].TableBody[0].PluginData - assert.Equal(t, "\"hello_world\"", pd["GoValue"]) + pd := doc.Feature.Children[0].Scenario.PluginData + assert.Equal(t, "\"Name\"", pd["GoValue"]) + assert.Equal(t, "Keyword", pd["GoName"]) } }) - t.Run("TableCell", func(t *testing.T) { + t.Run("Step", func(t *testing.T) { t.Parallel() p := goplugin.New() doc := getExampleDocument() if assert.NoError(t, p.Process(ctx, doc)) { - pd := doc.Feature.Children[0].Scenario.Examples[0].TableHeader.Cells[0].PluginData - assert.Equal(t, "Title", pd["GoName"]) - assert.Equal(t, "\"<Title>\"", pd["GoValue"]) - - pd = doc.Feature.Children[0].Scenario.Examples[0].TableHeader.Cells[0].PluginData - assert.Equal(t, "int", pd["GoType"]) + pd := doc.Feature.Children[0].Scenario.Steps[0].PluginData + assert.Equal(t, "Keyword", pd["GoName"]) + assert.Equal(t, "\"Text\"", pd["GoValue"]) } }) +} - t.Run("Feature", func(t *testing.T) { +func TestExample(t *testing.T) { + t.Parallel() + + ctx := context.Background() + + t.Run("Examples", func(t *testing.T) { t.Parallel() p := goplugin.New() doc := getExampleDocument() if assert.NoError(t, p.Process(ctx, doc)) { - pd := doc.Feature.PluginData - assert.Equal(t, "\"Name\"", pd["GoValue"]) + pd := doc.Feature.Children[0].Scenario.Examples[0].PluginData + assert.Equal(t, "\"Keyword\"", pd["GoValue"]) assert.Equal(t, "Name", pd["GoName"]) } }) - t.Run("Rule", func(t *testing.T) { + t.Run("Examples_EmptyTableBody_NoError", func(t *testing.T) { t.Parallel() p := goplugin.New() doc := getExampleDocument() - if assert.NoError(t, p.Process(ctx, doc)) { - pd := doc.Feature.Children[0].Rule.PluginData - assert.Equal(t, "\"Name\"", pd["GoValue"]) - assert.Equal(t, "Keyword", pd["GoName"]) - } + doc.Feature.Children[0].Scenario.Examples[0].TableBody = nil + assert.NoError(t, p.Process(ctx, doc)) }) - t.Run("Scenario", func(t *testing.T) { + t.Run("Examples_TableBody_TableHeader_mismatch", func(t *testing.T) { t.Parallel() p := goplugin.New() doc := getExampleDocument() - if assert.NoError(t, p.Process(ctx, doc)) { - pd := doc.Feature.Children[0].Scenario.PluginData - assert.Equal(t, "\"Name\"", pd["GoValue"]) - assert.Equal(t, "Keyword", pd["GoName"]) - } + doc.Feature.Children[0].Scenario.Examples[0].TableHeader.Cells = nil + assert.Error(t, p.Process(ctx, doc)) }) - t.Run("Step", func(t *testing.T) { + t.Run("Examples_underscore", func(t *testing.T) { t.Parallel() p := goplugin.New() + // It tests https://github.com/hedhyw/gherkingen/v2/issues/26. + doc := getExampleDocument() if assert.NoError(t, p.Process(ctx, doc)) { - pd := doc.Feature.Children[0].Scenario.Steps[0].PluginData - assert.Equal(t, "Keyword", pd["GoName"]) - assert.Equal(t, "\"Text\"", pd["GoValue"]) + pd := doc.Feature.Children[0].Scenario.Examples[1].TableBody[0].PluginData + assert.Equal(t, "\"hello_world\"", pd["GoValue"]) } }) @@ -251,6 +242,12 @@ func TestGoPluginProcess(t *testing.T) { assert.Error(t, p.Process(ctx, doc)) }) +} + +func TestDescription_singleLine(t *testing.T) { + t.Parallel() + + ctx := context.Background() t.Run("Description_one_line", func(t *testing.T) { t.Parallel() @@ -277,6 +274,12 @@ func TestGoPluginProcess(t *testing.T) { assert.Equal(t, "Hello world", doc.Feature.PluginData["GoComment"]) } }) +} + +func TestDescription_multiLine(t *testing.T) { + t.Parallel() + + ctx := context.Background() t.Run("Description_multline", func(t *testing.T) { t.Parallel() @@ -334,6 +337,105 @@ func TestGoPluginProcess(t *testing.T) { }) } +func TestBackground(t *testing.T) { + t.Parallel() + + ctx := context.Background() + + t.Run("Background", func(t *testing.T) { + t.Parallel() + + p := goplugin.New() + + doc := getExampleDocument() + if assert.NoError(t, p.Process(ctx, doc)) { + pd := doc.Feature.Children[0].Background.PluginData + assert.Equal(t, "\"Name\"", pd["GoValue"]) + assert.Equal(t, "Keyword", pd["GoName"]) + assert.Equal(t, "string", pd["GoType"]) + } + }) + + t.Run("Background_Scenario", func(t *testing.T) { + t.Parallel() + + p := goplugin.New() + doc := getExampleDocument() + + doc.Feature.Children = []*model.FeatureChild{{ + Background: &model.Background{ + PluginData: make(map[string]any), + }, + Scenario: &model.Scenario{ + PluginData: make(map[string]any), + }, + }} + + if assert.NoError(t, p.Process(ctx, doc)) && + assert.NotNil(t, doc.Feature.Description) { + assert.Equal(t, true, doc.Feature.Children[0].Scenario.PluginData["GoHasBackground"]) + } + }) + + t.Run("No_Background_Scenario", func(t *testing.T) { + t.Parallel() + + p := goplugin.New() + doc := getExampleDocument() + + doc.Feature.Children = []*model.FeatureChild{{ + Background: nil, + Scenario: &model.Scenario{ + PluginData: make(map[string]any), + }, + }} + + if assert.NoError(t, p.Process(ctx, doc)) && + assert.NotNil(t, doc.Feature.Description) { + assert.NotEqual(t, true, doc.Feature.Children[0].Scenario.PluginData["GoHasBackground"]) + } + }) + + t.Run("Background_Rule", func(t *testing.T) { + t.Parallel() + + p := goplugin.New() + doc := getExampleDocument() + + doc.Feature.Children[0].Rule.Children = []*model.RuleChild{{ + Background: &model.Background{ + PluginData: make(map[string]any), + }, + Scenario: &model.Scenario{ + PluginData: make(map[string]any), + }, + }} + + if assert.NoError(t, p.Process(ctx, doc)) { + scenario := doc.Feature.Children[0].Rule.Children[0].Scenario + assert.Equal(t, true, scenario.PluginData["GoHasBackground"]) + } + }) + + t.Run("No_Background_Rule", func(t *testing.T) { + t.Parallel() + + p := goplugin.New() + doc := getExampleDocument() + + doc.Feature.Children[0].Rule.Children = []*model.RuleChild{{ + Scenario: &model.Scenario{ + PluginData: make(map[string]any), + }, + }} + + if assert.NoError(t, p.Process(ctx, doc)) { + scenario := doc.Feature.Children[0].Rule.Children[0].Scenario + assert.NotEqual(t, true, scenario.PluginData["GoHasBackground"]) + } + }) +} + func TestGoPluginName(t *testing.T) { t.Parallel() diff --git a/internal/generator/examples/background.feature.json b/internal/generator/examples/background.feature.json index 58fa4b6..b1805b4 100644 --- a/internal/generator/examples/background.feature.json +++ b/internal/generator/examples/background.feature.json @@ -153,6 +153,7 @@ "Examples": [], "ID": "f3ede2d6-becc-4ea3-ae5e-88526a9f4a57", "PluginData": { + "GoHasBackground": true, "GoName": "Scenario", "GoType": "string", "GoValue": "\"Dr. Bill posts to his own blog\"" @@ -221,6 +222,7 @@ "Examples": [], "ID": "cdd01d75-045c-4f00-8f8a-796bce6c512c", "PluginData": { + "GoHasBackground": true, "GoName": "Scenario", "GoType": "string", "GoValue": "\"Dr. Bill tries to post to somebody else's blog, and fails\"" @@ -289,6 +291,7 @@ "Examples": [], "ID": "280ae943-9eb0-46ae-8a08-23ae02d67d86", "PluginData": { + "GoHasBackground": true, "GoName": "Scenario", "GoType": "string", "GoValue": "\"Greg posts to a client's blog\"" diff --git a/internal/generator/examples/background.feature_test.go b/internal/generator/examples/background.feature_test.go index a580b85..fe6723c 100644 --- a/internal/generator/examples/background.feature_test.go +++ b/internal/generator/examples/background.feature_test.go @@ -14,7 +14,8 @@ func TestMultipleSiteSupport(t *testing.T) { who can post to all blogs. */ - f.Background("", func(t *testing.T, f *bdd.Feature) { + background := func(t *testing.T, f *bdd.Feature) interface{} { + /* TODO: Feel free to modify return value(s). */ f.Given("a global administrator named \"Greg\"", func() { }) @@ -27,40 +28,51 @@ func TestMultipleSiteSupport(t *testing.T) { f.And("a blog named \"Expensive Therapy\" owned by \"Dr. Bill\"", func() { }) - }) + + return nil + } f.Scenario("Dr. Bill posts to his own blog", func(t *testing.T, f *bdd.Feature) { f.Given("I am logged in as Dr. Bill", func() { + _ = background(t, f) }) f.When("I try to post to \"Expensive Therapy\"", func() { + _ = background(t, f) }) f.Then("I should see \"Your article was published.\"", func() { + _ = background(t, f) }) }) f.Scenario("Dr. Bill tries to post to somebody else's blog, and fails", func(t *testing.T, f *bdd.Feature) { f.Given("I am logged in as Dr. Bill", func() { + _ = background(t, f) }) f.When("I try to post to \"Greg's anti-tax rants\"", func() { + _ = background(t, f) }) f.Then("I should see \"Hey! That's not your blog!\"", func() { + _ = background(t, f) }) }) f.Scenario("Greg posts to a client's blog", func(t *testing.T, f *bdd.Feature) { f.Given("I am logged in as Greg", func() { + _ = background(t, f) }) f.When("I try to post to \"Expensive Therapy\"", func() { + _ = background(t, f) }) f.Then("I should see \"Your article was published.\"", func() { + _ = background(t, f) }) }) diff --git a/internal/generator/examples/complex.feature b/internal/generator/examples/complex.feature new file mode 100644 index 0000000..9ec2926 --- /dev/null +++ b/internal/generator/examples/complex.feature @@ -0,0 +1,19 @@ +Feature: Nested background + Background: + Given a global administrator named "Greg" + And a blog named "Greg's anti-tax rants" + And a customer named "Dr. Bill" + And a blog named "Expensive Therapy" owned by "Dr. Bill" + + Scenario: Dr. Bill posts to his own blog + Given I am logged in as Dr. Bill + When I try to post to "Expensive Therapy" + Then I should see "Your article was published." + + Rule: There can be only One + Background: + Given I have overdue tasks + + Example: Only One -- One alive + Given there is only 1 ninja alive + Then he (or she) will live forever ;-) diff --git a/internal/generator/examples/complex.feature.json b/internal/generator/examples/complex.feature.json new file mode 100644 index 0000000..c9209ea --- /dev/null +++ b/internal/generator/examples/complex.feature.json @@ -0,0 +1,288 @@ +{ + "Feature": { + "Location": { + "Line": 1, + "Column": 1, + "PluginData": {} + }, + "Tags": [], + "Language": "en", + "Keyword": "Feature", + "Name": "Nested background", + "Description": "", + "Children": [ + { + "Background": { + "Location": { + "Line": 2, + "Column": 3, + "PluginData": {} + }, + "Keyword": "Background", + "Name": "", + "Description": "", + "Steps": [ + { + "Location": { + "Line": 3, + "Column": 5, + "PluginData": {} + }, + "Keyword": "Given ", + "Text": "a global administrator named \"Greg\"", + "ID": "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8", + "PluginData": { + "GoName": "Given", + "GoType": "string", + "GoValue": "\"a global administrator named \\\"Greg\\\"\"" + } + }, + { + "Location": { + "Line": 4, + "Column": 5, + "PluginData": {} + }, + "Keyword": "And ", + "Text": "a blog named \"Greg's anti-tax rants\"", + "ID": "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75", + "PluginData": { + "GoName": "And", + "GoType": "string", + "GoValue": "\"a blog named \\\"Greg's anti-tax rants\\\"\"" + } + }, + { + "Location": { + "Line": 5, + "Column": 5, + "PluginData": {} + }, + "Keyword": "And ", + "Text": "a customer named \"Dr. Bill\"", + "ID": "fb180daf-48a7-4ee0-b10d-394651850fd4", + "PluginData": { + "GoName": "And", + "GoType": "string", + "GoValue": "\"a customer named \\\"Dr. Bill\\\"\"" + } + }, + { + "Location": { + "Line": 6, + "Column": 5, + "PluginData": {} + }, + "Keyword": "And ", + "Text": "a blog named \"Expensive Therapy\" owned by \"Dr. Bill\"", + "ID": "a178892e-e285-4ce1-9114-55780875d64e", + "PluginData": { + "GoName": "And", + "GoType": "string", + "GoValue": "\"a blog named \\\"Expensive Therapy\\\" owned by \\\"Dr. Bill\\\"\"" + } + } + ], + "ID": "e2d3d0d0-de6b-48f9-b44c-e85ff044c6b1", + "PluginData": { + "GoName": "Background", + "GoType": "string", + "GoValue": "\"\"" + } + }, + "PluginData": {} + }, + { + "Scenario": { + "Location": { + "Line": 8, + "Column": 3, + "PluginData": {} + }, + "Tags": [], + "Keyword": "Scenario", + "Name": "Dr. Bill posts to his own blog", + "Description": "", + "Steps": [ + { + "Location": { + "Line": 9, + "Column": 5, + "PluginData": {} + }, + "Keyword": "Given ", + "Text": "I am logged in as Dr. Bill", + "ID": "f83b8e88-3bbf-457a-ab99-c5b252c7429c", + "PluginData": { + "GoName": "Given", + "GoType": "string", + "GoValue": "\"I am logged in as Dr. Bill\"" + } + }, + { + "Location": { + "Line": 10, + "Column": 5, + "PluginData": {} + }, + "Keyword": "When ", + "Text": "I try to post to \"Expensive Therapy\"", + "ID": "32f3a8ae-b79e-4856-b659-c18f0dcecc77", + "PluginData": { + "GoName": "When", + "GoType": "string", + "GoValue": "\"I try to post to \\\"Expensive Therapy\\\"\"" + } + }, + { + "Location": { + "Line": 11, + "Column": 5, + "PluginData": {} + }, + "Keyword": "Then ", + "Text": "I should see \"Your article was published.\"", + "ID": "c75e7a81-bfde-475f-a7cf-e242cf3cc354", + "PluginData": { + "GoName": "Then", + "GoType": "string", + "GoValue": "\"I should see \\\"Your article was published.\\\"\"" + } + } + ], + "Examples": [], + "ID": "f3ede2d6-becc-4ea3-ae5e-88526a9f4a57", + "PluginData": { + "GoHasBackground": true, + "GoName": "Scenario", + "GoType": "string", + "GoValue": "\"Dr. Bill posts to his own blog\"" + } + }, + "PluginData": {} + }, + { + "Rule": { + "Location": { + "Line": 13, + "Column": 3, + "PluginData": {} + }, + "Tags": [], + "Keyword": "Rule", + "Name": "There can be only One", + "Description": "", + "Children": [ + { + "Background": { + "Location": { + "Line": 14, + "Column": 5, + "PluginData": {} + }, + "Keyword": "Background", + "Name": "", + "Description": "", + "Steps": [ + { + "Location": { + "Line": 15, + "Column": 7, + "PluginData": {} + }, + "Keyword": "Given ", + "Text": "I have overdue tasks", + "ID": "8bcb9ef2-d4a6-4314-b68d-6d299761ea9e", + "PluginData": { + "GoName": "Given", + "GoType": "string", + "GoValue": "\"I have overdue tasks\"" + } + } + ], + "ID": "4f5aa6ae-c3fc-48c6-aae0-81ac8120c720", + "PluginData": { + "GoName": "Background", + "GoType": "string", + "GoValue": "\"\"" + } + }, + "PluginData": {} + }, + { + "Scenario": { + "Location": { + "Line": 17, + "Column": 5, + "PluginData": {} + }, + "Tags": [], + "Keyword": "Example", + "Name": "Only One -- One alive", + "Description": "", + "Steps": [ + { + "Location": { + "Line": 18, + "Column": 7, + "PluginData": {} + }, + "Keyword": "Given ", + "Text": "there is only 1 ninja alive", + "ID": "efcd6cea-84b6-425e-a07b-e063716f96dd", + "PluginData": { + "GoName": "Given", + "GoType": "string", + "GoValue": "\"there is only 1 ninja alive\"" + } + }, + { + "Location": { + "Line": 19, + "Column": 7, + "PluginData": {} + }, + "Keyword": "Then ", + "Text": "he (or she) will live forever ;-)", + "ID": "cdd01d75-045c-4f00-8f8a-796bce6c512c", + "PluginData": { + "GoName": "Then", + "GoType": "string", + "GoValue": "\"he (or she) will live forever ;-)\"" + } + } + ], + "Examples": [], + "ID": "3801aaca-eedf-4d5b-9066-64e8c0e4a771", + "PluginData": { + "GoHasBackground": true, + "GoName": "Example", + "GoType": "string", + "GoValue": "\"Only One -- One alive\"" + } + }, + "PluginData": {} + } + ], + "ID": "ece0b8b7-c196-4d91-8125-1b7c9c9ca520", + "PluginData": { + "GoHasBackground": true, + "GoName": "Rule", + "GoType": "string", + "GoValue": "\"There can be only One\"" + } + }, + "PluginData": {} + } + ], + "PluginData": { + "GoComment": "", + "GoName": "NestedBackground", + "GoType": "string", + "GoValue": "\"Nested background\"" + } + }, + "Comments": [], + "PluginData": {}, + "PackageName": "examples_test" +} diff --git a/internal/generator/examples/complex.feature_test.go b/internal/generator/examples/complex.feature_test.go new file mode 100644 index 0000000..5060d7b --- /dev/null +++ b/internal/generator/examples/complex.feature_test.go @@ -0,0 +1,66 @@ +package examples_test + +import ( + "testing" + + "github.com/hedhyw/gherkingen/v2/pkg/bdd" +) + +func TestNestedBackground(t *testing.T) { + f := bdd.NewFeature(t, "Nested background") + + background := func(t *testing.T, f *bdd.Feature) interface{} { + /* TODO: Feel free to modify return value(s). */ + f.Given("a global administrator named \"Greg\"", func() { + + }) + f.And("a blog named \"Greg's anti-tax rants\"", func() { + + }) + f.And("a customer named \"Dr. Bill\"", func() { + + }) + f.And("a blog named \"Expensive Therapy\" owned by \"Dr. Bill\"", func() { + + }) + + return nil + } + + f.Scenario("Dr. Bill posts to his own blog", func(t *testing.T, f *bdd.Feature) { + f.Given("I am logged in as Dr. Bill", func() { + _ = background(t, f) + + }) + f.When("I try to post to \"Expensive Therapy\"", func() { + _ = background(t, f) + + }) + f.Then("I should see \"Your article was published.\"", func() { + _ = background(t, f) + + }) + }) + + f.Rule("There can be only One", func(_ *testing.T, f *bdd.Feature) { + background := func(t *testing.T, f *bdd.Feature) interface{} { + /* TODO: Feel free to modify return value(s). */ + f.Given("I have overdue tasks", func() { + + }) + + return nil + } + + f.Example("Only One -- One alive", func(t *testing.T, f *bdd.Feature) { + f.Given("there is only 1 ninja alive", func() { + _ = background(t, f) + + }) + f.Then("he (or she) will live forever ;-)", func() { + _ = background(t, f) + + }) + }) + }) +} diff --git a/internal/generator/examples/rules.feature.json b/internal/generator/examples/rules.feature.json index 97648a3..a0188ea 100644 --- a/internal/generator/examples/rules.feature.json +++ b/internal/generator/examples/rules.feature.json @@ -150,6 +150,7 @@ "Examples": [], "ID": "c75e7a81-bfde-475f-a7cf-e242cf3cc354", "PluginData": { + "GoHasBackground": true, "GoName": "Example", "GoType": "string", "GoValue": "\"Only One -- More than one alive\"" @@ -203,6 +204,7 @@ "Examples": [], "ID": "4f5aa6ae-c3fc-48c6-aae0-81ac8120c720", "PluginData": { + "GoHasBackground": true, "GoName": "Example", "GoType": "string", "GoValue": "\"Only One -- One alive\"" diff --git a/internal/generator/examples/rules.feature_test.go b/internal/generator/examples/rules.feature_test.go index d7f2e53..2b78285 100644 --- a/internal/generator/examples/rules.feature_test.go +++ b/internal/generator/examples/rules.feature_test.go @@ -10,33 +10,44 @@ func TestHighlander(t *testing.T) { f := bdd.NewFeature(t, "Highlander") f.Rule("There can be only One", func(_ *testing.T, f *bdd.Feature) { - f.Background("", func(t *testing.T, f *bdd.Feature) { + background := func(t *testing.T, f *bdd.Feature) interface{} { + /* TODO: Feel free to modify return value(s). */ f.Given("I have overdue tasks", func() { }) - }) + + return nil + } + f.Example("Only One -- More than one alive", func(t *testing.T, f *bdd.Feature) { f.Given("there are 3 ninjas", func() { + _ = background(t, f) }) f.And("there are more than one ninja alive", func() { + _ = background(t, f) }) f.When("2 ninjas meet, they will fight", func() { + _ = background(t, f) }) f.Then("one ninja dies (but not me)", func() { + _ = background(t, f) }) f.And("there is one ninja less alive", func() { + _ = background(t, f) }) }) f.Example("Only One -- One alive", func(t *testing.T, f *bdd.Feature) { f.Given("there is only 1 ninja alive", func() { + _ = background(t, f) }) f.Then("he (or she) will live forever ;-)", func() { + _ = background(t, f) }) }) diff --git a/pkg/bdd/bdd.go b/pkg/bdd/bdd.go index cb85278..368169b 100644 --- a/pkg/bdd/bdd.go +++ b/pkg/bdd/bdd.go @@ -108,6 +108,7 @@ func (f *Feature) subBlock(name string, fn func(t *testing.T, f *Feature)) { // Background defines a background block. // // Notice: Background is not running for each step. +// Deprecated: the current template uses a new syntax. func (f *Feature) Background(name string, fn func(t *testing.T, f *Feature)) { f.T.Helper()