-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_test.go
More file actions
78 lines (70 loc) · 1.42 KB
/
parse_test.go
File metadata and controls
78 lines (70 loc) · 1.42 KB
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
package money_test
import (
"testing"
"github.com/dahaiyiyimcom/money"
)
func TestParseString_OK(t *testing.T) {
cases := []struct {
in string
want int64
}{
{"0", 0},
{"0.00", 0},
{"12", 1200},
{"12.3", 1230},
{"12.30", 1230},
{"12.34", 1234},
{" 12.34 ", 1234},
{"+12.34", 1234},
{"-12.34", -1234},
{"-0.50", -50},
{".00", 0}, // wholeStr -> "0" (bu davranışı istiyorsan)
{"0.", 0},
}
for _, tc := range cases {
a, err := money.ParseString(tc.in)
if err != nil {
t.Fatalf("in=%q unexpected err: %v", tc.in, err)
}
if got := a.Minor(); got != tc.want {
t.Fatalf("in=%q got=%d want=%d", tc.in, got, tc.want)
}
}
}
func TestParseString_Errors(t *testing.T) {
cases := []string{
"",
" ",
"abc",
"12.a",
"12.345", // too many decimals (strict)
"12..34", // invalid
"--12.34", // invalid
"12-34", // invalid
}
for _, in := range cases {
_, err := money.ParseString(in)
if err == nil {
t.Fatalf("in=%q expected error, got nil", in)
}
}
}
// Parse edge tests
func TestParseString_LeadingDot(t *testing.T) {
a, err := money.ParseString(".50")
if err != nil {
t.Fatalf("err: %v", err)
}
if a.Minor() != 50 {
t.Fatalf("got=%d want=50", a.Minor())
}
}
func TestParseString_TrailingDot(t *testing.T) {
a, err := money.ParseString("12.")
if err != nil {
t.Fatalf("err: %v", err)
}
if a.Minor() != 1200 {
t.Fatalf("got=%d want=1200", a.Minor())
}
}