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 <stevenic@microsoft.com>
This commit is contained in:
Shuai Wang 2020-03-04 08:18:44 +08:00 коммит произвёл GitHub
Родитель 5456287f07
Коммит 8a7456e8b1
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 15 добавлений и 11 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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

Просмотреть файл

@ -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');

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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);
}