forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
price_test.go
82 lines (70 loc) · 1.93 KB
/
price_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
package txnbuild
import (
"math"
"testing"
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/assert"
)
func TestPriceFromXDR(t *testing.T) {
for _, testCase := range []struct {
name string
input xdr.Price
expected price
}{
{
"1/2",
xdr.Price{N: 1, D: 2},
price{n: 1, d: 2, s: "0.5"},
},
{
"1",
xdr.Price{N: 1, D: 1},
price{n: 1, d: 1, s: "1"},
},
{
"1 / 1000000000",
xdr.Price{N: 1, D: 1000000000},
price{n: 1, d: 1000000000, s: "0.000000001"},
},
{
"max int 32",
xdr.Price{N: math.MaxInt32, D: 1},
price{n: math.MaxInt32, d: 1, s: "2147483600"},
},
{
"1/3",
xdr.Price{N: 1, D: 3},
price{n: 1, d: 3, s: "0.33333334"},
},
} {
t.Run(testCase.name, func(t *testing.T) {
var p price
p.fromXDR(testCase.input)
assert.Equal(t, testCase.expected, p)
assert.Equal(t, testCase.input, p.toXDR())
assert.NoError(t, p.parse(p.string()))
assert.Equal(t, testCase.expected, p)
})
}
}
func TestPriceParse(t *testing.T) {
var p price
assert.NoError(t, p.parse("0.5"))
assert.Equal(t, price{n: 1, d: 2, s: "0.5"}, p)
assert.NoError(t, p.parse("00.5"))
assert.Equal(t, price{n: 1, d: 2, s: "0.5"}, p)
assert.NoError(t, p.parse("0.50"))
assert.Equal(t, price{n: 1, d: 2, s: "0.5"}, p)
assert.EqualError(t, p.parse(""), "cannot parse price from empty string")
assert.Equal(t, price{n: 1, d: 2, s: "0.5"}, p)
assert.EqualError(t, p.parse("abc"), "failed to parse price from string: invalid price format: abc")
assert.Equal(t, price{n: 1, d: 2, s: "0.5"}, p)
assert.NoError(t, p.parse("0.33333334"))
assert.Equal(t, price{n: 16666667, d: 50000000, s: "0.33333334"}, p)
p.fromXDR(xdr.Price{N: 1, D: 3})
assert.Equal(t, price{n: 1, d: 3, s: "0.33333334"}, p)
assert.NoError(t, p.parse("00.33333334"))
assert.Equal(t, price{n: 1, d: 3, s: "0.33333334"}, p)
assert.NoError(t, p.parse("0.333333340"))
assert.Equal(t, price{n: 1, d: 3, s: "0.33333334"}, p)
}