-
Notifications
You must be signed in to change notification settings - Fork 10
/
backend_test.go
88 lines (69 loc) · 2.17 KB
/
backend_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gitobj
import (
"bytes"
"encoding/hex"
"io"
"io/ioutil"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewMemoryBackend(t *testing.T) {
backend, err := NewMemoryBackend(nil)
assert.NoError(t, err)
ro, rw := backend.Storage()
assert.Equal(t, ro, rw)
assert.NotNil(t, ro.(*memoryStorer))
}
func TestNewMemoryBackendWithReadOnlyData(t *testing.T) {
sha := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
oid, err := hex.DecodeString(sha)
assert.Nil(t, err)
m := map[string]io.ReadWriter{
sha: bytes.NewBuffer([]byte{0x1}),
}
backend, err := NewMemoryBackend(m)
assert.NoError(t, err)
ro, _ := backend.Storage()
reader, err := ro.Open(oid)
assert.NoError(t, err)
contents, err := ioutil.ReadAll(reader)
assert.NoError(t, err)
assert.Equal(t, []byte{0x1}, contents)
}
func TestNewMemoryBackendWithWritableData(t *testing.T) {
sha := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
oid, err := hex.DecodeString(sha)
assert.Nil(t, err)
backend, err := NewMemoryBackend(make(map[string]io.ReadWriter))
assert.NoError(t, err)
buf := bytes.NewBuffer([]byte{0x1})
ro, rw := backend.Storage()
rw.Store(oid, buf)
reader, err := ro.Open(oid)
assert.NoError(t, err)
contents, err := ioutil.ReadAll(reader)
assert.NoError(t, err)
assert.Equal(t, []byte{0x1}, contents)
}
func TestSplitAlternatesString(t *testing.T) {
testCases := []struct {
input string
expected []string
}{
{"abc", []string{"abc"}},
{"abc:def", []string{"abc", "def"}},
{`"abc":def`, []string{"abc", "def"}},
{`"i\alike\bcomplicated\tstrings":def`, []string{"i\alike\bcomplicated\tstrings", "def"}},
{`abc:"i\nlike\vcomplicated\fstrings\r":def`, []string{"abc", "i\nlike\vcomplicated\fstrings\r", "def"}},
{`abc:"uni\xc2\xa9ode":def`, []string{"abc", "uni©ode", "def"}},
{`abc:"uni\302\251ode\10\0":def`, []string{"abc", "uni©ode\x08\x00", "def"}},
{`abc:"cookie\\monster\"":def`, []string{"abc", "cookie\\monster\"", "def"}},
}
for _, test := range testCases {
actual := splitAlternateString(test.input, ":")
if !reflect.DeepEqual(actual, test.expected) {
t.Errorf("unexpected output for %q: got %v, expected %v", test.input, actual, test.expected)
}
}
}