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