-
Notifications
You must be signed in to change notification settings - Fork 1
/
decimal_marshal_test.go
46 lines (40 loc) · 1.38 KB
/
decimal_marshal_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
package decimal
import (
"gopkg.in/yaml.v3"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDecimal_MarshalJSON(t *testing.T) {
x, ok := NewDecimalFromString("1")
assert.True(t, ok)
xJson, err := json.Marshal(x)
assert.NoError(t, err)
assert.Equal(t, []byte(`"1"`), xJson)
assert.NoError(t, json.Unmarshal(xJson, &x))
assert.Equal(t, x, NewDecimalFromStringOrDefault("1", NewDecimalZero()))
x, ok = NewDecimalFromString("1.5555555")
assert.True(t, ok)
xJson, err = json.Marshal(x)
assert.NoError(t, err)
assert.Equal(t, []byte(`"1.5555555"`), xJson)
assert.NoError(t, json.Unmarshal(xJson, &x))
assert.Equal(t, x, NewDecimalFromStringOrDefault("1.5555555", NewDecimalZero()))
}
func TestDecimal_MarshalYAML(t *testing.T) {
x, ok := NewDecimalFromString("1")
assert.True(t, ok)
xYaml, err := yaml.Marshal(x)
assert.NoError(t, err)
actualXYaml := append([]byte(`"1"`), 0xa)
assert.Equal(t, actualXYaml, xYaml)
assert.NoError(t, yaml.Unmarshal(actualXYaml, &x))
assert.Equal(t, x, NewDecimalFromStringOrDefault("1", NewDecimalZero()))
x, ok = NewDecimalFromString("1.5555555")
assert.True(t, ok)
xYaml, err = yaml.Marshal(x)
assert.NoError(t, err)
actualXYaml = append([]byte(`"1.5555555"`), 0xa)
assert.Equal(t, actualXYaml, xYaml)
assert.NoError(t, yaml.Unmarshal(actualXYaml, &x))
assert.Equal(t, x, NewDecimalFromStringOrDefault("1.5555555", NewDecimalZero()))
}