Skip to content

Commit b7d0155

Browse files
authored
Merge pull request webpack#4041 from shubheksha/refactor-test-Parser
refactor(ES6): Parser.test.js
2 parents 41dfaf2 + c04cdcc commit b7d0155

File tree

1 file changed

+100
-85
lines changed

1 file changed

+100
-85
lines changed

test/Parser.test.js

Lines changed: 100 additions & 85 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,6 +75,16 @@ describe("Parser", function() {
7375
})();
7476
}, {}
7577
],
78+
"const definition": [
79+
function() {
80+
let abc, cde, fgh;
81+
abc("test");
82+
cde.abc("test");
83+
cde.ddd.abc("test");
84+
fgh;
85+
fgh.sub;
86+
}, {}
87+
],
7688
"var definition": [
7789
function() {
7890
var abc, cde, fgh;
@@ -116,6 +128,14 @@ describe("Parser", function() {
116128
fgh: ["", "test ttt", "test e"]
117129
}
118130
],
131+
"renaming with const": [
132+
function() {
133+
const xyz = abc;
134+
xyz("test");
135+
}, {
136+
abc: ["test"]
137+
}
138+
],
119139
"renaming with var": [
120140
function() {
121141
var xyz = abc;
@@ -126,8 +146,7 @@ describe("Parser", function() {
126146
],
127147
"renaming with assignment": [
128148
function() {
129-
var xyz;
130-
xyz = abc;
149+
const xyz = abc;
131150
xyz("test");
132151
}, {
133152
abc: ["test"]
@@ -154,96 +173,92 @@ describe("Parser", function() {
154173
],
155174
};
156175

157-
Object.keys(testCases).forEach(function(name) {
158-
it("should parse " + name, function() {
159-
var source = testCases[name][0].toString();
176+
Object.keys(testCases).forEach((name) => {
177+
it("should parse " + name, () => {
178+
let source = testCases[name][0].toString();
160179
source = source.substr(13, source.length - 14).trim();
161-
var state = testCases[name][1];
180+
const state = testCases[name][1];
162181

163-
var testParser = new Parser({});
164-
testParser.plugin("can-rename abc", function(expr) {
182+
const testParser = new Parser({});
183+
testParser.plugin("can-rename abc", (expr) => true);
184+
testParser.plugin("call abc", (expr) => {
185+
if(!testParser.state.abc) testParser.state.abc = [];
186+
testParser.state.abc.push(testParser.parseString(expr.arguments[0]));
165187
return true;
166188
});
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]));
189+
testParser.plugin("call cde.abc", (expr) => {
190+
if(!testParser.state.cdeabc) testParser.state.cdeabc = [];
191+
testParser.state.cdeabc.push(testParser.parseString(expr.arguments[0]));
170192
return true;
171193
});
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]));
194+
testParser.plugin("call cde.ddd.abc", (expr) => {
195+
if(!testParser.state.cdedddabc) testParser.state.cdedddabc = [];
196+
testParser.state.cdedddabc.push(testParser.parseString(expr.arguments[0]));
175197
return true;
176198
});
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]));
199+
testParser.plugin("expression fgh", (expr) => {
200+
if(!testParser.state.fgh) testParser.state.fgh = [];
201+
testParser.state.fgh.push(testParser.scope.definitions.join(" "));
180202
return true;
181203
});
182-
testParser.plugin("expression fgh", function(expr) {
183-
if(!this.state.fgh) this.state.fgh = []
184-
this.state.fgh.push(this.scope.definitions.join(" "));
204+
testParser.plugin("expression fgh.sub", (expr) => {
205+
if(!testParser.state.fghsub) testParser.state.fghsub = [];
206+
testParser.state.fghsub.push(testParser.scope.inTry ? "try" : "notry");
185207
return true;
186208
});
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");
209+
testParser.plugin("expression memberExpr", (expr) => {
210+
if(!testParser.state.expressions) testParser.state.expressions = [];
211+
testParser.state.expressions.push(expr.name);
190212
return true;
191213
});
192-
testParser.plugin("expression memberExpr", function(expr) {
193-
if(!this.state.expressions) this.state.expressions = []
194-
this.state.expressions.push(expr.name);
195-
return true;
196-
});
197-
var actual = testParser.parse(source);
214+
const actual = testParser.parse(source);
198215
should.strictEqual(typeof actual, "object");
199216
actual.should.be.eql(state);
200217
});
201218
});
202219

203-
it("should parse comments", function() {
204-
var source = "//comment1\n/*comment2*/";
205-
var state = [{
206-
type: 'Line',
207-
value: 'comment1'
220+
it("should parse comments", () => {
221+
const source = "//comment1\n/*comment2*/";
222+
const state = [{
223+
type: "Line",
224+
value: "comment1"
208225
}, {
209-
type: 'Block',
210-
value: 'comment2'
226+
type: "Block",
227+
value: "comment2"
211228
}];
212229

213-
var testParser = new Parser({});
230+
const testParser = new Parser({});
214231

215-
testParser.plugin("program", function(ast, comments) {
216-
if(!this.state.comments) this.state.comments = comments;
232+
testParser.plugin("program", (ast, comments) => {
233+
if(!testParser.state.comments) testParser.state.comments = comments;
217234
return true;
218235
});
219236

220-
var actual = testParser.parse(source);
237+
const actual = testParser.parse(source);
221238
should.strictEqual(typeof actual, "object");
222239
should.strictEqual(typeof actual.comments, "object");
223-
actual.comments.forEach(function(element, index) {
240+
actual.comments.forEach((element, index) => {
224241
should.strictEqual(typeof element.type, "string");
225242
should.strictEqual(typeof element.value, "string");
226243
element.type.should.be.eql(state[index].type);
227244
element.value.should.be.eql(state[index].value);
228245
});
229246
});
230247

231-
describe("expression evaluation", function() {
248+
describe("expression evaluation", () => {
232249
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);
250+
const parser = new Parser();
251+
parser.plugin("call test", (expr) => {
252+
parser.state.result = parser.evaluateExpression(expr.arguments[0]);
242253
});
254+
parser.plugin("evaluate Identifier aString", (expr) =>
255+
new BasicEvaluatedExpression().setString("aString").setRange(expr.range));
256+
parser.plugin("evaluate Identifier b.Number", (expr) =>
257+
new BasicEvaluatedExpression().setNumber(123).setRange(expr.range));
243258
return parser.parse("test(" + source + ");").result;
244259
}
245260

246-
var testCases = {
261+
const testCases = {
247262
"\"strrring\"": "string=strrring",
248263
"\"strr\" + \"ring\"": "string=strrring",
249264
"\"s\" + (\"trr\" + \"rin\") + \"g\"": "string=strrring",
@@ -279,81 +294,81 @@ describe("Parser", function() {
279294
"'abc'[substr](1)": "",
280295
};
281296

282-
Object.keys(testCases).forEach(function(key) {
297+
Object.keys(testCases).forEach((key) => {
283298

284299
function evalExprToString(evalExpr) {
285300
if(!evalExpr) {
286301
return "null";
287302
} else {
288-
var result = [];
303+
const result = [];
289304
if(evalExpr.isString()) result.push("string=" + evalExpr.string);
290305
if(evalExpr.isNumber()) result.push("number=" + evalExpr.number);
291306
if(evalExpr.isRegExp()) result.push("regExp=" + evalExpr.regExp);
292307
if(evalExpr.isConditional()) result.push("options=[" + evalExpr.options.map(evalExprToString).join("],[") + "]");
293308
if(evalExpr.isArray()) result.push("items=[" + evalExpr.items.map(evalExprToString).join("],[") + "]");
294309
if(evalExpr.isWrapped()) result.push("wrapped=[" + evalExprToString(evalExpr.prefix) + "]+[" + evalExprToString(evalExpr.postfix) + "]");
295310
if(evalExpr.range) {
296-
var start = evalExpr.range[0] - 5;
297-
var end = evalExpr.range[1] - 5;
311+
const start = evalExpr.range[0] - 5;
312+
const end = evalExpr.range[1] - 5;
298313
return key.substr(start, end - start) + (result.length > 0 ? " " + result.join(" ") : "");
299314
}
300315
return result.join(" ");
301316
}
302317
}
303318

304-
it("should eval " + key, function() {
305-
var evalExpr = evaluateInParser(key);
319+
it("should eval " + key, () => {
320+
const evalExpr = evaluateInParser(key);
306321
evalExprToString(evalExpr).should.be.eql(testCases[key] ? key + " " + testCases[key] : key);
307322
});
308323
});
309324
});
310325

311-
describe("async/await support", function() {
312-
describe("should accept", function() {
313-
var cases = {
326+
describe("async/await support", () => {
327+
describe("should accept", () => {
328+
const cases = {
314329
"async function": "async function x() {}",
315330
"async arrow function": "async () => {}",
316331
"await expression": "async function x(y) { await y }"
317332
};
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);
333+
const parser = new Parser();
334+
Object.keys(cases).forEach((name) => {
335+
const expr = cases[name];
336+
it(name, () => {
337+
const actual = parser.parse(expr);
323338
should.strictEqual(typeof actual, "object");
324339
});
325340
});
326341
});
327-
describe("should parse await", function() {
328-
var cases = {
342+
describe("should parse await", () => {
343+
const cases = {
329344
"require": [
330345
"async function x() { await require('y'); }", {
331346
param: "y"
332347
}
333348
],
334349
"System.import": [
335-
"async function x() { var y = await System.import('z'); }", {
350+
"async function x() { const y = await System.import('z'); }", {
336351
param: "z"
337352
}
338353
]
339354
};
340355

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;
356+
const parser = new Parser();
357+
parser.plugin("call require", (expr) => {
358+
const param = parser.evaluateExpression(expr.arguments[0]);
359+
parser.state.param = param.string;
345360
});
346-
parser.plugin("call System.import", function(expr) {
347-
var param = this.evaluateExpression(expr.arguments[0]);
348-
this.state.param = param.string;
361+
parser.plugin("call System.import", (expr) => {
362+
const param = parser.evaluateExpression(expr.arguments[0]);
363+
parser.state.param = param.string;
349364
});
350365

351-
Object.keys(cases).forEach(function(name) {
352-
it(name, function() {
353-
var actual = parser.parse(cases[name][0]);
366+
Object.keys(cases).forEach((name) => {
367+
it(name, () => {
368+
const actual = parser.parse(cases[name][0]);
354369
actual.should.be.eql(cases[name][1]);
355370
});
356371
});
357372
});
358-
})
373+
});
359374
});

0 commit comments

Comments
 (0)