123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- import * as chai from 'chai';
- import * as ESTree from 'estree';
- import { CustomNodeAppender } from '../../../src/custom-nodes/CustomNodeAppender';
- import { NodeUtils } from '../../../src/NodeUtils';
- const assert: any = chai.assert;
- describe('CustomNodeAppender', () => {
- describe('appendNode (blockScopeNode: TNodeWithBlockStatement, node: ESTree.Node): void', () => {
- let astTree: ESTree.Program,
- expectedAstTree: ESTree.Program,
- node: ESTree.Node;
- beforeEach(() => {
- node = NodeUtils.convertCodeToStructure(`
- var test = 1;
- `);
- });
- it('should append node into first and deepest function call in calls trace - variant #1', () => {
- astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- baz();
- foo();
- bar();
- `, false);
- expectedAstTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
- var test = 1;
- }
-
- baz();
- foo();
- bar();
- `, false);
- CustomNodeAppender.appendNode(astTree.body, node);
- NodeUtils.parentize(astTree);
- assert.deepEqual(astTree, expectedAstTree);
- });
- it('should append node into first and deepest function call in calls trace - variant #2', () => {
- astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- bar();
- baz();
- foo();
- `, false);
- expectedAstTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
- var test = 1;
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- bar();
- baz();
- foo();
- `, false);
- CustomNodeAppender.appendNode(astTree.body, node);
- NodeUtils.parentize(astTree);
- assert.deepEqual(astTree, expectedAstTree);
- });
- describe('append by specific index', () => {
- let astTree: ESTree.Program;
- beforeEach(() => {
- astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- bar();
- baz();
- foo();
- `, false);
- });
- it('should append node into deepest function call by specified index in calls trace - variant #1', () => {
- expectedAstTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- function foo () {
- var test = 1;
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- bar();
- baz();
- foo();
- `, false);
- CustomNodeAppender.appendNode(astTree.body, node, 2);
- NodeUtils.parentize(astTree);
- assert.deepEqual(astTree, expectedAstTree);
- });
- it('should append node into deepest function call by specified index in calls trace - variant #2', () => {
- expectedAstTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
- var test = 1;
- }
-
- bar();
- baz();
- foo();
- `, false);
- CustomNodeAppender.appendNode(astTree.body, node, 1);
- NodeUtils.parentize(astTree);
- assert.deepEqual(astTree, expectedAstTree);
- });
- });
- });
- describe('getIndexByThreshold (blockStatementBodyLength: number, threshold: number = 0.1): number', () => {
- it('should returns random index between 0 and index based on threshold value', () => {
- let index: number;
- for (let i = 0; i < 10; i++) {
- index = CustomNodeAppender.getIndexByThreshold(100, 0.1);
- assert.isAtLeast(index, 0);
- assert.isAtMost(index, 10);
- }
- for (let i = 0; i < 10; i++) {
- index = CustomNodeAppender.getIndexByThreshold(10, 0.5);
- assert.isAtLeast(index, 0);
- assert.isAtMost(index, 5);
- }
- for (let i = 0; i < 10; i++) {
- index = CustomNodeAppender.getIndexByThreshold(1, 1);
- assert.isAtLeast(index, 0);
- assert.isAtMost(index, 1);
- }
- });
- });
- });
|