Imported Upstream version 2.4.2
[scm/test.git] / vendor / github.com / git-lfs / gitobj / blob_test.go
1 package gitobj
2
3 import (
4         "bytes"
5         "errors"
6         "io/ioutil"
7         "strings"
8         "sync/atomic"
9         "testing"
10
11         "github.com/stretchr/testify/assert"
12 )
13
14 func TestBlobReturnsCorrectObjectType(t *testing.T) {
15         assert.Equal(t, BlobObjectType, new(Blob).Type())
16 }
17
18 func TestBlobFromString(t *testing.T) {
19         given := []byte("example")
20         glen := len(given)
21
22         b := NewBlobFromBytes(given)
23
24         assert.EqualValues(t, glen, b.Size)
25
26         contents, err := ioutil.ReadAll(b.Contents)
27         assert.NoError(t, err)
28         assert.Equal(t, given, contents)
29 }
30
31 func TestBlobEncoding(t *testing.T) {
32         const contents = "Hello, world!\n"
33
34         b := &Blob{
35                 Size:     int64(len(contents)),
36                 Contents: strings.NewReader(contents),
37         }
38
39         var buf bytes.Buffer
40         if _, err := b.Encode(&buf); err != nil {
41                 t.Fatal(err.Error())
42         }
43         assert.Equal(t, contents, (&buf).String())
44 }
45
46 func TestBlobDecoding(t *testing.T) {
47         const contents = "Hello, world!\n"
48         from := strings.NewReader(contents)
49
50         b := new(Blob)
51         n, err := b.Decode(from, int64(len(contents)))
52
53         assert.Equal(t, 0, n)
54         assert.Nil(t, err)
55
56         assert.EqualValues(t, len(contents), b.Size)
57
58         got, err := ioutil.ReadAll(b.Contents)
59         assert.Nil(t, err)
60         assert.Equal(t, []byte(contents), got)
61 }
62
63 func TestBlobCallCloseFn(t *testing.T) {
64         var calls uint32
65
66         expected := errors.New("some close error")
67
68         b := &Blob{
69                 closeFn: func() error {
70                         atomic.AddUint32(&calls, 1)
71                         return expected
72                 },
73         }
74
75         got := b.Close()
76
77         assert.Equal(t, expected, got)
78         assert.EqualValues(t, 1, calls)
79 }
80
81 func TestBlobCanCloseWithoutCloseFn(t *testing.T) {
82         b := &Blob{
83                 closeFn: nil,
84         }
85
86         assert.Nil(t, b.Close())
87 }
88
89 func TestBlobEqualReturnsTrueWithUnchangedContents(t *testing.T) {
90         c := strings.NewReader("Hello, world!")
91
92         b1 := &Blob{Size: int64(c.Len()), Contents: c}
93         b2 := &Blob{Size: int64(c.Len()), Contents: c}
94
95         assert.True(t, b1.Equal(b2))
96 }
97
98 func TestBlobEqualReturnsFalseWithChangedContents(t *testing.T) {
99         c1 := strings.NewReader("Hello, world!")
100         c2 := strings.NewReader("Goodbye, world!")
101
102         b1 := &Blob{Size: int64(c1.Len()), Contents: c1}
103         b2 := &Blob{Size: int64(c2.Len()), Contents: c2}
104
105         assert.False(t, b1.Equal(b2))
106 }
107
108 func TestBlobEqualReturnsTrueWhenOneBlobIsNil(t *testing.T) {
109         b1 := &Blob{Size: 1, Contents: bytes.NewReader([]byte{0xa})}
110         b2 := (*Blob)(nil)
111
112         assert.False(t, b1.Equal(b2))
113         assert.False(t, b2.Equal(b1))
114 }
115
116 func TestBlobEqualReturnsTrueWhenBothBlobsAreNil(t *testing.T) {
117         b1 := (*Blob)(nil)
118         b2 := (*Blob)(nil)
119
120         assert.True(t, b1.Equal(b2))
121 }