Skip to content

Commit

Permalink
Move panicx.Location into its own package.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Nov 25, 2020
1 parent ac6c3ce commit 1c97f4e
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 61 deletions.
5 changes: 3 additions & 2 deletions engine/controller/aggregate/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/dogmatiq/testkit/engine/envelope"
"github.com/dogmatiq/testkit/engine/fact"
"github.com/dogmatiq/testkit/engine/panicx"
"github.com/dogmatiq/testkit/internal/location"
)

// Controller is an implementation of engine.Controller for
Expand Down Expand Up @@ -65,7 +66,7 @@ func (c *Controller) Handle(
Implementation: c.Config.Handler(),
Message: env.Message,
Description: fmt.Sprintf("routed a command of type %T to an empty instance ID", env.Message),
Location: panicx.LocationOfMethod(c.Config.Handler(), "RouteCommandToInstance"),
Location: location.OfMethod(c.Config.Handler(), "RouteCommandToInstance"),
})
}

Expand All @@ -79,7 +80,7 @@ func (c *Controller) Handle(
Implementation: c.Config.Handler(),
Message: env.Message,
Description: "returned a nil AggregateRoot",
Location: panicx.LocationOfMethod(c.Config.Handler(), "New"),
Location: location.OfMethod(c.Config.Handler(), "New"),
})
}

Expand Down
5 changes: 3 additions & 2 deletions engine/controller/aggregate/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/dogmatiq/testkit/engine/envelope"
"github.com/dogmatiq/testkit/engine/fact"
"github.com/dogmatiq/testkit/engine/panicx"
"github.com/dogmatiq/testkit/internal/location"
)

// scope is an implementation of dogma.AggregateCommandScope.
Expand Down Expand Up @@ -53,7 +54,7 @@ func (s *scope) RecordEvent(m dogma.Message) {
Implementation: s.config.Handler(),
Message: s.command.Message,
Description: fmt.Sprintf("recorded an event of type %T, which is not produced by this handler", m),
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand All @@ -65,7 +66,7 @@ func (s *scope) RecordEvent(m dogma.Message) {
Implementation: s.config.Handler(),
Message: s.command.Message,
Description: fmt.Sprintf("recorded an invalid %T event: %s", m, err),
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand Down
5 changes: 3 additions & 2 deletions engine/controller/integration/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/dogmatiq/testkit/engine/envelope"
"github.com/dogmatiq/testkit/engine/fact"
"github.com/dogmatiq/testkit/engine/panicx"
"github.com/dogmatiq/testkit/internal/location"
)

// scope is an implementation of dogma.IntegrationCommandScope.
Expand All @@ -30,7 +31,7 @@ func (s *scope) RecordEvent(m dogma.Message) {
Implementation: s.config.Handler(),
Message: s.command.Message,
Description: fmt.Sprintf("recorded an event of type %T, which is not produced by this handler", m),
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand All @@ -42,7 +43,7 @@ func (s *scope) RecordEvent(m dogma.Message) {
Implementation: s.config.Handler(),
Message: s.command.Message,
Description: fmt.Sprintf("recorded an invalid %T event: %s", m, err),
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand Down
5 changes: 3 additions & 2 deletions engine/controller/process/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/dogmatiq/testkit/engine/envelope"
"github.com/dogmatiq/testkit/engine/fact"
"github.com/dogmatiq/testkit/engine/panicx"
"github.com/dogmatiq/testkit/internal/location"
)

// Controller is an implementation of engine.Controller for
Expand Down Expand Up @@ -114,7 +115,7 @@ func (c *Controller) Handle(
Implementation: c.Config.Handler(),
Message: env.Message,
Description: "returned a nil ProcessRoot",
Location: panicx.LocationOfMethod(c.Config.Handler(), "New"),
Location: location.OfMethod(c.Config.Handler(), "New"),
})
}
}
Expand Down Expand Up @@ -203,7 +204,7 @@ func (c *Controller) routeEvent(
Implementation: handler,
Message: env.Message,
Description: fmt.Sprintf("routed an event of type %T to an empty instance ID", env.Message),
Location: panicx.LocationOfMethod(c.Config.Handler(), "RouteEventToInstance"),
Location: location.OfMethod(c.Config.Handler(), "RouteEventToInstance"),
})
}

Expand Down
17 changes: 9 additions & 8 deletions engine/controller/process/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/dogmatiq/testkit/engine/envelope"
"github.com/dogmatiq/testkit/engine/fact"
"github.com/dogmatiq/testkit/engine/panicx"
"github.com/dogmatiq/testkit/internal/location"
)

// scope is an implementation of dogma.ProcessEventScope and
Expand Down Expand Up @@ -63,7 +64,7 @@ func (s *scope) End() {
Message: s.env.Message,
Implementation: s.config.Handler(),
Description: "ended a process instance that has not begun",
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand All @@ -88,7 +89,7 @@ func (s *scope) Root() dogma.ProcessRoot {
Message: s.env.Message,
Implementation: s.config.Handler(),
Description: "accessed the root of a process instance that has not begun",
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand All @@ -104,7 +105,7 @@ func (s *scope) ExecuteCommand(m dogma.Message) {
Implementation: s.config.Handler(),
Message: s.env.Message,
Description: fmt.Sprintf("executed a command of type %T, which is not produced by this handler", m),
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand All @@ -116,7 +117,7 @@ func (s *scope) ExecuteCommand(m dogma.Message) {
Message: s.env.Message,
Implementation: s.config.Handler(),
Description: fmt.Sprintf("executed an invalid %T command: %s", m, err),
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand All @@ -128,7 +129,7 @@ func (s *scope) ExecuteCommand(m dogma.Message) {
Message: s.env.Message,
Implementation: s.config.Handler(),
Description: fmt.Sprintf("executed a command of type %T on a process instance that has not begun", m),
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand Down Expand Up @@ -167,7 +168,7 @@ func (s *scope) ScheduleTimeout(m dogma.Message, t time.Time) {
Implementation: s.config.Handler(),
Message: s.env.Message,
Description: fmt.Sprintf("scheduled a timeout of type %T, which is not produced by this handler", m),
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand All @@ -179,7 +180,7 @@ func (s *scope) ScheduleTimeout(m dogma.Message, t time.Time) {
Message: s.env.Message,
Implementation: s.config.Handler(),
Description: fmt.Sprintf("scheduled an invalid %T timeout: %s", m, err),
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand All @@ -191,7 +192,7 @@ func (s *scope) ScheduleTimeout(m dogma.Message, t time.Time) {
Message: s.env.Message,
Implementation: s.config.Handler(),
Description: fmt.Sprintf("scheduled a timeout of type %T on a process instance that has not begun", m),
Location: panicx.LocationOfCall(),
Location: location.OfCall(),
})
}

Expand Down
11 changes: 2 additions & 9 deletions engine/panicx/linenumber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package panicx_test

import (
"github.com/dogmatiq/dogma"
. "github.com/dogmatiq/testkit/engine/panicx"
// import padding
// import padding
// import padding
// import padding
Expand Down Expand Up @@ -47,11 +47,4 @@ import (
// import padding
)

func doNothing() {}
func panicWithUnexpectedMessage() { panic(dogma.UnexpectedMessage) }
func locationOfCallLayer1() Location { return LocationOfCall() }
func locationOfCallLayer2() Location { return locationOfCallLayer1() }

type locationOfMethodT struct{}

func (locationOfMethodT) Method() {}
func doPanic() { panic(dogma.UnexpectedMessage) }
3 changes: 2 additions & 1 deletion engine/panicx/unexpectedbehavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/dogmatiq/configkit"
"github.com/dogmatiq/dogma"
"github.com/dogmatiq/testkit/internal/location"
)

// UnexpectedBehavior is a panic value that occurs when a handler exhibits some
Expand Down Expand Up @@ -33,7 +34,7 @@ type UnexpectedBehavior struct {

// Location is the engine's best attempt at pinpointing the location of the
// unexpected behavior.
Location Location
Location location.Location
}

func (x UnexpectedBehavior) String() string {
Expand Down
5 changes: 3 additions & 2 deletions engine/panicx/unexpectedmessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/dogmatiq/configkit"
"github.com/dogmatiq/dogma"
"github.com/dogmatiq/testkit/internal/location"
)

// UnexpectedMessage is a panic value that provides more context when a handler
Expand All @@ -28,7 +29,7 @@ type UnexpectedMessage struct {
Message dogma.Message

// PanicLocation is the location of the function that panicked, if known.
PanicLocation Location
PanicLocation location.Location
}

func (x UnexpectedMessage) String() string {
Expand Down Expand Up @@ -66,7 +67,7 @@ func EnrichUnexpectedMessage(
Method: method,
Implementation: impl,
Message: m,
PanicLocation: LocationOfPanic(),
PanicLocation: location.OfPanic(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions engine/panicx/unexpectedmessage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ var _ = Describe("func EnrichUnexpectedMessage()", func() {
"<method>",
config.Handler(),
MessageA1,
panicWithUnexpectedMessage,
doPanic,
)
}).To(PanicWith(
MatchAllFields(
Expand All @@ -108,9 +108,9 @@ var _ = Describe("func EnrichUnexpectedMessage()", func() {
"Message": Equal(MessageA1),
"PanicLocation": MatchAllFields(
Fields{
"Func": Equal("github.com/dogmatiq/testkit/engine/panicx_test.panicWithUnexpectedMessage"),
"Func": Equal("github.com/dogmatiq/testkit/engine/panicx_test.doPanic"),
"File": HaveSuffix("/engine/panicx/linenumber_test.go"),
"Line": Equal(51),
"Line": Equal(50),
},
),
},
Expand Down
3 changes: 3 additions & 0 deletions internal/location/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package location contains utilities for obtaining the function, file and line
// number of various Go types and values.
package location
15 changes: 15 additions & 0 deletions internal/location/ginkgo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package location_test

import (
"reflect"
"testing"

"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
)

func TestSuite(t *testing.T) {
type tag struct{}
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, reflect.TypeOf(tag{}).PkgPath())
}
57 changes: 57 additions & 0 deletions internal/location/linenumber_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package location_test

// This file contains definitions used within tests that check for specific line
// numbers. To minimize test disruption edit this file as infrequently as
// possible.
//
// New additions should always be made at the end so that the line numbers of
// existing definitions do not change. The padding below can be removed as
// imports statements added.

import (
. "github.com/dogmatiq/testkit/internal/location"
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
// import padding
)

func doNothing() {}
func doPanic() { panic("<panic>") }
func ofCallLayer1() Location { return OfCall() }
func ofCallLayer2() Location { return ofCallLayer1() }

type ofMethodT struct{}

func (ofMethodT) Method() {}
Loading

0 comments on commit 1c97f4e

Please sign in to comment.