|
| 1 | +/* |
| 2 | + Copyright (C) 2012-2013 Yusuke Suzuki <[email protected]> |
| 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 expectedTree, actual, actualTree, options; |
| 39 | + |
| 40 | + options = { |
| 41 | + ranges: false, |
| 42 | + locations: false, |
| 43 | + ecmaVersion: 2022, |
| 44 | + }; |
| 45 | + |
| 46 | + expectedTree = acorn.parse(code, options); |
| 47 | + |
| 48 | + // for UNIX text comment |
| 49 | + actual = escodegen.generate(expectedTree) + "\n"; |
| 50 | + actualTree = acorn.parse(actual, options); |
| 51 | + |
| 52 | + console.dir(expectedTree, { depth: null }); |
| 53 | + console.dir(actualTree, { depth: null }); |
| 54 | + |
| 55 | + expect(actual).to.be.equal(expected); |
| 56 | + expect(actualTree).excludingEvery(['start', 'end', 'raw']).to.deep.equal(expectedTree); |
| 57 | +} |
| 58 | + |
| 59 | +function testMin(code, expected) { |
| 60 | + var expectedTree, actual, actualTree, options; |
| 61 | + |
| 62 | + options = { |
| 63 | + ranges: false, |
| 64 | + locations: false, |
| 65 | + ecmaVersion: 2022, |
| 66 | + }; |
| 67 | + |
| 68 | + expectedTree = acorn.parse(code, options); |
| 69 | + |
| 70 | + // for UNIX text comment |
| 71 | + actual = escodegen.generate(expectedTree, { |
| 72 | + format: escodegen.FORMAT_MINIFY, |
| 73 | + raw: false |
| 74 | + }).replace(/[\n\r]$/, '') + '\n'; |
| 75 | + actualTree = acorn.parse(actual, options); |
| 76 | + |
| 77 | + expect(actual).to.be.equal(expected); |
| 78 | + expect(actualTree).excludingEvery(['start', 'end', 'raw']).to.deep.equal(expectedTree); |
| 79 | +} |
| 80 | + |
| 81 | +describe('compare acorn es2022 test', function () { |
| 82 | + fs.readdirSync(__dirname + '/compare-acorn-es2022').sort().forEach(function(file) { |
| 83 | + var code, expected, exp, min; |
| 84 | + if (/\.js$/.test(file) && !/expected\.js$/.test(file) && !/expected\.min\.js$/.test(file)) { |
| 85 | + it(file, function () { |
| 86 | + exp = file.replace(/\.js$/, '.expected.js'); |
| 87 | + min = file.replace(/\.js$/, '.expected.min.js'); |
| 88 | + code = fs.readFileSync(__dirname + '/compare-acorn-es2022/' + file, 'utf-8'); |
| 89 | + expected = fs.readFileSync(__dirname + '/compare-acorn-es2022/' + exp, 'utf-8'); |
| 90 | + test(code, expected); |
| 91 | + if (fs.existsSync(__dirname + '/compare-acorn-es2022/' + min)) { |
| 92 | + expected = fs.readFileSync(__dirname + '/compare-acorn-es2022/' + min, 'utf-8'); |
| 93 | + testMin(code, expected); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + }); |
| 98 | +}); |
| 99 | +/* vim: set sw=4 ts=4 et tw=80 : */ |
0 commit comments