Skip to content

Commit

Permalink
feat: replace service field with a function
Browse files Browse the repository at this point in the history
This commit replaces the `service` field on the config struct,
with a function for getting the service to be tested.
This is a preparation for upcomming commit.
  • Loading branch information
thall committed Oct 9, 2024
1 parent 19aad90 commit b984d6b
Show file tree
Hide file tree
Showing 29 changed files with 1,998 additions and 1,864 deletions.
4 changes: 3 additions & 1 deletion internal/plugin/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ func (r *resourceGenerator) generateFixture(f *protogen.GeneratedFile) {
})

f.P("type ", resourceTestSuiteConfigName(r.service.Desc, r.resource), " struct {")
f.P("service ", service)
f.P("currParent int")
f.P()

f.P("// Service should return the service that should be tested.")
f.P("// The service will be used for several tests.")
f.P("Service", " func() ", service)
f.P("// Context should return a new context.")
f.P("// The context will be used for several tests.")
f.P("Context", " func() ", context)
Expand Down
6 changes: 5 additions & 1 deletion internal/plugin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ func (s *serviceGenerator) generateTestMethods(f *protogen.GeneratedFile) {
GoName: "T",
GoImportPath: "testing",
})
service := f.QualifiedGoIdent(protogen.GoIdent{
GoName: s.service.GoName + "Server",
GoImportPath: s.service.Methods[0].Input.GoIdent.GoImportPath,
})
serviceFx := serviceTestSuiteName(s.service.Desc)
for _, resource := range s.resources {
resourceFx := resourceTestSuiteConfigName(s.service.Desc, resource)
f.P("func (fx ", serviceFx, ") Test", resourceType(resource), "(ctx ", context, ", options ", resourceFx, ") {")
f.P("fx.T.Run(", strconv.Quote(resourceType(resource)), ", func(t *", testingT, ") {")
f.P("options.Context = func() ", context, " { return ctx }")
f.P("options.service = fx.Server")
f.P("options.Service = func() ", service, " { return fx.Server", "}")
f.P("options.test(t)")
f.P("})")
f.P("}")
Expand Down
14 changes: 7 additions & 7 deletions internal/util/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (m MethodCreate) Generate(f *protogen.GeneratedFile, response, err, assign
f.P("}")
}

f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{")
f.P(response, ", ", err, " ", assign, " fx.Service().", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{") //nolint:lll
if HasParent(m.Resource) {
f.P("Parent: ", m.Parent, ",")
}
Expand Down Expand Up @@ -59,7 +59,7 @@ type MethodGet struct {
}

func (m MethodGet) Generate(f *protogen.GeneratedFile, response, err, assign string) {
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{")
f.P(response, ", ", err, " ", assign, " fx.Service().", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{") //nolint:lll
f.P("Name: ", m.Name, ",")
f.P("})")
}
Expand All @@ -73,7 +73,7 @@ type MethodBatchGet struct {
}

func (m MethodBatchGet) Generate(f *protogen.GeneratedFile, response, err, assign string) {
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{")
f.P(response, ", ", err, " ", assign, " fx.Service().", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{") //nolint:lll
if HasParent(m.Resource) {
f.P("Parent: ", m.Parent, ",")
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func (m MethodUpdate) Generate(f *protogen.GeneratedFile, response, err, assign
}
f.P("msg.Name = ", m.Name)
}
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{")
f.P(response, ", ", err, " ", assign, " fx.Service().", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{") //nolint:lll
if m.Msg != "" {
f.P(upper, ":", m.Msg, ",")
} else {
Expand Down Expand Up @@ -154,7 +154,7 @@ type MethodList struct {
}

func (m MethodList) Generate(f *protogen.GeneratedFile, response, err, assign string) {
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{")
f.P(response, ", ", err, " ", assign, " fx.Service().", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{") //nolint:lll
if HasParent(m.Resource) {
f.P("Parent: ", m.Parent, ",")
}
Expand All @@ -177,7 +177,7 @@ type MethodSearch struct {
}

func (m MethodSearch) Generate(f *protogen.GeneratedFile, response, err, assign string) {
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{")
f.P(response, ", ", err, " ", assign, " fx.Service().", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{") //nolint:lll
if HasParent(m.Resource) {
f.P("Parent: ", m.Parent, ",")
}
Expand All @@ -200,7 +200,7 @@ type MethodDelete struct {
}

func (m MethodDelete) Generate(f *protogen.GeneratedFile, response, err, assign string) {
f.P(response, ", ", err, " ", assign, " fx.service.", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{")
f.P(response, ", ", err, " ", assign, " fx.Service().", m.Method.GoName, "(fx.Context(), &", m.Method.Input.GoIdent, "{") //nolint:lll
if m.Name != "" {
f.P("Name: ", m.Name, ",")
} else {
Expand Down
Loading

0 comments on commit b984d6b

Please sign in to comment.