using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using NewLife.XCoder.Models;
using NewLife.XCoder.Tools.Regex;
using Xunit;
namespace XCoder.App.UiTests;
/// <summary>正则工具:匹配结果表格、选中联动与捕获展开</summary>
public class RegexToolViewTests
{
private static RegexToolView CreateView()
{
var view = new RegexToolView();
view.Measure(new Size(1200, 900));
view.Arrange(new Rect(0, 0, 1200, 900));
return view;
}
private static List<T> Items<T>(ListBox list) => list.ItemsSource?.Cast<T>().ToList() ?? [];
[Fact]
[DisplayName("匹配结果按行列出,选中匹配行高亮原文并联动分组与捕获")]
public async Task Match_Rows_And_Selection_Drilldown()
{
await UiSession.Run(async () =>
{
var view = CreateView();
var pattern = view.FindControl<TextBox>("PatternBox")!;
var input = view.FindControl<TextBox>("InputBox")!;
var matchList = view.FindControl<ListBox>("MatchList")!;
var groupList = view.FindControl<ListBox>("GroupList")!;
var captureList = view.FindControl<ListBox>("CaptureList")!;
pattern.Text = @"(?<word>[A-Za-z]+)-(\d+)";
input.Text = "abc-12\nde-3";
var count = await view.RunMatchAsync();
Assert.Equal(2, count);
Assert.Equal(2, matchList.ItemCount);
var matches = Items<RegexMatchItem>(matchList);
Assert.Equal("abc-12", matches[0].Value);
Assert.Equal(1, matches[0].Line);
Assert.Equal(0, matches[0].Position);
Assert.Equal(6, matches[0].Length);
Assert.Equal(2, matches[1].Line);
// 选中第一项:原文高亮 + 右侧分组表刷新
matchList.SelectedIndex = 0;
Assert.Equal(0, input.SelectionStart);
Assert.Equal(6, input.SelectionEnd);
var groups = Items<RegexGroupItem>(groupList);
Assert.Equal(3, groups.Count);
Assert.Equal("word", groups[1].Name);
Assert.Equal("abc", groups[1].Value);
Assert.Equal(1, groupList.SelectedIndex);
// 选中第二组:原文选中数字片段 + 捕获表出现
groupList.SelectedIndex = 2;
Assert.Equal(4, input.SelectionStart);
Assert.Equal(6, input.SelectionEnd);
var captures = Items<RegexCaptureItem>(captureList);
Assert.Single(captures);
Assert.Equal("12", captures[0].Value);
});
}
[Fact]
[DisplayName("重复分组展开全部捕获,选中捕获行高亮对应片段")]
public async Task Group_Captures_Expand()
{
await UiSession.Run(async () =>
{
var view = CreateView();
var pattern = view.FindControl<TextBox>("PatternBox")!;
var input = view.FindControl<TextBox>("InputBox")!;
var matchList = view.FindControl<ListBox>("MatchList")!;
var groupList = view.FindControl<ListBox>("GroupList")!;
var captureList = view.FindControl<ListBox>("CaptureList")!;
pattern.Text = @"(\w)+";
input.Text = "abc";
await view.RunMatchAsync();
matchList.SelectedIndex = 0;
groupList.SelectedIndex = 1;
var captures = Items<RegexCaptureItem>(captureList);
Assert.Equal(3, captures.Count);
String[] expected = ["a", "b", "c"];
Assert.Equal(expected, captures.Select(e => e.Value).ToArray());
captureList.SelectedIndex = 0;
Assert.Equal(0, input.SelectionStart);
Assert.Equal(1, input.SelectionEnd);
});
}
[Fact]
[DisplayName("可选分组未命中时标记失败,替换结果写入结果框")]
public async Task FailedGroup_And_Replace()
{
await UiSession.Run(async () =>
{
var view = CreateView();
var pattern = view.FindControl<TextBox>("PatternBox")!;
var input = view.FindControl<TextBox>("InputBox")!;
var replacement = view.FindControl<TextBox>("ReplacementBox")!;
var output = view.FindControl<TextBox>("OutputBox")!;
var matchList = view.FindControl<ListBox>("MatchList")!;
var groupList = view.FindControl<ListBox>("GroupList")!;
pattern.Text = @"(a)?(b)";
input.Text = "b";
replacement.Text = "[$2]";
var count = await view.RunReplaceAsync();
Assert.Equal(1, count);
Assert.Equal("[b]", output.Text);
Assert.Equal(1, matchList.ItemCount);
matchList.SelectedIndex = 0;
var groups = Items<RegexGroupItem>(groupList);
Assert.False(groups[1].Success);
Assert.Equal("—", groups[1].SuccessText);
Assert.True(groups[2].Success);
Assert.Equal("b", groups[2].Value);
});
}
}
|