Skip to content

Commit

Permalink
Added expr analyser tests for coverage improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
luvies committed Jun 22, 2019
1 parent ad5c9bf commit 3d6acef
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/analyzer/expression-analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,16 @@ export class ExpressionAnalyzer {
ctx = this._tryResolveFromIdentifier(expression.object);
gotCtx = true;
break;
case 'ThisExpression':
// 'this' is a special case, in that it refers to the context
// object directly.
return this._options.context[index];
}

if (ctx instanceof RuntimeValue) {
return ctx;
} else if (gotCtx) {
if (!this._options) {
return (ctx as any)[index];
} else if (canAccessMember(this._options.memberChecks, ctx, index)) {
if (canAccessMember(this._options.memberChecks, ctx, index)) {
return (ctx as any)[index];
} else {
this._tryAddError(
Expand Down
76 changes: 74 additions & 2 deletions test/expression-analyzer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExpressionAnalyzer, FunctionCall, RuntimeValue, standardMemberChecks } from '../src';
import jsep from 'jsep';

describe('Expression Analyzer', () => {
it('analyses simple expressions without context', () => {
Expand Down Expand Up @@ -45,6 +46,52 @@ describe('Expression Analyzer', () => {
expect(res.functionCalls[0].name).toBe('c');
expect(res.functionCalls[0].args).toStrictEqual([1, 2]);
expect(res.functionCalls[0].path).toHaveLength(0);

// Run through jsep first to test arguments.
const expr = jsep('a ? b() : c()');
res = analyzer.analyze(expr);

expect(res.functionCalls).toHaveLength(2);
expect(res.errors).toHaveLength(0);

expect(res.functionCalls[0].name).toBe('b');
expect(res.functionCalls[0].args).toHaveLength(0);
expect(res.functionCalls[0].path).toHaveLength(0);

expect(res.functionCalls[1].name).toBe('c');
expect(res.functionCalls[1].args).toHaveLength(0);
expect(res.functionCalls[1].path).toHaveLength(0);

res = analyzer.analyze('+a(1)');

expect(res.functionCalls).toHaveLength(1);
expect(res.errors).toHaveLength(0);

expect(res.functionCalls[0].name).toBe('a');
expect(res.functionCalls[0].args).toStrictEqual([1]);
expect(res.functionCalls[0].path).toHaveLength(0);

res = analyzer.analyze('');

expect(res.functionCalls).toHaveLength(0);
expect(res.errors).toHaveLength(1);

res = analyzer.analyze('a(1), b("2"), c(true)');

expect(res.functionCalls).toHaveLength(3);
expect(res.errors).toHaveLength(0);

expect(res.functionCalls[0].name).toBe('a');
expect(res.functionCalls[0].args).toStrictEqual([1]);
expect(res.functionCalls[0].path).toHaveLength(0);

expect(res.functionCalls[1].name).toBe('b');
expect(res.functionCalls[1].args).toStrictEqual(['2']);
expect(res.functionCalls[1].path).toHaveLength(0);

expect(res.functionCalls[2].name).toBe('c');
expect(res.functionCalls[2].args).toStrictEqual([true]);
expect(res.functionCalls[2].path).toHaveLength(0);
});

it('analyses simple expressions with simple context', () => {
Expand All @@ -53,6 +100,7 @@ describe('Expression Analyzer', () => {
context: {
a: '__a__',
b: '__b__',
f: () => undefined,
},
},
});
Expand Down Expand Up @@ -98,6 +146,15 @@ describe('Expression Analyzer', () => {
expect(res.functionCalls[0].name).toBe('c');
expect(res.functionCalls[0].args).toStrictEqual([1, 2]);
expect(res.functionCalls[0].path).toHaveLength(0);

res = analyzer.analyze('f(a, this.b)');

expect(res.functionCalls).toHaveLength(1);
expect(res.errors).toHaveLength(0);

expect(res.functionCalls[0].name).toBe('f');
expect(res.functionCalls[0].args).toStrictEqual(['__a__', '__b__']);
expect(res.functionCalls[0].path).toHaveLength(0);
});

it('analyses complex expressions with member access', () => {
Expand All @@ -108,6 +165,8 @@ describe('Expression Analyzer', () => {
b: () => undefined,
},
c: () => undefined,
d: 'd',
e: 'e',
},
memberChecks: standardMemberChecks,
},
Expand Down Expand Up @@ -159,7 +218,7 @@ describe('Expression Analyzer', () => {
expect(res.functionCalls[0].args).toStrictEqual([1, 2]);
expect(res.functionCalls[0].path).toHaveLength(0);

res = analyzer.analyze('a[d()].b()');
res = analyzer.analyze('a[f()].b()');

expect(res.functionCalls).toHaveLength(2);
expect(res.errors).toHaveLength(1);
Expand All @@ -169,8 +228,21 @@ describe('Expression Analyzer', () => {
expect(res.functionCalls[0].path).toHaveLength(1);
expect(res.functionCalls[0].path[0]).toBeInstanceOf(RuntimeValue);

expect(res.functionCalls[1].name).toBe('d');
expect(res.functionCalls[1].name).toBe('f');
expect(res.functionCalls[1].args).toHaveLength(0);
expect(res.functionCalls[1].path).toHaveLength(0);

res = analyzer.analyze('a.b(d) + c(this.e)');

expect(res.functionCalls).toHaveLength(2);
expect(res.errors).toHaveLength(0);

expect(res.functionCalls[0].name).toBe('b');
expect(res.functionCalls[0].args).toStrictEqual(['d']);
expect(res.functionCalls[0].path).toStrictEqual(['a']);

expect(res.functionCalls[1].name).toBe('c');
expect(res.functionCalls[1].args).toStrictEqual(['e']);
expect(res.functionCalls[1].path).toHaveLength(0);
});
});

0 comments on commit 3d6acef

Please sign in to comment.