add: Katheryne机器人单元测试

This commit is contained in:
jackfiled 2023-11-19 21:24:41 +08:00
parent 222bd715e7
commit baf68f3676
7 changed files with 136 additions and 2 deletions

View File

@ -9,8 +9,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
<PackageReference Include="xunit" Version="2.4.2"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.20.69" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
@ -25,4 +26,14 @@
<ProjectReference Include="..\Katheryne\Katheryne.csproj" />
</ItemGroup>
<Target Name="CopyTestFiles" AfterTargets="CoreCompile">
<ItemGroup>
<TestTxt Include="Katheryne/**/*.*"/>
</ItemGroup>
<Copy SourceFiles="@(TestTxt)"
DestinationFolder="$(OutputPath)/%(RecursiveDir)"
/>
</Target>
</Project>

View File

@ -0,0 +1 @@
你好

View File

@ -0,0 +1,5 @@
坏了,被你发现默认机器人了。
使用这个粪机器人,怎么能得高分呢?
必须要出重拳!
啊对对对。
我不到啊。

View File

@ -0,0 +1,14 @@
robotName: 凯瑟琳
stages:
- name: start
answer: 向着星辰和深渊!欢迎来到冒险家协会。
transformers:
- pattern: .*?
nextStageName: running
- name: running
answer: 对不起,做不到。
transformers:
- pattern: .*?
nextStageName: running
beginStageName: start

View File

@ -0,0 +1,3 @@
你说得对
但是
原神

View File

@ -0,0 +1,5 @@
向着星辰和深渊!欢迎来到冒险家协会。
对不起,做不到。
对不起,做不到。
对不起,做不到。
再见。

View File

@ -0,0 +1,95 @@
using Katheryne.Abstractions;
using Katheryne.Services;
using Microsoft.Extensions.Logging;
using Moq;
namespace Katheryne.Tests.Katheryne;
public class KatheryneRobotTests
{
private readonly Mock<ILogger<DefaultChatRobot>> _defaultChatRobotLogger = new();
private readonly Mock<ILogger<KatheryneChatRobot>> _katheryneChatRobotLogger = new();
private readonly Mock<ILogger<KatheryneChatRobotFactory>> _katheryneChatRobotFactoryLogger = new();
private readonly DefaultChatRobot _defaultChatRobot;
private readonly KatheryneChatRobotFactory _katheryneChatRobotFactory;
public KatheryneRobotTests()
{
_defaultChatRobot = new DefaultChatRobot(_defaultChatRobotLogger.Object);
_katheryneChatRobotFactory = new KatheryneChatRobotFactory(new YamlDeserializerFactory(),
_katheryneChatRobotFactoryLogger.Object,
_katheryneChatRobotLogger.Object,
_defaultChatRobot);
}
[Fact]
public void DefaultRobotTest()
{
InputOutputFile file = new("DefaultRobot");
ValidateOutput(_defaultChatRobot, file);
}
[Fact]
public void FactoryDefaultRobotTest()
{
InputOutputFile file = new("DefaultRobot");
ValidateOutput(_katheryneChatRobotFactory.GetRobot(), file);
}
[Fact]
public void KatheryneRobotTest1()
{
InputOutputFile file = new("Grammar1");
StreamReader reader = new(Path.Combine(file.PrefixPath, "grammar.yaml"));
_katheryneChatRobotFactory.SetGrammar(reader.ReadToEnd());
ValidateOutput(_katheryneChatRobotFactory.GetRobot(), file);
}
private void ValidateOutput(IChatRobot robot, InputOutputFile file)
{
foreach (string output in robot.OnChatStart())
{
string? except = file.Output.ReadLine();
Assert.NotNull(except);
Assert.Equal(except, output);
}
while (file.Input.Peek() >= 0)
{
string? input = file.Input.ReadLine();
Assert.NotNull(input);
foreach (string output in robot.ChatNext(input))
{
string? except = file.Output.ReadLine();
Assert.NotNull(except);
Assert.Equal(except, output);
}
}
foreach (string output in robot.OnChatStop())
{
string? except = file.Output.ReadLine();
Assert.NotNull(except);
Assert.Equal(except, output);
}
}
private class InputOutputFile
{
public StreamReader Input { get; }
public StreamReader Output { get; }
public string PrefixPath { get; }
public InputOutputFile(string testName)
{
PrefixPath = Path.Combine(Environment.CurrentDirectory, testName);
Input = new StreamReader(Path.Combine(PrefixPath, "in.txt"));
Output = new StreamReader(Path.Combine(PrefixPath, "out.txt"));
}
}
}