Skip to content

Commit 2b332dc

Browse files
authored
feat: use simple template #81 (#82)
1 parent 3e9a015 commit 2b332dc

26 files changed

+887
-0
lines changed

Diff for: README.md

+38
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,44 @@ f.TestCases(testCases, func(t *testing.T, f *bdd.Feature, tc testCase) {
9595
})
9696
```
9797

98+
## Simplified template
99+
100+
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.
101+
Provide `-template std.simple.v1.go.tmpl` to to use [this](internal/assets/std.simple.v1.go.tmpl) template.
102+
103+
```go
104+
func TestApplicationCommandLineTool(t *testing.T) {
105+
t.Parallel()
106+
107+
t.Run("User wants to see usage information", func(t *testing.T) {
108+
t.Parallel()
109+
110+
type testCase struct {
111+
Flag string `field:"<flag>"`
112+
ExitStatus int `field:"<exit_status>"`
113+
Printed bool `field:"<printed>"`
114+
}
115+
116+
testCases := map[string]testCase{
117+
"--help_0_true": {"--help", 0, true},
118+
"-help_0_true": {"-help", 0, true},
119+
"-invalid_1_false": {"-invalid", 1, false},
120+
}
121+
122+
for name, tc := range testCases {
123+
t.Run(name, func(t *testing.T) {
124+
// When flag <flag> is provided
125+
126+
// Then usage should be printed <printed>
127+
128+
// And exit status should be <exit_status>
129+
130+
})
131+
}
132+
})
133+
}
134+
```
135+
98136
## More advanced example
99137

100138
See [internal/app/app.feature](internal/app/app.feature) and [internal/app/app_test.go](internal/app/app_test.go).

Diff for: internal/app/app.feature

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Feature: Application command line tool
3333
| <feature> | <template> |
3434
| app.feature | ../assets/std.struct.v1.go.tmpl |
3535
| app.feature | @/std.struct.v1.go.tmpl |
36+
| app.feature | @/std.simple.v1.go.tmpl |
3637

3738
Scenario: User wants to set custom package
3839
When <package> is provided

Diff for: internal/app/app_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func TestApplicationCommandLineTool(t *testing.T) {
9595
testCases := map[string]testCase{
9696
"app.feature_../assets/std.struct.v1.go.tmpl": {"app.feature", "../assets/std.struct.v1.go.tmpl"},
9797
"app.feature_@/std.struct.v1.go.tmpl": {"app.feature", "@/std.struct.v1.go.tmpl"},
98+
"app.feature_@/std.simple.v1.go.tmpl": {"app.feature", "@/std.simple.v1.go.tmpl"},
9899
}
99100

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

Diff for: internal/assets/assets_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestOpenTemplate(t *testing.T) {
2424

2525
files := [...]string{
2626
"std.struct.v1.go.tmpl",
27+
"std.simple.v1.go.tmpl",
2728
}
2829

2930
for _, f := range files {

Diff for: internal/assets/std.simple.v1.go.tmpl

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package {{ .PackageName }}
2+
3+
import (
4+
"testing"
5+
)
6+
7+
{{ define "Background" }}
8+
{{- $Background := . -}}
9+
background := func(t *testing.T) interface{} {
10+
{{- range $Background.Steps }}
11+
// {{ .Keyword }}{{ .Text }}
12+
13+
{{ end }}
14+
15+
return nil // TODO: Feel free to modify return value(s).
16+
}
17+
18+
{{ end }}
19+
20+
{{ define "Scenario" }}
21+
{{- $Scenario := . -}}
22+
t.Run({{ $Scenario.PluginData.GoValue }}, func({{- /*
23+
t is usualy unused if there are no examples
24+
*/ -}}{{- if and $Scenario.Examples (not $Scenario.PluginData.GoParallel) -}}_{{- else -}}t{{- end -}} *testing.T) {
25+
{{- range $Scenario.Examples }}
26+
{{- if $Scenario.PluginData.GoParallel }}
27+
t.Parallel()
28+
29+
{{ end -}}
30+
31+
{{- /* Define test case struct. */ -}}
32+
33+
type testCase struct {
34+
{{- range .TableHeader.Cells }}
35+
{{ .PluginData.GoName }} {{ .PluginData.GoType }} `field:"{{.Value}}"`
36+
{{- end -}}
37+
}
38+
39+
testCases := map[string]testCase{
40+
{{- range .TableBody }}
41+
{{ .PluginData.GoValue }}: {
42+
{{- /* Struct fields start. */ -}}
43+
{{- range $index, $cell := .Cells -}}
44+
{{- if $index -}},{{ end }} {{- $cell.PluginData.GoValue -}}
45+
{{- end -}}
46+
{{- /* Struct fields end. */ -}}
47+
},
48+
{{- end }}
49+
}
50+
51+
for name, tc := range testCases {
52+
{{- if $Scenario.PluginData.GoParallel }}
53+
tc := tc
54+
{{ end -}}
55+
56+
t.Run(name, func(t *testing.T) {
57+
{{- if $Scenario.PluginData.GoParallel }}
58+
t.Parallel()
59+
60+
{{ end -}}
61+
_ = tc // TODO: Use and remove.
62+
{{- if $Scenario.PluginData.GoHasBackground }}
63+
_ = background(t)
64+
65+
{{ end -}}
66+
67+
{{- range $Scenario.Steps }}
68+
// {{ .Keyword }}{{ .Text }}
69+
70+
{{ end }}
71+
})
72+
}
73+
{{- else }}
74+
{{- if $Scenario.PluginData.GoParallel }}
75+
t.Parallel()
76+
77+
{{ end -}}
78+
{{- if $Scenario.PluginData.GoHasBackground }}
79+
_ = background(t)
80+
81+
{{ end }}
82+
{{- range $Scenario.Steps }}
83+
// {{ .Keyword }}{{ .Text }}
84+
85+
{{ end -}}
86+
{{ end }}
87+
})
88+
{{ end }}
89+
90+
{{ define "Rule" }}
91+
{{ $Rule := . }}
92+
t.Run({{ $Rule.PluginData.GoValue }}, func({{- if $Rule.PluginData.GoParallel -}}t{{- else -}}_{{- end -}} *testing.T) {
93+
{{- if $Rule.PluginData.GoParallel }}
94+
t.Parallel()
95+
96+
{{ end -}}
97+
{{- range $Rule.Children -}}
98+
99+
{{- if .Background }}
100+
{{ template "Background" .Background }}
101+
{{- end }}
102+
103+
{{- if .Scenario }}
104+
{{- template "Scenario" .Scenario -}}
105+
{{- end }}
106+
107+
{{- end -}}
108+
})
109+
{{ end }}
110+
111+
func Test{{ .Feature.PluginData.GoName }}(t *testing.T) {
112+
{{- if .Feature.PluginData.GoParallel }}
113+
t.Parallel()
114+
115+
{{ end -}}
116+
{{ if .Feature.PluginData.GoComment }}
117+
/* {{ .Feature.PluginData.GoComment }} */
118+
{{ end }}
119+
120+
{{- range .Feature.Children }}
121+
122+
{{ if .Background }}
123+
{{ template "Background" .Background }}
124+
{{- end -}}
125+
126+
{{ if .Scenario }}
127+
{{ template "Scenario" .Scenario }}
128+
{{- end -}}
129+
130+
{{ if .Rule }}
131+
{{ template "Rule" .Rule }}
132+
{{- end -}}
133+
134+
{{- end -}}
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package examples_test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMultipleSiteSupport(t *testing.T) {
8+
/*
9+
Only blog owners can post to a blog, except administrators,
10+
who can post to all blogs.
11+
*/
12+
13+
background := func(t *testing.T) interface{} {
14+
// Given a global administrator named "Greg"
15+
16+
// And a blog named "Greg's anti-tax rants"
17+
18+
// And a customer named "Dr. Bill"
19+
20+
// And a blog named "Expensive Therapy" owned by "Dr. Bill"
21+
22+
return nil // TODO: Feel free to modify return value(s).
23+
}
24+
25+
t.Run("Dr. Bill posts to his own blog", func(t *testing.T) {
26+
_ = background(t)
27+
28+
// Given I am logged in as Dr. Bill
29+
30+
// When I try to post to "Expensive Therapy"
31+
32+
// Then I should see "Your article was published."
33+
34+
})
35+
36+
t.Run("Dr. Bill tries to post to somebody else's blog, and fails", func(t *testing.T) {
37+
_ = background(t)
38+
39+
// Given I am logged in as Dr. Bill
40+
41+
// When I try to post to "Greg's anti-tax rants"
42+
43+
// Then I should see "Hey! That's not your blog!"
44+
45+
})
46+
47+
t.Run("Greg posts to a client's blog", func(t *testing.T) {
48+
_ = background(t)
49+
50+
// Given I am logged in as Greg
51+
52+
// When I try to post to "Expensive Therapy"
53+
54+
// Then I should see "Your article was published."
55+
56+
})
57+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package examples_test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestTypeDeterminatiopn(t *testing.T) {
8+
9+
t.Run("All type are determinated", func(_ *testing.T) {
10+
type testCase struct {
11+
Bool bool `field:"<bool>"`
12+
Int int `field:"<int>"`
13+
String string `field:"<string>"`
14+
Flag bool `field:"<flag>"`
15+
Float64 float64 `field:"<float64>"`
16+
}
17+
18+
testCases := map[string]testCase{
19+
"true_1_hello_-_1.0": {true, 1, "hello", false, 1.0},
20+
"false_2_world_+_0.0": {false, 2, "world", true, 0.0},
21+
}
22+
23+
for name, tc := range testCases {
24+
t.Run(name, func(t *testing.T) {
25+
_ = tc // TODO: Use and remove.
26+
// When generator comleted
27+
28+
// Then correct types are shown
29+
30+
})
31+
}
32+
})
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package examples_test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNestedBackground(t *testing.T) {
8+
9+
background := func(t *testing.T) interface{} {
10+
// Given a global administrator named "Greg"
11+
12+
// And a blog named "Greg's anti-tax rants"
13+
14+
// And a customer named "Dr. Bill"
15+
16+
// And a blog named "Expensive Therapy" owned by "Dr. Bill"
17+
18+
return nil // TODO: Feel free to modify return value(s).
19+
}
20+
21+
t.Run("Dr. Bill posts to his own blog", func(t *testing.T) {
22+
_ = background(t)
23+
24+
// Given I am logged in as Dr. Bill
25+
26+
// When I try to post to "Expensive Therapy"
27+
28+
// Then I should see "Your article was published."
29+
30+
})
31+
32+
t.Run("There can be only One", func(_ *testing.T) {
33+
background := func(t *testing.T) interface{} {
34+
// Given I have overdue tasks
35+
36+
return nil // TODO: Feel free to modify return value(s).
37+
}
38+
39+
t.Run("Only One -- One alive", func(t *testing.T) {
40+
_ = background(t)
41+
42+
// Given there is only 1 ninja alive
43+
44+
// Then he (or she) will live forever ;-)
45+
46+
})
47+
})
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package examples_test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIssueExample(t *testing.T) {
8+
9+
t.Run("Just a hello world", func(_ *testing.T) {
10+
type testCase struct {
11+
Name string `field:"<name>"`
12+
}
13+
14+
testCases := map[string]testCase{
15+
"hello_world": {"hello world"},
16+
}
17+
18+
for name, tc := range testCases {
19+
t.Run(name, func(t *testing.T) {
20+
_ = tc // TODO: Use and remove.
21+
})
22+
}
23+
})
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package examples_test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestExampleIssue27Multi(t *testing.T) {
8+
/*
9+
Details:
10+
- example 1
11+
- example 2
12+
13+
- example 3
14+
- example 3.1
15+
- example 3.2
16+
*/
17+
18+
t.Run("Multi-line comment with indents", func(t *testing.T) {
19+
})
20+
}

0 commit comments

Comments
 (0)