-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathether_test.go
106 lines (101 loc) · 1.66 KB
/
ether_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
package ether
import (
"math/big"
"testing"
)
func TestPrint(t *testing.T) {
cases := []struct {
Amount *big.Int
Want string
}{
{
Amount: big.NewInt(0),
Want: "0 wei",
},
{
Amount: big.NewInt(5000000000),
Want: "5 gwei",
},
{
Amount: big.NewInt(500000),
Want: "0.0005 gwei",
},
{
Amount: big.NewInt(-10000000),
Want: "-0.01 gwei",
},
{
Amount: new(big.Int).Mul(ethInWei, big.NewInt(15)),
Want: "15 ether",
},
}
for i, tc := range cases {
got := Print(tc.Amount)
if got != tc.Want {
t.Errorf("case #%d: got: %q; want %q", i, got, tc.Want)
}
}
}
func TestParse(t *testing.T) {
cases := []struct {
Input string
Want *big.Int
IsError bool
}{
{
Want: big.NewInt(0),
Input: "0",
},
{
Want: big.NewInt(42),
Input: "42",
},
{
Want: big.NewInt(0),
Input: "0 wei",
},
{
Want: big.NewInt(5000000000),
Input: "5 gwei",
},
{
Want: big.NewInt(500000),
Input: "0.0005 gwei",
},
{
Want: big.NewInt(-10000000),
Input: "-0.01 gwei",
},
{
Want: new(big.Int).Mul(ethInWei, big.NewInt(15)),
Input: "15 ether",
},
{
Input: "",
IsError: true,
},
{
Input: "foo",
IsError: true,
},
{
Input: "1 foo",
IsError: true,
},
{
Input: "- eth",
IsError: true,
},
}
for i, tc := range cases {
got, err := Parse(tc.Input)
if tc.IsError && err != nil {
continue
}
if (err != nil) != tc.IsError {
t.Errorf("case #%d: got error: %v; wanted IsError=%t", i, err, tc.IsError)
} else if got.Cmp(tc.Want) != 0 {
t.Errorf("case #%d: got: %q; want %q (input: %q)", i, got, tc.Want, tc.Input)
}
}
}