XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWorkflow

【WinUI】XFE基于windows的工作流

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Text.Json.Nodes;
using XFEExtension.NetCore.XUnit.Assertions;
using XFEExtension.NetCore.XUnit.Attributes;
using XFEWorkflow.Core.Runtime;

namespace XFEWorkflow.Test;

[TestFixture]
public sealed class BasicTextTests
{
    [TestCase("concat", """{"left":"  张三 🌚","separator":" / ","right":"ID=1\n"}""", """"  张三 🌚 / ID=1\n"""")]
    [TestCase("join", """{"items":["张三","👨‍👩‍👧‍👦",""],"separator":"\n"}""", """"张三\n👨‍👩‍👧‍👦\n"""")]
    [TestCase("split", """{"value":"A,,B,","separator":","}""", """["A","","B",""]""")]
    [TestCase("split", """{"value":"甲👨‍👩‍👧‍👦👍🏽🇨🇳é","separator":""}""", """["甲","👨‍👩‍👧‍👦","👍🏽","🇨🇳","é"]""")]
    [TestCase("split", """{"value":" A , , B ","removeEmpty":true,"trimEntries":true}""", """["A","B"]""")]
    [TestCase("substring", """{"value":"甲👨‍👩‍👧‍👦👍🏽🇨🇳é","start":1,"length":3}""", """"👨‍👩‍👧‍👦👍🏽🇨🇳"""")]
    [TestCase("substring", """{"value":"A😀B","start":1,"length":2,"unit":"utf16"}""", """"😀"""")]
    [TestCase("replace", """{"value":"A.aA","old":"a","replacement":"🌚","ignoreCase":true}""", """"🌚.🌚🌚"""")]
    [TestCase("trim", """{"value":" \t甲\n  ","mode":"end"}""", """" \t甲"""")]
    [TestCase("case", """{"value":"i 中文","mode":"upper"}""", """"I 中文"""")]
    [TestCase("contains", """{"value":"AbC","text":"bc","ignoreCase":true}""", "true")]
    [TestCase("startsWith", """{"value":"AbC","text":"ab"}""", "false")]
    [TestCase("endsWith", """{"value":"AbC","text":"BC","ignoreCase":true}""", "true")]
    [TestCase("pad", """{"value":"甲","fill":"👍🏽","width":3,"side":"left"}""", """"👍🏽👍🏽甲"""")]
    [TestCase("repeat", """{"value":"🌚\n","count":3}""", """"🌚\n🌚\n🌚\n"""")]
    [TestCase("lines", """{"value":"A\r\n\rB\n","removeEmpty":false}""", """["A","","B",""]""")]
    [TestCase("normalize", """{"value":"ABC","form":"FormKC"}""", """"ABC"""")]
    [TestCase("isEmpty", """{"value":" \t","includeWhitespace":true}""", "true")]
    [TestCase("isEmpty", """{"value":" \t","includeWhitespace":false}""", "false")]
    [TestCase("insert", """{"value":"A👨‍👩‍👧‍👦B","index":2,"text":"👍🏽"}""", """"A👨‍👩‍👧‍👦👍🏽B"""")]
    [TestCase("remove", """{"value":"A👨‍👩‍👧‍👦👍🏽B","start":1,"length":2}""", """"AB"""")]
    [TestCase("format", """{"format":"姓名:{0},金额:{1:F2},{{原文}}","arguments":["张三 🌚",12.3]}""", """"姓名:张三 🌚,金额:12.30,{原文}"""")]
    [TestCase("regex.replace", """{"value":"编号123","pattern":"(?<n>\\d+)","replacement":"[$1]"}""", """"编号[123]"""")]
    [TestCase("regex.escape", """{"value":"a+b[0]"}""", """"a\+b\[0]"""")]
    public void TextOperationsPreserveLiteralsAndHaveExplicitUnicodeRules(string id, string input, string expected)
    {
        var output = WorkflowBasicNodes.Execute("core.text." + id, JsonNode.Parse(input)!.AsObject());
        // Regex.Escape output must be compared as text, since its backslashes are not JSON escapes.
        if (id == "regex.escape") Assert.Equal(@"a\+b\[0]", output["value"]!.GetValue<string>());
        else
        {
            var json = output["value"]?.GetValueKind() == System.Text.Json.JsonValueKind.String ? "\"" + expected + "\"" : expected;
            Assert.True(JsonNode.DeepEquals(JsonNode.Parse(json), output["value"]), id + ":" + output["value"]);
        }
    }

    [Test]
    public void LengthSearchAndNamedRegexGroupsExposeUsefulWiredOutputs()
    {
        var length = WorkflowBasicNodes.Execute("core.text.length", new() { ["value"] = "A👨‍👩‍👧‍👦👍🏽🇨🇳é" });
        Assert.Equal(5, length["length"]!.GetValue<int>());
        Assert.Equal("A👨‍👩‍👧‍👦👍🏽🇨🇳é".Length, length["utf16Length"]!.GetValue<int>());
        var index = WorkflowBasicNodes.Execute("core.text.indexOf", new() { ["value"] = "😀abcabc", ["text"] = "abc", ["fromEnd"] = true });
        Assert.Equal(5, index["index"]!.GetValue<int>()); Assert.True(index["found"]!.GetValue<bool>());
        var regex = WorkflowBasicNodes.Execute("core.text.regex.match", new() { ["value"] = "张三=123,李四=456", ["pattern"] = @"(?<name>[\p{L}]+)=(?<id>\d+)" });
        Assert.True(regex["matched"]!.GetValue<bool>()); Assert.Equal(2, regex["count"]!.GetValue<int>());
        Assert.Equal("李四", regex["matches"]![1]!["groups"]!["name"]!.GetValue<string>());
        Assert.Equal("456", regex["matches"]![1]!["groups"]!["id"]!.GetValue<string>());
    }

    [Test]
    public void BadTypesBoundsPatternsAndExcessiveResultsFailClearly()
    {
        Assert.Throws<ArgumentException>(() => WorkflowBasicNodes.Execute("core.text.concat", new() { ["left"] = 1 }));
        Assert.Throws<ArgumentException>(() => WorkflowBasicNodes.Execute("core.text.join", new() { ["items"] = new JsonArray("1", 2) }));
        Assert.Throws<ArgumentException>(() => WorkflowBasicNodes.Execute("core.text.replace", new() { ["old"] = "" }));
        Assert.Throws<ArgumentOutOfRangeException>(() => WorkflowBasicNodes.Execute("core.text.substring", new() { ["value"] = "😀", ["start"] = 1, ["length"] = 1 }));
        Assert.Throws<ArgumentException>(() => WorkflowBasicNodes.Execute("core.text.pad", new() { ["fill"] = "ab" }));
        Assert.Throws<ArgumentException>(() => WorkflowBasicNodes.Execute("core.text.repeat", new() { ["value"] = new string('a', 100), ["count"] = 100_000 }));
        Assert.Throws<ArgumentException>(() => WorkflowBasicNodes.Execute("core.text.join", new() { ["items"] = new JsonArray(new string('a', 600_000), new string('b', 600_000)) }));
        Assert.Throws<ArgumentException>(() => WorkflowBasicNodes.Execute("core.text.replace", new() { ["value"] = "aaaaaaaaaa", ["old"] = "a", ["replacement"] = new string('b', 200_000) }));
        Assert.Throws<ArgumentException>(() => WorkflowBasicNodes.Execute("core.text.regex.match", new() { ["pattern"] = "[" }));
        Assert.Throws<FormatException>(() => WorkflowBasicNodes.Execute("core.text.format", new() { ["format"] = "{1}", ["arguments"] = new JsonArray("一项") }));
        using var cancelled = new CancellationTokenSource(); cancelled.Cancel();
        Assert.Throws<OperationCanceledException>(() => WorkflowBasicNodes.Execute("core.text.concat", [], cancelled.Token));
    }
}