-
Notifications
You must be signed in to change notification settings - Fork 2
/
ast_test.go
116 lines (96 loc) · 2.19 KB
/
ast_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
107
108
109
110
111
112
113
114
115
116
package codf
import "testing"
func TestStringConversion(t *testing.T) {
exprs := mkexprs(
mkstr("quoted"),
mkrawstr("raw quoted"),
mkword("word-string"),
mkdec("4.1234"),
mkexpr(1234),
mkrat(1, 2),
)
check := func(t *testing.T, convert func(Node) (string, bool), expected ...bool) {
for i, wantOK := range expected {
ex := exprs[i]
got, ok := convert(ex)
if ok != wantOK {
t.Errorf("%d: converting %v returned %t; want %t", i, ex, ok, wantOK)
continue
} else if !ok {
continue
}
want := ex.(*Literal).Value().(string)
if got != want {
t.Errorf("%d: converting %v returned %q; want %q", i, ex, got, want)
}
}
}
t.Run("String", func(t *testing.T) {
check(t, String,
true, true, true,
false, false, false,
)
})
t.Run("Quote", func(t *testing.T) {
check(t, Quote,
true, true, false,
false, false, false,
)
})
t.Run("Word", func(t *testing.T) {
check(t, Word,
false, false, true,
false, false, false,
)
})
}
func TestFloat64Conversion(t *testing.T) {
wants := mkexprs(
mkdec("0.25"),
mkdec("-4"),
mkdec("4"),
mkdec("4.1234"),
)
doc := mustParse(t, "input 1/4 -24/6 4 4.1234;")
params := doc.Children[0].(*Statement).Params
if got, want := len(params), len(wants); got != want {
t.Fatalf("len(params) = %d; want %d", got, want)
}
for i, got := range params {
want := wants[i]
gotf, ok := Float64(got)
if !ok {
t.Fatalf("couldn't convert got=%q to float64: ", got)
}
wantf, ok := Float64(want)
if !ok {
t.Fatalf("couldn't convert want=%q to float64: ", want)
}
if gotf != wantf {
t.Fatalf("got = %f (%#f); want %f (%#f)", gotf, gotf, wantf, wantf)
}
}
}
func TestInt64Conversion(t *testing.T) {
wants := []int64{
0,
-4,
4,
4,
}
doc := mustParse(t, "input 1/4 -24/6 4 4.1234;")
params := doc.Children[0].(*Statement).Params
if got, want := len(params), len(wants); got != want {
t.Fatalf("len(params) = %d; want %d", got, want)
}
for i, got := range params {
want := wants[i]
goti, ok := Int64(got)
if !ok {
t.Fatalf("couldn't convert got=%q to int64: ", got)
}
if goti != want {
t.Fatalf("got = %d (%016x); want %d (%016x)", goti, goti, want, want)
}
}
}