Skip to content

Commit

Permalink
Merge branch 'master' into hond/portCsharp
Browse files Browse the repository at this point in the history
  • Loading branch information
Danieladu authored Feb 22, 2020
2 parents 8c010bd + 80ebf56 commit f5fa55e
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 8 deletions.
11 changes: 11 additions & 0 deletions libraries/botbuilder-dialogs/src/prompts/promptCultureModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface PromptCultureModel {
* Class container for currently-supported Culture Models in Confirm and Choice Prompt.
*/
export class PromptCultureModels {

public static Chinese: PromptCultureModel = {
locale: Culture.Chinese,
separator: ', ',
Expand Down Expand Up @@ -88,6 +89,15 @@ export class PromptCultureModels {
noInLanguage: 'Nein',
}

public static Italian: PromptCultureModel = {
locale: Culture.Italian,
separator: ', ',
inlineOr: ' o ',
inlineOrMore: ' o ',
yesInLanguage: 'Si',
noInLanguage: 'No',
}

public static Japanese: PromptCultureModel = {
locale: Culture.Japanese,
separator: '、 ',
Expand Down Expand Up @@ -152,6 +162,7 @@ export class PromptCultureModels {
PromptCultureModels.English,
PromptCultureModels.French,
PromptCultureModels.German,
PromptCultureModels.Italian,
PromptCultureModels.Japanese,
PromptCultureModels.Portuguese,
PromptCultureModels.Spanish,
Expand Down
1 change: 1 addition & 0 deletions libraries/botbuilder-dialogs/tests/choicePrompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ describe('ChoicePrompt', function () {
'en-us',
'fr-fr',
'de-de',
'it-it',
'ja-jp',
'pt-br',
'zh-cn'
Expand Down
5 changes: 3 additions & 2 deletions libraries/botbuilder-dialogs/tests/confirmPrompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ describe('ConfirmPrompt', function () {
'fr-fr',
'de-de',
'ja-jp',
'it-it',
'pt-br',
'zh-cn'
];
Expand Down Expand Up @@ -556,7 +557,7 @@ describe('ConfirmPrompt', function () {
.assertReply(`The result found is 'true'.`);
});

it('should recogize valid number and default to en if locale invalid string.', async function () {
it('should recognize valid number and default to en if locale invalid string.', async function () {
const adapter = new TestAdapter(async (turnContext) => {

turnContext.activity.locale = 'invalid-locale';
Expand Down Expand Up @@ -591,7 +592,7 @@ describe('ConfirmPrompt', function () {
.assertReply(`The result found is 'true'.`);
});

it('should recogize valid number and default to en if defaultLocale invalid string.', async function () {
it('should recognize valid number and default to en if defaultLocale invalid string.', async function () {
const adapter = new TestAdapter(async (turnContext) => {

const dc = await dialogs.createContext(turnContext);
Expand Down
73 changes: 73 additions & 0 deletions libraries/botbuilder-dialogs/tests/promptCultureModels.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const assert = require('assert');
const { PromptCultureModels } = require('../lib')

describe('Prompt Culture Models Tests', function() {
this.timeout(5000);

it('should correctly map to nearest language', function() {
const locales = [
'es-es',
'nl-nl',
'en-us',
'fr-fr',
'de-de',
'ja-jp',
'it-it',
'pt-br',
'zh-cn'
];
// es-ES
const capEnding = (locale) => {
return `${ locale.split('-')[0] }-${ locale.split('-')[1].toUpperCase() }`;
};
// es-Es
const titleEnding = (locale) => {
locale[3] = locale.charAt(3).toUpperCase();
return locale;
};
// ES
const capTwoLetter = (locale) => {
return locale.split('-')[0].toUpperCase();
};
// es
const lowerTwoLetter = (locale) => {
return locale.split('-')[0].toLowerCase();
};

// This creates an object of the correct locale along with its test locales
const localeTests = locales.reduce((obj, locale) => {
obj[locale] = [
locale,
capEnding(locale),
titleEnding(locale),
capTwoLetter(locale),
lowerTwoLetter(locale)
];
return obj;
}, {});

Object.keys(localeTests).map((validLocale) => {
localeTests[validLocale].map((testLocale) => {
assert.strictEqual(PromptCultureModels.mapToNearestLanguage(testLocale), validLocale);
});
});
});

it('should return all supported cultures', function() {
const expected = [
PromptCultureModels.Chinese,
PromptCultureModels.Dutch,
PromptCultureModels.English,
PromptCultureModels.French,
PromptCultureModels.German,
PromptCultureModels.Italian,
PromptCultureModels.Japanese,
PromptCultureModels.Portuguese,
PromptCultureModels.Spanish
];

const supportedCultures = PromptCultureModels.getSupportedCultures();

assert.deepEqual(supportedCultures, expected);
});
});
32 changes: 26 additions & 6 deletions libraries/botbuilder-lg/src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
public static readonly fromFileFunctionName = 'fromFile';
public static readonly templateFunctionName = 'template';
public static readonly isTemplateFunctionName = 'isTemplate';
private static readonly ReExecuteSuffix = '!';

public constructor(templates: LGTemplate[], expressionEngine: ExpressionEngine) {
super();
Expand All @@ -65,11 +66,15 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa

/**
* Evaluate a template with given name and scope.
* @param templateName template name.
* @param inputTemplateName template name.
* @param scope scope.
* @returns Evaluate result.
*/
public evaluateTemplate(templateName: string, scope: any): any {
public evaluateTemplate(inputTemplateName: string, scope: any): any {
let templateName: string;
let reExecute: boolean;
({reExecute, pureTemplateName: templateName} = this.parseTemplateName(inputTemplateName));

if (!(templateName in this.templateMap)) {
throw new Error(`No such template: ${ templateName }`);
}
Expand All @@ -91,7 +96,7 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa

if (this.evalutationTargetStack.length !== 0) {
previousEvaluateTarget = this.evalutationTargetStack[this.evalutationTargetStack.length - 1];
if (previousEvaluateTarget.evaluatedChildren.has(currentEvulateId)) {
if (!reExecute && previousEvaluateTarget.evaluatedChildren.has(currentEvulateId)) {
return previousEvaluateTarget.evaluatedChildren.get(currentEvulateId);
}
}
Expand Down Expand Up @@ -236,7 +241,9 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
}).join('');
}

public constructScope(templateName: string, args: any[]): any {
public constructScope(inputTemplateName: string, args: any[]): any {
var templateName = this.parseTemplateName(inputTemplateName).pureTemplateName;

if (!this.templateMap[templateName]) {
throw new Error(`No such template ${ templateName }`);
}
Expand Down Expand Up @@ -403,9 +410,10 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
return baseLookup(name.substring(prebuiltPrefix.length));
}

if (this.templateMap[name]) {
var templateName = this.parseTemplateName(name).pureTemplateName;
if (templateName in this.templateMap) {
// tslint:disable-next-line: max-line-length
return new ExpressionEvaluator(name, ExpressionFunctions.apply(this.templateEvaluator(name)), ReturnType.Object, this.validTemplateReference);
return new ExpressionEvaluator(templateName, ExpressionFunctions.apply(this.templateEvaluator(name)), ReturnType.Object, this.validTemplateReference);
}

if (name === Evaluator.templateFunctionName) {
Expand Down Expand Up @@ -581,4 +589,16 @@ export class Evaluator extends AbstractParseTreeVisitor<any> implements LGFilePa
throw new Error(`arguments mismatch for template ${ templateName }, expect ${ expectedArgsCount } actual ${ actualArgsCount }`);
}
}

private parseTemplateName(templateName: string): { reExecute: boolean; pureTemplateName: string } {
if (!templateName) {
throw new Error('template name is empty.');
}

if (templateName.endsWith(Evaluator.ReExecuteSuffix)) {
return {reExecute:true, pureTemplateName: templateName.substr(0, templateName.length - Evaluator.ReExecuteSuffix.length)};
} else {
return {reExecute:false, pureTemplateName: templateName};
}
}
}
7 changes: 7 additions & 0 deletions libraries/botbuilder-lg/tests/lg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,4 +771,11 @@ describe('LG', function() {
evaled = LGFile.evaluateTemplate('T3', scope);
assert.strictEqual(evaled, 'p1enum');
});

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

// may be has different values
LGFile.evaluateTemplate('templateWithSameParams', {param1:'ms', param2:'newms'});
});
});
9 changes: 9 additions & 0 deletions libraries/botbuilder-lg/tests/testData/examples/ReExecute.lg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# templateWithSameParams(param)
- ${listTemplate1(param)} ${listTemplate1!(param)}

# listTemplate1(param)
- item1
- item2
- item3
- item4
- item5

0 comments on commit f5fa55e

Please sign in to comment.