From 79962727ba76703214428f9bd221cb6680c24be5 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sun, 26 Nov 2023 11:27:05 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=A3=80=E6=B5=8B=E9=80=92=E5=BD=92?= =?UTF-8?q?=E8=BD=AC=E7=A7=BB=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Katheryne/Grammar3/grammar.yaml | 22 +++++++++++++++++++ Katheryne.Tests/Katheryne/Grammar3/in.txt | 2 ++ Katheryne.Tests/Katheryne/Grammar3/out.txt | 6 +++++ .../Katheryne/KatheryneRobotTests.cs | 17 ++++++++++++++ Katheryne.Tests/Mocks/MockWeatherModule.cs | 21 ++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 Katheryne.Tests/Katheryne/Grammar3/grammar.yaml create mode 100644 Katheryne.Tests/Katheryne/Grammar3/in.txt create mode 100644 Katheryne.Tests/Katheryne/Grammar3/out.txt create mode 100644 Katheryne.Tests/Mocks/MockWeatherModule.cs diff --git a/Katheryne.Tests/Katheryne/Grammar3/grammar.yaml b/Katheryne.Tests/Katheryne/Grammar3/grammar.yaml new file mode 100644 index 0000000..b44da18 --- /dev/null +++ b/Katheryne.Tests/Katheryne/Grammar3/grammar.yaml @@ -0,0 +1,22 @@ +robotName: 啊准 +stages: + - name: start + answer: 我是啊准,一个可以查询天气的机器人。 + transformers: + - pattern: .*?天气|气温.*? + nextStageName: weather + - pattern: .*? + nextStageName: running + + - name: running + answer: 对不起,做不到。 + transformers: + - pattern: + nextStageName: running + + - name: weather + answer: 今天北京市的天气是@weather/text,气温是@weather/temp 摄氏度。 + transformers: + - pattern: + nextStageName: running +beginStageName: start \ No newline at end of file diff --git a/Katheryne.Tests/Katheryne/Grammar3/in.txt b/Katheryne.Tests/Katheryne/Grammar3/in.txt new file mode 100644 index 0000000..3ebfd5e --- /dev/null +++ b/Katheryne.Tests/Katheryne/Grammar3/in.txt @@ -0,0 +1,2 @@ +今天北京的天气怎么样? +你说的对,但是 \ No newline at end of file diff --git a/Katheryne.Tests/Katheryne/Grammar3/out.txt b/Katheryne.Tests/Katheryne/Grammar3/out.txt new file mode 100644 index 0000000..f75fdc6 --- /dev/null +++ b/Katheryne.Tests/Katheryne/Grammar3/out.txt @@ -0,0 +1,6 @@ +我是啊准,一个可以查询天气的机器人。 +今天北京市的天气是晴,气温是20 摄氏度。 +我是啊准,一个可以查询天气的机器人。 +对不起,做不到。 +我是啊准,一个可以查询天气的机器人。 +再见。 \ No newline at end of file diff --git a/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs b/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs index 1512e61..8baf335 100644 --- a/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs +++ b/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs @@ -1,6 +1,8 @@ using Katheryne.Abstractions; +using Katheryne.Exceptions; using Katheryne.Modules; using Katheryne.Services; +using Katheryne.Tests.Mocks; using Microsoft.Extensions.Logging; using Moq; @@ -57,6 +59,21 @@ public class KatheryneRobotTests ValidateOutput(_katheryneChatRobotFactory.GetRobot(), file); } + [Fact] + public void RecursivelyExceptionTest() + { + IParamsModule weatherModule = new MockWeatherModule(); + _katheryneChatRobotFactory.Modules.Clear(); + _katheryneChatRobotFactory.Modules.Add(weatherModule.ModuleName, weatherModule); + + InputOutputFile file = new("Grammar3"); + StreamReader reader = new(Path.Combine(file.PrefixPath, "grammar.yaml")); + _katheryneChatRobotFactory.SetGrammar(reader.ReadToEnd()); + + Assert.Throws( + () => ValidateOutput(_katheryneChatRobotFactory.GetRobot(), file)); + } + [Fact] public void WeatherModuleTest() { diff --git a/Katheryne.Tests/Mocks/MockWeatherModule.cs b/Katheryne.Tests/Mocks/MockWeatherModule.cs new file mode 100644 index 0000000..9d6a010 --- /dev/null +++ b/Katheryne.Tests/Mocks/MockWeatherModule.cs @@ -0,0 +1,21 @@ +using Katheryne.Abstractions; + +namespace Katheryne.Tests.Mocks; + +public class MockWeatherModule : IParamsModule +{ + private readonly Dictionary _param = new() + { + { "temp", "20" }, + { "text", "晴" } + }; + + public string ModuleName => "weather"; + + public string this[string param] => _param[param]; + + public bool ContainsParam(string param) + { + return _param.ContainsKey(param); + } +} \ No newline at end of file