add: 检测递归转移单元测试
This commit is contained in:
		
							
								
								
									
										22
									
								
								Katheryne.Tests/Katheryne/Grammar3/grammar.yaml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								Katheryne.Tests/Katheryne/Grammar3/grammar.yaml
									
									
									
									
									
										Normal file
									
								
							@@ -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
 | 
				
			||||||
							
								
								
									
										2
									
								
								Katheryne.Tests/Katheryne/Grammar3/in.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								Katheryne.Tests/Katheryne/Grammar3/in.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
				
			|||||||
 | 
					今天北京的天气怎么样?
 | 
				
			||||||
 | 
					你说的对,但是
 | 
				
			||||||
							
								
								
									
										6
									
								
								Katheryne.Tests/Katheryne/Grammar3/out.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								Katheryne.Tests/Katheryne/Grammar3/out.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
				
			|||||||
 | 
					我是啊准,一个可以查询天气的机器人。
 | 
				
			||||||
 | 
					今天北京市的天气是晴,气温是20 摄氏度。
 | 
				
			||||||
 | 
					我是啊准,一个可以查询天气的机器人。
 | 
				
			||||||
 | 
					对不起,做不到。
 | 
				
			||||||
 | 
					我是啊准,一个可以查询天气的机器人。
 | 
				
			||||||
 | 
					再见。
 | 
				
			||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
using Katheryne.Abstractions;
 | 
					using Katheryne.Abstractions;
 | 
				
			||||||
 | 
					using Katheryne.Exceptions;
 | 
				
			||||||
using Katheryne.Modules;
 | 
					using Katheryne.Modules;
 | 
				
			||||||
using Katheryne.Services;
 | 
					using Katheryne.Services;
 | 
				
			||||||
 | 
					using Katheryne.Tests.Mocks;
 | 
				
			||||||
using Microsoft.Extensions.Logging;
 | 
					using Microsoft.Extensions.Logging;
 | 
				
			||||||
using Moq;
 | 
					using Moq;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -57,6 +59,21 @@ public class KatheryneRobotTests
 | 
				
			|||||||
        ValidateOutput(_katheryneChatRobotFactory.GetRobot(), file);
 | 
					        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<GrammarException>(
 | 
				
			||||||
 | 
					            () => ValidateOutput(_katheryneChatRobotFactory.GetRobot(), file));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    [Fact]
 | 
					    [Fact]
 | 
				
			||||||
    public void WeatherModuleTest()
 | 
					    public void WeatherModuleTest()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										21
									
								
								Katheryne.Tests/Mocks/MockWeatherModule.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								Katheryne.Tests/Mocks/MockWeatherModule.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					using Katheryne.Abstractions;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace Katheryne.Tests.Mocks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class MockWeatherModule : IParamsModule
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    private readonly Dictionary<string, string> _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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user