|
3 | 3 | const expect = require('chai').expect
|
4 | 4 | const compiler = require('./compiler')
|
5 | 5 |
|
6 |
| -global.prop1 = 3 |
7 |
| -global.prop2 = 4 |
8 |
| -const localProp = 1 |
| 6 | +const localProp = 'localProp' |
| 7 | +global.globalProp = 'globalProp' |
9 | 8 |
|
10 | 9 | describe('nx-compile', () => {
|
11 | 10 | describe('compileCode()', () => {
|
12 |
| - it('should throw TypeError on invalid source argument', () => { |
13 |
| - expect(() => compiler.compileCode({}, {})).to.throw(TypeError) |
14 |
| - expect(() => compiler.compileCode(undefined, {})).to.throw(TypeError) |
15 |
| - expect(() => compiler.compileCode(12, {})).to.throw(TypeError) |
16 |
| - }) |
17 |
| - |
18 |
| - it('should throw TypeError on invalid sandbox argument', () => { |
19 |
| - expect(() => compiler.compileExpression('prop1 + prop2', 12)).to.throw(TypeError) |
20 |
| - expect(() => compiler.compileExpression('prop1 + prop2', undefined)).to.throw(TypeError) |
21 |
| - expect(() => compiler.compileExpression('prop1 + prop2', '')).to.throw(TypeError) |
| 11 | + it('should throw a TypeError on non string source argument', () => { |
| 12 | + expect(() => compiler.compileCode({})).to.throw(TypeError) |
| 13 | + expect(() => compiler.compileCode(undefined)).to.throw(TypeError) |
| 14 | + expect(() => compiler.compileCode(12)).to.throw(TypeError) |
22 | 15 | })
|
23 | 16 | })
|
24 | 17 |
|
25 | 18 | describe('compileExpression()', () => {
|
26 |
| - it('should throw TypeError on invalid source argument', () => { |
27 |
| - expect(() => compiler.compileCode({}, {})).to.throw(TypeError) |
28 |
| - expect(() => compiler.compileCode(undefined, {})).to.throw(TypeError) |
29 |
| - expect(() => compiler.compileCode(12, {})).to.throw(TypeError) |
30 |
| - }) |
31 |
| - |
32 |
| - it('should throw TypeError on invalid sandbox argument', () => { |
33 |
| - expect(() => compiler.compileExpression('prop1 + prop2', 12)).to.throw(TypeError) |
34 |
| - expect(() => compiler.compileExpression('prop1 + prop2', undefined)).to.throw(TypeError) |
35 |
| - expect(() => compiler.compileExpression('prop1 + prop2', '')).to.throw(TypeError) |
| 19 | + it('should throw a TypeError on non string source argument', () => { |
| 20 | + expect(() => compiler.compileCode({})).to.throw(TypeError) |
| 21 | + expect(() => compiler.compileCode(undefined)).to.throw(TypeError) |
| 22 | + expect(() => compiler.compileCode(12)).to.throw(TypeError) |
36 | 23 | })
|
37 | 24 | })
|
38 | 25 |
|
39 | 26 | describe('returned function (compiled code or expression)', () => {
|
| 27 | + it('should throw a TypeError on non object sandbox argument', () => { |
| 28 | + const expression = compiler.compileExpression('prop1 + prop2') |
| 29 | + expect(() => expression()).to.throw(TypeError) |
| 30 | + expect(() => expression('string')).to.throw(TypeError) |
| 31 | + expect(() => expression(12)).to.throw(TypeError) |
| 32 | + }) |
| 33 | + |
40 | 34 | it('should execute in the context of the sandbox', () => {
|
41 |
| - const expression = compiler.compileExpression('prop1 + prop2', {prop1: 1, prop2: 2}) |
42 |
| - expect(expression()).to.equal(3) |
| 35 | + const expression = compiler.compileExpression('prop1 + prop2') |
| 36 | + expect(expression({prop1: 1, prop2: 2})).to.equal(3) |
43 | 37 | })
|
44 | 38 |
|
45 | 39 | it('should not expose local variables', () => {
|
46 |
| - const expression = compiler.compileExpression('localProp', {}) |
47 |
| - expect(expression()).to.equal(undefined) |
| 40 | + const expression = compiler.compileExpression('localProp') |
| 41 | + expect(expression({})).to.equal(undefined) |
48 | 42 | })
|
49 | 43 |
|
50 |
| - it('should favour sandbox variables over global ones', () => { |
51 |
| - const expression = compiler.compileExpression('prop1 + prop2', {prop1: 1, prop2: 2}) |
52 |
| - expect(expression()).to.equal(3) |
| 44 | + it('should not expose global variables', () => { |
| 45 | + const expression = compiler.compileExpression('globalProp') |
| 46 | + expect(expression({})).to.equal(undefined) |
53 | 47 | })
|
| 48 | + }) |
54 | 49 |
|
55 |
| - it('should set "this" to the sandbox instead of the global object', () => { |
56 |
| - const expression = compiler.compileExpression('this.prop1 + this.prop2', {prop1: 1, prop2: 2}) |
57 |
| - expect(expression()).to.equal(3) |
| 50 | + describe('returned function expression', () => { |
| 51 | + it('should return undefined instead of throwing on invalid property access', () => { |
| 52 | + const expression = compiler.compileExpression('inner.prop1') |
| 53 | + expect(() => expression({})).to.not.throw(TypeError) |
| 54 | + expect(expression({})).to.equal(undefined) |
58 | 55 | })
|
| 56 | + }) |
59 | 57 |
|
60 |
| - it('should set "this" to be undefined inside functions defined in the passed code', () => { |
61 |
| - const code = compiler.compileCode('(function () { return this })()', {}) |
62 |
| - expect(code()).to.equal(undefined) |
| 58 | + describe('returned code expression', () => { |
| 59 | + it('should throw on invalid property access', () => { |
| 60 | + const code = compiler.compileCode('inner.prop1') |
| 61 | + expect(() => code({})).to.throw(TypeError) |
63 | 62 | })
|
64 | 63 | })
|
65 | 64 | })
|
0 commit comments