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

feat: use simple template #81 #82

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,44 @@ f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
})
```

## Simplified template

A simplified template is also available. It uses only the std [testing](https://pkg.go.dev/testing) package without any other dependency. Steps are defined by comments.
Provide `-template std.simple.v1.go.tmpl` to to use [this](internal/assets/std.simple.v1.go.tmpl) template.

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

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

type testCase struct {
Flag string `field:"<flag>"`
ExitStatus int `field:"<exit_status>"`
Printed bool `field:"<printed>"`
}

testCases := map[string]testCase{
"--help_0_true": {"--help", 0, true},
"-help_0_true": {"-help", 0, true},
"-invalid_1_false": {"-invalid", 1, false},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
// When flag <flag> is provided

// Then usage should be printed <printed>

// And exit status should be <exit_status>

})
}
})
}
```

## More advanced example

See [internal/app/app.feature](internal/app/app.feature) and [internal/app/app_test.go](internal/app/app_test.go).
Expand Down
1 change: 1 addition & 0 deletions internal/app/app.feature
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Feature: Application command line tool
| <feature> | <template> |
| app.feature | ../assets/std.struct.v1.go.tmpl |
| app.feature | @/std.struct.v1.go.tmpl |
| app.feature | @/std.simple.v1.go.tmpl |

Scenario: User wants to set custom package
When <package> is provided
Expand Down
1 change: 1 addition & 0 deletions internal/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestApplicationCommandLineTool(t *testing.T) {
testCases := map[string]testCase{
"app.feature_../assets/std.struct.v1.go.tmpl": {"app.feature", "../assets/std.struct.v1.go.tmpl"},
"app.feature_@/std.struct.v1.go.tmpl": {"app.feature", "@/std.struct.v1.go.tmpl"},
"app.feature_@/std.simple.v1.go.tmpl": {"app.feature", "@/std.simple.v1.go.tmpl"},
}

f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
Expand Down
1 change: 1 addition & 0 deletions internal/assets/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestOpenTemplate(t *testing.T) {

files := [...]string{
"std.struct.v1.go.tmpl",
"std.simple.v1.go.tmpl",
}

for _, f := range files {
Expand Down
135 changes: 135 additions & 0 deletions internal/assets/std.simple.v1.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package {{ .PackageName }}

import (
"testing"
)

{{ define "Background" }}
{{- $Background := . -}}
background := func(t *testing.T) interface{} {
{{- range $Background.Steps }}
// {{ .Keyword }}{{ .Text }}

{{ end }}

return nil // TODO: Feel free to modify return value(s).
}

{{ end }}

{{ define "Scenario" }}
{{- $Scenario := . -}}
t.Run({{ $Scenario.PluginData.GoValue }}, func({{- /*
t is usualy unused if there are no examples
*/ -}}{{- if and $Scenario.Examples (not $Scenario.PluginData.GoParallel) -}}_{{- else -}}t{{- end -}} *testing.T) {
{{- range $Scenario.Examples }}
{{- if $Scenario.PluginData.GoParallel }}
t.Parallel()

{{ end -}}

{{- /* Define test case struct. */ -}}

type testCase struct {
{{- range .TableHeader.Cells }}
{{ .PluginData.GoName }} {{ .PluginData.GoType }} `field:"{{.Value}}"`
{{- end -}}
}

testCases := map[string]testCase{
{{- range .TableBody }}
{{ .PluginData.GoValue }}: {
{{- /* Struct fields start. */ -}}
{{- range $index, $cell := .Cells -}}
{{- if $index -}},{{ end }} {{- $cell.PluginData.GoValue -}}
{{- end -}}
{{- /* Struct fields end. */ -}}
},
{{- end }}
}

for name, tc := range testCases {
{{- if $Scenario.PluginData.GoParallel }}
tc := tc
{{ end -}}

t.Run(name, func(t *testing.T) {
{{- if $Scenario.PluginData.GoParallel }}
t.Parallel()

{{ end -}}
_ = tc // TODO: Use and remove.
{{- if $Scenario.PluginData.GoHasBackground }}
_ = background(t)

{{ end -}}

{{- range $Scenario.Steps }}
// {{ .Keyword }}{{ .Text }}

{{ end }}
})
}
{{- else }}
{{- if $Scenario.PluginData.GoParallel }}
t.Parallel()

{{ end -}}
{{- if $Scenario.PluginData.GoHasBackground }}
_ = background(t)

{{ end }}
{{- range $Scenario.Steps }}
// {{ .Keyword }}{{ .Text }}

{{ end -}}
{{ end }}
})
{{ end }}

{{ define "Rule" }}
{{ $Rule := . }}
t.Run({{ $Rule.PluginData.GoValue }}, func({{- if $Rule.PluginData.GoParallel -}}t{{- else -}}_{{- end -}} *testing.T) {
{{- if $Rule.PluginData.GoParallel }}
t.Parallel()

{{ end -}}
{{- range $Rule.Children -}}

{{- if .Background }}
{{ template "Background" .Background }}
{{- end }}

{{- if .Scenario }}
{{- template "Scenario" .Scenario -}}
{{- end }}

{{- end -}}
})
{{ end }}

func Test{{ .Feature.PluginData.GoName }}(t *testing.T) {
{{- if .Feature.PluginData.GoParallel }}
t.Parallel()

{{ end -}}
{{ if .Feature.PluginData.GoComment }}
/* {{ .Feature.PluginData.GoComment }} */
{{ end }}

{{- range .Feature.Children }}

{{ if .Background }}
{{ template "Background" .Background }}
{{- end -}}

{{ if .Scenario }}
{{ template "Scenario" .Scenario }}
{{- end -}}

{{ if .Rule }}
{{ template "Rule" .Rule }}
{{- end -}}

{{- end -}}
}
57 changes: 57 additions & 0 deletions internal/generator/examples/simple/background.feature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package examples_test

import (
"testing"
)

func TestMultipleSiteSupport(t *testing.T) {
/*
Only blog owners can post to a blog, except administrators,
who can post to all blogs.
*/

background := func(t *testing.T) interface{} {
// Given a global administrator named "Greg"

// And a blog named "Greg's anti-tax rants"

// And a customer named "Dr. Bill"

// And a blog named "Expensive Therapy" owned by "Dr. Bill"

return nil // TODO: Feel free to modify return value(s).
}

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

// Given I am logged in as Dr. Bill

// When I try to post to "Expensive Therapy"

// Then I should see "Your article was published."

})

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

// Given I am logged in as Dr. Bill

// When I try to post to "Greg's anti-tax rants"

// Then I should see "Hey! That's not your blog!"

})

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

