11 "github.com/stretchr/testify/assert"
14 func TestBlobReturnsCorrectObjectType(t *testing.T) {
15 assert.Equal(t, BlobObjectType, new(Blob).Type())
18 func TestBlobFromString(t *testing.T) {
19 given := []byte("example")
22 b := NewBlobFromBytes(given)
24 assert.EqualValues(t, glen, b.Size)
26 contents, err := ioutil.ReadAll(b.Contents)
27 assert.NoError(t, err)
28 assert.Equal(t, given, contents)
31 func TestBlobEncoding(t *testing.T) {
32 const contents = "Hello, world!\n"
35 Size: int64(len(contents)),
36 Contents: strings.NewReader(contents),
40 if _, err := b.Encode(&buf); err != nil {
43 assert.Equal(t, contents, (&buf).String())
46 func TestBlobDecoding(t *testing.T) {
47 const contents = "Hello, world!\n"
48 from := strings.NewReader(contents)
51 n, err := b.Decode(from, int64(len(contents)))
56 assert.EqualValues(t, len(contents), b.Size)
58 got, err := ioutil.ReadAll(b.Contents)
60 assert.Equal(t, []byte(contents), got)
63 func TestBlobCallCloseFn(t *testing.T) {
66 expected := errors.New("some close error")
69 closeFn: func() error {
70 atomic.AddUint32(&calls, 1)
77 assert.Equal(t, expected, got)
78 assert.EqualValues(t, 1, calls)
81 func TestBlobCanCloseWithoutCloseFn(t *testing.T) {
86 assert.Nil(t, b.Close())
89 func TestBlobEqualReturnsTrueWithUnchangedContents(t *testing.T) {
90 c := strings.NewReader("Hello, world!")
92 b1 := &Blob{Size: int64(c.Len()), Contents: c}
93 b2 := &Blob{Size: int64(c.Len()), Contents: c}
95 assert.True(t, b1.Equal(b2))
98 func TestBlobEqualReturnsFalseWithChangedContents(t *testing.T) {
99 c1 := strings.NewReader("Hello, world!")
100 c2 := strings.NewReader("Goodbye, world!")
102 b1 := &Blob{Size: int64(c1.Len()), Contents: c1}
103 b2 := &Blob{Size: int64(c2.Len()), Contents: c2}
105 assert.False(t, b1.Equal(b2))
108 func TestBlobEqualReturnsTrueWhenOneBlobIsNil(t *testing.T) {
109 b1 := &Blob{Size: 1, Contents: bytes.NewReader([]byte{0xa})}
112 assert.False(t, b1.Equal(b2))
113 assert.False(t, b2.Equal(b1))
116 func TestBlobEqualReturnsTrueWhenBothBlobsAreNil(t *testing.T) {
120 assert.True(t, b1.Equal(b2))