Skip to content

Commit 46cdf8e

Browse files
authored
Fix example test suites without result generated by testing/codegen (#216)
* Add test for generateSuiteTopLevel() in testing/codegen. * Refactor suite-test template * Fix example test suites without result generated by testing/codegen. * Add a newline to a comment
1 parent e53e3a6 commit 46cdf8e

3 files changed

Lines changed: 92 additions & 8 deletions

File tree

testing/codegen/suite_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package codegen
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
httpcodegen "goa.design/goa/v3/http/codegen"
8+
"goa.design/plugins/v3/testing/codegen/testdata"
9+
)
10+
11+
func TestGenerateSuiteTopLevel(t *testing.T) {
12+
cases := map[string]struct {
13+
DSL func()
14+
Code map[string][]string
15+
Path string
16+
}{
17+
"with-result": {
18+
DSL: testdata.WithResultDSL,
19+
Code: map[string][]string{
20+
"suite-test": {testdata.SuiteTestWithResultCode},
21+
},
22+
Path: "with_result_service_suite_test.go",
23+
},
24+
"without-result": {
25+
DSL: testdata.WithoutResultDSL,
26+
Code: map[string][]string{
27+
"suite-test": {testdata.SuiteTestWithoutResultCode},
28+
},
29+
Path: "without_result_service_suite_test.go",
30+
},
31+
}
32+
for name, c := range cases {
33+
t.Run(name, func(t *testing.T) {
34+
root := httpcodegen.RunHTTPDSL(t, c.DSL)
35+
svc := root.Services[0]
36+
f := generateSuiteTopLevel("", "", root, svc)
37+
assert.Equal(t, c.Path, f.Path)
38+
for sec, secCode := range c.Code {
39+
testCode(t, f, sec, secCode)
40+
}
41+
})
42+
}
43+
}

testing/codegen/templates/suite_test.go.tpl

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
{{- printf "Run%sHarness exercises the generated harness against your service implementation." .Service.Name | comment }}
2-
{{- printf "Call this helper from your test, passing your service implementation." | comment }}
1+
{{ printf "Run%sHarness exercises the generated harness against your service implementation." .Service.Name | comment }}
2+
{{ printf "Call this helper from your test, passing your service implementation." | comment }}
33
func Run{{ .Service.StructName }}Harness(t *testing.T, svc {{ .Service.PkgName }}.Service) {
44
t.Helper()
55
{{- if .UseCtx }}
@@ -15,11 +15,7 @@ func Run{{ .Service.StructName }}Harness(t *testing.T, svc {{ .Service.PkgName }
1515
{{- range .NonStream }}
1616
{{- $m := . }}
1717
t.Run("{{ $m.Method.Name }}", func(t *testing.T) {
18-
{{- if $m.Method.PayloadEx }}
19-
result, err := h.Client.{{ $m.Method.VarName }}({{ if $.UseCtx }}ctx{{ else }}context.Background(){{ end }}, td.Valid{{ goify $m.Method.Name true }}Payload())
20-
{{- else }}
21-
result, err := h.Client.{{ $m.Method.VarName }}({{ if $.UseCtx }}ctx{{ else }}context.Background(){{ end }})
22-
{{- end }}
18+
{{ if $m.Method.ResultRef }}result, {{ end }}err := h.Client.{{ $m.Method.VarName }}({{ if $.UseCtx }}ctx{{ else }}context.Background(){{ end }}{{- if $m.Method.PayloadEx }}, td.Valid{{ goify $m.Method.Name true }}Payload(){{ end }})
2319
if err != nil {
2420
t.Errorf("{{ $m.Method.Name }} failed: %v", err)
2521
}
@@ -116,4 +112,4 @@ func Run{{ .Service.StructName }}Harness(t *testing.T, svc {{ .Service.PkgName }
116112
{{- end }}
117113
})
118114
{{- end }}
119-
}
115+
}

testing/codegen/testdata/code.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,48 @@ func (r *ScenarioRunner) selectTransport(client *Client, transport string) *Clie
912912
}
913913
}
914914
`
915+
916+
var SuiteTestWithResultCode = `// RunWithResultServiceHarness exercises the generated harness against your
917+
// service implementation.
918+
// Call this helper from your test, passing your service implementation.
919+
func RunWithResultServiceHarness(t *testing.T, svc withresultservice.Service) {
920+
t.Helper()
921+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
922+
defer cancel()
923+
924+
h := withResultServicetest.NewHarness(t, svc)
925+
defer h.Close()
926+
927+
td := withResultServicetest.NewTestData()
928+
t.Run("WithResultMethod", func(t *testing.T) {
929+
result, err := h.Client.WithResultMethod(ctx)
930+
if err != nil {
931+
t.Errorf("WithResultMethod failed: %v", err)
932+
}
933+
if result == nil {
934+
t.Error("WithResultMethod returned nil result")
935+
}
936+
})
937+
}
938+
`
939+
940+
var SuiteTestWithoutResultCode = `// RunWithoutResultServiceHarness exercises the generated harness against your
941+
// service implementation.
942+
// Call this helper from your test, passing your service implementation.
943+
func RunWithoutResultServiceHarness(t *testing.T, svc withoutresultservice.Service) {
944+
t.Helper()
945+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
946+
defer cancel()
947+
948+
h := withoutResultServicetest.NewHarness(t, svc)
949+
defer h.Close()
950+
951+
td := withoutResultServicetest.NewTestData()
952+
t.Run("WithoutResultMethod", func(t *testing.T) {
953+
err := h.Client.WithoutResultMethod(ctx)
954+
if err != nil {
955+
t.Errorf("WithoutResultMethod failed: %v", err)
956+
}
957+
})
958+
}
959+
`

0 commit comments

Comments
 (0)