Skip to content

Commit

Permalink
Update tests allowing removal of stretchr/testify test dependency (#175)
Browse files Browse the repository at this point in the history
This change refactors a test to remove the final testify test dependency (all other tests already adopt the convention to not use an assertion library. More about some thinking on why at https://golang.org/doc/faq#testing_framework).
  • Loading branch information
suyashkumar authored Jan 5, 2021
1 parent 3fd2198 commit 69adfab
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ go 1.13
require (
github.com/golang/mock v1.4.4
github.com/google/go-cmp v0.5.2
github.com/stretchr/testify v1.4.0
golang.org/x/text v0.3.0
)
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -19,7 +12,3 @@ golang.org/x/tools v0.0.0-20190425150028-36563e24a262 h1:qsl9y/CJx34tuA7QCPNp86J
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
5 changes: 5 additions & 0 deletions pkg/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func (t Tag) Compare(other Tag) int {
return 0
}

// Equals returns true if this tag equals the provided tag.
func (t Tag) Equals(other Tag) bool {
return t.Compare(other) == 0
}

// IsPrivate indicates if the input group is part of a private tag.
func IsPrivate(group uint16) bool {
return group%2 == 1
Expand Down
64 changes: 48 additions & 16 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math/rand"
"strconv"
Expand All @@ -16,29 +15,49 @@ import (
"github.com/suyashkumar/dicom/pkg/dicomio"
"github.com/suyashkumar/dicom/pkg/frame"

"github.com/golang/mock/gomock"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
mock_dicomio "github.com/suyashkumar/dicom/mocks/pkg/dicomio"
"github.com/suyashkumar/dicom/pkg/tag"
)

func TestReadTag(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

assert := assert.New(t)

m := mock_dicomio.NewMockReader(ctrl)
cases := []struct {
name string
data []byte
wantTag tag.Tag
wantErr error
}{
{
name: "basic",
data: buildTagData(t, tag.Rows),
wantTag: tag.Rows,
wantErr: nil,
},
{
name: "custom",
data: buildTagData(t, tag.Tag{0x0011, 0x0010}),
wantTag: tag.Tag{0x0011, 0x0010},
wantErr: nil,
},
}

first := m.EXPECT().ReadUInt16().Return(uint16(0x7FE0), nil)
m.EXPECT().ReadUInt16().Return(uint16(0x0010), nil).After(first)
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
data := bytes.NewBuffer(tc.data)
r, err := dicomio.NewReader(bufio.NewReader(data), binary.LittleEndian, int64(data.Len()))
if err != nil {
t.Errorf("TestReadTag: unable to create new dicomio.Reader")
}

retTag, err := readTag(m)
gotTag, err := readTag(r)
if err != tc.wantErr {
t.Errorf("TestReadTag: unexpected err. got: %v, want: %v", err, tc.wantErr)
}

assert.NoError(err)
fmt.Println(err)
assert.Equal(tag.PixelData, *retTag)
if !gotTag.Equals(tc.wantTag) {
t.Errorf("TestReadTag: unexpected output. got: %v, want: %v", gotTag, tc.wantTag)
}
})
}

}

Expand Down Expand Up @@ -384,3 +403,16 @@ func buildReadNativeFramesInput(rows, cols, numFrames, samplesPerPixel int, b *t

return &dataset, r
}

func buildTagData(t *testing.T, tg tag.Tag) []byte {
t.Helper()
data := bytes.Buffer{}
if err := binary.Write(&data, binary.LittleEndian, tg.Group); err != nil {
t.Errorf("buildTagData: Unable to setup test buffer: %v", err)
}
if err := binary.Write(&data, binary.LittleEndian, tg.Element); err != nil {
t.Errorf("buildTagData: Unable to setup test buffer: %v", err)
}

return data.Bytes()
}

0 comments on commit 69adfab

Please sign in to comment.