Skip to content

Commit

Permalink
Pretty Print when using rules (cucumber#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
dumpsterfireproject committed May 30, 2022
1 parent e9afe74 commit 2e4d7d8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 11 deletions.
13 changes: 13 additions & 0 deletions internal/models/feature.go
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
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
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 |`

0 comments on commit 2e4d7d8

Please sign in to comment.