Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty Print when using rules (#440) #480

Merged
merged 4 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pretty Print when using rules (#440)
  • Loading branch information
dumpsterfireproject committed May 30, 2022
commit 2e4d7d85eec75dd0acbb6eb2964bf37ede09859e
13 changes: 13 additions & 0 deletions internal/models/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 59 additions & 11 deletions internal/models/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
})
}
}
50 changes: 50 additions & 0 deletions internal/testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,53 @@ Scenario Outline: Eat <dec> out of <beginning>
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 <begin> godogs

Scenario: Eat 5 out of 12
When I eat 5
Then there should be 7 remaining

Scenario Outline: Eat <dec> out of <beginning>
When I eat <dec>
Then there should be <remain> remaining

Examples:
| begin | dec | remain |
| 12 | 5 | 7 |`