forked from mvdan/sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmain.js
251 lines (220 loc) · 5.3 KB
/
testmain.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const assert = require('assert').strict
const stream = require('stream')
const sh = require('./index')
const syntax = sh.syntax
const parser = syntax.NewParser()
const printer = syntax.NewPrinter()
{
// parsing a simple program
const src = "echo 'foo'"
var f = parser.Parse(src, "src")
var stmts = f.StmtList.Stmts
assert.equal(stmts.length, 1)
var args = stmts[0].Cmd.Args
assert.equal(args.length, 2)
assert.equal(args[0].Parts.length, 1)
assert.equal(args[0].Parts[0].Value, "echo")
}
{
// accessing fields or methods creates separate objects
const src = "echo 'foo'"
var f = parser.Parse(src, "src")
assert.equal(f.StmtList.Stmts == f.StmtList.Stmts, false)
assert.equal(f.StmtList.Stmts === f.StmtList.Stmts, false)
var stmts = f.StmtList.Stmts
assert.equal(stmts == stmts, true)
assert.equal(stmts === stmts, true)
}
{
// fatal parse error
const src = "echo )"
try {
parser.Parse(src, "src")
assert.fail("did not error")
} catch (err) {
assert.equal(err.Filename, "src")
assert.equal(err.Pos.Line(), 1)
assert.equal(syntax.IsIncomplete(err), false)
}
}
{
// incomplete parse error
const src = "echo ${"
try {
parser.Parse(src, "src")
assert.fail("did not error")
} catch (err) {
assert.equal(syntax.IsIncomplete(err), true)
}
}
{
// js error from the wrapper layer
var foo = {}
try {
syntax.NodeType(foo)
assert.fail("did not error")
} catch (err) {
assert.equal(err.message.includes("requires a Node argument"), true)
}
}
{
// node types, operators, and positions
const src = "foo || bar"
var f = parser.Parse(src, "src")
var cmd = f.StmtList.Stmts[0].Cmd
assert.equal(syntax.NodeType(cmd), "BinaryCmd")
// TODO: see https://github.com/myitcv/gopherjs/issues/26
// assert.equal(syntax.String(cmd.Op), "||")
assert.equal(cmd.Pos().String(), "1:1")
assert.equal(cmd.OpPos.String(), "1:5")
assert.equal(cmd.OpPos.Line(), 1)
assert.equal(cmd.OpPos.Col(), 5)
assert.equal(cmd.OpPos.Offset(), 4)
}
{
// running Walk
const src = "foo bar"
var f = parser.Parse(src, "src")
var nilCount = 0
var nonNilCount = 0
var seenBar = false
var seenCall = false
syntax.Walk(f, function(node) {
var typ = syntax.NodeType(node)
if (node == null) {
nilCount++
assert.equal(typ, "nil")
} else {
nonNilCount++
if (node.Value == "bar") {
seenBar = true
}
assert.notEqual(typ, "nil")
}
if (typ == "CallExpr") {
seenCall = true
}
return true
})
assert.equal(nonNilCount, 7)
assert.equal(nilCount, 7)
assert.equal(seenBar, true)
assert.equal(seenCall, true)
}
{
// printing
const src = "echo 'foo'"
var f = parser.Parse(src, "src")
var out = printer.Print(f)
assert.equal(out, "echo 'foo'\n")
}
{
// parser options
const parser = syntax.NewParser(
syntax.KeepComments,
syntax.Variant(syntax.LangMirBSDKorn),
syntax.StopAt("$$")
)
const src = "echo ${|stmts;} # bar\n$$"
var f = parser.Parse(src, "src")
var out = printer.Print(f)
assert.equal(out, "echo ${|stmts;} # bar\n")
}
{
// parsing a readable stream
const src = new stream.Readable
src.push("echo foo")
src.push(null)
var f = parser.Parse(src, "src")
var cmd = f.StmtList.Stmts[0].Cmd
assert.equal(cmd.Args.length, 2)
}
{
// using the parser interactively
const lines = [
"foo\n",
"bar; baz\n",
"\n",
"foo; 'incom\n",
" \n",
"plete'\n",
]
const wantCallbacks = [
{"count": 1, "incomplete": false},
{"count": 2, "incomplete": false},
{"count": 0, "incomplete": false},
{"count": 1, "incomplete": true},
{"count": 1, "incomplete": true},
{"count": 2, "incomplete": false},
]
var gotCallbacks = []
const src = {"read": function(size) {
if (lines.length == 0) {
if (gotCallbacks.length == 0) {
throw "did not see any callbacks before EOF"
}
return null // EOF
}
s = lines[0]
lines.shift()
return s
}}
parser.Interactive(src, function(stmts) {
for (var i in stmts) {
var stmt = stmts[i]
assert.equal(syntax.NodeType(stmt), "Stmt")
}
gotCallbacks.push({
"count": stmts.length,
"incomplete": parser.Incomplete(),
})
return true
})
assert.deepEqual(gotCallbacks, wantCallbacks)
}
{
// using the parser interactively with steps
const lines = [
"foo\n",
"bar; baz\n",
"\n",
"foo; 'incom\n",
" \n",
"plete'\n",
]
const wantResults = [
{"count": 1, "incomplete": false},
{"count": 2, "incomplete": false},
{"count": 0, "incomplete": false},
{"count": 1, "incomplete": true},
{"count": 1, "incomplete": true},
{"count": 2, "incomplete": false},
]
var gotResults = []
for (var i = 0; i < lines.length; i++) {
var line = lines[i]
var want = wantResults[i]
var stmts = parser.InteractiveStep(line)
gotResults.push({
"count": stmts.length,
"incomplete": parser.Incomplete(),
})
}
assert.deepEqual(gotResults, wantResults)
}
{
// splitting brace expressions
const parser = syntax.NewParser()
const src = "{foo,bar}"
var f = parser.Parse(src, "src")
var word = f.StmtList.Stmts[0].Cmd.Args[0]
assert.equal(word.Parts.length, 1)
assert.equal(syntax.NodeType(word.Parts[0]), "Lit")
word = syntax.SplitBraces(word)
// TODO: get rid of the empty lit
assert.equal(word.Parts.length, 2)
assert.equal(syntax.NodeType(word.Parts[0]), "BraceExp")
assert.equal(word.Parts[0].Elems.length, 2)
assert.equal(syntax.NodeType(word.Parts[1]), "Lit")
assert.equal(word.Parts[1].Value, "")
}