Skip to content

Commit 11a756c

Browse files
authored
Fix ScenarioRunner using methods without result generated by testing/codegen (#214)
* Change variable names * Add test for generateScenarios() in testing/codegen * Refactor scenario-runner template * Fix ScenarioRunner using methods without result generated by testing/codegen.
1 parent 5e17793 commit 11a756c

4 files changed

Lines changed: 631 additions & 8 deletions

File tree

testing/codegen/clients_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ func TestGenerateClient(t *testing.T) {
1818
"with-result": {
1919
DSL: testdata.WithResultDSL,
2020
Code: map[string][]string{
21-
"client-methods": {testdata.WithResultCode},
21+
"client-methods": {testdata.ClientMethodsWithResultCode},
2222
},
2323
Path: "gen/with_result_service/with_result_servicetest/client.go",
2424
},
2525
"without-result": {
2626
DSL: testdata.WithoutResultDSL,
2727
Code: map[string][]string{
28-
"client-methods": {testdata.WithoutResultCode},
28+
"client-methods": {testdata.ClientMethodsWithoutResultCode},
2929
},
3030
Path: "gen/without_result_service/without_result_servicetest/client.go",
3131
},

testing/codegen/scenarios_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package codegen
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"goa.design/goa/v3/codegen/service"
8+
httpcodegen "goa.design/goa/v3/http/codegen"
9+
"goa.design/plugins/v3/testing/codegen/testdata"
10+
)
11+
12+
func TestGenerateScenarios(t *testing.T) {
13+
cases := map[string]struct {
14+
DSL func()
15+
Code map[string][]string
16+
Path string
17+
}{
18+
"with-result": {
19+
DSL: testdata.WithResultDSL,
20+
Code: map[string][]string{
21+
"scenario-runner": {testdata.ScenarioRunnerWithResultCode},
22+
},
23+
Path: "gen/with_result_service/with_result_servicetest/scenarios.go",
24+
},
25+
"without-result": {
26+
DSL: testdata.WithoutResultDSL,
27+
Code: map[string][]string{
28+
"scenario-runner": {testdata.ScenarioRunnerWithoutResultCode},
29+
},
30+
Path: "gen/without_result_service/without_result_servicetest/scenarios.go",
31+
},
32+
}
33+
for name, c := range cases {
34+
t.Run(name, func(t *testing.T) {
35+
root := httpcodegen.RunHTTPDSL(t, c.DSL)
36+
services := service.NewServicesData(root)
37+
svc := root.Services[0]
38+
svcData := services.Get(svc.Name)
39+
fs := generateScenarios("", svcData, root, svc)
40+
f := fs[0]
41+
assert.Equal(t, c.Path, f.Path)
42+
for sec, secCode := range c.Code {
43+
testCode(t, f, sec, secCode)
44+
}
45+
})
46+
}
47+
}

testing/codegen/templates/scenario_runner.go.tpl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,8 @@ func (r *ScenarioRunner) executeMethod(ctx context.Context, client *Client, meth
164164
if err := r.mapToStruct(payload, p); err != nil {
165165
return nil, fmt.Errorf("invalid payload for {{ .Name }}: %w", err)
166166
}
167-
return client.{{ .VarName }}(ctx, p)
168-
{{- else }}
169-
return client.{{ .VarName }}(ctx)
170167
{{- end }}
168+
return {{ if not .ResultRef }}nil, {{ end }}client.{{ .VarName }}(ctx{{ if .PayloadRef }}, p{{ end }})
171169
{{- end }}
172170
default:
173171
return nil, fmt.Errorf("unknown method: %s", method)
@@ -329,4 +327,4 @@ func (r *ScenarioRunner) selectTransport(client *Client, transport string) *Clie
329327
default:
330328
return client // auto or unknown - use default
331329
}
332-
}
330+
}

0 commit comments

Comments
 (0)