generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarshalbinary_test.go
100 lines (85 loc) · 2.71 KB
/
marshalbinary_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
package dosh_test
import (
"strings"
. "github.com/dogmatiq/dosh"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/shopspring/decimal"
)
var _ = Describe("type Amount (binary marshaling)", func() {
Describe("func MarshalBinary() and UnmarshalBinary()", func() {
It("marshals and unmarshals an amount", func() {
a := FromString("XYZ", "10.123")
data, err := a.MarshalBinary()
Expect(err).ShouldNot(HaveOccurred())
var b Amount
err = b.UnmarshalBinary(data)
Expect(err).ShouldNot(HaveOccurred())
Expect(a.EqualTo(b)).To(BeTrue())
})
It("marshals and unmarshals a zero value", func() {
var a Amount
data, err := a.MarshalBinary()
Expect(err).ShouldNot(HaveOccurred())
var b Amount
err = b.UnmarshalBinary(data)
Expect(err).ShouldNot(HaveOccurred())
Expect(a.EqualTo(b)).To(BeTrue())
})
})
Describe("func MarshalBinary()", func() {
It("returns an error if the currency code is longer than 255 bytes", func() {
a := Zero(strings.Repeat("X", 256))
_, err := a.MarshalBinary()
Expect(err).To(MatchError("cannot marshal amount to binary representation: currency code is 256 characters long, maximum is 255"))
})
})
// binaryZero is the binary representation of a decimal with a value of 0.
//
// This is built inline (as opposed to in BeforeEach) because it is used
// within a call to Entry().
var binaryZero string
{
data, err := decimal.Decimal{}.MarshalBinary()
if err != nil {
panic(err)
}
binaryZero = string(data)
}
Describe("func UnmarshalBinary()", func() {
DescribeTable(
"it returns an error if the data is invalid",
func(data string, expect string) {
var a Amount
err := a.UnmarshalBinary([]byte(data))
Expect(err).To(MatchError(expect))
},
Entry(
"empty",
"",
"cannot unmarshal amount from binary representation: data is empty",
),
Entry(
"empty currency",
"\x00"+binaryZero,
"cannot unmarshal amount from binary representation: currency code is empty, codes must consist only of 3 or more uppercase ASCII letters",
),
Entry(
"invalid currency",
"\x01X"+binaryZero,
"cannot unmarshal amount from binary representation: currency code (X) is invalid, codes must consist only of 3 or more uppercase ASCII letters",
),
Entry(
"empty magnitude",
"\x03USD",
"cannot unmarshal amount from binary representation: error decoding binary []: expected at least 4 bytes, got 0",
),
Entry(
"invalid magnitude",
"\x03USD<invalid>",
"cannot unmarshal amount from binary representation: error decoding binary [60 105 110 118 97 108 105 100 62]: Int.GobDecode: encoding version 48 not supported",
),
)
})
})