-
Notifications
You must be signed in to change notification settings - Fork 0
/
rational_test.go
102 lines (92 loc) · 2.13 KB
/
rational_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
package rational_test
import (
"encoding/json"
"errors"
"testing"
"github.com/go-x-pkg/rational"
)
func TestRationalUnmarshal(t *testing.T) {
tests := []struct {
raw []byte
e *rational.Rational
eIsNil bool
eErr error
}{
{
raw: []byte(`{"rational": "24000/1001"}`),
e: &rational.Rational{24000, 1001},
},
{
raw: []byte(`{"rational": "1920x1080"}`),
e: &rational.Rational{1920, 1080},
},
{
raw: []byte(`{"rational": "16:9"}`),
e: &rational.Rational{16, 9},
},
{
raw: []byte(`{"rational": "0"}`),
e: &rational.Rational{0, 0},
eIsNil: true,
},
{
raw: []byte(`{"rational": "18647"}`),
e: &rational.Rational{18647, 1},
},
{
raw: []byte(`{"rational": "0:0:0"}`),
eErr: rational.ErrWrongPartsNumber,
},
{
raw: []byte(`{"rational": "0~~~~xddqwdq"}`),
eErr: rational.ErrParseNum,
},
{
raw: []byte(`{"rational": "1xddqwdq"}`),
eErr: rational.ErrParseDen,
},
{
raw: []byte(`{}`),
e: nil,
},
}
for i, tt := range tests {
func() {
data := struct {
Rational *rational.Rational `json:"rational" yaml:"rational" bson:"rational"`
}{}
err := json.Unmarshal(tt.raw, &data)
if err != nil {
if tt.eErr == nil {
t.Errorf("#%d: err: %s", i, err)
return
} else if !errors.Is(err, tt.eErr) {
t.Errorf("#%d: unexpected error: got %#v expected %#v", i, err, tt.eErr)
return
}
}
if data.Rational == nil && tt.e != nil {
t.Errorf("#%d: bad rational: expected nil", i)
return
}
if data.Rational != nil && tt.e != nil && *data.Rational != *tt.e {
t.Errorf("#%d: bad rational: got %#v xpected %#v", i, data.Rational, tt.e)
}
if _, err := json.Marshal(data); err != nil {
t.Errorf("#%d: err marshal JSON: %s", i, err)
}
if data.Rational != nil {
// should not panic
data.Rational.Float64()
data.Rational.Reverse().Float64()
data.Rational.Percent()
data.Rational.String()
data.Rational.StringAlt()
data.Rational.StringAlt2()
if !data.Rational.IsNil() && tt.eIsNil {
t.Errorf("#%d: rational exp expeted to be nil", i)
}
}
}()
}
}