Skip to content

Commit

Permalink
test helpers identified
Browse files Browse the repository at this point in the history
  • Loading branch information
Karrick S. McDermott committed Jul 12, 2019
1 parent f0d7376 commit 6315d37
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func init() {
}

func testBinaryDecodeFail(t *testing.T, schema string, buf []byte, errorMessage string) {
t.Helper()
c, err := NewCodec(schema)
if err != nil {
t.Fatal(err)
Expand All @@ -61,6 +62,7 @@ func testBinaryDecodeFail(t *testing.T, schema string, buf []byte, errorMessage
}

func testBinaryEncodeFail(t *testing.T, schema string, datum interface{}, errorMessage string) {
t.Helper()
c, err := NewCodec(schema)
if err != nil {
t.Fatal(err)
Expand All @@ -73,14 +75,17 @@ func testBinaryEncodeFail(t *testing.T, schema string, datum interface{}, errorM
}

func testBinaryEncodeFailBadDatumType(t *testing.T, schema string, datum interface{}) {
t.Helper()
testBinaryEncodeFail(t, schema, datum, "received: ")
}

func testBinaryDecodeFailShortBuffer(t *testing.T, schema string, buf []byte) {
t.Helper()
testBinaryDecodeFail(t, schema, buf, "short buffer")
}

func testBinaryDecodePass(t *testing.T, schema string, datum interface{}, encoded []byte) {
t.Helper()
codec, err := NewCodec(schema)
if err != nil {
t.Fatal(err)
Expand All @@ -105,6 +110,7 @@ func testBinaryDecodePass(t *testing.T, schema string, datum interface{}, encode
}

func testBinaryEncodePass(t *testing.T, schema string, datum interface{}, expected []byte) {
t.Helper()
codec, err := NewCodec(schema)
if err != nil {
t.Fatalf("Schma: %q %s", schema, err)
Expand All @@ -122,6 +128,7 @@ func testBinaryEncodePass(t *testing.T, schema string, datum interface{}, expect
// testBinaryCodecPass does a bi-directional codec check, by encoding datum to
// bytes, then decoding bytes back to datum.
func testBinaryCodecPass(t *testing.T, schema string, datum interface{}, buf []byte) {
t.Helper()
testBinaryDecodePass(t, schema, datum, buf)
testBinaryEncodePass(t, schema, datum, buf)
}
6 changes: 6 additions & 0 deletions helperV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

func newCodecUsingV2(tb testing.TB, schema string) *Codec {
tb.Helper()
codec, err := NewCodec(schema)
if err != nil {
tb.Fatal(err)
Expand All @@ -23,6 +24,7 @@ func newCodecUsingV2(tb testing.TB, schema string) *Codec {
}

func nativeFromAvroUsingV2(tb testing.TB, avroBlob []byte) ([]interface{}, *Codec) {
tb.Helper()
ocf, err := NewOCFReader(bytes.NewReader(avroBlob))
if err != nil {
tb.Fatal(err)
Expand All @@ -43,6 +45,7 @@ func nativeFromAvroUsingV2(tb testing.TB, avroBlob []byte) ([]interface{}, *Code
}

func binaryFromNativeUsingV2(tb testing.TB, codec *Codec, nativeData []interface{}) [][]byte {
tb.Helper()
binaryData := make([][]byte, len(nativeData))
for i, datum := range nativeData {
binaryDatum, err := codec.BinaryFromNative(nil, datum)
Expand All @@ -55,6 +58,7 @@ func binaryFromNativeUsingV2(tb testing.TB, codec *Codec, nativeData []interface
}

func textFromNativeUsingV2(tb testing.TB, codec *Codec, nativeData []interface{}) [][]byte {
tb.Helper()
textData := make([][]byte, len(nativeData))
for i, nativeDatum := range nativeData {
textDatum, err := codec.TextualFromNative(nil, nativeDatum)
Expand All @@ -67,6 +71,7 @@ func textFromNativeUsingV2(tb testing.TB, codec *Codec, nativeData []interface{}
}

func nativeFromBinaryUsingV2(tb testing.TB, codec *Codec, binaryData [][]byte) []interface{} {
tb.Helper()
nativeData := make([]interface{}, len(binaryData))
for i, binaryDatum := range binaryData {
nativeDatum, buf, err := codec.NativeFromBinary(binaryDatum)
Expand All @@ -82,6 +87,7 @@ func nativeFromBinaryUsingV2(tb testing.TB, codec *Codec, binaryData [][]byte) [
}

func nativeFromTextUsingV2(tb testing.TB, codec *Codec, textData [][]byte) []interface{} {
tb.Helper()
nativeData := make([]interface{}, len(textData))
for i, textDatum := range textData {
nativeDatum, buf, err := codec.NativeFromTextual(textDatum)
Expand Down
1 change: 1 addition & 0 deletions number_recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

func testPrimitiveRecoverNative(t *testing.T, schema string, value interface{}) {
t.Helper()
codec, err := NewCodec(schema)
if err != nil {
t.Fatalf("Schema: %s; %s", schema, err)
Expand Down
1 change: 1 addition & 0 deletions ocf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestReadOCFHeaderMagicBytes(t *testing.T) {
//

func testCannotReadOCFHeader(t *testing.T, input []byte, expected ...string) {
t.Helper()
_, err := NewOCFReader(bytes.NewBuffer(append([]byte("Obj\x01"), input...)))
ensureError(t, err, append([]string{"cannot read OCF header"}, expected...)...)
}
Expand Down
2 changes: 2 additions & 0 deletions ocf_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

// createTestFile is used to create a new test file fixture with provided data
func createTestFile(t *testing.T, pathname string, data []byte) {
t.Helper()
nf, err := os.Create(pathname)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -90,6 +91,7 @@ func TestNewOCFWriterWhenFileNotEmptyWhenCannotReadOCFHeader(t *testing.T) {
}

func testNewOCFWriterWhenFile(t *testing.T, pathname string, expected ...string) {
t.Helper()
fh, err := os.Open(pathname)
if err != nil {
t.Fatal(err)
Expand Down
7 changes: 7 additions & 0 deletions text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

func testTextDecodeFail(t *testing.T, schema string, buf []byte, errorMessage string) {
t.Helper()
c, err := NewCodec(schema)
if err != nil {
t.Fatal(err)
Expand All @@ -32,6 +33,7 @@ func testTextDecodeFail(t *testing.T, schema string, buf []byte, errorMessage st
}

func testTextEncodeFail(t *testing.T, schema string, datum interface{}, errorMessage string) {
t.Helper()
c, err := NewCodec(schema)
if err != nil {
t.Fatal(err)
Expand All @@ -44,14 +46,17 @@ func testTextEncodeFail(t *testing.T, schema string, datum interface{}, errorMes
}

func testTextEncodeFailBadDatumType(t *testing.T, schema string, datum interface{}) {
t.Helper()
testTextEncodeFail(t, schema, datum, "received: ")
}

func testTextDecodeFailShortBuffer(t *testing.T, schema string, buf []byte) {
t.Helper()
testTextDecodeFail(t, schema, buf, "short buffer")
}

func testTextDecodePass(t *testing.T, schema string, datum interface{}, encoded []byte) {
t.Helper()
codec, err := NewCodec(schema)
if err != nil {
t.Fatalf("schema: %s; %s", schema, err)
Expand Down Expand Up @@ -213,6 +218,7 @@ func testTextDecodePass(t *testing.T, schema string, datum interface{}, encoded
}

func testTextEncodePass(t *testing.T, schema string, datum interface{}, expected []byte) {
t.Helper()
codec, err := NewCodec(schema)
if err != nil {
t.Fatalf("Schma: %q %s", schema, err)
Expand All @@ -230,6 +236,7 @@ func testTextEncodePass(t *testing.T, schema string, datum interface{}, expected
// testTextCodecPass does a bi-directional codec check, by encoding datum to
// bytes, then decoding bytes back to datum.
func testTextCodecPass(t *testing.T, schema string, datum interface{}, buf []byte) {
t.Helper()
testTextDecodePass(t, schema, datum, buf)
testTextEncodePass(t, schema, datum, buf)
}

0 comments on commit 6315d37

Please sign in to comment.