Skip to content

Commit

Permalink
feat(exp/gtest): export TransitionWithComparer
Browse files Browse the repository at this point in the history
  • Loading branch information
bounoable committed Sep 23, 2023
1 parent c370af7 commit 875903b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
3 changes: 3 additions & 0 deletions event/eventbus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func New() event.Bus {
return bus
}

// Close terminates the channel-based event bus. If the bus is already closed,
// it does nothing. Otherwise, it signals to all operations that they should
// stop and cleans up any resources associated with the bus.
func (bus *chanbus) Close() {
select {
case <-bus.done:
Expand Down
14 changes: 10 additions & 4 deletions exp/gtest/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Once() TransitionTestOption {
// event with the provided data when running the Run method on a *testing.T
// instance.
func Transition[EventData comparable](event string, data EventData, opts ...TransitionTestOption) *TransitionTest[EventData] {
return newTransitionTest(event, data, func(a, b EventData) bool { return a == b }, opts...)
return TransitionWithComparer(event, data, func(a, b EventData) bool { return a == b }, opts...)
}

// Comparable is an interface that provides a method for comparing two instances
Expand All @@ -127,14 +127,20 @@ type Comparable[T any] interface {
// TransitionTestOptions can be provided to customize the behavior of the
// TransitionTest.
func TransitionWithEqual[EventData Comparable[EventData]](event string, data EventData, opts ...TransitionTestOption) *TransitionTest[EventData] {
return newTransitionTest(event, data, func(a, b EventData) bool { return a.Equal(b) }, opts...)
return TransitionWithComparer(event, data, func(a, b EventData) bool { return a.Equal(b) }, opts...)
}

func newTransitionTest[EventData any](event string, data EventData, isEqual func(EventData, EventData) bool, opts ...TransitionTestOption) *TransitionTest[EventData] {
// TransitionWithComparer creates a new TransitionTest with the specified event
// name and data, using a custom comparison function to compare event data. It
// is used for testing if an aggregate transitions to the specified event with
// the provided data when running the Run method on a [*testing.T] instance.
// TransitionTestOptions can be provided to customize the behavior of the
// TransitionTest.
func TransitionWithComparer[EventData any](event string, data EventData, compare func(EventData, EventData) bool, opts ...TransitionTestOption) *TransitionTest[EventData] {
test := TransitionTest[EventData]{
Event: event,
Data: data,
isEqual: isEqual,
isEqual: compare,
}

for _, opt := range opts {
Expand Down

0 comments on commit 875903b

Please sign in to comment.