Skip to content

Instantly share code, notes, and snippets.

@dolanor
Created December 6, 2021 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolanor/405e85951af3877ef0913a0e267b10e8 to your computer and use it in GitHub Desktop.
Save dolanor/405e85951af3877ef0913a0e267b10e8 to your computer and use it in GitHub Desktop.
package diffpanicwriter
import (
"testing"
"io"
"errors"
)
type DiffPanicWriter struct {
pos int
golden []byte
out io.Writer
t *testing.T
}
func (w *DiffPanicWriter) Write(b []byte) (int, error) {
w.t.Helper()
sz := len(b)
if len(w.golden) < sz {
sz = len(w.golden)
}
for i := 0; i < len(b); i++ {
if w.golden[w.pos] != b[i] {
panic(fmt.Sprintf("tested data different than golden data: pos=%d: \n%x != %x", w.pos, w.golden[w.pos], b[i]))
}
w.pos++
if len(b) < i-1 {
return 0, errors.New("can't write 1 byte into writer")
}
n, err := w.out.Write([]byte{b[i]})
if n != 1 && err != nil {
return n, err
}
}
return len(b), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment