From 584500a24d9acd922f2cee984af50993004e6c76 Mon Sep 17 00:00:00 2001 From: hond Date: Fri, 19 Jun 2020 12:53:58 +0800 Subject: [PATCH 1/2] fix expandText return type error --- .../Microsoft.Bot.Builder.LanguageGeneration/Evaluator.cs | 7 ++++--- .../Microsoft.Bot.Builder.LanguageGeneration/Expander.cs | 2 +- .../Examples/ExpandText.lg | 2 +- .../TemplatesTest.cs | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Evaluator.cs b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Evaluator.cs index 81b67b647c..3b0796014a 100644 --- a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Evaluator.cs +++ b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Evaluator.cs @@ -508,7 +508,7 @@ private EvaluatorLookup CustomizedEvaluatorLookup(EvaluatorLookup baseLookup) if (name.Equals(expandText)) { - return new ExpressionEvaluator(expandText, ExpressionFunctions.Apply(this.ExpandText()), ReturnType.Boolean, ExpressionFunctions.ValidateUnaryString); + return new ExpressionEvaluator(expandText, ExpressionFunctions.Apply(this.ExpandText()), ReturnType.Object, ExpressionFunctions.ValidateUnaryString); } return null; @@ -518,7 +518,8 @@ private Func, object> ExpandText() => (IReadOnlyList args) => { var stringContent = args[0].ToString(); - var newScope = CurrentTarget().Scope; + + var newScope = evaluationTargetStack.Count == 0 ? null : CurrentTarget().Scope; var newTemplates = new Templates(templates: Templates, expressionParser: ExpressionParser); return newTemplates.EvaluateText(stringContent, newScope, lgOptions); }; @@ -549,7 +550,7 @@ private Func, object> FromFile() var resourcePath = GetResourcePath(filePath); var stringContent = File.ReadAllText(resourcePath); - var newScope = CurrentTarget().Scope; + var newScope = evaluationTargetStack.Count == 0 ? null : CurrentTarget().Scope; var newTemplates = new Templates(templates: Templates, expressionParser: ExpressionParser); return newTemplates.EvaluateText(stringContent, newScope, lgOptions); }; diff --git a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs index c3f5560278..03253b7272 100644 --- a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs +++ b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs @@ -541,7 +541,7 @@ private EvaluatorLookup CustomizedEvaluatorLookup(EvaluatorLookup baseLookup, bo if (name.Equals(expandText)) { - return new ExpressionEvaluator(expandText, ExpressionFunctions.Apply(this.ExpandText()), ReturnType.Boolean, ExpressionFunctions.ValidateUnaryString); + return new ExpressionEvaluator(expandText, ExpressionFunctions.Apply(this.ExpandText()), ReturnType.Object, ExpressionFunctions.ValidateUnaryString); } return null; diff --git a/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/Examples/ExpandText.lg b/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/Examples/ExpandText.lg index 5962ebd0b4..3e92277b45 100644 --- a/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/Examples/ExpandText.lg +++ b/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/Examples/ExpandText.lg @@ -1,2 +1,2 @@ # template -- ${expandText(@answer)} \ No newline at end of file +- ${length(expandText(@answer))} \ No newline at end of file diff --git a/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/TemplatesTest.cs b/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/TemplatesTest.cs index e7c9d1b47b..001e3e15a7 100644 --- a/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/TemplatesTest.cs +++ b/tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/TemplatesTest.cs @@ -124,9 +124,9 @@ public void TestExpandText() } }; - // - ${expandText(@answer)} + // - ${length(expandText(@answer))} var evaled = templates.Evaluate("template", scope); - Assert.AreEqual("hello vivian", evaled); + Assert.AreEqual("hello vivian".Length, evaled); } [TestMethod] From 235d60d9815b46220fd59683a6b1b5cabfec9210 Mon Sep 17 00:00:00 2001 From: hond Date: Fri, 19 Jun 2020 13:06:11 +0800 Subject: [PATCH 2/2] fix --- .../Microsoft.Bot.Builder.LanguageGeneration/Expander.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs index 03253b7272..27049fd9a0 100644 --- a/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs +++ b/libraries/Microsoft.Bot.Builder.LanguageGeneration/Expander.cs @@ -551,7 +551,7 @@ private Func, object> ExpandText() => (IReadOnlyList args) => { var stringContent = args[0].ToString(); - var newScope = CurrentTarget().Scope; + var newScope = evaluationTargetStack.Count == 0 ? null : CurrentTarget().Scope; var newTemplates = new Templates(templates: Templates, expressionParser: expressionParser); return newTemplates.EvaluateText(stringContent, newScope, lgOptions); }; @@ -678,7 +678,7 @@ private Func, object> FromFile() var resourcePath = GetResourcePath(filePath); var stringContent = File.ReadAllText(resourcePath); - var newScope = CurrentTarget().Scope; + var newScope = evaluationTargetStack.Count == 0 ? null : CurrentTarget().Scope; var newTemplates = new Templates(templates: Templates, expressionParser: expressionParser); return newTemplates.EvaluateText(stringContent, newScope, lgOptions); };