Skip to content

Commit ac57a35

Browse files
committed
Add test for generateSuiteTopLevel()
in testing/codegen.
1 parent ac97c49 commit ac57a35

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

testing/codegen/suite_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package codegen
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
httpcodegen "goa.design/goa/v3/http/codegen"
8+
"goa.design/plugins/v3/testing/codegen/testdata"
9+
)
10+
11+
func TestGenerateSuiteTopLevel(t *testing.T) {
12+
cases := map[string]struct {
13+
DSL func()
14+
Code map[string][]string
15+
Path string
16+
}{
17+
"with-result": {
18+
DSL: testdata.WithResultDSL,
19+
Code: map[string][]string{
20+
"suite-test": {testdata.SuiteTestWithResultCode},
21+
},
22+
Path: "with_result_service_suite_test.go",
23+
},
24+
"without-result": {
25+
DSL: testdata.WithoutResultDSL,
26+
Code: map[string][]string{
27+
"suite-test": {testdata.SuiteTestWithoutResultCode},
28+
},
29+
Path: "without_result_service_suite_test.go",
30+
},
31+
}
32+
for name, c := range cases {
33+
t.Run(name, func(t *testing.T) {
34+
root := httpcodegen.RunHTTPDSL(t, c.DSL)
35+
svc := root.Services[0]
36+
f := generateSuiteTopLevel("", "", root, svc)
37+
assert.Equal(t, c.Path, f.Path)
38+
for sec, secCode := range c.Code {
39+
testCode(t, f, sec, secCode)
40+
}
41+
})
42+
}
43+
}

testing/codegen/testdata/code.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,3 +912,46 @@ func (r *ScenarioRunner) selectTransport(client *Client, transport string) *Clie
912912
}
913913
}
914914
`
915+
916+
var SuiteTestWithResultCode = `// RunWithResultServiceHarness exercises the generated harness against your
917+
// service implementation.// Call this helper from your test, passing your service implementation.
918+
func RunWithResultServiceHarness(t *testing.T, svc withresultservice.Service) {
919+
t.Helper()
920+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
921+
defer cancel()
922+
923+
h := withResultServicetest.NewHarness(t, svc)
924+
defer h.Close()
925+
926+
td := withResultServicetest.NewTestData()
927+
t.Run("WithResultMethod", func(t *testing.T) {
928+
result, err := h.Client.WithResultMethod(ctx)
929+
if err != nil {
930+
t.Errorf("WithResultMethod failed: %v", err)
931+
}
932+
if result == nil {
933+
t.Error("WithResultMethod returned nil result")
934+
}
935+
})
936+
}
937+
`
938+
939+
var SuiteTestWithoutResultCode = `// RunWithoutResultServiceHarness exercises the generated harness against your
940+
// service implementation.// Call this helper from your test, passing your service implementation.
941+
func RunWithoutResultServiceHarness(t *testing.T, svc withoutresultservice.Service) {
942+
t.Helper()
943+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
944+
defer cancel()
945+
946+
h := withoutResultServicetest.NewHarness(t, svc)
947+
defer h.Close()
948+
949+
td := withoutResultServicetest.NewTestData()
950+
t.Run("WithoutResultMethod", func(t *testing.T) {
951+
result, err := h.Client.WithoutResultMethod(ctx)
952+
if err != nil {
953+
t.Errorf("WithoutResultMethod failed: %v", err)
954+
}
955+
})
956+
}
957+
`

0 commit comments

Comments
 (0)