11package codegen
22
33import (
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