generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarshalproto_test.go
147 lines (131 loc) · 4.15 KB
/
marshalproto_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
package dosh_test
import (
"math"
. "github.com/dogmatiq/dosh"
. "github.com/jmalloc/gomegax"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
"github.com/shopspring/decimal"
"google.golang.org/genproto/googleapis/type/money"
)
var _ = Describe("type Amount (protocol buffers marshaling)", func() {
invalidAmountVectors := []TableEntry{
Entry(
"integer component of the magnitude overflows an int64",
FromInt("XYZ", math.MaxInt64).Add(Unit("XYZ")),
"cannot marshal amount to protocol buffers representation: magnitude's integer component overflows int64",
),
Entry(
"fractional component of the magnitude requires more precision than available",
FromString("XYZ", "0.0123456789"),
"cannot marshal amount to protocol buffers representation: magnitude's fractional component has too many decimal places",
),
}
invalidProtoVectors := []TableEntry{
Entry(
"empty currency",
&money.Money{},
"cannot unmarshal amount from protocol buffers representation: currency code is empty, codes must consist only of 3 or more uppercase ASCII letters",
),
Entry(
"invalid currency",
&money.Money{CurrencyCode: "X"},
"cannot unmarshal amount from protocol buffers representation: currency code (X) is invalid, codes must consist only of 3 or more uppercase ASCII letters",
),
Entry(
"units positive, nanos negative",
&money.Money{CurrencyCode: "XYZ", Units: +1, Nanos: -1},
"cannot unmarshal amount from protocol buffers representation: units and nanos components must have the same sign",
),
Entry(
"units negative, nanos positive",
&money.Money{CurrencyCode: "XYZ", Units: -1, Nanos: +1},
"cannot unmarshal amount from protocol buffers representation: units and nanos components must have the same sign",
),
}
var _ = Describe("func FromProto()", func() {
It("unmarshals an amount from its protocol buffers representation", func() {
a := FromProto(&money.Money{
CurrencyCode: "XYZ",
Units: 10,
Nanos: 123000000,
})
Expect(a.CurrencyCode()).To(Equal("XYZ"))
m := decimal.RequireFromString("10.123")
Expect(a.Magnitude().Equal(m))
})
DescribeTable(
"it panics if the protocol buffers message is invalid",
func(pb *money.Money, expect string) {
Expect(func() {
FromProto(pb)
}).To(PanicWith(MatchError(expect)))
},
invalidProtoVectors...,
)
})
var _ = Describe("func ToProto()", func() {
It("returns the protocol buffers representation of the amount", func() {
a := FromString("XYZ", "10.123")
pb := ToProto(a)
Expect(pb).To(EqualX(&money.Money{
CurrencyCode: "XYZ",
Units: 10,
Nanos: 123000000,
}))
})
DescribeTable(
"it panics if the amount can not be marshaled",
func(a Amount, expect string) {
Expect(func() {
ToProto(a)
}).To(PanicWith(MatchError(expect)))
},
invalidAmountVectors...,
)
})
Describe("func MarshalProto()", func() {
It("returns the protocol buffers representation of the amount", func() {
a := FromString("XYZ", "10.123")
pb, err := a.MarshalProto()
Expect(err).ShouldNot(HaveOccurred())
Expect(pb).To(EqualX(&money.Money{
CurrencyCode: "XYZ",
Units: 10,
Nanos: 123000000,
}))
})
DescribeTable(
"it returns an error if the amount can not be marshaled",
func(a Amount, expect string) {
_, err := a.MarshalProto()
Expect(err).To(MatchError(expect))
},
invalidAmountVectors...,
)
})
Describe("func UnmarshalProto()", func() {
It("unmarshals an amount from its protocol buffers representation", func() {
var a Amount
err := a.UnmarshalProto(&money.Money{
CurrencyCode: "XYZ",
Units: 10,
Nanos: 123000000,
})
Expect(err).ShouldNot(HaveOccurred())
Expect(a.CurrencyCode()).To(Equal("XYZ"))
m := decimal.RequireFromString("10.123")
Expect(a.Magnitude().Equal(m))
})
DescribeTable(
"it returns an error if the protocol buffers message is invalid",
func(pb *money.Money, expect string) {
var a Amount
err := a.UnmarshalProto(pb)
Expect(err).To(MatchError(expect))
},
invalidProtoVectors...,
)
})
})