From 8a7456e8b1c0962cc29632f546887c5b87de448c Mon Sep 17 00:00:00 2001 From: Shuai Wang Date: Wed, 4 Mar 2020 08:18:44 +0800 Subject: [PATCH] Port some LG and Expression from BotBuilder-Dotnet (#1834) * init * port some changes in LG and Expression * change filePath to lowercase * revert substring change * fix * fix extra tabs * retrigger * retrigger Co-authored-by: Steven Ickman --- libraries/adaptive-expressions/src/expression.ts | 5 +++++ .../src/memory/simpleObjectMemory.ts | 2 +- .../tests/badExpression.test.js | 2 +- .../adaptive-expressions/tests/expression.test.js | 13 ++++++------- .../botbuilder-lg/tests/ActivityChecker.test.js | 2 +- .../botbuilder-lg/tests/ActivityFactory.test.js | 2 +- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/libraries/adaptive-expressions/src/expression.ts b/libraries/adaptive-expressions/src/expression.ts index 60d1bb256..a87f4675d 100644 --- a/libraries/adaptive-expressions/src/expression.ts +++ b/libraries/adaptive-expressions/src/expression.ts @@ -11,6 +11,7 @@ import { ExpressionEvaluator, EvaluateExpressionDelegate } from './expressionEva import { ExpressionType } from './expressionType'; import { SimpleObjectMemory, MemoryInterface } from './memory'; import { Extensions } from './extensions'; +import { ExpressionEngine } from './parser'; /** * Type expected from evalating an expression. @@ -76,6 +77,10 @@ export class Expression { this.children = children; } + public static parse(expression: string): Expression { + return new ExpressionEngine().parse(expression); + } + /** * Make an expression and validate it. * @param type Type of expression from ExpressionType diff --git a/libraries/adaptive-expressions/src/memory/simpleObjectMemory.ts b/libraries/adaptive-expressions/src/memory/simpleObjectMemory.ts index d25106fe8..8251e703f 100644 --- a/libraries/adaptive-expressions/src/memory/simpleObjectMemory.ts +++ b/libraries/adaptive-expressions/src/memory/simpleObjectMemory.ts @@ -36,7 +36,7 @@ export class SimpleObjectMemory implements MemoryInterface { } public getValue(path: string): any { - if (this.memory === undefined) { + if (this.memory === undefined || path.length === 0 || (path[0] !== '[' && !path[0].match(/[a-z]/i))) { return undefined; } diff --git a/libraries/adaptive-expressions/tests/badExpression.test.js b/libraries/adaptive-expressions/tests/badExpression.test.js index 364da47d1..d92b5ef02 100644 --- a/libraries/adaptive-expressions/tests/badExpression.test.js +++ b/libraries/adaptive-expressions/tests/badExpression.test.js @@ -50,7 +50,7 @@ const badExpressions = 'split(one, \'l\')', // split only accept string parameter 'split(hello, 1)', // split only accept string parameter 'substring(hello, 0.5)', // the second parameter of substring must be integer - 'substring(one, 0)', // the first parameter of substring must be string + 'substring(two, 0)', // the first parameter of substring must be string or null 'substring(hello, 10)', // the start index is out of the range of the string length 'substring(hello, 0, hello)', // length is not integer 'substring(hello, 0, \'hello\')', // length is not integer diff --git a/libraries/adaptive-expressions/tests/expression.test.js b/libraries/adaptive-expressions/tests/expression.test.js index 7266c8d80..f48012cb8 100644 --- a/libraries/adaptive-expressions/tests/expression.test.js +++ b/libraries/adaptive-expressions/tests/expression.test.js @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ /* eslint-disable @typescript-eslint/no-var-requires */ -const { ExpressionEngine, Extensions, SimpleObjectMemory, ExpressionFunctions } = require('../'); +const { Expression, Extensions, SimpleObjectMemory, ExpressionFunctions } = require('../'); const assert = require('assert'); const moment = require('moment'); @@ -629,7 +629,7 @@ describe('expression functional test', () => { for (const data of dataSource) { const input = data[0].toString(); console.log(input); - var parsed = new ExpressionEngine().parse(input); + var parsed = Expression.parse(input); assert(parsed !== undefined); var { value: actual, error } = parsed.tryEvaluate(scope); assert(error === undefined, `input: ${ input }, Has error: ${ error }`); @@ -670,10 +670,9 @@ describe('expression functional test', () => { 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 exp = Expression.parse('a[f].b[n].z'); let path = undefined; let left = undefined; let error = undefined; @@ -681,17 +680,17 @@ describe('expression functional test', () => { assert.strictEqual(path, 'a[\'foo\'].b[2].z'); // normal case - exp = parser.parse('a[z.z][z.z].y'); + exp = Expression.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]'); + exp = Expression.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'); + exp = Expression.parse('json(x).b'); ({path, left, error} = ExpressionFunctions.tryAccumulatePath(exp, memory)); assert.strictEqual(path, 'b'); diff --git a/libraries/botbuilder-lg/tests/ActivityChecker.test.js b/libraries/botbuilder-lg/tests/ActivityChecker.test.js index 414478869..ae2b7ad38 100644 --- a/libraries/botbuilder-lg/tests/ActivityChecker.test.js +++ b/libraries/botbuilder-lg/tests/ActivityChecker.test.js @@ -2,7 +2,7 @@ const { LGParser, ActivityChecker } = require('../lib'); const assert = require('assert'); function getTemplateEngine(){ - const filePath = `${ __dirname }/testData/Examples/DiagnosticStructuredLG.lg`; + const filePath = `${ __dirname }/testData/examples/DiagnosticStructuredLG.lg`; return LGParser.parseFile(filePath); } diff --git a/libraries/botbuilder-lg/tests/ActivityFactory.test.js b/libraries/botbuilder-lg/tests/ActivityFactory.test.js index b30479090..584fa4979 100644 --- a/libraries/botbuilder-lg/tests/ActivityFactory.test.js +++ b/libraries/botbuilder-lg/tests/ActivityFactory.test.js @@ -2,7 +2,7 @@ const { LGParser, ActivityFactory } = require('../lib'); const assert = require('assert'); function getTemplateEngine(){ - const filePath = `${ __dirname }/testData/Examples/NormalStructuredLG.lg`; + const filePath = `${ __dirname }/testData/examples/NormalStructuredLG.lg`; return LGParser.parseFile(filePath); }