Move templates to own assembly
This commit is contained in:
Родитель
1bb335b35e
Коммит
0f88416492
|
@ -62,6 +62,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Builder.Core.
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Builder.Core.Extensions.Tests", "tests\Microsoft.Bot.Builder.Core.Extensions.Tests\Microsoft.Bot.Builder.Core.Extensions.Tests.csproj", "{6034B365-C87B-4A90-A8C7-F24374BFE2E7}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Bot.Builder.TemplateManager", "libraries\Microsoft.Bot.Builder.TemplateManager\Microsoft.Bot.Builder.TemplateManager.csproj", "{EED5F0D3-6F00-4ED1-9108-5ADCB10A3734}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.Bot.Builder.TemplateManager.Tests", "tests\Microsoft.Bot.Builder.TemplateManager\Microsoft.Bot.Builder.TemplateManager.Tests.csproj", "{C93F6192-0123-4121-AD92-374A71E4B0F3}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -172,6 +176,14 @@ Global
|
|||
{6034B365-C87B-4A90-A8C7-F24374BFE2E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6034B365-C87B-4A90-A8C7-F24374BFE2E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6034B365-C87B-4A90-A8C7-F24374BFE2E7}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{EED5F0D3-6F00-4ED1-9108-5ADCB10A3734}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{EED5F0D3-6F00-4ED1-9108-5ADCB10A3734}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EED5F0D3-6F00-4ED1-9108-5ADCB10A3734}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EED5F0D3-6F00-4ED1-9108-5ADCB10A3734}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{C93F6192-0123-4121-AD92-374A71E4B0F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C93F6192-0123-4121-AD92-374A71E4B0F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C93F6192-0123-4121-AD92-374A71E4B0F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C93F6192-0123-4121-AD92-374A71E4B0F3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -204,6 +216,8 @@ Global
|
|||
{183B3324-4CFF-477A-8EAE-73953D3E383D} = {4269F3C3-6B42-419B-B64A-3E6DC0F1574A}
|
||||
{99100609-26D5-4BBD-ACD7-4CC810977A06} = {AD743B78-D61F-4FBF-B620-FA83CE599A50}
|
||||
{6034B365-C87B-4A90-A8C7-F24374BFE2E7} = {AD743B78-D61F-4FBF-B620-FA83CE599A50}
|
||||
{EED5F0D3-6F00-4ED1-9108-5ADCB10A3734} = {4269F3C3-6B42-419B-B64A-3E6DC0F1574A}
|
||||
{C93F6192-0123-4121-AD92-374A71E4B0F3} = {AD743B78-D61F-4FBF-B620-FA83CE599A50}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {7173C9F3-A7F9-496E-9078-9156E35D6E16}
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.Bot.Builder.TemplateManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Map of Template Ids-> Template Function()
|
||||
/// </summary>
|
||||
public class TemplateIdMap : Dictionary<string, Func<IBotContext, dynamic, object>>
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Map of language -> template functions
|
||||
/// </summary>
|
||||
public class LanguageTemplateDictionary : Dictionary<string, TemplateIdMap>
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is a simple template engine which has a resource map of template functions
|
||||
/// let myTemplates = {
|
||||
/// "en" : {
|
||||
/// "templateId": (context, data) => $"your name is {data.name}",
|
||||
/// "templateId": (context, data) => { return new Activity(); }
|
||||
/// }`
|
||||
/// }
|
||||
/// }
|
||||
/// To use, simply register with templateManager
|
||||
/// templateManager.Register(new DictionaryRenderer(myTemplates))
|
||||
/// </summary>
|
||||
public class DictionaryRenderer : ITemplateRenderer
|
||||
{
|
||||
private LanguageTemplateDictionary languages;
|
||||
|
||||
public DictionaryRenderer(LanguageTemplateDictionary templates)
|
||||
{
|
||||
this.languages = templates;
|
||||
}
|
||||
|
||||
public Task<object> RenderTemplate(IBotContext context, string language, string templateId, object data)
|
||||
{
|
||||
if (this.languages.TryGetValue(language, out var templates))
|
||||
{
|
||||
if (templates.TryGetValue(templateId, out var template))
|
||||
{
|
||||
dynamic result = template(context, data);
|
||||
if (result != null)
|
||||
{
|
||||
return Task.FromResult(result as object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Task.FromResult((object)null);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.Bot.Builder.TemplateManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines interface for data binding to template and rendering a string
|
||||
/// </summary>
|
||||
public interface ITemplateRenderer
|
||||
{
|
||||
/// <summary>
|
||||
/// render a template to an activity or string
|
||||
/// </summary>
|
||||
/// <param name="context">context</param>
|
||||
/// <param name="language">language to render</param>
|
||||
/// <param name="templateId">tenmplate to render</param>
|
||||
/// <param name="data">data object to use to render</param>
|
||||
/// <returns></returns>
|
||||
Task<object> RenderTemplate(IBotContext context, string language, string templateId, object data);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<Version Condition=" '$(BUILD_BUILDNUMBER)' == '' ">4.0.0-local</Version>
|
||||
<Version Condition=" '$(BUILD_BUILDNUMBER)' != '' ">$(BUILD_BUILDNUMBER)</Version>
|
||||
<PackageVersion Condition=" '$(PackageVersion)' == '' ">4.0.0-local</PackageVersion>
|
||||
<PackageVersion Condition=" '$(PackageVersion)' != '' ">$(PackageVersion)</PackageVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<SignAssembly>true</SignAssembly>
|
||||
<DelaySign>true</DelaySign>
|
||||
<AssemblyOriginatorKeyFile>..\..\build\35MSSharedLib1024.snk</AssemblyOriginatorKeyFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<PackageId>Microsoft.Bot.Builder.TemplateManager</PackageId>
|
||||
<Description>This library implements .NET TemplateManager classes to manage libraries of template renderers in Microsoft Bot Builder SDK v4</Description>
|
||||
<Summary>This library implements .NET TemplateManager classes to manage libraries of template renderers in Microsoft Bot Builder SDK v4</Summary>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Company>Microsoft</Company>
|
||||
<Authors>microsoft,BotFramework,nugetbotbuilder</Authors>
|
||||
<Product>Microsoft Bot Builder SDK</Product>
|
||||
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
|
||||
<PackageProjectUrl>https://github.com/Microsoft/botbuilder-dotnet</PackageProjectUrl>
|
||||
<PackageIconUrl>http://docs.botframework.com/images/bot_icon.png</PackageIconUrl>
|
||||
<PackageLicenseUrl>https://github.com/Microsoft/BotBuilder/blob/master/LICENSE</PackageLicenseUrl>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<RepositoryUrl>https://github.com/Microsoft/botbuilder-dotnet</RepositoryUrl>
|
||||
<LicenseUrl>https://github.com/Microsoft/BotBuilder-dotnet/blob/master/LICENSE</LicenseUrl>
|
||||
<RepositoryType />
|
||||
<PackageTags>bots;ai;botframework;botbuilder</PackageTags>
|
||||
<NeutralLanguage />
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Bot.Builder.Core" Condition=" '$(PackageVersion)' == '' " Version="4.0.0-local" />
|
||||
<PackageReference Include="Microsoft.Bot.Builder.Core" Condition=" '$(PackageVersion)' != '' " Version="$(PackageVersion)" />
|
||||
<PackageReference Include="Microsoft.Bot.Schema" Condition=" '$(PackageVersion)' == '' " Version="4.0.0-local" />
|
||||
<PackageReference Include="Microsoft.Bot.Schema" Condition=" '$(PackageVersion)' != '' " Version="$(PackageVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Microsoft.Bot.Builder.Core\Microsoft.Bot.Builder.Core.csproj" />
|
||||
<ProjectReference Include="..\Microsoft.Bot.Schema\Microsoft.Bot.Schema.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,119 @@
|
|||
using Microsoft.Bot.Schema;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.Bot.Builder.TemplateManager
|
||||
{
|
||||
/// <summary>
|
||||
/// TemplateManager manages set of ITemplateRenderer implementations
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// ITemplateRenderer implements
|
||||
/// </remarks>
|
||||
public class TemplateManager
|
||||
{
|
||||
private List<ITemplateRenderer> _templateRenderers = new List<ITemplateRenderer>();
|
||||
private List<string> _languageFallback = new List<string>();
|
||||
|
||||
public TemplateManager()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a template engine for binding templates
|
||||
/// </summary>
|
||||
/// <param name="renderer"></param>
|
||||
|
||||
public TemplateManager Register(ITemplateRenderer renderer)
|
||||
{
|
||||
if (!this._templateRenderers.Contains(renderer))
|
||||
this._templateRenderers.Add(renderer);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// List registered template engines
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IList<ITemplateRenderer> List()
|
||||
{
|
||||
return this._templateRenderers;
|
||||
}
|
||||
|
||||
public void SetLanguagePolicy(IEnumerable<string> languageFallback)
|
||||
{
|
||||
this._languageFallback = new List<string>(languageFallback);
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetLanguagePolicy()
|
||||
{
|
||||
return this._languageFallback;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send a reply with the template
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="templateId"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public async Task ReplyWith(IBotContext context, string templateId, object data = null)
|
||||
{
|
||||
BotAssert.ContextNotNull(context);
|
||||
|
||||
// apply template
|
||||
Activity boundActivity = await this.RenderTemplate(context, context.Request?.AsMessageActivity()?.Locale, templateId, data).ConfigureAwait(false);
|
||||
if (boundActivity != null)
|
||||
{
|
||||
await context.SendActivity(boundActivity);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Render the template
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="language"></param>
|
||||
/// <param name="templateId"></param>
|
||||
/// <param name="data"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Activity> RenderTemplate(IBotContext context, string language, string templateId, object data = null)
|
||||
{
|
||||
List<string> fallbackLocales = new List<string>(this._languageFallback);
|
||||
|
||||
if (!String.IsNullOrEmpty(language))
|
||||
{
|
||||
fallbackLocales.Add(language);
|
||||
}
|
||||
|
||||
fallbackLocales.Add("default");
|
||||
|
||||
// try each locale until successful
|
||||
foreach (var locale in fallbackLocales)
|
||||
{
|
||||
foreach (var renderer in this._templateRenderers)
|
||||
{
|
||||
object templateOutput = await renderer.RenderTemplate(context, locale, templateId, data);
|
||||
if (templateOutput != null)
|
||||
{
|
||||
if (templateOutput is string)
|
||||
{
|
||||
return new Activity(type: ActivityTypes.Message, text: (string)templateOutput);
|
||||
}
|
||||
else
|
||||
{
|
||||
return templateOutput as Activity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="..\..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{C93F6192-0123-4121-AD92-374A71E4B0F3}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Microsoft.Bot.Builder.TemplateManager.Tests</RootNamespace>
|
||||
<AssemblyName>Microsoft.Bot.Builder.TemplateManager.Tests</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
|
||||
<IsCodedUITest>False</IsCodedUITest>
|
||||
<TestProjectType>UnitTest</TestProjectType>
|
||||
<NuGetPackageImportStamp>
|
||||
</NuGetPackageImportStamp>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="TemplateManagerTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Builder.Core\Microsoft.Bot.Builder.Core.csproj">
|
||||
<Project>{f0c150e2-9b8a-444c-b70e-97ad918cb3d4}</Project>
|
||||
<Name>Microsoft.Bot.Builder.Core</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Builder.TemplateManager\Microsoft.Bot.Builder.TemplateManager.csproj">
|
||||
<Project>{eed5f0d3-6f00-4ed1-9108-5adcb10a3734}</Project>
|
||||
<Name>Microsoft.Bot.Builder.TemplateManager</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\libraries\Microsoft.Bot.Schema\Microsoft.Bot.Schema.csproj">
|
||||
<Project>{c1f54cdc-ad1d-45bb-8f7d-f49e411afaf1}</Project>
|
||||
<Name>Microsoft.Bot.Schema</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
<PropertyGroup>
|
||||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
|
||||
</PropertyGroup>
|
||||
<Error Condition="!Exists('..\..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props'))" />
|
||||
<Error Condition="!Exists('..\..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets'))" />
|
||||
</Target>
|
||||
<Import Project="..\..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets')" />
|
||||
</Project>
|
|
@ -0,0 +1,20 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[assembly: AssemblyTitle("Microsoft.Bot.Builder.TemplateManager")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Microsoft.Bot.Builder.TemplateManager")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2018")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
[assembly: Guid("c93f6192-0123-4121-ad92-374a71e4b0f3")]
|
||||
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
@ -0,0 +1,208 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Bot.Builder.Adapters;
|
||||
using Microsoft.Bot.Schema;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace Microsoft.Bot.Builder.TemplateManager.Tests
|
||||
{
|
||||
|
||||
[TestClass]
|
||||
[TestCategory("Template")]
|
||||
public class TemplateManagerTests
|
||||
{
|
||||
private static TestContext _testContext;
|
||||
private static LanguageTemplateDictionary templates1;
|
||||
private static LanguageTemplateDictionary templates2;
|
||||
|
||||
[AssemblyInitialize]
|
||||
public static void SetupDictionaries(TestContext testContext)
|
||||
{
|
||||
_testContext = testContext;
|
||||
templates1 = new LanguageTemplateDictionary
|
||||
{
|
||||
["default"] = new TemplateIdMap
|
||||
{
|
||||
{ "stringTemplate", (context, data) => $"default: { data.name}" },
|
||||
{ "activityTemplate", (context, data) => { return new Activity() { Type = ActivityTypes.Message, Text = $"(Activity)default: { data.name}" }; } },
|
||||
{ "stringTemplate2", (context, data) => $"default: Yo { data.name}" }
|
||||
},
|
||||
["en"] = new TemplateIdMap
|
||||
{
|
||||
{ "stringTemplate", (context, data) => $"en: { data.name}" },
|
||||
{ "activityTemplate", (context, data) => { return new Activity() { Type = ActivityTypes.Message, Text = $"(Activity)en: { data.name}" }; } },
|
||||
{ "stringTemplate2", (context, data) => $"en: Yo { data.name}" }
|
||||
},
|
||||
["fr"] = new TemplateIdMap
|
||||
{
|
||||
{ "stringTemplate", (context, data) => $"fr: { data.name}" },
|
||||
{ "activityTemplate", (context, data) => { return new Activity() { Type = ActivityTypes.Message, Text = $"(Activity)fr: { data.name}" }; } },
|
||||
{ "stringTemplate2", (context, data) => $"fr: Yo { data.name}" }
|
||||
}
|
||||
};
|
||||
templates2 = new LanguageTemplateDictionary
|
||||
{
|
||||
["en"] = new TemplateIdMap
|
||||
{
|
||||
{ "stringTemplate2", (context, data) => $"en: StringTemplate2 override {data.name}" }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TemplateManager_Registration()
|
||||
{
|
||||
var templateManager = new TemplateManager();
|
||||
Assert.AreEqual(templateManager.List().Count, 0, "nothing registered yet");
|
||||
var templateEngine1 = new DictionaryRenderer(templates1);
|
||||
var templateEngine2 = new DictionaryRenderer(templates2);
|
||||
templateManager.Register(templateEngine1);
|
||||
Assert.AreEqual(templateManager.List().Count, 1, "one registered");
|
||||
|
||||
templateManager.Register(templateEngine1);
|
||||
Assert.AreEqual(templateManager.List().Count, 1, "only one registered");
|
||||
|
||||
templateManager.Register(templateEngine2);
|
||||
Assert.AreEqual(templateManager.List().Count, 2, "two registered");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TemplateManager_MultiTemplate()
|
||||
{
|
||||
var templateManager = new TemplateManager();
|
||||
Assert.AreEqual(templateManager.List().Count, 0, "nothing registered yet");
|
||||
var templateEngine1 = new DictionaryRenderer(templates1);
|
||||
var templateEngine2 = new DictionaryRenderer(templates2);
|
||||
templateManager.Register(templateEngine1);
|
||||
Assert.AreEqual(templateManager.List().Count, 1, "one registered");
|
||||
|
||||
templateManager.Register(templateEngine1);
|
||||
Assert.AreEqual(templateManager.List().Count, 1, "only one registered");
|
||||
|
||||
templateManager.Register(templateEngine2);
|
||||
Assert.AreEqual(templateManager.List().Count, 2, "two registered");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DictionaryTemplateEngine_SimpleStringBinging()
|
||||
{
|
||||
var engine = new DictionaryRenderer(templates1);
|
||||
var result = await engine.RenderTemplate(null, "en", "stringTemplate", new { name = "joe" });
|
||||
Assert.IsInstanceOfType(result, typeof(string));
|
||||
Assert.AreEqual("en: joe", (string)result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task DictionaryTemplateEngine_SimpleActivityBinding()
|
||||
{
|
||||
var engine = new DictionaryRenderer(templates1);
|
||||
var result = await engine.RenderTemplate(null, "en", "activityTemplate", new { name = "joe" });
|
||||
Assert.IsInstanceOfType(result, typeof(Activity));
|
||||
var activity = result as Activity;
|
||||
Assert.AreEqual(ActivityTypes.Message, activity.Type);
|
||||
Assert.AreEqual("(Activity)en: joe", activity.Text);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public async Task TemplateManager_defaultlookup()
|
||||
{
|
||||
|
||||
TestAdapter adapter = new TestAdapter();
|
||||
|
||||
var templateManager = new TemplateManager()
|
||||
.Register(new DictionaryRenderer(templates1))
|
||||
.Register(new DictionaryRenderer(templates2));
|
||||
|
||||
await new TestFlow(adapter, async (context) =>
|
||||
{
|
||||
var templateId = context.Request.AsMessageActivity().Text.Trim();
|
||||
await templateManager.ReplyWith(context, templateId, new { name = "joe" });
|
||||
})
|
||||
.Send("stringTemplate").AssertReply("default: joe")
|
||||
.Send("activityTemplate").AssertReply("(Activity)default: joe")
|
||||
.StartTest();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TemplateManager_enLookup()
|
||||
{
|
||||
|
||||
TestAdapter adapter = new TestAdapter();
|
||||
var templateManager = new TemplateManager()
|
||||
.Register(new DictionaryRenderer(templates1))
|
||||
.Register(new DictionaryRenderer(templates2));
|
||||
|
||||
await new TestFlow(adapter, async (context) =>
|
||||
{
|
||||
context.Request.AsMessageActivity().Locale = "en"; // force to english
|
||||
var templateId = context.Request.AsMessageActivity().Text.Trim();
|
||||
await templateManager.ReplyWith(context, templateId, new { name = "joe" });
|
||||
})
|
||||
.Send("stringTemplate").AssertReply("en: joe")
|
||||
.Send("activityTemplate").AssertReply("(Activity)en: joe")
|
||||
.StartTest();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TemplateManager_frLookup()
|
||||
{
|
||||
|
||||
TestAdapter adapter = new TestAdapter();
|
||||
var templateManager = new TemplateManager()
|
||||
.Register(new DictionaryRenderer(templates1))
|
||||
.Register(new DictionaryRenderer(templates2));
|
||||
|
||||
await new TestFlow(adapter, async (context) =>
|
||||
{
|
||||
context.Request.AsMessageActivity().Locale = "fr"; // force to french
|
||||
var templateId = context.Request.AsMessageActivity().Text.Trim();
|
||||
await templateManager.ReplyWith(context, templateId, new { name = "joe" });
|
||||
})
|
||||
.Send("stringTemplate").AssertReply("fr: joe")
|
||||
.Send("activityTemplate").AssertReply("(Activity)fr: joe")
|
||||
.StartTest();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TemplateManager_override()
|
||||
{
|
||||
|
||||
TestAdapter adapter = new TestAdapter();
|
||||
var templateManager = new TemplateManager()
|
||||
.Register(new DictionaryRenderer(templates1))
|
||||
.Register(new DictionaryRenderer(templates2));
|
||||
|
||||
await new TestFlow(adapter, async (context) =>
|
||||
{
|
||||
context.Request.AsMessageActivity().Locale = "fr"; // force to french
|
||||
var templateId = context.Request.AsMessageActivity().Text.Trim();
|
||||
await templateManager.ReplyWith(context, templateId, new { name = "joe" });
|
||||
})
|
||||
.Send("stringTemplate2").AssertReply("fr: Yo joe")
|
||||
.Send("activityTemplate").AssertReply("(Activity)fr: joe")
|
||||
.StartTest();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public async Task TemplateManager_useTemplateEngine()
|
||||
{
|
||||
|
||||
TestAdapter adapter = new TestAdapter();
|
||||
var templateManager = new TemplateManager()
|
||||
.Register(new DictionaryRenderer(templates1))
|
||||
.Register(new DictionaryRenderer(templates2));
|
||||
|
||||
await new TestFlow(adapter, async (context) =>
|
||||
{
|
||||
var templateId = context.Request.AsMessageActivity().Text.Trim();
|
||||
await templateManager.ReplyWith(context, templateId, new { name = "joe" });
|
||||
})
|
||||
.Send("stringTemplate").AssertReply("default: joe")
|
||||
.Send("activityTemplate").AssertReply("(Activity)default: joe")
|
||||
.StartTest();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="MSTest.TestAdapter" version="1.2.0" targetFramework="net461" />
|
||||
<package id="MSTest.TestFramework" version="1.2.0" targetFramework="net461" />
|
||||
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
|
||||
</packages>
|
Загрузка…
Ссылка в новой задаче