Skip to content

Commit 1a2973d

Browse files
authored
Sort transports generated by testing plugin (#253)
* Add test cases about scenario-types * Sort transports generated by testing plugin * Add test for generateExampleScenarios() in testing/codegen * Sort valid transports generated by testing plugin
1 parent fb9f921 commit 1a2973d

3 files changed

Lines changed: 437 additions & 6 deletions

File tree

testing/codegen/scenarios.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package codegen
22

33
import (
44
"fmt"
5+
"maps"
56
"os"
67
"path/filepath"
8+
"slices"
79

810
"goa.design/goa/v3/codegen"
911
"goa.design/goa/v3/codegen/service"
@@ -173,9 +175,7 @@ func buildScenariosData(svcData *service.Data, root *expr.RootExpr, svc *expr.Se
173175
transportSet["jsonrpc-sse"] = true
174176
transportSet["jsonrpc-ws"] = true
175177
}
176-
for t := range transportSet {
177-
data.ValidTransports = append(data.ValidTransports, t)
178-
}
178+
data.ValidTransports = slices.Sorted(maps.Keys(transportSet))
179179

180180
// Build method data with available transports
181181
for i, m := range svc.Methods {
@@ -217,9 +217,7 @@ func buildScenariosData(svcData *service.Data, root *expr.RootExpr, svc *expr.Se
217217
}
218218

219219
// Convert set to sorted list
220-
for transport := range transportSet {
221-
md.Transports = append(md.Transports, transport)
222-
}
220+
md.Transports = slices.Sorted(maps.Keys(transportSet))
223221

224222
data.Methods = append(data.Methods, md)
225223
}

testing/codegen/scenarios_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package codegen
22

33
import (
4+
"bytes"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
79
"goa.design/goa/v3/codegen"
810
"goa.design/goa/v3/codegen/service"
911
httpcodegen "goa.design/goa/v3/http/codegen"
@@ -19,20 +21,23 @@ func TestGenerateScenarios(t *testing.T) {
1921
"with-payload": {
2022
DSL: testdata.WithPayloadDSL,
2123
Code: map[string][]string{
24+
"scenario-types": {testdata.ScenarioTypesWithPayloadCode},
2225
"scenario-runner": {testdata.ScenarioRunnerWithPayloadCode},
2326
},
2427
Path: "gen/with_payload_service/with_payload_servicetest/scenarios.go",
2528
},
2629
"with-result": {
2730
DSL: testdata.WithResultDSL,
2831
Code: map[string][]string{
32+
"scenario-types": {testdata.ScenarioTypesWithResultCode},
2933
"scenario-runner": {testdata.ScenarioRunnerWithResultCode},
3034
},
3135
Path: "gen/with_result_service/with_result_servicetest/scenarios.go",
3236
},
3337
"without-payload-result": {
3438
DSL: testdata.WithoutPayloadResultDSL,
3539
Code: map[string][]string{
40+
"scenario-types": {testdata.ScenarioTypesWithoutPayloadResultCode},
3641
"scenario-runner": {testdata.ScenarioRunnerWithoutPayloadResultCode},
3742
},
3843
Path: "gen/without_payload_result_service/without_payload_result_servicetest/scenarios.go",
@@ -77,3 +82,46 @@ func TestGenerateScenarios_ArrayResultTypeAssertion(t *testing.T) {
7782
assert.NotContains(t, code, svcData.PkgName+".[]")
7883
assert.NotContains(t, code, "*"+svcData.PkgName+".[]")
7984
}
85+
86+
func TestGenerateExampleScenarios(t *testing.T) {
87+
cases := map[string]struct {
88+
DSL func()
89+
Code map[string][]string
90+
}{
91+
"with-payload": {
92+
DSL: testdata.WithPayloadDSL,
93+
Code: map[string][]string{
94+
"example-scenarios": {testdata.ExampleScenariosWithPayloadCode},
95+
},
96+
},
97+
"with-result": {
98+
DSL: testdata.WithResultDSL,
99+
Code: map[string][]string{
100+
"example-scenarios": {testdata.ExampleScenariosWithResultCode},
101+
},
102+
},
103+
"without-payload-result": {
104+
DSL: testdata.WithoutPayloadResultDSL,
105+
Code: map[string][]string{
106+
"example-scenarios": {testdata.ExampleScenariosWithoutPayloadResultCode},
107+
},
108+
},
109+
}
110+
for name, c := range cases {
111+
t.Run(name, func(t *testing.T) {
112+
root := httpcodegen.RunHTTPDSL(t, c.DSL)
113+
svc := root.Services[0]
114+
f := generateExampleScenarios("", root, svc)
115+
assert.Equal(t, "scenarios.yaml", f.Path)
116+
for sec, secCode := range c.Code {
117+
sections := f.Section(sec)
118+
require.Len(t, sections, len(secCode))
119+
for i, c := range secCode {
120+
var buf bytes.Buffer
121+
assert.NoError(t, sections[i].Write(&buf))
122+
assert.Equal(t, c, buf.String())
123+
}
124+
}
125+
})
126+
}
127+
}

0 commit comments

Comments
 (0)