Skip to content

Commit

Permalink
Testing: Add FixtureToDataHandlerWithErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Nov 3, 2024
1 parent 8e1854f commit bd236e2
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions internal/testing/exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,44 @@ func MockWsInstance[T any, PT interface {
return e
}

// FixtureToDataHandler squirts the contents of a file to a reader function (probably e.wsHandleData)
type FixtureError struct {
Err error
Msg []byte
}

// FixtureToDataHandler squirts the contents of a file to a reader function (probably e.wsHandleData) and asserts no errors are returned
func FixtureToDataHandler(tb testing.TB, fixturePath string, reader func([]byte) error) {
tb.Helper()

for _, e := range FixtureToDataHandlerWithErrors(tb, fixturePath, reader) {
assert.NoErrorf(tb, e.Err, "Should not error handling message:\n%s", e.Msg)
}
}

// FixtureToDataHandlerWithErrors squirts the contents of a file to a reader function (probably e.wsHandleData) and returns handler errors
// Any errors setting up the fixture will fail tests
func FixtureToDataHandlerWithErrors(tb testing.TB, fixturePath string, reader func([]byte) error) []FixtureError {
tb.Helper()

fixture, err := os.Open(fixturePath)
assert.NoError(tb, err, "Opening fixture '%s' should not error", fixturePath)
require.NoError(tb, err, "Opening fixture '%s' should not error", fixturePath)
defer func() {
assert.NoError(tb, fixture.Close(), "Closing the fixture file should not error")
}()

errs := []FixtureError{}
s := bufio.NewScanner(fixture)
for s.Scan() {
msg := s.Bytes()
err := reader(msg)
assert.NoErrorf(tb, err, "Fixture message should not error:\n%s", msg)
if err := reader(msg); err != nil {
errs = append(errs, FixtureError{
Err: err,
Msg: msg,
})
}
}
assert.NoError(tb, s.Err(), "Fixture Scanner should not error")
return errs
}

var setupWsMutex sync.Mutex
Expand Down

0 comments on commit bd236e2

Please sign in to comment.