Skip to content
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
43 changes: 43 additions & 0 deletions testing/codegen/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package codegen

import (
"testing"

"github.com/stretchr/testify/assert"
httpcodegen "goa.design/goa/v3/http/codegen"
"goa.design/plugins/v3/testing/codegen/testdata"
)

func TestGenerateSuiteTopLevel(t *testing.T) {
cases := map[string]struct {
DSL func()
Code map[string][]string
Path string
}{
"with-result": {
DSL: testdata.WithResultDSL,
Code: map[string][]string{
"suite-test": {testdata.SuiteTestWithResultCode},
},
Path: "with_result_service_suite_test.go",
},
"without-result": {
DSL: testdata.WithoutResultDSL,
Code: map[string][]string{
"suite-test": {testdata.SuiteTestWithoutResultCode},
},
Path: "without_result_service_suite_test.go",
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
root := httpcodegen.RunHTTPDSL(t, c.DSL)
svc := root.Services[0]
f := generateSuiteTopLevel("", "", root, svc)
assert.Equal(t, c.Path, f.Path)
for sec, secCode := range c.Code {
testCode(t, f, sec, secCode)
}
})
}
}
12 changes: 4 additions & 8 deletions testing/codegen/templates/suite_test.go.tpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{- printf "Run%sHarness exercises the generated harness against your service implementation." .Service.Name | comment }}
{{- printf "Call this helper from your test, passing your service implementation." | comment }}
{{ printf "Run%sHarness exercises the generated harness against your service implementation." .Service.Name | comment }}
{{ printf "Call this helper from your test, passing your service implementation." | comment }}
func Run{{ .Service.StructName }}Harness(t *testing.T, svc {{ .Service.PkgName }}.Service) {
t.Helper()
{{- if .UseCtx }}
Expand All @@ -15,11 +15,7 @@ func Run{{ .Service.StructName }}Harness(t *testing.T, svc {{ .Service.PkgName }
{{- range .NonStream }}
{{- $m := . }}
t.Run("{{ $m.Method.Name }}", func(t *testing.T) {
{{- if $m.Method.PayloadEx }}
result, err := h.Client.{{ $m.Method.VarName }}({{ if $.UseCtx }}ctx{{ else }}context.Background(){{ end }}, td.Valid{{ goify $m.Method.Name true }}Payload())
{{- else }}
result, err := h.Client.{{ $m.Method.VarName }}({{ if $.UseCtx }}ctx{{ else }}context.Background(){{ end }})
{{- end }}
{{ 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 }})
if err != nil {
t.Errorf("{{ $m.Method.Name }} failed: %v", err)
}
Expand Down Expand Up @@ -116,4 +112,4 @@ func Run{{ .Service.StructName }}Harness(t *testing.T, svc {{ .Service.PkgName }
{{- end }}
})
{{- end }}
}
}
45 changes: 45 additions & 0 deletions testing/codegen/testdata/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -912,3 +912,48 @@ func (r *ScenarioRunner) selectTransport(client *Client, transport string) *Clie
}
}
`

var SuiteTestWithResultCode = `// RunWithResultServiceHarness exercises the generated harness against your
// service implementation.
// Call this helper from your test, passing your service implementation.
func RunWithResultServiceHarness(t *testing.T, svc withresultservice.Service) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

h := withResultServicetest.NewHarness(t, svc)
defer h.Close()

td := withResultServicetest.NewTestData()
Comment thread
tchssk marked this conversation as resolved.
t.Run("WithResultMethod", func(t *testing.T) {
result, err := h.Client.WithResultMethod(ctx)
if err != nil {
t.Errorf("WithResultMethod failed: %v", err)
}
if result == nil {
t.Error("WithResultMethod returned nil result")
}
})
}
`

var SuiteTestWithoutResultCode = `// RunWithoutResultServiceHarness exercises the generated harness against your
// service implementation.
// Call this helper from your test, passing your service implementation.
func RunWithoutResultServiceHarness(t *testing.T, svc withoutresultservice.Service) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

h := withoutResultServicetest.NewHarness(t, svc)
defer h.Close()

td := withoutResultServicetest.NewTestData()
Comment thread
tchssk marked this conversation as resolved.
t.Run("WithoutResultMethod", func(t *testing.T) {
err := h.Client.WithoutResultMethod(ctx)
if err != nil {
t.Errorf("WithoutResultMethod failed: %v", err)
}
})
}
`
Loading