add: Katheryne机器人单元测试
This commit is contained in:
		@@ -10,6 +10,7 @@
 | 
			
		||||
 | 
			
		||||
    <ItemGroup>
 | 
			
		||||
        <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>
 | 
			
		||||
@@ -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>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								Katheryne.Tests/Katheryne/DefaultRobot/in.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								Katheryne.Tests/Katheryne/DefaultRobot/in.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
你好
 | 
			
		||||
							
								
								
									
										5
									
								
								Katheryne.Tests/Katheryne/DefaultRobot/out.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								Katheryne.Tests/Katheryne/DefaultRobot/out.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
坏了,被你发现默认机器人了。
 | 
			
		||||
使用这个粪机器人,怎么能得高分呢?
 | 
			
		||||
必须要出重拳!
 | 
			
		||||
啊对对对。
 | 
			
		||||
我不到啊。
 | 
			
		||||
							
								
								
									
										14
									
								
								Katheryne.Tests/Katheryne/Grammar1/grammar.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								Katheryne.Tests/Katheryne/Grammar1/grammar.yaml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
robotName: 凯瑟琳
 | 
			
		||||
stages:
 | 
			
		||||
  - name: start
 | 
			
		||||
    answer: 向着星辰和深渊!欢迎来到冒险家协会。
 | 
			
		||||
    transformers:
 | 
			
		||||
      - pattern: .*?
 | 
			
		||||
        nextStageName: running
 | 
			
		||||
 | 
			
		||||
  - name: running
 | 
			
		||||
    answer: 对不起,做不到。
 | 
			
		||||
    transformers:
 | 
			
		||||
      - pattern: .*?
 | 
			
		||||
        nextStageName: running
 | 
			
		||||
beginStageName: start
 | 
			
		||||
							
								
								
									
										3
									
								
								Katheryne.Tests/Katheryne/Grammar1/in.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								Katheryne.Tests/Katheryne/Grammar1/in.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
你说得对
 | 
			
		||||
但是
 | 
			
		||||
原神
 | 
			
		||||
							
								
								
									
										5
									
								
								Katheryne.Tests/Katheryne/Grammar1/out.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								Katheryne.Tests/Katheryne/Grammar1/out.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
向着星辰和深渊!欢迎来到冒险家协会。
 | 
			
		||||
对不起,做不到。
 | 
			
		||||
对不起,做不到。
 | 
			
		||||
对不起,做不到。
 | 
			
		||||
再见。
 | 
			
		||||
							
								
								
									
										95
									
								
								Katheryne.Tests/Katheryne/KatheryneRobotTests.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								Katheryne.Tests/Katheryne/KatheryneRobotTests.cs
									
									
									
									
									
										Normal 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"));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user