add: 解析YAML语法文件
This commit is contained in:
parent
4aa89c4385
commit
9c27d534ce
28
Katheryne.Tests/Katheryne.Tests.csproj
Normal file
28
Katheryne.Tests/Katheryne.Tests.csproj
Normal file
|
@ -0,0 +1,28 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/>
|
||||
<PackageReference Include="xunit" Version="2.4.2"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.2">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Katheryne\Katheryne.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
1
Katheryne.Tests/Usings.cs
Normal file
1
Katheryne.Tests/Usings.cs
Normal file
|
@ -0,0 +1 @@
|
|||
global using Xunit;
|
16
Katheryne.Tests/YamlDeserializerTest1.yaml
Normal file
16
Katheryne.Tests/YamlDeserializerTest1.yaml
Normal file
|
@ -0,0 +1,16 @@
|
|||
robotName: 凯瑟琳
|
||||
stages:
|
||||
- name: start
|
||||
answer: 向着星辰和深渊!欢迎来到冒险家协会。
|
||||
transformers:
|
||||
- pattern: .*?
|
||||
nextStageName: running
|
||||
|
||||
- name: running
|
||||
answer: 对不起,做不到。
|
||||
transformers:
|
||||
- pattern: .*?
|
||||
nextStageName: running
|
||||
beginStageName: start
|
||||
|
||||
|
42
Katheryne.Tests/YamlDeserializerTests.cs
Normal file
42
Katheryne.Tests/YamlDeserializerTests.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
using Katheryne.Models;
|
||||
using Katheryne.Services;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace Katheryne.Tests;
|
||||
|
||||
public class YamlDeserializerTests
|
||||
{
|
||||
private readonly YamlDeserializerFactory _factory = new();
|
||||
|
||||
[Fact]
|
||||
public void DeserializerTest1()
|
||||
{
|
||||
const string document =
|
||||
"""
|
||||
robotName: 凯瑟琳
|
||||
stages:
|
||||
- name: start
|
||||
answer: 向着星辰和深渊!欢迎来到冒险家协会。
|
||||
transformers:
|
||||
- pattern: .*?
|
||||
nextStageName: running
|
||||
|
||||
- name: running
|
||||
answer: 对不起,做不到。
|
||||
transformers:
|
||||
- pattern: .*?
|
||||
nextStageName: running
|
||||
beginStageName: start
|
||||
""";
|
||||
|
||||
IDeserializer deserializer = _factory.GetDeserializer();
|
||||
LexicalModel actual = deserializer.Deserialize<LexicalModel>(document);
|
||||
|
||||
Assert.Equal("凯瑟琳", actual.RobotName);
|
||||
Assert.Equal("start", actual.BeginStageName);
|
||||
|
||||
Assert.Contains(actual.Stages, s => s.Name == "start");
|
||||
Assert.Contains(actual.Stages, s => s.Name == "running");
|
||||
}
|
||||
|
||||
}
|
|
@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Frontend", "Frontend\Fronte
|
|||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Katheryne", "Katheryne\Katheryne.csproj", "{74F745FB-2F2B-4B56-A387-3B490EFE9615}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Katheryne.Tests", "Katheryne.Tests\Katheryne.Tests.csproj", "{F8577DDF-85FB-4610-A8AC-38C390BC80AB}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -18,5 +20,9 @@ Global
|
|||
{74F745FB-2F2B-4B56-A387-3B490EFE9615}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{74F745FB-2F2B-4B56-A387-3B490EFE9615}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{74F745FB-2F2B-4B56-A387-3B490EFE9615}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{F8577DDF-85FB-4610-A8AC-38C390BC80AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F8577DDF-85FB-4610-A8AC-38C390BC80AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F8577DDF-85FB-4610-A8AC-38C390BC80AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F8577DDF-85FB-4610-A8AC-38C390BC80AB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
|
|
@ -7,7 +7,9 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
|
||||
<PackageReference Include="YamlDotNet" Version="13.7.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
10
Katheryne/Models/LexicalModel.cs
Normal file
10
Katheryne/Models/LexicalModel.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
namespace Katheryne.Models;
|
||||
|
||||
public class LexicalModel
|
||||
{
|
||||
public required string RobotName { get; set; }
|
||||
|
||||
public required List<Stage> Stages { get; set; }
|
||||
|
||||
public required string BeginStageName { get; set; }
|
||||
}
|
30
Katheryne/Models/Stage.cs
Normal file
30
Katheryne/Models/Stage.cs
Normal file
|
@ -0,0 +1,30 @@
|
|||
namespace Katheryne.Models;
|
||||
|
||||
public class Stage
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
|
||||
public required List<Transformer> Transformers { get; set; }
|
||||
|
||||
public required string Answer { get; set; }
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj is not Stage other)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return other.Name == Name;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return Name.GetHashCode();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
8
Katheryne/Models/Transformer.cs
Normal file
8
Katheryne/Models/Transformer.cs
Normal file
|
@ -0,0 +1,8 @@
|
|||
namespace Katheryne.Models;
|
||||
|
||||
public class Transformer
|
||||
{
|
||||
public required string Pattern { get; set; }
|
||||
|
||||
public required string NextStageName { get; set; }
|
||||
}
|
20
Katheryne/Services/YamlDeserializerFactory.cs
Normal file
20
Katheryne/Services/YamlDeserializerFactory.cs
Normal file
|
@ -0,0 +1,20 @@
|
|||
using YamlDotNet.Serialization;
|
||||
using YamlDotNet.Serialization.NamingConventions;
|
||||
|
||||
namespace Katheryne.Services;
|
||||
|
||||
public class YamlDeserializerFactory
|
||||
{
|
||||
private readonly DeserializerBuilder _builder;
|
||||
|
||||
public YamlDeserializerFactory()
|
||||
{
|
||||
_builder = new DeserializerBuilder();
|
||||
_builder.WithNamingConvention(CamelCaseNamingConvention.Instance);
|
||||
}
|
||||
|
||||
public IDeserializer GetDeserializer()
|
||||
{
|
||||
return _builder.Build();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user