-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion_test.go
177 lines (163 loc) · 4.33 KB
/
version_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package dvs
import (
"bytes"
"errors"
"reflect"
"testing"
"github.com/mus-format/mus-go"
)
func TestVersion(t *testing.T) {
var (
ver = Version[FooV1, Foo]{
DTS: FooV1DTS,
MigrateOld: func(t FooV1) (v Foo, err error) {
return Foo{num: t.num, str: "undefined"}, nil
},
MigrateCurrent: func(v Foo) (t FooV1, err error) {
return FooV1{num: v.num}, nil
},
}
)
t.Run("MigrateCurrentAndReliablyMarshal should marshal data if it receives too big bs",
func(t *testing.T) {
var (
foo = Foo{num: 11}
bs = make([]byte, 1000)
wantBS = []byte{0, 22}
wantN = 2
wantErr error = nil
)
testMigrateCurrentAndReliablyMarshal(ver, foo, bs, wantBS, wantN, wantErr,
t)
})
t.Run("MigrateCurrentAndReliablyMarshal should marshal data if it receives too small bs",
func(t *testing.T) {
var (
foo = Foo{num: 11}
bs = []byte{}
wantBS = []byte{0, 22}
wantN = 2
wantErr error = nil
)
testMigrateCurrentAndReliablyMarshal(ver, foo, bs, wantBS, wantN, wantErr,
t)
})
t.Run("If MigrateCurrent fails with an error, MigrateCurrentAndReliablyMarshal should return it",
func(t *testing.T) {
var (
wantBS []byte = nil
wantN = 0
wantErr = errors.New("MigrateCurrent error")
ver = Version[FooV1, Foo]{
MigrateCurrent: func(v Foo) (t FooV1, err error) {
err = wantErr
return
},
}
)
testMigrateCurrentAndReliablyMarshal(ver, Foo{}, []byte{},
wantBS,
wantN,
wantErr,
t)
})
t.Run("MigrateCurrentAndMakeBSAndMarshal should marshal data",
func(t *testing.T) {
var (
foo = Foo{num: 22}
wantBS = []byte{0, 44}
wantN = 2
wantErr error = nil
)
testMigrateCurrentAndMakeBSAndMarshal(ver, foo, wantBS, wantN, wantErr, t)
})
t.Run("If MigrateCurrent fails with an error, MigrateCurrentAndMakeBSAndMarshal should return it",
func(t *testing.T) {
var (
wantBS []byte = nil
wantN = 0
wantErr = errors.New("MigrateCurrent error")
ver = Version[FooV1, Foo]{
MigrateCurrent: func(v Foo) (t FooV1, err error) {
err = wantErr
return
},
}
)
testMigrateCurrentAndMakeBSAndMarshal(ver, Foo{}, wantBS, wantN,
wantErr,
t)
})
t.Run("UnmarshalAndMigrateOld should unmarshal data", func(t *testing.T) {
var (
bs = []byte{22}
wantFoo = Foo{num: 11, str: "undefined"}
wantN = 1
wantErr error = nil
)
testUnmarshalAndMigrateOld[FooV1, Foo](ver, bs, wantFoo, wantErr, wantN, t)
})
t.Run("If UnmarshalData fails with an error, UnmarshalAndMigrateOld should return it",
func(t *testing.T) {
var (
bs = []byte{}
wantFoo = Foo{}
wantN = 0
wantErr error = mus.ErrTooSmallByteSlice
)
testUnmarshalAndMigrateOld[FooV1, Foo](ver, bs, wantFoo, wantErr, wantN,
t)
})
}
func testMigrateCurrentAndReliablyMarshal[T, V any](ver Version[T, V], v V,
bs []byte,
wantBS []byte,
wantN int,
wantErr error,
t *testing.T,
) {
bs, n, err := ver.MigrateCurrentAndReliablyMarshal(v, bs)
if err != wantErr {
t.Errorf("unexpected error, want '%v' actual '%v'", wantErr, err)
}
if !bytes.Equal(bs[:n], wantBS) {
t.Errorf("unexpected bs, want '%v' actual '%v'", wantBS, bs)
}
if n != wantN {
t.Errorf("unexpected n, want '%v' actual '%v'", wantN, n)
}
}
func testUnmarshalAndMigrateOld[T, V any](ver Version[T, V], bs []byte,
wantV V,
wantErr error,
wantN int,
t *testing.T,
) {
v, n, err := ver.UnmarshalAndMigrateOld(bs)
if err != wantErr {
t.Errorf("unexpected error, want '%v' actual '%v'", wantErr, err)
}
if !reflect.DeepEqual(v, wantV) {
t.Errorf("unexpected v, want '%v' actual '%v'", wantV, v)
}
if n != wantN {
t.Errorf("unexpected n, want '%v' actual '%v'", wantN, n)
}
}
func testMigrateCurrentAndMakeBSAndMarshal[T, V any](ver Version[T, V], v V,
wantBS []byte,
wantN int,
wantErr error,
t *testing.T,
) {
bs, n, err := ver.MigrateCurrentAndMakeBSAndMarshal(v)
if err != wantErr {
t.Errorf("unexpected error, want '%v' actual '%v'", wantErr, err)
}
if !bytes.Equal(bs, wantBS) {
t.Errorf("unexpected bs, want '%v' actual '%v'", wantBS, bs)
}
if n != wantN {
t.Errorf("unexpected n, want '%v' actual '%v'", wantN, n)
}
}