Added tests for EsprimaFacade

This commit is contained in:
sanex3339
2018-02-17 20:54:57 +03:00
parent 92475af89e
commit 7753d53028
5 changed files with 85 additions and 3 deletions
+1
View File
@@ -17,6 +17,7 @@ import './unit-tests/cli/utils/SourceCodeReader.spec';
import './unit-tests/decorators/initializable/Initializable.spec';
import './unit-tests/generators/identifier-names-generators/HexadecimalIdentifierNamesGenerator.spec';
import './unit-tests/generators/identifier-names-generators/MangledlIdentifierNamesGenerator.spec';
import './unit-tests/javascript-obfuscator/EsprimaFacade.spec';
import './unit-tests/javascript-obfuscator/JavaScriptObfuscator.spec';
import './unit-tests/logger/Logger.spec';
import './unit-tests/node/node-appender/NodeAppender.spec';
@@ -0,0 +1,80 @@
import { assert } from 'chai';
import { EsprimaFacade } from '../../../src/EsprimaFacade';
describe('EsprimaFacade', () => {
describe(`parseScript (input: string, config: esprima.ParseOptions): ESTree.Program`, () => {
describe(`\`Unexpected token\` error code preview`, () => {
describe('variant #1: 5 lines of code', () => {
const sourceCode: string = `` +
`var foo = 1;
var bar = 2;
var baz = 3;,
var bark = 4;
var hawk = 5;`;
let testFunc: () => void;
before(() => {
testFunc = () => EsprimaFacade.parseScript(sourceCode, {});
});
it('should output code preview when `esprima` throws a parse error', () => {
assert.throws(testFunc, /Line 3: Unexpected token ,\n.*\.\.\.var baz = 3;,\.\.\./);
});
});
describe('variant #2: 15 lines of code', () => {
const sourceCode: string = `` +
`var var1 = 1;
var var2 = 2;
var var3 = 3;
var var4 = 4;
var var5 = 5;
var var6 = 6;
var var7 = 7;
var var8 = 8;
var var9 = 9;
var var10 = 10;
var foo = 1;
var bar = 2;
var baz = 3;,
var bark = 4;
var hawk = 5;`;
let testFunc: () => void;
before(() => {
testFunc = () => EsprimaFacade.parseScript(sourceCode, {});
});
it('should output code preview when `esprima` throws a parse error', () => {
assert.throws(testFunc, /Line 13: Unexpected token ,\n.*\.\.\.var baz = 3;,\.\.\./);
});
});
});
describe(`\`Unexpected identifier\` error code preview`, () => {
const sourceCode: string = `` +
`function bar () {
var a = 1;
}
functin baz () {
var a = 1;
}
function bark () {
var a = 1;
}`;
let testFunc: () => void;
before(() => {
testFunc = () => EsprimaFacade.parseScript(sourceCode, {});
});
it('should output code preview when `esprima` throws a parse error', () => {
assert.throws(testFunc, /Line 4: Unexpected identifier\n.*\.\.\.functin baz \(\) {\.\.\./);
});
});
});
});