diff --git a/Katheryne.Tests/Katheryne.Tests.csproj b/Katheryne.Tests/Katheryne.Tests.csproj index 8a45476..fe769ed 100644 --- a/Katheryne.Tests/Katheryne.Tests.csproj +++ b/Katheryne.Tests/Katheryne.Tests.csproj @@ -9,8 +9,9 @@ - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all @@ -24,5 +25,15 @@ + + + + + + + + diff --git a/Katheryne.Tests/Katheryne/DefaultRobot/in.txt b/Katheryne.Tests/Katheryne/DefaultRobot/in.txt new file mode 100644 index 0000000..f3ca610 --- /dev/null +++ b/Katheryne.Tests/Katheryne/DefaultRobot/in.txt @@ -0,0 +1 @@ +你好 \ No newline at end of file diff --git a/Katheryne.Tests/Katheryne/DefaultRobot/out.txt b/Katheryne.Tests/Katheryne/DefaultRobot/out.txt new file mode 100644 index 0000000..98535bd --- /dev/null +++ b/Katheryne.Tests/Katheryne/DefaultRobot/out.txt @@ -0,0 +1,5 @@ +坏了,被你发现默认机器人了。 +使用这个粪机器人,怎么能得高分呢? +必须要出重拳! +啊对对对。 +我不到啊。 \ No newline at end of file diff --git a/Katheryne.Tests/Katheryne/Grammar1/grammar.yaml b/Katheryne.Tests/Katheryne/Grammar1/grammar.yaml new file mode 100644 index 0000000..c2ac7b6 --- /dev/null +++ b/Katheryne.Tests/Katheryne/Grammar1/grammar.yaml @@ -0,0 +1,14 @@ +robotName: 凯瑟琳 +stages: + - name: start + answer: 向着星辰和深渊!欢迎来到冒险家协会。 + transformers: + - pattern: .*? + nextStageName: running + + - name: running + answer: 对不起,做不到。 + transformers: + - pattern: .*? + nextStageName: running +beginStageName: start \ No newline at end of file diff --git a/Katheryne.Tests/Katheryne/Grammar1/in.txt b/Katheryne.Tests/Katheryne/Grammar1/in.txt new file mode 100644 index 0000000..ff9bcfd --- /dev/null +++ b/Katheryne.Tests/Katheryne/Grammar1/in.txt @@ -0,0 +1,3 @@ +你说得对 +但是 +原神 \ No newline at end of file diff --git a/Katheryne.Tests/Katheryne/Grammar1/out.txt b/Katheryne.Tests/Katheryne/Grammar1/out.txt new file mode 100644 index 0000000..7579e69 --- /dev/null +++ b/Katheryne.Tests/Katheryne/Grammar1/out.txt @@ -0,0 +1,5 @@ +向着星辰和深渊!欢迎来到冒险家协会。 +对不起,做不到。 +对不起,做不到。 +对不起,做不到。 +再见。 \ No newline at end of file diff --git a/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs b/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs new file mode 100644 index 0000000..ad2ee0a --- /dev/null +++ b/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs @@ -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> _defaultChatRobotLogger = new(); + private readonly Mock> _katheryneChatRobotLogger = new(); + private readonly Mock> _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")); + } + } +} \ No newline at end of file