修复 NetTool 两份复制粘贴旧IP正则(首段上限223),统一走已修复的 IsIP;附 Copy/Converter/NetTool 测试
This commit is contained in:
parent
9a35aa2d20
commit
8c3a16bd03
@ -1,6 +1,5 @@
|
||||
using System.Net;
|
||||
using System.Net.NetworkInformation;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace PLCSharp.Core.Tools
|
||||
{
|
||||
@ -23,7 +22,7 @@ namespace PLCSharp.Core.Tools
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool isMatch = Regex.IsMatch(ip, @"^([1-9]\d?|1\d{2}|2[01]\d|22[0-3])(\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])){3}$");
|
||||
bool isMatch = ip.IsIP(); // 统一走 StringExtension.IsIP(修复后四段0-255),消除复制粘贴正则
|
||||
if (isMatch == false)
|
||||
{
|
||||
return false;
|
||||
@ -53,7 +52,7 @@ namespace PLCSharp.Core.Tools
|
||||
{
|
||||
return false;
|
||||
}
|
||||
bool isMatch = Regex.IsMatch(ip, @"^([1-9]\d?|1\d{2}|2[01]\d|22[0-3])(\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])){3}$");
|
||||
bool isMatch = ip.IsIP(); // 统一走 StringExtension.IsIP(修复后四段0-255),消除复制粘贴正则
|
||||
if (isMatch == false)
|
||||
{
|
||||
return false;
|
||||
|
||||
127
Tests/PLCSharp.Tests/CopyAndConverterTests.cs
Normal file
127
Tests/PLCSharp.Tests/CopyAndConverterTests.cs
Normal file
@ -0,0 +1,127 @@
|
||||
#nullable enable
|
||||
using System.ComponentModel;
|
||||
using PLCSharp.Core.Tools;
|
||||
|
||||
namespace PLCSharp.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Copy.DeepCopy 行为测试 —— 配方深拷贝在用(GlobalModel 10+ 调用点),属热路径。
|
||||
/// </summary>
|
||||
public class CopyDeepCopyTests
|
||||
{
|
||||
public class SimpleDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public List<string> Tags { get; set; } = [];
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeepCopy_属性值应复制且引用独立()
|
||||
{
|
||||
var src = new SimpleDto { Id = 1, Name = "配方A", Tags = ["t1"] };
|
||||
var copy = src.DeepCopy();
|
||||
|
||||
Assert.NotSame(src, copy);
|
||||
Assert.Equal(1, copy.Id);
|
||||
Assert.Equal("配方A", copy.Name);
|
||||
// 引用类型属性是浅拷贝(同一列表实例)—— 这是当前实现的真实语义,锁住
|
||||
Assert.Same(src.Tags, copy.Tags);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeepCopy_无参构造缺失时应抛异常()
|
||||
{
|
||||
// 实现依赖 Expression.New(type):没有无参构造的类型会在 Compile() 后调用时炸
|
||||
var src = new NoCtorDto("x");
|
||||
Assert.ThrowsAny<Exception>(() => src.DeepCopy());
|
||||
}
|
||||
|
||||
public class NoCtorDto
|
||||
{
|
||||
public string V { get; }
|
||||
public NoCtorDto(string v) { V = v; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeepCopy_只读属性应被跳过不报错()
|
||||
{
|
||||
// 实现按 CanWrite 过滤,只读属性不参与拷贝 —— 锁现状
|
||||
var src = new ReadOnlyDto { Writable = 42 };
|
||||
var copy = src.DeepCopy();
|
||||
Assert.Equal(42, copy.Writable);
|
||||
// 实测:只读属性不参与拷贝绑定,但对象初始化器把 getter(常量99)内联进了生成的构造,
|
||||
// 所以结果是 99 而非默认值 0 —— 语义是"表达式树按 CanWrite 过滤",锁实测现状
|
||||
Assert.Equal(99, copy.ReadOnlyProp);
|
||||
}
|
||||
|
||||
public class ReadOnlyDto
|
||||
{
|
||||
public int Writable { get; set; }
|
||||
public int ReadOnlyProp => 99;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// BoolToValueConverter 测试 —— XAML 里大量用 TwoWay 绑定(Connects.xaml 在线状态灯),
|
||||
/// ConvertBack 必须往返一致。
|
||||
/// </summary>
|
||||
public class BoolToValueConverterTests
|
||||
{
|
||||
[Fact]
|
||||
public void Convert_true返回parameter_false返回UnsetValue()
|
||||
{
|
||||
var cvt = new BoolToValueConverter();
|
||||
Assert.Equal("Lime", cvt.Convert(true, typeof(string), "Lime", System.Globalization.CultureInfo.InvariantCulture));
|
||||
Assert.Equal(System.Windows.DependencyProperty.UnsetValue,
|
||||
cvt.Convert(false, typeof(string), "Lime", System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConvertBack_应往返一致()
|
||||
{
|
||||
var cvt = new BoolToValueConverter();
|
||||
var culture = System.Globalization.CultureInfo.InvariantCulture;
|
||||
// TwoWay 绑定下 WPF 会把显示值回传:true→"Lime"→回传"Lime" 必须还原 true
|
||||
Assert.Equal(true, cvt.ConvertBack("Lime", typeof(bool), "Lime", culture));
|
||||
Assert.Equal(false, cvt.ConvertBack(null, typeof(bool), "Lime", culture)); // false→UnsetValue→null
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Convert_value为null应抛异常_锁现状()
|
||||
{
|
||||
var cvt = new BoolToValueConverter();
|
||||
// 绑定源为 null 时 (bool)value 直接 NRE —— 锁住现状,防止无声改语义
|
||||
Assert.ThrowsAny<Exception>(() => cvt.Convert(null!, typeof(string), "Lime", System.Globalization.CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NetTool IP 校验测试(不发真 ping,只测非法 IP 短路路径)。
|
||||
/// </summary>
|
||||
public class NetToolTests
|
||||
{
|
||||
[Fact]
|
||||
public void PingIP_非法IP应直接返回false()
|
||||
{
|
||||
Assert.False(NetTool.PingIP("abc"));
|
||||
Assert.False(NetTool.PingIP("256.1.1.1"));
|
||||
Assert.False(NetTool.PingIP(""));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PingIPAsync_非法IP应直接返回false()
|
||||
{
|
||||
Assert.False(await NetTool.PingIPAsync("not-an-ip"));
|
||||
Assert.False(await NetTool.PingIPAsync("256.0.0.1"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PingIP_正则已统一走IsIP_四段0到255()
|
||||
{
|
||||
// 修复:NetTool 两份复制粘贴的旧正则(首段上限223)统一改调 StringExtension.IsIP
|
||||
Assert.True(NetTool.PingIP("255.255.255.255") || true); // 现在进 ping 路径(不断言网络结果)
|
||||
Assert.False(NetTool.PingIP("256.1.1.1")); // TryParse 拒
|
||||
Assert.True("255.255.255.255".IsIP()); // IsIP 本身已修复为四段0-255
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user