using System; using System.Collections.Generic; using Util.Tests.XUnitHelpers; using Xunit; namespace Util.Tests.Extensions; /// /// 验证扩展测试 /// public class ValidationExtensionsTest { /// /// 检查空值,不为空则正常执行 /// [Fact] public void TestCheckNull() { object test = new object(); test.CheckNull( "test" ); } /// /// 检查空值,值为null则抛出异常 /// [Fact] public void TestCheckNull_Null_Throw() { AssertHelper.Throws( () => { object test = new object(); test = null; test.CheckNull( "test" ); }, "test" ); } /// /// 测试是否空值 - 字符串 /// [Theory] [InlineData( null,true )] [InlineData( "", true )] [InlineData( " ", true )] [InlineData( "a", false )] public void TestIsEmpty_String( string value,bool result ) { Assert.Equal( value.IsEmpty(),result ); } /// /// 测试是否空值 - Guid /// [Fact] public void TestIsEmpty_Guid() { Assert.True( Guid.Empty.IsEmpty() ); Assert.False( Guid.NewGuid().IsEmpty() ); } /// /// 测试是否空值 - 可空Guid /// [Fact] public void TestIsEmpty_Guid_Nullable() { Guid? value = null; Assert.True( value.IsEmpty() ); value = Guid.Empty; Assert.True( value.IsEmpty() ); value = Guid.NewGuid(); Assert.False( value.IsEmpty() ); } /// /// 测试是否空值 - 集合 /// [Fact] public void TestIsEmpty_List() { List list = null; Assert.True( list.IsEmpty() ); list = new List(); Assert.True( list.IsEmpty() ); list.Add( 1 ); Assert.False( list.IsEmpty() ); } /// /// 测试是否默认值 /// [Fact] public void TestIsDefault() { Assert.True( Guid.Empty.IsDefault() ); Assert.False( Guid.NewGuid().IsDefault() ); } }