// Given I am logged in as Greg

// When I try to post to "Expensive Therapy"

// Then I should see "Your article was published."

})
}
33 changes: 33 additions & 0 deletions internal/generator/examples/simple/bool.feature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package examples_test

import (
"testing"
)

func TestTypeDeterminatiopn(t *testing.T) {

t.Run("All type are determinated", func(_ *testing.T) {
type testCase struct {
Bool bool `field:"<bool>"`
Int int `field:"<int>"`
String string `field:"<string>"`
Flag bool `field:"<flag>"`
Float64 float64 `field:"<float64>"`
}

testCases := map[string]testCase{
"true_1_hello_-_1.0": {true, 1, "hello", false, 1.0},
"false_2_world_+_0.0": {false, 2, "world", true, 0.0},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
_ = tc // TODO: Use and remove.
// When generator comleted

// Then correct types are shown

})
}
})
}
48 changes: 48 additions & 0 deletions internal/generator/examples/simple/complex.feature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package examples_test

import (
"testing"
)

func TestNestedBackground(t *testing.T) {

background := func(t *testing.T) interface{} {
// Given a global administrator named "Greg"

// And a blog named "Greg's anti-tax rants"

// And a customer named "Dr. Bill"

// And a blog named "Expensive Therapy" owned by "Dr. Bill"

return nil // TODO: Feel free to modify return value(s).
}

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

// Given I am logged in as Dr. Bill

// When I try to post to "Expensive Therapy"

// Then I should see "Your article was published."

})

t.Run("There can be only One", func(_ *testing.T) {
background := func(t *testing.T) interface{} {
// Given I have overdue tasks

return nil // TODO: Feel free to modify return value(s).
}

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

// Given there is only 1 ninja alive

// Then he (or she) will live forever ;-)

})
})
}
24 changes: 24 additions & 0 deletions internal/generator/examples/simple/issue_26.feature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package examples_test

import (
"testing"
)

func TestIssueExample(t *testing.T) {

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

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

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
_ = tc // TODO: Use and remove.
})
}
})
}
20 changes: 20 additions & 0 deletions internal/generator/examples/simple/issue_27_multi.feature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package examples_test

import (
"testing"
)

func TestExampleIssue27Multi(t *testing.T) {
/*
Details:
- example 1
- example 2

- example 3
- example 3.1
- example 3.2
*/

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