123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- import * as chai from 'chai';
- import * as ESTree from 'estree';
- import { TStatement } from '../../../src/types/TStatement';
- import { IStackTraceData } from '../../../src/interfaces/stack-trace-analyzer/IStackTraceData';
- import { CustomNodeAppender } from '../../../src/custom-nodes/CustomNodeAppender';
- import { NodeMocks } from '../../mocks/NodeMocks';
- import { NodeUtils } from '../../../src/NodeUtils';
- import { StackTraceAnalyzer } from '../../../src/stack-trace-analyzer/StackTraceAnalyzer';
- const assert: any = chai.assert;
- describe('CustomNodeAppender', () => {
- describe('appendNode (blockScopeStackTraceData: IStackTraceData[], blockScopeNode: TNodeWithBlockStatement, nodeBodyStatements: TStatement[], index: number = 0)', () => {
- let astTree: ESTree.Program,
- expectedAstTree: ESTree.Program,
- node: TStatement[],
- stackTraceData: IStackTraceData[];
- beforeEach(() => {
- node = NodeUtils.convertCodeToStructure(`
- var test = 1;
- `);
- });
- it('should append node into first and deepest function call in calls trace - variant #1', () => {
- astTree = NodeMocks.getProgramNode(
- <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- baz();
- foo();
- bar();
- `)
- );
- expectedAstTree = NodeMocks.getProgramNode(
- <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
- var test = 1;
- }
-
- baz();
- foo();
- bar();
- `)
- );
- stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
- CustomNodeAppender.appendNode(stackTraceData, astTree, node);
- assert.deepEqual(astTree, expectedAstTree);
- });
- it('should append node into first and deepest function call in calls trace - variant #2', () => {
- astTree = NodeMocks.getProgramNode(
- <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- bar();
- baz();
- foo();
- `)
- );
- expectedAstTree = NodeMocks.getProgramNode(
- <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
- var test = 1;
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- bar();
- baz();
- foo();
- `)
- );
- stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
- CustomNodeAppender.appendNode(stackTraceData, astTree, node);
- assert.deepEqual(astTree, expectedAstTree);
- });
- describe('append by specific index', () => {
- let astTree: ESTree.Program;
- beforeEach(() => {
- astTree = NodeMocks.getProgramNode(
- <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- bar();
- baz();
- foo();
- `)
- );
- });
- it('should append node into deepest function call by specified index in calls trace - variant #1', () => {
- expectedAstTree = NodeMocks.getProgramNode(
- <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
- function foo () {
- var test = 1;
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
-
- }
-
- bar();
- baz();
- foo();
- `)
- );
- stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
- CustomNodeAppender.appendNode(stackTraceData, astTree, node, 2);
- assert.deepEqual(astTree, expectedAstTree);
- });
- it('should append node into deepest function call by specified index in calls trace - variant #2', () => {
- expectedAstTree = NodeMocks.getProgramNode(
- <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
- function foo () {
-
- }
-
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- function baz () {
- var test = 1;
- }
-
- bar();
- baz();
- foo();
- `)
- );
- stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
- CustomNodeAppender.appendNode(stackTraceData, astTree, node, 1);
- assert.deepEqual(astTree, expectedAstTree);
- });
- it('should append node into deepest function call by specified index in calls trace - variant #3', () => {
- astTree = NodeMocks.getProgramNode(
- <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
- var start = new Date();
- var log = console.log;
-
- console.log = function () {};
-
- (function () {
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
-
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- bar();
- })();
- console.log = log;
- console.log(new Date() - start);
- `)
- );
- expectedAstTree = NodeMocks.getProgramNode(
- <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
- var start = new Date();
- var log = console.log;
-
- console.log = function () {};
-
- (function () {
- function bar () {
- function inner1 () {
-
- }
-
- function inner2 () {
- var inner3 = function () {
- var test = 1;
- }
-
- inner3();
- }
-
- inner2();
- inner1();
- }
-
- bar();
- })();
- console.log = log;
- console.log(new Date() - start);
- `)
- );
- stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
- CustomNodeAppender.appendNode(
- stackTraceData,
- astTree,
- node,
- CustomNodeAppender.getRandomStackTraceIndex(stackTraceData.length)
- );
- assert.deepEqual(astTree, expectedAstTree);
- });
- });
- });
- describe('getRandomStackTraceIndex (stackTraceRootLength: number): number', () => {
- it('should returns random index between 0 and stack trace data root length', () => {
- let index: number;
- for (let i: number = 0; i < 100; i++) {
- index = CustomNodeAppender.getRandomStackTraceIndex(100);
- assert.isAtLeast(index, 0);
- assert.isAtMost(index, 100);
- }
- });
- });
- });
|