generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarshaljson_test.go
96 lines (86 loc) · 3.2 KB
/
marshaljson_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
package dosh_test
import (
"math"
. "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 (JSON marshaling)", func() {
Describe("func MarshalJSON()", func() {
It("returns the JSON representation of the amount", func() {
a := FromString("XYZ", "10.123")
data, err := a.MarshalJSON()
Expect(err).ShouldNot(HaveOccurred())
// note: units represented as a string
Expect(data).To(MatchJSON([]byte(`{"currency_code":"XYZ","units":"10","nanos":123000000}`)))
})
DescribeTable(
"it returns an error if the amount can not be marshaled",
func(a Amount, expect string) {
_, err := a.MarshalJSON()
Expect(err).To(MatchError(expect))
},
Entry(
"integer component of the magnitude overflows an int64",
FromInt("XYZ", math.MaxInt64).Add(Unit("XYZ")),
"cannot marshal amount to JSON representation: magnitude's integer component overflows int64",
),
Entry(
"fractional component of the magnitude requires more precision that available",
FromString("XYZ", "0.0123456789"),
"cannot marshal amount to JSON representation: magnitude's fractional component has too many decimal places",
),
)
})
Describe("func UnmarshalJSON()", func() {
It("unmarshals an amount from its JSON representation", func() {
var a Amount
err := a.UnmarshalJSON([]byte(`{"currency_code":"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 JSON message is invalid",
func(data string, expect string) {
var a Amount
err := a.UnmarshalJSON([]byte(data))
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(MatchRegexp(expect))
},
Entry(
"malformed JSON",
`<invalid>`,
// Note, protocol buffers package randomly emits different
// output to avoid code that checks errors by string value,
// which is fair enough but makes simple tests like this
// incredibly frustrating. This is the reason for the use of
// regular expressions.
`cannot unmarshal amount from JSON representation: proto:.+syntax error`,
),
Entry(
"empty currency",
`{"units":"0","nanos":0}`,
`cannot unmarshal amount from JSON representation: currency code is empty, codes must consist only of 3 or more uppercase ASCII letters`,
),
Entry(
"invalid currency",
`{"currency_code":"X","units":"0","nanos":0}`,
`cannot unmarshal amount from JSON representation: currency code \(X\) is invalid, codes must consist only of 3 or more uppercase ASCII letters`,
),
Entry(
"units positive, nanos negative",
`{"currency_code": "XYZ", "units": "1", "nanos": -1}`,
`cannot unmarshal amount from JSON representation: units and nanos components must have the same sign`,
),
Entry(
"units negative, nanos positive",
`{"currency_code": "XYZ", "units": "-1", "nanos": 1}`,
`cannot unmarshal amount from JSON representation: units and nanos components must have the same sign`,
),
)
})
})