Skip to content

Commit

Permalink
Feat: underscore in example name [#26] (#32)
Browse files Browse the repository at this point in the history
* Underscore instead of space

* Fix test
  • Loading branch information
hedhyw authored Jun 12, 2022
1 parent 72e014a commit 62b1dc0
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 8 deletions.
22 changes: 14 additions & 8 deletions internal/docplugin/goplugin/goplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ const (

// GoPlugin injects golang specific information: go types, aliases.
type GoPlugin struct {
aliaser *goaliaser.Aliaser
aliaser *goaliaser.Aliaser
exampleNameReplacer *strings.Replacer
}

// New initializes a new go plugin.
func New() *GoPlugin {
return &GoPlugin{
aliaser: goaliaser.New(),
aliaser: goaliaser.New(),
exampleNameReplacer: strings.NewReplacer(" ", "_", "\t", "_"),
}
}

Expand Down Expand Up @@ -158,17 +160,21 @@ func (p GoPlugin) handleStruct(
val.PluginData[dataFieldGoType] = string(goTypeString)
}
case model.TableRow:
values := make([]string, 0, len(val.Cells))
for _, c := range val.Cells {
values = append(values, c.Value)
}

val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(strings.Join(values, "_"))
val.PluginData[dataFieldGoValue] = p.prepareExampleName(val)
}

return nil
}

func (p GoPlugin) prepareExampleName(row model.TableRow) string {
values := make([]string, 0, len(row.Cells))
for _, c := range row.Cells {
values = append(values, p.exampleNameReplacer.Replace(c.Value))
}

return p.aliaser.StringValue(strings.Join(values, "_"))
}

func (p GoPlugin) fillExampleHeaderTypes(examples *model.Examples) (err error) {
if len(examples.TableBody) == 0 {
return nil
Expand Down
30 changes: 30 additions & 0 deletions internal/docplugin/goplugin/goplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ func TestGoPluginProcess(t *testing.T) {
assert.Error(t, p.Process(ctx, doc))
})

t.Run("Examples_underscore", func(t *testing.T) {
t.Parallel()

// It tests https://github.com/hedhyw/gherkingen/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"])
}
})

t.Run("TableCell", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -163,6 +175,24 @@ func getExampleDocument() *model.GherkinDocument {
PluginData: make(map[string]any),
}},
PluginData: map[string]any{},
}, {
Keyword: "Keyword",
Name: "Name",
TableHeader: &model.TableRow{
Cells: []*model.TableCell{{
Value: "<Message>",
PluginData: make(map[string]any),
}},
PluginData: make(map[string]any),
},
TableBody: []*model.TableRow{{
Cells: []*model.TableCell{{
Value: "hello world",
PluginData: make(map[string]any),
}},
PluginData: make(map[string]any),
}},
PluginData: map[string]any{},
}},
PluginData: map[string]any{},
},
Expand Down
5 changes: 5 additions & 0 deletions internal/generator/examples/issue_26.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Feature: Issue example
Scenario: Just a hello world
Examples:
| <name> |
| hello world |
118 changes: 118 additions & 0 deletions internal/generator/examples/issue_26.feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"Feature": {
"Location": {
"Line": 1,
"Column": 1,
"PluginData": {}
},
"Tags": [],
"Language": "en",
"Keyword": "Feature",
"Name": "Issue example",
"Description": "",
"Children": [
{
"Scenario": {
"Location": {
"Line": 2,
"Column": 5,
"PluginData": {}
},
"Tags": [],
"Keyword": "Scenario",
"Name": "Just a hello world",
"Description": "",
"Steps": [],
"Examples": [
{
"Location": {
"Line": 3,
"Column": 9,
"PluginData": {}
},
"Tags": [],
"Keyword": "Examples",
"Name": "",
"Description": "",
"ID": "fb180daf-48a7-4ee0-b10d-394651850fd4",
"TableHeader": {
"Location": {
"Line": 4,
"Column": 9,
"PluginData": {}
},
"Cells": [
{
"Location": {
"Line": 4,
"Column": 11,
"PluginData": {}
},
"Value": "\u003cname\u003e",
"PluginData": {
"GoName": "Name",
"GoType": "string",
"GoValue": "\"\u003cname\u003e\""
}
}
],
"ID": "0194fdc2-fa2f-4cc0-81d3-ff12045b73c8",
"PluginData": {
"GoValue": "\"\u003cname\u003e\""
}
},
"TableBody": [
{
"Location": {
"Line": 5,
"Column": 9,
"PluginData": {}
},
"Cells": [
{
"Location": {
"Line": 5,
"Column": 11,
"PluginData": {}
},
"Value": "hello world",
"PluginData": {
"GoType": "string",
"GoValue": "\"hello world\""
}
}
],
"ID": "6e4ff95f-f662-45ee-a82a-bdf44a2d0b75",
"PluginData": {
"GoType": "string",
"GoValue": "\"hello_world\""
}
}
],
"PluginData": {
"GoName": "Undefined",
"GoType": "string",
"GoValue": "\"Examples\""
}
}
],
"ID": "a178892e-e285-4ce1-9114-55780875d64e",
"PluginData": {
"GoName": "Scenario",
"GoType": "string",
"GoValue": "\"Just a hello world\""
}
},
"PluginData": {}
}
],
"PluginData": {
"GoName": "IssueExample",
"GoType": "string",
"GoValue": "\"Issue example\""
}
},
"Comments": [],
"PluginData": {},
"PackageName": "examples_test"
}
25 changes: 25 additions & 0 deletions internal/generator/examples/issue_26.feature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package examples_test

import (
"testing"

"github.com/hedhyw/gherkingen/pkg/v1/bdd"
)

func TestIssueExample(t *testing.T) {
f := bdd.NewFeature(t, "Issue example")

f.Scenario("Just a hello world", func(t *testing.T, f *bdd.Feature) {
type testCase struct {
Name string `field:"<name>"`
}

testCases := map[string]testCase{
"hello_world": {"hello world"},
}

f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
})
})

}

0 comments on commit 62b1dc0

Please sign in to comment.