|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
| 5 | +import { expect } from 'chai'; |
| 6 | +import Cdp from '../cdp/api'; |
| 7 | +import { stubbedCdpApi, StubCdpApi } from '../cdp/stubbedApi'; |
| 8 | +import { Base01Position } from '../common/positions'; |
| 9 | +import { RenameMapping } from '../common/sourceMaps/renameProvider'; |
| 10 | +import { Evaluator } from './evaluator'; |
| 11 | + |
| 12 | +describe('Evaluator', () => { |
| 13 | + let evaluator: Evaluator; |
| 14 | + let stubCdp: StubCdpApi; |
| 15 | + let renameMapping: RenameMapping; |
| 16 | + |
| 17 | + const result: Cdp.Debugger.EvaluateOnCallFrameResult = { |
| 18 | + result: { |
| 19 | + type: 'string', |
| 20 | + value: 'foo', |
| 21 | + }, |
| 22 | + }; |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + stubCdp = stubbedCdpApi(); |
| 26 | + renameMapping = RenameMapping.None; |
| 27 | + evaluator = new Evaluator(stubCdp.actual, { |
| 28 | + provideForSource: () => renameMapping, |
| 29 | + provideOnStackframe: () => renameMapping, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('prepares simple expressions', async () => { |
| 34 | + const prep = evaluator.prepare('foo', { isInternalScript: false }); |
| 35 | + expect(prep.canEvaluateDirectly).to.be.true; |
| 36 | + stubCdp.Debugger.evaluateOnCallFrame.resolves(result); |
| 37 | + expect(await prep.invoke({ callFrameId: '' })).to.equal(result); |
| 38 | + expect(stubCdp.Debugger.evaluateOnCallFrame.args[0][0]).to.deep.equal({ |
| 39 | + callFrameId: '', |
| 40 | + expression: 'foo', |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('appends eval source url to internal', async () => { |
| 45 | + const prep = evaluator.prepare('foo'); |
| 46 | + expect(prep.canEvaluateDirectly).to.be.true; |
| 47 | + stubCdp.Debugger.evaluateOnCallFrame.resolves(result); |
| 48 | + expect(await prep.invoke({ callFrameId: '' })).to.equal(result); |
| 49 | + expect(stubCdp.Debugger.evaluateOnCallFrame.args[0][0].expression).to.match( |
| 50 | + /^foo\n\/\/# sourceURL=eval/m, |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('replaces renamed identifiers', async () => { |
| 55 | + const prep = evaluator.prepare('foo', { |
| 56 | + isInternalScript: false, |
| 57 | + renames: { |
| 58 | + mapping: new RenameMapping([ |
| 59 | + { original: 'foo', compiled: 'bar', position: new Base01Position(0, 1) }, |
| 60 | + ]), |
| 61 | + position: new Base01Position(0, 1), |
| 62 | + }, |
| 63 | + }); |
| 64 | + expect(prep.canEvaluateDirectly).to.be.true; |
| 65 | + stubCdp.Debugger.evaluateOnCallFrame.resolves(result); |
| 66 | + expect(await prep.invoke({ callFrameId: '' })).to.equal(result); |
| 67 | + expect(stubCdp.Debugger.evaluateOnCallFrame.args[0][0]).to.deep.equal({ |
| 68 | + callFrameId: '', |
| 69 | + expression: 'typeof bar !== "undefined" ? bar : foo;\n', |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('does not replace identifiers in invalid contexts', async () => { |
| 74 | + const prep = evaluator.prepare( |
| 75 | + `const baz = foo; |
| 76 | +z.find(foo => true) |
| 77 | +const { foo } = z; |
| 78 | +for (const { foo } of z) {} |
| 79 | +try {} catch ({ foo }) {}`, |
| 80 | + { |
| 81 | + isInternalScript: false, |
| 82 | + renames: { |
| 83 | + mapping: new RenameMapping([ |
| 84 | + { original: 'foo', compiled: 'bar', position: new Base01Position(0, 1) }, |
| 85 | + ]), |
| 86 | + position: new Base01Position(0, 1), |
| 87 | + }, |
| 88 | + }, |
| 89 | + ); |
| 90 | + expect(prep.canEvaluateDirectly).to.be.true; |
| 91 | + stubCdp.Debugger.evaluateOnCallFrame.resolves(result); |
| 92 | + expect(await prep.invoke({ callFrameId: '' })).to.equal(result); |
| 93 | + expect(stubCdp.Debugger.evaluateOnCallFrame.args[0][0]).to.deep.equal({ |
| 94 | + callFrameId: '', |
| 95 | + expression: [ |
| 96 | + `const baz = typeof bar !== \"undefined\" ? bar : foo;`, |
| 97 | + `z.find(bar => true);`, |
| 98 | + `const {bar} = z;`, |
| 99 | + `for (const {bar} of z) {}`, |
| 100 | + `try {} catch ({bar}) {}`, |
| 101 | + '', |
| 102 | + ].join('\n'), |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments