-
Notifications
You must be signed in to change notification settings - Fork 1
/
exception_test.go
139 lines (123 loc) · 2.89 KB
/
exception_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package estree
import (
"testing"
)
func TestThrowStatement(t *testing.T) {
var ts ThrowStatement
if !ts.IsZero() {
t.Error("expected IsZero()")
}
ts.Argument = UpdateExpression{
Operator: Increment,
Argument: NumberLiteral{Value: 1234},
}
if ts.IsZero() {
t.Error("expected !IsZero()")
}
if ts.MinVersion() != ES5 {
t.Errorf("expected ES5, got %s", ts.MinVersion())
}
var v mockVisitor
ts.Walk(&v)
v.expect(t, ts,
ts.Argument,
ts.Argument.(UpdateExpression).Argument, nil, nil, nil)
testRoundtripJSON(t, ts, new(ThrowStatement))
if errs := ts.Errors(); len(errs) != 0 {
t.Fatalf("unexpected errors: %v", errs)
}
ts.Argument = LogicalExpression{}
if !hasError(ErrMissingNode, ts.Errors()...) {
t.Error("expected ErrMissingNode for zero Argument")
}
ts.Argument = nil
if !hasError(ErrMissingNode, ts.Errors()...) {
t.Error("expected ErrMissingNode for nil Argument")
}
}
func TestTryStatement(t *testing.T) {
var ts TryStatement
if !ts.IsZero() {
t.Error("expected IsZero()")
}
ts.Block = BlockStatement{
Body: []Statement{
ThrowStatement{
Argument: Identifier{Name: "foo"},
},
},
}
ts.Handler = CatchClause{
Param: Identifier{Name: "bar"},
Body: BlockStatement{
Body: []Statement{
ReturnStatement{},
},
},
}
ts.Finalizer = BlockStatement{
Body: []Statement{
EmptyStatement{},
},
}
if ts.IsZero() {
t.Error("expected !IsZero()")
}
if ts.MinVersion() != ES5 {
t.Errorf("expected ES5, got %s", ts.MinVersion())
}
var v mockVisitor
ts.Walk(&v)
v.expect(t, ts,
ts.Block,
ts.Block.Body[0],
ts.Block.Body[0].(ThrowStatement).Argument, nil, nil, nil,
ts.Handler,
ts.Handler.Param, nil,
ts.Handler.Body,
ts.Handler.Body.Body[0], nil, nil, nil,
ts.Finalizer,
ts.Finalizer.Body[0], nil, nil, nil)
testRoundtripJSON(t, ts, new(TryStatement))
if errs := ts.Errors(); len(errs) != 0 {
t.Fatalf("unexpected errors: %v", errs)
}
ts.Handler = CatchClause{}
ts.Finalizer = BlockStatement{}
if !hasError(ErrMissingNode, ts.Errors()...) {
t.Error("expected ErrMissingNode for zero Handler and Finalizer")
}
}
func TestCatchClause(t *testing.T) {
var cc CatchClause
if !cc.IsZero() {
t.Error("expected IsZero()")
}
cc.Param = Identifier{Name: "foo"}
cc.Body.Body = []Statement{
BreakStatement{},
}
if cc.IsZero() {
t.Error("expected !IsZero()")
}
if cc.MinVersion() != ES5 {
t.Errorf("expected ES5, got %s", cc.MinVersion())
}
var v mockVisitor
cc.Walk(&v)
v.expect(t, cc,
cc.Param, nil,
cc.Body, cc.Body.Body[0], nil, nil, nil)
testRoundtripJSON(t, cc, new(CatchClause))
if errs := cc.Errors(); len(errs) != 0 {
t.Fatalf("unexpected errors: %v", errs)
}
cc.Param = Identifier{}
if !hasError(ErrMissingNode, cc.Errors()...) {
t.Error("expected ErrMissingNode for zero Param")
}
cc.Param = nil
if !hasError(ErrMissingNode, cc.Errors()...) {
t.Error("expected ErrMissingNode for nil Param")
}
}