Skip to content

Commit 7e74eb8

Browse files
committed
Test logical assignments
escodegen can transparently support logical assignments without any code. But we should test it by using acorn with 2021 option. This patch adds logical-assignments test cases.
1 parent 2781cf5 commit 7e74eb8

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

test/compare-acorn-es2021.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com>
3+
Copyright (C) 2020 Apple Inc. All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
* Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
18+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
*/
25+
26+
'use strict';
27+
28+
var fs = require('fs'),
29+
acorn = require('acorn'),
30+
escodegen = require('./loader'),
31+
chai = require('chai'),
32+
chaiExclude = require('chai-exclude'),
33+
expect = chai.expect;
34+
35+
chai.use(chaiExclude);
36+
37+
function test(code, expected) {
38+
var tree, actual, actualTree, options;
39+
40+
options = {
41+
ranges: false,
42+
locations: false,
43+
ecmaVersion: 12
44+
};
45+
46+
tree = acorn.parse(code, options);
47+
48+
// for UNIX text comment
49+
actual = escodegen.generate(tree);
50+
actualTree = acorn.parse(actual, options);
51+
52+
expect(actual).to.be.equal(expected);
53+
expect(tree).excludingEvery(['start', 'end', 'raw']).to.deep.equal(actualTree);
54+
}
55+
56+
function testMin(code, expected) {
57+
var tree, actual, actualTree, options;
58+
59+
options = {
60+
ranges: false,
61+
locations: false,
62+
ecmaVersion: 12
63+
};
64+
65+
tree = acorn.parse(code, options);
66+
67+
// for UNIX text comment
68+
actual = escodegen.generate(tree, {
69+
format: escodegen.FORMAT_MINIFY,
70+
raw: false
71+
}).replace(/[\n\r]$/, '') + '\n';
72+
actualTree = acorn.parse(actual, options);
73+
74+
expect(actual).to.be.equal(expected);
75+
expect(tree).excludingEvery(['start', 'end', 'raw']).to.deep.equal(actualTree);
76+
}
77+
78+
describe('compare acorn es2021 test', function () {
79+
fs.readdirSync(__dirname + '/compare-acorn-es2021').sort().forEach(function(file) {
80+
var code, expected, exp, min;
81+
if (/\.js$/.test(file) && !/expected\.js$/.test(file) && !/expected\.min\.js$/.test(file)) {
82+
it(file, function () {
83+
exp = file.replace(/\.js$/, '.expected.js');
84+
min = file.replace(/\.js$/, '.expected.min.js');
85+
code = fs.readFileSync(__dirname + '/compare-acorn-es2021/' + file, 'utf-8');
86+
expected = fs.readFileSync(__dirname + '/compare-acorn-es2021/' + exp, 'utf-8');
87+
test(code, expected);
88+
if (fs.existsSync(__dirname + '/compare-acorn-es2021/' + min)) {
89+
expected = fs.readFileSync(__dirname + '/compare-acorn-es2021/' + min, 'utf-8');
90+
testMin(code, expected);
91+
}
92+
});
93+
}
94+
});
95+
});
96+
/* vim: set sw=4 ts=4 et tw=80 : */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
undefined &&= 42;
2+
x &&= ++counter;
3+
x.a &&= 42;
4+
x['a'] &&= 42;
5+
undefined ||= 42;
6+
x ||= ++counter;
7+
x.a ||= 42;
8+
x['a'] ||= 42;
9+
undefined ??= 42;
10+
x ??= ++counter;
11+
x.a ??= 42;
12+
x['a'] ??= 42;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
undefined&&=42;x&&=++counter;x.a&&=42;x['a']&&=42;undefined||=42;x||=++counter;x.a||=42;x['a']||=42;undefined??=42;x??=++counter;x.a??=42;x['a']??=42
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
undefined &&= 42;
2+
x &&= ++counter;
3+
x.a &&= 42;
4+
x['a'] &&= 42;
5+
6+
undefined ||= 42;
7+
x ||= ++counter;
8+
x.a ||= 42;
9+
x['a'] ||= 42;
10+
11+
undefined ??= 42;
12+
x ??= ++counter;
13+
x.a ??= 42;
14+
x['a'] ??= 42;

0 commit comments

Comments
 (0)