Skip to content

Commit d91d17f

Browse files
committed
refactor(ES6): Parser.test.js
1 parent d9ac043 commit d91d17f

1 file changed

Lines changed: 86 additions & 89 deletions

File tree

‎test/Parser.test.js‎

Lines changed: 86 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
var should = require("should");
1+
"use strict";
22

3-
var Parser = require("../lib/Parser");
4-
var BasicEvaluatedExpression = require("../lib/BasicEvaluatedExpression");
3+
const should = require("should");
54

6-
describe("Parser", function() {
7-
var testCases = {
5+
const Parser = require("../lib/Parser");
6+
const BasicEvaluatedExpression = require("../lib/BasicEvaluatedExpression");
7+
8+
describe("Parser", () => {
9+
const testCases = {
810
"call ident": [
911
function() {
1012
abc("test");
@@ -73,9 +75,9 @@ describe("Parser", function() {
7375
})();
7476
}, {}
7577
],
76-
"var definition": [
78+
"const definition": [
7779
function() {
78-
var abc, cde, fgh;
80+
let abc, cde, fgh;
7981
abc("test");
8082
cde.abc("test");
8183
cde.ddd.abc("test");
@@ -116,18 +118,17 @@ describe("Parser", function() {
116118
fgh: ["", "test ttt", "test e"]
117119
}
118120
],
119-
"renaming with var": [
121+
"renaming with const": [
120122
function() {
121-
var xyz = abc;
123+
const xyz = abc;
122124
xyz("test");
123125
}, {
124126
abc: ["test"]
125127
}
126128
],
127129
"renaming with assignment": [
128130
function() {
129-
var xyz;
130-
xyz = abc;
131+
const xyz = abc;
131132
xyz("test");
132133
}, {
133134
abc: ["test"]
@@ -154,96 +155,92 @@ describe("Parser", function() {
154155
],
155156
};
156157

157-
Object.keys(testCases).forEach(function(name) {
158-
it("should parse " + name, function() {
159-
var source = testCases[name][0].toString();
158+
Object.keys(testCases).forEach((name) => {
159+
it("should parse " + name, () => {
160+
let source = testCases[name][0].toString();
160161
source = source.substr(13, source.length - 14).trim();
161-
var state = testCases[name][1];
162+
const state = testCases[name][1];
162163

163-
var testParser = new Parser({});
164-
testParser.plugin("can-rename abc", function(expr) {
165-
return true;
166-
});
167-
testParser.plugin("call abc", function(expr) {
168-
if(!this.state.abc) this.state.abc = []
169-
this.state.abc.push(this.parseString(expr.arguments[0]));
164+
const testParser = new Parser({});
165+
testParser.plugin("can-rename abc", (expr) => true);
166+
testParser.plugin("call abc", (expr) => {
167+
if(!testParser.state.abc) testParser.state.abc = [];
168+
testParser.state.abc.push(testParser.parseString(expr.arguments[0]));
170169
return true;
171170
});
172-
testParser.plugin("call cde.abc", function(expr) {
173-
if(!this.state.cdeabc) this.state.cdeabc = []
174-
this.state.cdeabc.push(this.parseString(expr.arguments[0]));
171+
testParser.plugin("call cde.abc", (expr) => {
172+
if(!testParser.state.cdeabc) testParser.state.cdeabc = [];
173+
testParser.state.cdeabc.push(testParser.parseString(expr.arguments[0]));
175174
return true;
176175
});
177-
testParser.plugin("call cde.ddd.abc", function(expr) {
178-
if(!this.state.cdedddabc) this.state.cdedddabc = []
179-
this.state.cdedddabc.push(this.parseString(expr.arguments[0]));
176+
testParser.plugin("call cde.ddd.abc", (expr) => {
177+
if(!testParser.state.cdedddabc) testParser.state.cdedddabc = [];
178+
testParser.state.cdedddabc.push(testParser.parseString(expr.arguments[0]));
180179
return true;
181180
});
182-
testParser.plugin("expression fgh", function(expr) {
183-
if(!this.state.fgh) this.state.fgh = []
184-
this.state.fgh.push(this.scope.definitions.join(" "));
181+
testParser.plugin("expression fgh", (expr) => {
182+
if(!testParser.state.fgh) testParser.state.fgh = [];
183+
testParser.state.fgh.push(testParser.scope.definitions.join(" "));
185184
return true;
186185
});
187-
testParser.plugin("expression fgh.sub", function(expr) {
188-
if(!this.state.fghsub) this.state.fghsub = []
189-
this.state.fghsub.push(this.scope.inTry ? "try" : "notry");
186+
testParser.plugin("expression fgh.sub", (expr) => {
187+
if(!testParser.state.fghsub) testParser.state.fghsub = [];
188+
testParser.state.fghsub.push(testParser.scope.inTry ? "try" : "notry");
190189
return true;
191190
});
192-
testParser.plugin("expression memberExpr", function(expr) {
193-
if(!this.state.expressions) this.state.expressions = []
194-
this.state.expressions.push(expr.name);
191+
testParser.plugin("expression memberExpr", (expr) => {
192+
if(!testParser.state.expressions) testParser.state.expressions = [];
193+
testParser.state.expressions.push(expr.name);
195194
return true;
196195
});
197-
var actual = testParser.parse(source);
196+
const actual = testParser.parse(source);
198197
should.strictEqual(typeof actual, "object");
199198
actual.should.be.eql(state);
200199
});
201200
});
202201

203-
it("should parse comments", function() {
204-
var source = "//comment1\n/*comment2*/";
205-
var state = [{
206-
type: 'Line',
207-
value: 'comment1'
202+
it("should parse comments", () => {
203+
const source = "//comment1\n/*comment2*/";
204+
const state = [{
205+
type: "Line",
206+
value: "comment1"
208207
}, {
209-
type: 'Block',
210-
value: 'comment2'
208+
type: "Block",
209+
value: "comment2"
211210
}];
212211

213-
var testParser = new Parser({});
212+
const testParser = new Parser({});
214213

215-
testParser.plugin("program", function(ast, comments) {
216-
if(!this.state.comments) this.state.comments = comments;
214+
testParser.plugin("program", (ast, comments) => {
215+
if(!testParser.state.comments) testParser.state.comments = comments;
217216
return true;
218217
});
219218

220-
var actual = testParser.parse(source);
219+
const actual = testParser.parse(source);
221220
should.strictEqual(typeof actual, "object");
222221
should.strictEqual(typeof actual.comments, "object");
223-
actual.comments.forEach(function(element, index) {
222+
actual.comments.forEach((element, index) => {
224223
should.strictEqual(typeof element.type, "string");
225224
should.strictEqual(typeof element.value, "string");
226225
element.type.should.be.eql(state[index].type);
227226
element.value.should.be.eql(state[index].value);
228227
});
229228
});
230229

231-
describe("expression evaluation", function() {
230+
describe("expression evaluation", () => {
232231
function evaluateInParser(source) {
233-
var parser = new Parser();
234-
parser.plugin("call test", function(expr) {
235-
this.state.result = this.evaluateExpression(expr.arguments[0]);
236-
});
237-
parser.plugin("evaluate Identifier aString", function(expr) {
238-
return new BasicEvaluatedExpression().setString("aString").setRange(expr.range);
239-
});
240-
parser.plugin("evaluate Identifier b.Number", function(expr) {
241-
return new BasicEvaluatedExpression().setNumber(123).setRange(expr.range);
232+
const parser = new Parser();
233+
parser.plugin("call test", (expr) => {
234+
parser.state.result = parser.evaluateExpression(expr.arguments[0]);
242235
});
236+
parser.plugin("evaluate Identifier aString", (expr) =>
237+
new BasicEvaluatedExpression().setString("aString").setRange(expr.range));
238+
parser.plugin("evaluate Identifier b.Number", (expr) =>
239+
new BasicEvaluatedExpression().setNumber(123).setRange(expr.range));
243240
return parser.parse("test(" + source + ");").result;
244241
}
245242

246-
var testCases = {
243+
const testCases = {
247244
"\"strrring\"": "string=strrring",
248245
"\"strr\" + \"ring\"": "string=strrring",
249246
"\"s\" + (\"trr\" + \"rin\") + \"g\"": "string=strrring",
@@ -279,81 +276,81 @@ describe("Parser", function() {
279276
"'abc'[substr](1)": "",
280277
};
281278

282-
Object.keys(testCases).forEach(function(key) {
279+
Object.keys(testCases).forEach((key) => {
283280

284281
function evalExprToString(evalExpr) {
285282
if(!evalExpr) {
286283
return "null";
287284
} else {
288-
var result = [];
285+
const result = [];
289286
if(evalExpr.isString()) result.push("string=" + evalExpr.string);
290287
if(evalExpr.isNumber()) result.push("number=" + evalExpr.number);
291288
if(evalExpr.isRegExp()) result.push("regExp=" + evalExpr.regExp);
292289
if(evalExpr.isConditional()) result.push("options=[" + evalExpr.options.map(evalExprToString).join("],[") + "]");
293290
if(evalExpr.isArray()) result.push("items=[" + evalExpr.items.map(evalExprToString).join("],[") + "]");
294291
if(evalExpr.isWrapped()) result.push("wrapped=[" + evalExprToString(evalExpr.prefix) + "]+[" + evalExprToString(evalExpr.postfix) + "]");
295292
if(evalExpr.range) {
296-
var start = evalExpr.range[0] - 5;
297-
var end = evalExpr.range[1] - 5;
293+
const start = evalExpr.range[0] - 5;
294+
const end = evalExpr.range[1] - 5;
298295
return key.substr(start, end - start) + (result.length > 0 ? " " + result.join(" ") : "");
299296
}
300297
return result.join(" ");
301298
}
302299
}
303300

304-
it("should eval " + key, function() {
305-
var evalExpr = evaluateInParser(key);
301+
it("should eval " + key, () => {
302+
const evalExpr = evaluateInParser(key);
306303
evalExprToString(evalExpr).should.be.eql(testCases[key] ? key + " " + testCases[key] : key);
307304
});
308305
});
309306
});
310307

311-
describe("async/await support", function() {
312-
describe("should accept", function() {
313-
var cases = {
308+
describe("async/await support", () => {
309+
describe("should accept", () => {
310+
const cases = {
314311
"async function": "async function x() {}",
315312
"async arrow function": "async () => {}",
316313
"await expression": "async function x(y) { await y }"
317314
};
318-
var parser = new Parser();
319-
Object.keys(cases).forEach(function(name) {
320-
var expr = cases[name];
321-
it(name, function() {
322-
var actual = parser.parse(expr);
315+
const parser = new Parser();
316+
Object.keys(cases).forEach((name) => {
317+
const expr = cases[name];
318+
it(name, () => {
319+
const actual = parser.parse(expr);
323320
should.strictEqual(typeof actual, "object");
324321
});
325322
});
326323
});
327-
describe("should parse await", function() {
328-
var cases = {
324+
describe("should parse await", () => {
325+
const cases = {
329326
"require": [
330327
"async function x() { await require('y'); }", {
331328
param: "y"
332329
}
333330
],
334331
"System.import": [
335-
"async function x() { var y = await System.import('z'); }", {
332+
"async function x() { const y = await System.import('z'); }", {
336333
param: "z"
337334
}
338335
]
339336
};
340337

341-
var parser = new Parser();
342-
parser.plugin("call require", function(expr) {
343-
var param = this.evaluateExpression(expr.arguments[0]);
344-
this.state.param = param.string;
338+
const parser = new Parser();
339+
parser.plugin("call require", (expr) => {
340+
const param = parser.evaluateExpression(expr.arguments[0]);
341+
parser.state.param = param.string;
345342
});
346-
parser.plugin("call System.import", function(expr) {
347-
var param = this.evaluateExpression(expr.arguments[0]);
348-
this.state.param = param.string;
343+
parser.plugin("call System.import", (expr) => {
344+
const param = parser.evaluateExpression(expr.arguments[0]);
345+
parser.state.param = param.string;
349346
});
350347

351-
Object.keys(cases).forEach(function(name) {
352-
it(name, function() {
353-
var actual = parser.parse(cases[name][0]);
348+
Object.keys(cases).forEach((name) => {
349+
it(name, () => {
350+
const actual = parser.parse(cases[name][0]);
354351
actual.should.be.eql(cases[name][1]);
355352
});
356353
});
357354
});
358-
})
355+
});
359356
});

0 commit comments

Comments
 (0)