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
10 changes: 4 additions & 6 deletions testing/codegen/scenarios.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package codegen

import (
"fmt"
"maps"
"os"
"path/filepath"
"slices"

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

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

// Convert set to sorted list
for transport := range transportSet {
md.Transports = append(md.Transports, transport)
}
md.Transports = slices.Sorted(maps.Keys(transportSet))

data.Methods = append(data.Methods, md)
}
Expand Down
48 changes: 48 additions & 0 deletions testing/codegen/scenarios_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package codegen

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"goa.design/goa/v3/codegen"
"goa.design/goa/v3/codegen/service"
httpcodegen "goa.design/goa/v3/http/codegen"
Expand All @@ -19,20 +21,23 @@ func TestGenerateScenarios(t *testing.T) {
"with-payload": {
DSL: testdata.WithPayloadDSL,
Code: map[string][]string{
"scenario-types": {testdata.ScenarioTypesWithPayloadCode},
"scenario-runner": {testdata.ScenarioRunnerWithPayloadCode},
},
Path: "gen/with_payload_service/with_payload_servicetest/scenarios.go",
},
"with-result": {
DSL: testdata.WithResultDSL,
Code: map[string][]string{
"scenario-types": {testdata.ScenarioTypesWithResultCode},
"scenario-runner": {testdata.ScenarioRunnerWithResultCode},
},
Path: "gen/with_result_service/with_result_servicetest/scenarios.go",
},
"without-payload-result": {
DSL: testdata.WithoutPayloadResultDSL,
Code: map[string][]string{
"scenario-types": {testdata.ScenarioTypesWithoutPayloadResultCode},
"scenario-runner": {testdata.ScenarioRunnerWithoutPayloadResultCode},
},
Path: "gen/without_payload_result_service/without_payload_result_servicetest/scenarios.go",
Expand Down Expand Up @@ -77,3 +82,46 @@ func TestGenerateScenarios_ArrayResultTypeAssertion(t *testing.T) {
assert.NotContains(t, code, svcData.PkgName+".[]")
assert.NotContains(t, code, "*"+svcData.PkgName+".[]")
}

func TestGenerateExampleScenarios(t *testing.T) {
cases := map[string]struct {
DSL func()
Code map[string][]string
}{
"with-payload": {
DSL: testdata.WithPayloadDSL,
Code: map[string][]string{
"example-scenarios": {testdata.ExampleScenariosWithPayloadCode},
},
},
"with-result": {
DSL: testdata.WithResultDSL,
Code: map[string][]string{
"example-scenarios": {testdata.ExampleScenariosWithResultCode},
},
},
"without-payload-result": {
DSL: testdata.WithoutPayloadResultDSL,
Code: map[string][]string{
"example-scenarios": {testdata.ExampleScenariosWithoutPayloadResultCode},
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
root := httpcodegen.RunHTTPDSL(t, c.DSL)
svc := root.Services[0]
f := generateExampleScenarios("", root, svc)
assert.Equal(t, "scenarios.yaml", f.Path)
for sec, secCode := range c.Code {
sections := f.Section(sec)
require.Len(t, sections, len(secCode))
for i, c := range secCode {
var buf bytes.Buffer
assert.NoError(t, sections[i].Write(&buf))
assert.Equal(t, c, buf.String())
}
}
})
}
}
Loading
Loading