Skip to content

Commit

Permalink
fix: more specific names for test suite structs
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwenn committed Aug 21, 2021
1 parent 9518608 commit 82a1cfe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
9 changes: 9 additions & 0 deletions internal/plugin/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ package plugin
import (
"go.einride.tech/aip/reflect/aipreflect"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/reflect/protoreflect"
)

func resourceType(r *annotations.ResourceDescriptor) string {
return aipreflect.ResourceType(r.GetType()).Type()
}

func serviceTestSuiteName(service protoreflect.ServiceDescriptor) string {
return string(service.Name()) + "TestSuite"
}

func resourceTestSuiteConfigName(resource *annotations.ResourceDescriptor) string {
return resourceType(resource) + "TestSuiteConfig"
}
12 changes: 6 additions & 6 deletions internal/plugin/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (r *resourceGenerator) generateFixture(f *protogen.GeneratedFile) {
GoImportPath: r.service.Methods[0].Input.GoIdent.GoImportPath,
})

f.P("type ", resourceType(r.resource), " struct {")
f.P("type ", resourceTestSuiteConfigName(r.resource), " struct {")
f.P("ctx ", context)
f.P("service ", service)
f.P("currParent int")
Expand Down Expand Up @@ -88,7 +88,7 @@ func (r *resourceGenerator) generateTestMethod(f *protogen.GeneratedFile) {
GoImportPath: "testing",
})

f.P("func (fx *", resourceType(r.resource), ") test(t *", testingT, ") {")
f.P("func (fx *", resourceTestSuiteConfigName(r.resource), ") test(t *", testingT, ") {")
scope := suite.Scope{
Service: r.service,
Resource: r.resource,
Expand Down Expand Up @@ -117,7 +117,7 @@ func (r *resourceGenerator) generateTestCases(f *protogen.GeneratedFile) error {
if !s.Enabled(scope) {
continue
}
f.P("func (fx *", resourceType(r.resource), ") test", s.Name, "(t *", testingT, ") {")
f.P("func (fx *", resourceTestSuiteConfigName(r.resource), ") test", s.Name, "(t *", testingT, ") {")
for _, t := range s.Tests {
if err := r.generateTestCase(f, t, scope); err != nil {
return err
Expand Down Expand Up @@ -173,7 +173,7 @@ func (r *resourceGenerator) generateSkip(f *protogen.GeneratedFile) {
GoName: "Contains",
GoImportPath: "strings",
})
f.P("func (fx *", resourceType(r.resource), ") maybeSkip(t *", testingT, ") {")
f.P("func (fx *", resourceTestSuiteConfigName(r.resource), ") maybeSkip(t *", testingT, ") {")
f.P("for _, skip := range fx.Skip {")
f.P("if ", stringsContains, "(t.Name(), skip) {")
f.P("t.Skip(\"skipped because of .Skip\")")
Expand All @@ -191,7 +191,7 @@ func (r *resourceGenerator) generateParentMethods(f *protogen.GeneratedFile) {
GoName: "T",
GoImportPath: "testing",
})
f.P("func (fx *", resourceType(r.resource), ") nextParent(t *", testingT, ", pristine bool) string {")
f.P("func (fx *", resourceTestSuiteConfigName(r.resource), ") nextParent(t *", testingT, ", pristine bool) string {")
f.P("if pristine {")
f.P("fx.currParent++")
f.P("}")
Expand All @@ -201,7 +201,7 @@ func (r *resourceGenerator) generateParentMethods(f *protogen.GeneratedFile) {
f.P("return fx.Parents[fx.currParent]")
f.P("}")
f.P()
f.P("func (fx *", resourceType(r.resource), ") peekNextParent(t *", testingT, ") string {")
f.P("func (fx *", resourceTestSuiteConfigName(r.resource), ") peekNextParent(t *", testingT, ") string {")
f.P("next := fx.currParent + 1")
f.P("if next >= len(fx.Parents) {")
f.P("t.Fatal(\"need at least\", next +1, \"parents\")")
Expand Down
6 changes: 3 additions & 3 deletions internal/plugin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *serviceGenerator) generateFixture(f *protogen.GeneratedFile) {
GoImportPath: s.service.Methods[0].Input.GoIdent.GoImportPath,
})

f.P("type ", s.service.GoName, " struct {")
f.P("type ", serviceTestSuiteName(s.service.Desc), " struct {")
f.P("T *", testingT)
f.P("// Server to test.")
f.P("Server ", service)
Expand All @@ -60,9 +60,9 @@ func (s *serviceGenerator) generateTestMethods(f *protogen.GeneratedFile) {
GoName: "T",
GoImportPath: "testing",
})
serviceFx := s.service.GoName
serviceFx := serviceTestSuiteName(s.service.Desc)
for _, resource := range s.resources {
resourceFx := resourceType(resource)
resourceFx := resourceTestSuiteConfigName(resource)
f.P("func (fx ", serviceFx, ") Test", resourceFx, "(ctx ", context, ", options ", resourceFx, ") {")
f.P("fx.T.Run(", strconv.Quote(resourceType(resource)), ", func(t *", testingT, ") {")
f.P("options.ctx = ctx")
Expand Down
40 changes: 20 additions & 20 deletions proto/gen/einride/example/freight/v1/testing/freight_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 82a1cfe

Please sign in to comment.