Skip to content

Commit

Permalink
feat: support a new flag to enable go1.22 features
Browse files Browse the repository at this point in the history
  • Loading branch information
hedhyw committed Jun 19, 2024
1 parent 03e6cb4 commit d478d82
Show file tree
Hide file tree
Showing 17 changed files with 55 additions and 84 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ See [internal/app/app.feature](internal/app/app.feature) and [internal/app/app_t
## Version 4 changes

1. Removed template "std.struct.v1.go.tmpl".
2. The flag "go-parallel" has been removed.
3. Loop variable "testCase" is not copied. Disable using `disable-go-22-scope` flag.

# Install

Expand Down Expand Up @@ -196,12 +198,12 @@ gherkingen -list
gherkingen --help
Usage of gherkingen [FEATURE_FILE]:
-enable-go-22-scope
do not copy loop variables
-disable-go-parallel
disable execution of tests in parallel
-format string
output format: autodetect, json, go, raw (default "autodetect")
-go-parallel
add parallel mark (deprecated, enabled by default) (default true)
-help
print usage
-list
Expand Down
12 changes: 7 additions & 5 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
internalPathPrefix = "@/"
defaultTemplate = "std.simple.v1.go.tmpl"
defaultDisableGoParallel = false
defaultEnableGo22Scope = false
defaultOutputFormat = model.FormatAutoDetect
)

Expand Down Expand Up @@ -44,16 +45,16 @@ func Run(arguments []string, out io.Writer, version string) (err error) {
false,
"print usage",
)
_ = flagSet.Bool(
"go-parallel",
!defaultDisableGoParallel,
"add parallel mark (deprecated, enabled by default)",
)
disableGoParallel := flagSet.Bool(
"disable-go-parallel",
defaultDisableGoParallel,
"disable execution of tests in parallel",
)
enableGo22Scope := flagSet.Bool(
"enable-go-22-scope",
defaultEnableGo22Scope,
"do not copy loop variables",
)
listCmd := flagSet.Bool(
"list",
false,
Expand Down Expand Up @@ -101,6 +102,7 @@ func Run(arguments []string, out io.Writer, version string) (err error) {
InputFile: inputFile,
PackageName: *packageName,
GoParallel: !(*disableGoParallel),
Go22Scope: *enableGo22Scope,
GenerateUUID: newUUIDRandomGenerator(seed),
})
}
Expand Down
4 changes: 3 additions & 1 deletion internal/app/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type appArgs struct {
InputFile string
PackageName string
GoParallel bool
Go22Scope bool
GenerateUUID func() string
}

Expand All @@ -41,7 +42,8 @@ func runGenerator(
}

goPlugin := goplugin.New(goplugin.Args{
Parallel: args.GoParallel,
Parallel: args.GoParallel,
Go22Scope: args.Go22Scope,
})

