Explorar o código

Added tests for VariableDeclarationsMergeTransformer

sanex3339 %!s(int64=4) %!d(string=hai) anos
pai
achega
87aca85f3e

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
dist/index.cli.js


A diferenza do arquivo foi suprimida porque é demasiado grande
+ 0 - 0
dist/index.js


+ 173 - 0
test/functional-tests/node-transformers/minification-transformers/variable-declarations-merge-transformer/VariableDeclarationsMergeTransformer.spec.ts

@@ -0,0 +1,173 @@
+import { assert } from 'chai';
+
+import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
+
+import { readFileAsString } from '../../../../helpers/readFileAsString';
+
+import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
+
+describe('VariableDeclarationsMergeTransformer', () => {
+    describe('base behaviour', () => {
+        describe('Variant #1: single variable declaration', () => {
+            const regExp: RegExp = new RegExp(
+                'var foo *= *0x1;'
+            );
+
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/single-declaration.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        minify: true
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should keep single declaration', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
+        });
+
+        describe('Variant #2: multiple variable declarations', () => {
+            const regExp: RegExp = new RegExp(
+                'var foo *= *0x1, *' +
+                    'bar *= *0x2, *' +
+                    'baz *= *0x3;'
+            );
+
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/multiple-declarations.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        minify: true
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should merge variable declarations', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
+        });
+
+        describe('Variant #3: multiple variable declarations with multiple declarators', () => {
+            const regExp: RegExp = new RegExp(
+                'var foo *= *0x1, *' +
+                    'bar *= *0x2, *' +
+                    'baz *= *0x3, *' +
+                    'bark *= *0x4;'
+            );
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/multiple-declarations-with-multiple-declarators.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        minify: true
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should merge variable declarations', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
+        });
+
+        describe('Variant #4: Splitted declarations with other statement', () => {
+            const regExp: RegExp = new RegExp(
+                'var foo *= *0x1, *' +
+                    'bar *= *0x2; *' +
+                'console\\[\'log\']\\(\'123\'\\); *' +
+                'var baz *= *0x3, *' +
+                    'bark *= *0x4;'
+            );
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/splitted-declarations-with-other-statement.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        minify: true
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('should merge variable declarations', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
+        });
+    });
+
+    describe('object pattern as initializer', () => {
+        const regExp: RegExp = new RegExp(
+            'var foo *= *0x1, *' +
+            '{bar} *= *{\'bar\': *0x2}, *' +
+            'baz *= *0x3;'
+        );
+
+        let obfuscatedCode: string;
+
+        before(() => {
+            const code: string = readFileAsString(__dirname + '/fixtures/object-pattern-as-initializer.js');
+
+            obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                code,
+                {
+                    ...NO_ADDITIONAL_NODES_PRESET,
+                    minify: true
+                }
+            ).getObfuscatedCode();
+        });
+
+        it('should merge variable declarations with object pattern', () => {
+            assert.match(obfuscatedCode, regExp);
+        });
+    });
+
+    describe('variables kind', () => {
+        const regExp: RegExp = new RegExp(
+            'var foo *= *0x1, *' +
+                'bar *= *0x2; *' +
+            'let baz *= *0x3, *' +
+                'bark *= *0x4;' +
+            'const hawk *= *0x5, *' +
+                'pork *= *0x6;'
+        );
+
+        let obfuscatedCode: string;
+
+        before(() => {
+            const code: string = readFileAsString(__dirname + '/fixtures/different-variables-kind.js');
+
+            obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                code,
+                {
+                    ...NO_ADDITIONAL_NODES_PRESET,
+                    minify: true
+                }
+            ).getObfuscatedCode();
+        });
+
+        it('should keep unmerged variable declarations with different variable kinds', () => {
+            assert.match(obfuscatedCode, regExp);
+        });
+    });
+});

+ 6 - 0
test/functional-tests/node-transformers/minification-transformers/variable-declarations-merge-transformer/fixtures/different-variables-kind.js

@@ -0,0 +1,6 @@
+var foo = 1;
+var bar = 2;
+let baz = 3;
+let bark = 4;
+const hawk = 5;
+const pork = 6;

+ 4 - 0
test/functional-tests/node-transformers/minification-transformers/variable-declarations-merge-transformer/fixtures/multiple-declarations-with-multiple-declarators.js

@@ -0,0 +1,4 @@
+var foo = 1,
+    bar = 2;
+var baz = 3,
+    bark = 4;

+ 3 - 0
test/functional-tests/node-transformers/minification-transformers/variable-declarations-merge-transformer/fixtures/multiple-declarations.js

@@ -0,0 +1,3 @@
+var foo = 1;
+var bar = 2;
+var baz = 3;

+ 3 - 0
test/functional-tests/node-transformers/minification-transformers/variable-declarations-merge-transformer/fixtures/object-pattern-as-initializer.js

@@ -0,0 +1,3 @@
+var foo = 1;
+var {bar} = {bar: 2};
+var baz = 3;

+ 1 - 0
test/functional-tests/node-transformers/minification-transformers/variable-declarations-merge-transformer/fixtures/single-declaration.js

@@ -0,0 +1 @@
+var foo = 1;

+ 5 - 0
test/functional-tests/node-transformers/minification-transformers/variable-declarations-merge-transformer/fixtures/splitted-declarations-with-other-statement.js

@@ -0,0 +1,5 @@
+var foo = 1;
+var bar = 2;
+console.log('123');
+var baz = 3;
+var bark = 4;

+ 1 - 0
test/index.spec.ts

@@ -90,6 +90,7 @@ import './functional-tests/node-transformers/converting-transformers/split-strin
 import './functional-tests/node-transformers/converting-transformers/template-literal-transformer/TemplateLiteralTransformer.spec';
 import './functional-tests/node-transformers/dead-code-injection-transformers/DeadCodeInjectionTransformer.spec';
 import './functional-tests/node-transformers/initializing-transformers/comments-transformer/CommentsTransformer.spec';
+import './functional-tests/node-transformers/minification-transformers/variable-declarations-merge-transformer/VariableDeclarationsMergeTransformer.spec';
 import './functional-tests/node-transformers/obfuscating-transformers/labeled-statement-transformer/LabeledStatementTransformer.spec';
 import './functional-tests/node-transformers/obfuscating-transformers/literal-transformer/LiteralTransformer.spec';
 import './functional-tests/node-transformers/obfuscating-transformers/scope-identifiers-transformer/catch-clause/CatchClause.spec';

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio