Skip to content

Commit 83f0216

Browse files
committed
fix(gtest): fix TransitionTest when matching same event multiple times with different event data
1 parent 3fcc13e commit 83f0216

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

exp/gtest/aggregate.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,30 +162,28 @@ func Signal(event string, opts ...TransitionTestOption) *TransitionTest[any] {
162162
func (test *TransitionTest[EventData]) Run(t *testing.T, a aggregate.Aggregate) {
163163
t.Helper()
164164

165+
wantMatches := test.MatchCount
166+
if wantMatches == 0 {
167+
wantMatches = 1
168+
}
169+
165170
var matches uint
166171
for _, evt := range a.AggregateChanges() {
167172
if evt.Name() != test.Event {
168173
continue
169174
}
170175

171-
if test.MatchCount == 0 {
172-
if err := test.testEquality(evt); err != nil {
173-
t.Errorf("Aggregate %q should transition to %q with %T; %s", pick.AggregateName(a), test.Event, test.Data, err)
174-
}
175-
return
176-
}
177-
178176
if test.testEquality(evt) == nil {
179177
matches++
180178
}
181179
}
182180

183-
if test.MatchCount == 0 {
184-
t.Errorf("Aggregate %q should transition to %q with %T", pick.AggregateName(a), test.Event, test.Data)
185-
return
186-
}
181+
if matches != wantMatches {
182+
if wantMatches == 1 {
183+
t.Errorf("Aggregate %q should transition to %q with %T", pick.AggregateName(a), test.Event, test.Data)
184+
return
185+
}
187186

188-
if matches != test.MatchCount {
189187
t.Errorf("Aggregate %q should transition to %q with %T %d times; got %d", pick.AggregateName(a), test.Event, test.Data, test.MatchCount, matches)
190188
}
191189
}

exp/gtest/aggregate_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ func TestTransition(t *testing.T) {
2323
gtest.Transition("foo", d).Run(t, foo)
2424
}
2525

26+
func TestTransition_sameEventNameOtherData(t *testing.T) {
27+
type data struct {
28+
Foo string
29+
Bar bool
30+
}
31+
32+
foo := aggregate.New("foo", uuid.New())
33+
34+
d1 := data{Foo: "foo", Bar: false}
35+
d2 := data{Foo: "foo", Bar: true}
36+
37+
aggregate.Next(foo, "foo", d1)
38+
aggregate.Next(foo, "foo", d2)
39+
40+
gtest.Transition("foo", d1).Run(t, foo)
41+
gtest.Transition("foo", d2).Run(t, foo)
42+
}
43+
2644
type comparableData struct {
2745
Foo string
2846
Bar int

0 commit comments

Comments
 (0)