data, err := generator.Generate(generator.Args{
Expand Down
2 changes: 1 addition & 1 deletion internal/assets/std.simple.v1.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import (
}

for name, testCase := range testCases {
{{- if $Scenario.PluginData.GoParallel }}
{{- if and $Scenario.PluginData.GoParallel (not $Scenario.PluginData.Go22Scope) }}
testCase := testCase

{{ end -}}
Expand Down
15 changes: 10 additions & 5 deletions internal/docplugin/goplugin/goplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const (
dataFieldGoName = "GoName"
dataFieldGoComment = "GoComment"
dataFieldGoBackground = "GoHasBackground"
dataFieldGoparallel = "GoParallel"
dataFieldGoParallel = "GoParallel"
dataFieldGo22Scope = "Go22Scope"
)

// GoPlugin injects golang specific information: go types, aliases.
Expand All @@ -35,7 +36,8 @@ type GoPlugin struct {

// Args contains optional arguments for GoPlugin.
type Args struct {
Parallel bool
Parallel bool
Go22Scope bool
}

// New initializes a new go plugin.
Expand Down Expand Up @@ -160,19 +162,22 @@ func (p GoPlugin) handleStruct(
val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Name)
val.PluginData[dataFieldGoType] = string(goTypeString)
val.PluginData[dataFieldGoComment] = p.prepareFeatureDescription(val.Description)
val.PluginData[dataFieldGoparallel] = p.args.Parallel
val.PluginData[dataFieldGoParallel] = p.args.Parallel
val.PluginData[dataFieldGo22Scope] = p.args.Go22Scope
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)
val.PluginData[dataFieldGoparallel] = p.args.Parallel
val.PluginData[dataFieldGoParallel] = p.args.Parallel
val.PluginData[dataFieldGo22Scope] = p.args.Go22Scope
p.processRuleBackground(val)
case model.Scenario:
val.PluginData[dataFieldGoName] = p.aliaser.NameAlias(formatScenarioKeyword(val.Keyword))
val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Name)
val.PluginData[dataFieldGoType] = string(goTypeString)
val.PluginData[dataFieldGoparallel] = p.args.Parallel
val.PluginData[dataFieldGoParallel] = p.args.Parallel
val.PluginData[dataFieldGo22Scope] = p.args.Go22Scope
case model.Step:
val.PluginData[dataFieldGoName] = p.aliaser.NameAlias(val.Keyword)
val.PluginData[dataFieldGoValue] = p.aliaser.StringValue(val.Text)
Expand Down
23 changes: 23 additions & 0 deletions internal/docplugin/goplugin/goplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,29 @@ func TestParallel(t *testing.T) {
}
}

func TestGo22Scope(t *testing.T) {
t.Parallel()

ctx := context.Background()

for _, testCase := range [2]bool{true, false} {
t.Run(strconv.FormatBool(testCase), func(t *testing.T) {
t.Parallel()

p := goplugin.New(goplugin.Args{
Go22Scope: testCase,
})
doc := getExampleDocument()

if assert.NoError(t, p.Process(ctx, doc)) {
assert.Equal(t, testCase, doc.Feature.PluginData["Go22Scope"])
assert.Equal(t, testCase, doc.Feature.Children[0].Scenario.PluginData["Go22Scope"])
assert.Equal(t, testCase, doc.Feature.Children[0].Rule.PluginData["Go22Scope"])
}
})
}
}

func getExampleDocument() *model.GherkinDocument {
return &model.GherkinDocument{
Feature: &model.Feature{
Expand Down
8 changes: 0 additions & 8 deletions internal/generator/examples/simple/background.feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
)

func TestMultipleSiteSupport(t *testing.T) {
t.Parallel()

/*
Only blog owners can post to a blog, except administrators,
who can post to all blogs.
Expand All @@ -29,8 +27,6 @@ func TestMultipleSiteSupport(t *testing.T) {
}

t.Run("Dr. Bill posts to his own blog", func(t *testing.T) {
t.Parallel()

_ = background(t)

// Given I am logged in as Dr. Bill.
Expand All @@ -42,8 +38,6 @@ func TestMultipleSiteSupport(t *testing.T) {
})

t.Run("Dr. Bill tries to post to somebody else's blog, and fails", func(t *testing.T) {
t.Parallel()

_ = background(t)

// Given I am logged in as Dr. Bill.
Expand All @@ -55,8 +49,6 @@ func TestMultipleSiteSupport(t *testing.T) {
})

t.Run("Greg posts to a client's blog", func(t *testing.T) {
t.Parallel()

_ = background(t)

// Given I am logged in as Greg.
Expand Down
9 changes: 1 addition & 8 deletions internal/generator/examples/simple/bool.feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
)

func TestTypeDeterminatiopn(t *testing.T) {
t.Parallel()

t.Run("All type are determinated", func(t *testing.T) {
t.Parallel()

t.Run("All type are determinated", func(_ *testing.T) {
type testCase struct {
Bool bool `field:"<bool>"`
Int int `field:"<int>"`
Expand All @@ -24,11 +21,7 @@ func TestTypeDeterminatiopn(t *testing.T) {
}

for name, testCase := range testCases {
testCase := testCase

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

_ = testCase // TODO: Use and remove.
// When generator completed.

Expand Down
9 changes: 1 addition & 8 deletions internal/generator/examples/simple/complex.feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
)

func TestNestedBackground(t *testing.T) {
t.Parallel()

type backgroundData struct{}

Expand All @@ -24,8 +23,6 @@ func TestNestedBackground(t *testing.T) {
}

t.Run("Dr. Bill posts to his own blog", func(t *testing.T) {
t.Parallel()

_ = background(t)

// Given I am logged in as Dr. Bill.
Expand All @@ -36,9 +33,7 @@ func TestNestedBackground(t *testing.T) {

})

t.Run("There can be only One", func(t *testing.T) {
t.Parallel()

t.Run("There can be only One", func(_ *testing.T) {
type backgroundData struct{}

background := func(t *testing.T) backgroundData {
Expand All @@ -50,8 +45,6 @@ func TestNestedBackground(t *testing.T) {
}

t.Run("Only One -- One alive", func(t *testing.T) {
t.Parallel()

_ = background(t)

// Given there is only 1 ninja alive.
Expand Down
9 changes: 1 addition & 8 deletions internal/generator/examples/simple/issue_26.feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
)

func TestIssueExample(t *testing.T) {
t.Parallel()

t.Run("Just a hello world", func(t *testing.T) {
t.Parallel()

t.Run("Just a hello world", func(_ *testing.T) {
type testCase struct {
Name string `field:"<name>"`
}
Expand All @@ -19,11 +16,7 @@ func TestIssueExample(t *testing.T) {
}

for name, testCase := range testCases {
testCase := testCase

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

_ = testCase // TODO: Use and remove.
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
)

func TestExampleIssue27Multi(t *testing.T) {
t.Parallel()

/*
Details:
- example 1
Expand All @@ -18,7 +16,5 @@ func TestExampleIssue27Multi(t *testing.T) {
*/

t.Run("Multi-line comment with indents", func(t *testing.T) {
t.Parallel()

})
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import (
)

func TestExampleIssue27Single(t *testing.T) {
t.Parallel()

/* Hello world. */

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

})
}
9 changes: 1 addition & 8 deletions internal/generator/examples/simple/readme.feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
)

func TestApplicationCommandLineTool(t *testing.T) {
t.Parallel()

t.Run("User wants to see usage information", func(t *testing.T) {
t.Parallel()

t.Run("User wants to see usage information", func(_ *testing.T) {
type testCase struct {
Flag string `field:"<flag>"`
ExitStatus int `field:"<exit_status>"`
Expand All @@ -23,11 +20,7 @@ func TestApplicationCommandLineTool(t *testing.T) {
}

for name, testCase := range testCases {
testCase := testCase

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

_ = testCase // TODO: Use and remove.
// When the application is started with <flag>.

Expand Down
15 changes: 2 additions & 13 deletions internal/generator/examples/simple/rules.feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import (
)

func TestHighlander(t *testing.T) {
t.Parallel()

t.Run("There can be only One", func(t *testing.T) {
t.Parallel()

t.Run("There can be only One", func(_ *testing.T) {
type backgroundData struct{}

background := func(t *testing.T) backgroundData {
Expand All @@ -21,8 +18,6 @@ func TestHighlander(t *testing.T) {
}

t.Run("Only One -- More than one alive", func(t *testing.T) {
t.Parallel()

_ = background(t)

// Given there are 3 ninjas.
Expand All @@ -37,8 +32,6 @@ func TestHighlander(t *testing.T) {

})
t.Run("Only One -- One alive", func(t *testing.T) {
t.Parallel()

_ = background(t)

// Given there is only 1 ninja alive.
Expand All @@ -48,12 +41,8 @@ func TestHighlander(t *testing.T) {
})
})

t.Run("There can be Two (in some cases)", func(t *testing.T) {
t.Parallel()

t.Run("There can be Two (in some cases)", func(_ *testing.T) {
t.Run("Two -- Dead and Reborn as Phoenix", func(t *testing.T) {
t.Parallel()

})
})
}
Loading

0 comments on commit d478d82

Please sign in to comment.