2023-10-23 22:38:13 +08:00
|
|
|
|
using System.Text.RegularExpressions;
|
2023-10-30 22:25:20 +08:00
|
|
|
|
using Katheryne.Abstractions;
|
|
|
|
|
using Katheryne.Exceptions;
|
2023-10-23 22:38:13 +08:00
|
|
|
|
using Katheryne.Models;
|
2023-10-30 22:25:20 +08:00
|
|
|
|
using Katheryne.Tests.Mocks;
|
2023-10-23 22:38:13 +08:00
|
|
|
|
|
|
|
|
|
namespace Katheryne.Tests;
|
|
|
|
|
|
|
|
|
|
public class StringFormatterTests
|
|
|
|
|
{
|
2023-10-30 22:25:20 +08:00
|
|
|
|
private readonly Dictionary<string, IParamsModule> _modules = new();
|
|
|
|
|
|
|
|
|
|
public StringFormatterTests()
|
|
|
|
|
{
|
|
|
|
|
var module = new MockParamModule();
|
|
|
|
|
_modules.Add(module.ModuleName, module);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 22:38:13 +08:00
|
|
|
|
[Fact]
|
|
|
|
|
public void FormatTest1()
|
|
|
|
|
{
|
2023-10-30 22:25:20 +08:00
|
|
|
|
StringFormatter formatter = new("Hello $1", _modules);
|
2023-10-23 22:38:13 +08:00
|
|
|
|
|
|
|
|
|
Regex regex = new("I'm (.*)");
|
|
|
|
|
|
|
|
|
|
Match match = regex.Match("I'm jackfiled");
|
|
|
|
|
|
|
|
|
|
Assert.True(match.Success);
|
|
|
|
|
Assert.True(formatter.IsFormat);
|
|
|
|
|
|
|
|
|
|
Assert.Equal("Hello jackfiled", formatter.Format(match.Groups));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void FormatTest2()
|
|
|
|
|
{
|
2023-10-30 22:25:20 +08:00
|
|
|
|
StringFormatter formatter = new("$你好呀 $1, 欢迎你离开垃圾的$2.", _modules);
|
2023-10-23 22:38:13 +08:00
|
|
|
|
|
|
|
|
|
Regex regex = new("你好,我是(.*),我来自(.*)\\.");
|
|
|
|
|
|
|
|
|
|
Match match = regex.Match("你好,我是Ichirinko,我来自Trinity.");
|
|
|
|
|
|
|
|
|
|
Assert.True(match.Success);
|
|
|
|
|
Assert.True(formatter.IsFormat);
|
|
|
|
|
|
|
|
|
|
Assert.Equal("$你好呀 Ichirinko, 欢迎你离开垃圾的Trinity.", formatter.Format(match.Groups));
|
|
|
|
|
}
|
2023-10-30 22:25:20 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ParamFormatTest1()
|
|
|
|
|
{
|
|
|
|
|
StringFormatter formatter = new("Test @test/hello", _modules);
|
|
|
|
|
|
|
|
|
|
Regex regex = new(".*?");
|
|
|
|
|
Match match = regex.Match("Test Input");
|
|
|
|
|
|
2023-11-02 13:24:50 +08:00
|
|
|
|
Assert.True(formatter.IsFormat);
|
2023-10-30 22:25:20 +08:00
|
|
|
|
Assert.Equal("Test Hello, Katheryne", formatter.Format(match.Groups));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ParamFormatTest2()
|
|
|
|
|
{
|
|
|
|
|
Assert.Throws<GrammarException>(() => new StringFormatter("Test @test/nonexistent", _modules));
|
|
|
|
|
}
|
2023-10-23 22:38:13 +08:00
|
|
|
|
}
|