Skip to content

Commit

Permalink
Merge pull request #37 from SimonRichardson/dependency-testing-update
Browse files Browse the repository at this point in the history
#37

The new changes ensure that the context for testing is correctly used. This removes the Abort from the context and renames it to Getter.
  • Loading branch information
jujubot authored Jan 12, 2024
2 parents 25c552c + b2526cb commit 6268855
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions dependency/testing/stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,35 +50,29 @@ func NewStubResources(raw map[string]interface{}) StubResources {
// StubResources defines the complete behaviour of a StubGetResource func.
type StubResources map[string]StubResource

// Context returns a dependency.Context that never aborts, backed by resources.
func (resources StubResources) Context() dependency.Getter {
return &Context{
// Getter returns a dependency.Getter that never aborts, backed by resources.
func (resources StubResources) Getter() dependency.Getter {
return &Getter{
resources: resources,
}
}

// StubContext returns a Context backed by abort and resources derived from raw.
func StubContext(abort <-chan struct{}, raw map[string]interface{}) *Context {
return &Context{
abort: abort,
// StubGetter returns a Getter.
func StubGetter(raw map[string]interface{}) *Getter {
return &Getter{
resources: NewStubResources(raw),
}
}

// Context implements dependency.Context for convenient testing of dependency.StartFuncs.
type Context struct {
abort <-chan struct{}
// Getter implements dependency.Getter for convenient testing of
// dependency.StartFuncs.
type Getter struct {
resources StubResources
}

// Abort is part of the dependency.Context interface.
func (ctx *Context) Abort() <-chan struct{} {
return ctx.abort
}

// Get is part of the dependency.Context interface.
func (ctx *Context) Get(name string, outPtr interface{}) error {
resource, found := ctx.resources[name]
// Get is part of the dependency.Getter interface.
func (g *Getter) Get(name string, outPtr interface{}) error {
resource, found := g.resources[name]
if !found {
return errors.Errorf("unexpected resource name: %s", name)
} else if resource.Error != nil {
Expand Down

0 comments on commit 6268855

Please sign in to comment.