From 2e4d7d85eec75dd0acbb6eb2964bf37ede09859e Mon Sep 17 00:00:00 2001 From: Brian Hnat Date: Mon, 30 May 2022 11:07:04 -0400 Subject: [PATCH] Pretty Print when using rules (#440) --- internal/models/feature.go | 13 ++++++ internal/models/feature_test.go | 70 +++++++++++++++++++++++++++------ internal/testutils/utils.go | 50 +++++++++++++++++++++++ 3 files changed, 122 insertions(+), 11 deletions(-) diff --git a/internal/models/feature.go b/internal/models/feature.go index d9cbd3c3..4edd65da 100644 --- a/internal/models/feature.go +++ b/internal/models/feature.go @@ -88,6 +88,19 @@ func (f Feature) FindExample(exampleAstID string) (*messages.Examples, *messages } } } + if ru := child.Rule; ru != nil { + for _, rc := range ru.Children { + if sc := rc.Scenario; sc != nil { + for _, example := range sc.Examples { + for _, row := range example.TableBody { + if row.Id == exampleAstID { + return example, row + } + } + } + } + } + } } return nil, nil diff --git a/internal/models/feature_test.go b/internal/models/feature_test.go index 89fac1e1..100ad2b1 100644 --- a/internal/models/feature_test.go +++ b/internal/models/feature_test.go @@ -3,6 +3,7 @@ package models_test import ( "testing" + "github.com/cucumber/godog/internal/models" "github.com/cucumber/godog/internal/testutils" "github.com/stretchr/testify/assert" ) @@ -32,29 +33,76 @@ func Test_Find(t *testing.T) { assert.NotNilf(t, step, "expected step to not be nil") } }) + + t.Run("rule", func(t *testing.T) { + sc := ft.FindRule(ft.Pickles[0].AstNodeIds[0]) + assert.Nilf(t, sc, "expected rule to be nil") + }) } -func Test_NotFind(t *testing.T) { - ft := testutils.BuildTestFeature(t) +func Test_FindInRule(t *testing.T) { + + ft := testutils.BuildTestFeatureWithRules(t) + + t.Run("rule", func(t *testing.T) { + sc := ft.FindRule(ft.Pickles[0].AstNodeIds[0]) + assert.NotNilf(t, sc, "expected rule to not be nil") + }) t.Run("scenario", func(t *testing.T) { - sc := ft.FindScenario("-") - assert.Nilf(t, sc, "expected scenario to be nil") + sc := ft.FindScenario(ft.Pickles[0].AstNodeIds[0]) + assert.NotNilf(t, sc, "expected scenario to not be nil") }) t.Run("background", func(t *testing.T) { - bg := ft.FindBackground("-") - assert.Nilf(t, bg, "expected background to be nil") + bg := ft.FindBackground(ft.Pickles[0].AstNodeIds[0]) + assert.NotNilf(t, bg, "expected background to not be nil") }) t.Run("example", func(t *testing.T) { - example, row := ft.FindExample("-") - assert.Nilf(t, example, "expected example to be nil") - assert.Nilf(t, row, "expected table row to be nil") + example, row := ft.FindExample(ft.Pickles[1].AstNodeIds[1]) + assert.NotNilf(t, example, "expected example to not be nil") + assert.NotNilf(t, row, "expected table row to not be nil") }) t.Run("step", func(t *testing.T) { - step := ft.FindStep("-") - assert.Nilf(t, step, "expected step to be nil") + for _, ps := range ft.Pickles[0].Steps { + step := ft.FindStep(ps.AstNodeIds[0]) + assert.NotNilf(t, step, "expected step to not be nil") + } }) } + +func Test_NotFind(t *testing.T) { + testCases := []struct { + Feature models.Feature + }{ + {testutils.BuildTestFeature(t)}, + {testutils.BuildTestFeatureWithRules(t)}, + } + + for _, tc := range testCases { + + ft := tc.Feature + t.Run("scenario", func(t *testing.T) { + sc := ft.FindScenario("-") + assert.Nilf(t, sc, "expected scenario to be nil") + }) + + t.Run("background", func(t *testing.T) { + bg := ft.FindBackground("-") + assert.Nilf(t, bg, "expected background to be nil") + }) + + t.Run("example", func(t *testing.T) { + example, row := ft.FindExample("-") + assert.Nilf(t, example, "expected example to be nil") + assert.Nilf(t, row, "expected table row to be nil") + }) + + t.Run("step", func(t *testing.T) { + step := ft.FindStep("-") + assert.Nilf(t, step, "expected step to be nil") + }) + } +} diff --git a/internal/testutils/utils.go b/internal/testutils/utils.go index fa70ebfd..4c1c8afe 100644 --- a/internal/testutils/utils.go +++ b/internal/testutils/utils.go @@ -58,3 +58,53 @@ Scenario Outline: Eat out of Examples: | begin | dec | remain | | 12 | 5 | 7 |` + +// BuildTestFeature creates a feature with rules for testing purpose. +// +// The created feature includes: +// - a background +// - one normal scenario with three steps +// - one outline scenario with one example and three steps +func BuildTestFeatureWithRules(t *testing.T) models.Feature { + newIDFunc := (&messages.Incrementing{}).NewId + + gherkinDocument, err := gherkin.ParseGherkinDocument(strings.NewReader(featureWithRuleContent), newIDFunc) + require.NoError(t, err) + + path := t.Name() + gherkinDocument.Uri = path + pickles := gherkin.Pickles(*gherkinDocument, path, newIDFunc) + + ft := models.Feature{GherkinDocument: gherkinDocument, Pickles: pickles, Content: []byte(featureWithRuleContent)} + require.Len(t, ft.Pickles, 2) + + require.Len(t, ft.Pickles[0].AstNodeIds, 1) + require.Len(t, ft.Pickles[0].Steps, 3) + + require.Len(t, ft.Pickles[1].AstNodeIds, 2) + require.Len(t, ft.Pickles[1].Steps, 3) + + return ft +} + +const featureWithRuleContent = `Feature: eat godogs +In order to be happy +As a hungry gopher +I need to be able to eat godogs + +Rule: eating godogs + +Background: + Given there are godogs + +Scenario: Eat 5 out of 12 + When I eat 5 + Then there should be 7 remaining + +Scenario Outline: Eat out of + When I eat + Then there should be remaining + + Examples: + | begin | dec | remain | + | 12 | 5 | 7 |`