Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Port] Add tests around memory access path #1760

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions libraries/adaptive-expressions/src/expressionFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ export class ExpressionFunctions {
* @param expression
* @param state
*/
private static tryAccumulatePath(expression: Expression, state: MemoryInterface): {path: string; left: Expression; error: string} {
public static tryAccumulatePath(expression: Expression, state: MemoryInterface): {path: string; left: Expression; error: string} {
let path = '';
let left = expression;
while (left !== undefined) {
Expand All @@ -833,11 +833,14 @@ export class ExpressionFunctions {
return {path: undefined, left: undefined, error};
}

if (isNaN(parseInt(value)) && typeof value !== 'string') {
if (ExpressionFunctions.isNumber(parseInt(value))) {
path = `[${ value }].${ path }`;
} else if (typeof value === 'string'){
path = `['${ value }'].${ path }`;
} else {
return {path: undefined, left: undefined, error:`${ left.children[1].toString() } dones't return a int or string`};
}

path = `[${ value }].${ path }`;
left = left.children[0];
} else {
break;
Expand Down
21 changes: 19 additions & 2 deletions libraries/adaptive-expressions/src/memory/simpleObjectMemory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MemoryInterface } from './memoryInterface';
import { Extensions } from '../extensions';
import { Util } from '../parser/util';

/**
* @module adaptive-expressions
Expand Down Expand Up @@ -39,7 +40,15 @@ export class SimpleObjectMemory implements MemoryInterface {
return undefined;
}

const parts: string[] = path.split(/[.\[\]]+/).filter((u: string): boolean => u !== undefined && u !== '');
const parts: string[] = path.split(/[.\[\]]+/)
.filter((u: string): boolean => u !== undefined && u !== '')
.map((u: string): string => {
if ((u.startsWith('"') && u.endsWith('"')) || (u.startsWith('\'') && u.endsWith('\''))) {
return u.substr(1, u.length - 2);
} else {
return u;
}
});
let value: any;
let curScope = this.memory;

Expand Down Expand Up @@ -74,7 +83,15 @@ export class SimpleObjectMemory implements MemoryInterface {
return;;
}

const parts: string[] = path.split(/[.\[\]]+/).filter((u: string): boolean => u !== undefined && u !== '');
const parts: string[] = path.split(/[.\[\]]+/)
.filter((u: string): boolean => u !== undefined && u !== '')
.map((u: string): string => {
if ((u.startsWith('"') && u.endsWith('"')) || (u.startsWith('\'') && u.endsWith('\''))) {
return u.substr(1, u.length - 2);
} else {
return u;
}
});
let curScope: any = this.memory;
let curPath = '';
let error: string = undefined;
Expand Down
39 changes: 38 additions & 1 deletion libraries/adaptive-expressions/tests/expression.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable @typescript-eslint/no-var-requires */
const { ExpressionEngine, Extensions } = require('../');
const { ExpressionEngine, Extensions, SimpleObjectMemory, ExpressionFunctions } = require('../');
const assert = require('assert');
const moment = require('moment');

Expand Down Expand Up @@ -645,6 +645,43 @@ describe('expression functional test', () => {
}
}
});

it('Test AccumulatePath', () => {
const scope = {
f: 'foo',
b: 'bar',
z: {
z: 'zar'
},
n: 2
};
const memory = new SimpleObjectMemory(scope);
let parser = new ExpressionEngine();

// normal case, note, we doesn't append a " yet
let exp = parser.parse('a[f].b[n].z');
let path = undefined;
let left = undefined;
let error = undefined;
({path, left, error} = ExpressionFunctions.tryAccumulatePath(exp, memory));
assert.strictEqual(path, 'a[\'foo\'].b[2].z');

// normal case
exp = parser.parse('a[z.z][z.z].y');
({path, left, error} = ExpressionFunctions.tryAccumulatePath(exp, memory));
assert.strictEqual(path, 'a[\'zar\'][\'zar\'].y');

// normal case
exp = parser.parse('a.b[z.z]');
({path, left, error} = ExpressionFunctions.tryAccumulatePath(exp, memory));
assert.strictEqual(path, 'a.b[\'zar\']');

// stop evaluate at middle
exp = parser.parse('json(x).b');
({path, left, error} = ExpressionFunctions.tryAccumulatePath(exp, memory));
assert.strictEqual(path, 'b');

});
});

var isArraySame = (actual, expected) => { //return [isSuccess, errorMessage]
Expand Down
29 changes: 29 additions & 0 deletions libraries/botbuilder-lg/tests/lg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,4 +742,33 @@ describe('LG', function() {
evaled = LGFile.evaluateTemplate('StringTemplateWithTemplateRef');
assert.strictEqual(evaled, 'hello jack , welcome. nice weather!');
});

it('TestMemoryAccessPath', function() {
var LGFile = LGParser.parseFile(GetExampleFilePath('MemoryAccess.lg'));

const scope = {
myProperty: {
name: 'p1'
},
turn: {
properties: {
p1: {
enum: 'p1enum'
}
}
}
};

// this evaulate will hit memory access twice
// first for "property", and get "p1", from local
// sencond for "turn.property[p1].enum" and get "p1enum" from global
var evaled = LGFile.evaluateTemplate('T1', scope);
assert.strictEqual(evaled, 'p1enum');

// this evaulate will hit memory access twice
// first for "myProperty.name", and get "p1", from global
// sencond for "turn.property[p1].enum" and get "p1enum" from global
evaled = LGFile.evaluateTemplate('T3', scope);
assert.strictEqual(evaled, 'p1enum');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# T1
- ${T2("p1")}

# T2(property)
- ${turn.properties[property].enum}

# T3
- ${turn.properties[myProperty.name].enum}