Report all errors when trying multiple matchers

This commit is contained in:
David Robertson 2023-10-19 14:21:51 +01:00
parent e7e78a6754
commit 5e9b594826
No known key found for this signature in database
GPG Key ID: 903ECE108A39DEDD

View File

@ -276,11 +276,18 @@ func MatchRoomSubscription(roomID string, matchers ...RoomMatcher) RespMatcher {
if !ok {
return fmt.Errorf("MatchRoomSubscription[%s]: want sub but it was missing", roomID)
}
errs := make([]error, 0, len(matchers))
for _, m := range matchers {
if err := m(room); err != nil {
return fmt.Errorf("MatchRoomSubscription[%s]: %s", roomID, err)
errs = append(errs, err)
}
}
if len(errs) > 1 {
return fmt.Errorf("MatchRoomSubscription[%s]: %d errors:\n%w", roomID, len(errs), errors.Join(errs...))
} else if len(errs) == 1 {
return fmt.Errorf("MatchRoomSubscription[%s]: %w", roomID, errs[0])
}
return nil
}
}
@ -755,13 +762,22 @@ const AnsiResetForeground = "\x1b[39m"
func MatchResponse(t *testing.T, res *sync3.Response, matchers ...RespMatcher) {
t.Helper()
errs := []error{}
for _, m := range matchers {
err := m(res)
if err != nil {
b, _ := json.MarshalIndent(res, "", " ")
t.Errorf("%vMatchResponse: %s\n%s%v", AnsiRedForeground, err, string(b), AnsiResetForeground)
errs = append(errs, fmt.Errorf("%v%s%v", AnsiRedForeground, err, AnsiResetForeground))
}
}
if len(errs) > 0 {
if len(errs) == 1 {
t.Errorf("%vMatchResponse: %s", AnsiRedForeground, errs[0])
} else {
t.Errorf("%vMatchResponse: there were %d errors\n%s", AnsiRedForeground, len(errs), errors.Join(errs...))
}
LogResponse(t)(res)
}
}
func CheckRoom(r sync3.Room, matchers ...RoomMatcher) error {