using GFramework.Core.extensions; using NUnit.Framework; namespace GFramework.Core.Tests.extensions; /// /// 测试 GuardExtensions 扩展方法的功能 /// [TestFixture] public class GuardExtensionsTests { const string TestParamName = "testParam"; /// /// 测试ThrowIfNull方法在值不为null时返回值本身 /// [Test] public void ThrowIfNull_Should_Return_Value_When_Value_Is_Not_Null() { // Arrange var value = "test"; // Act var result = value.ThrowIfNull(); // Assert Assert.That(result, Is.EqualTo("test")); } /// /// 测试ThrowIfNull方法在值为null时抛出ArgumentNullException /// [Test] public void ThrowIfNull_Should_Throw_ArgumentNullException_When_Value_Is_Null() { // Arrange string? value = null; // Act & Assert Assert.Throws(() => value.ThrowIfNull()); } /// /// 测试ThrowIfNull方法在抛出异常时包含参数名称 /// [Test] public void ThrowIfNull_Should_Include_ParamName_In_Exception() { // Arrange string? value = null; // Act & Assert var ex = Assert.Throws(() => value.ThrowIfNull(TestParamName)); Assert.That(ex?.ParamName, Is.EqualTo(TestParamName)); } /// /// 测试ThrowIfNullOrEmpty方法在值不为空时返回值本身 /// [Test] public void ThrowIfNullOrEmpty_Should_Return_Value_When_Value_Is_Not_Empty() { // Arrange var value = "test"; // Act var result = value.ThrowIfNullOrEmpty(); // Assert Assert.That(result, Is.EqualTo("test")); } /// /// 测试ThrowIfNullOrEmpty方法在值为null时抛出ArgumentNullException /// [Test] public void ThrowIfNullOrEmpty_Should_Throw_ArgumentNullException_When_Value_Is_Null() { // Arrange string? value = null; // Act & Assert Assert.Throws(() => value.ThrowIfNullOrEmpty()); } /// /// 测试ThrowIfNullOrEmpty方法在值为空时抛出ArgumentException /// [Test] public void ThrowIfNullOrEmpty_Should_Throw_ArgumentException_When_Value_Is_Empty() { // Arrange var value = string.Empty; // Act & Assert Assert.Throws(() => value.ThrowIfNullOrEmpty()); } /// /// 测试ThrowIfNullOrEmpty方法在抛出异常时包含参数名称 /// [Test] public void ThrowIfNullOrEmpty_Should_Include_ParamName_In_Exception() { // Arrange var value = string.Empty; // Act & Assert var ex = Assert.Throws(() => value.ThrowIfNullOrEmpty(TestParamName)); Assert.That(ex?.ParamName, Is.EqualTo(TestParamName)); } /// /// 测试ThrowIfEmpty方法在集合有元素时返回集合本身 /// [Test] public void ThrowIfEmpty_Should_Return_Collection_When_Collection_Has_Elements() { // Arrange var collection = new[] { 1, 2, 3 }; // Act var result = collection.ThrowIfEmpty(); // Assert Assert.That(result, Is.EqualTo(collection)); } /// /// 测试ThrowIfEmpty方法在集合为null时抛出ArgumentNullException /// [Test] public void ThrowIfEmpty_Should_Throw_ArgumentNullException_When_Collection_Is_Null() { // Arrange IEnumerable? collection = null; // Act & Assert Assert.Throws(() => collection.ThrowIfEmpty()); } /// /// 测试ThrowIfEmpty方法在集合为空时抛出ArgumentException /// [Test] public void ThrowIfEmpty_Should_Throw_ArgumentException_When_Collection_Is_Empty() { // Arrange var collection = Array.Empty(); // Act & Assert Assert.Throws(() => collection.ThrowIfEmpty()); } /// /// 测试ThrowIfEmpty方法在抛出异常时包含参数名称 /// [Test] public void ThrowIfEmpty_Should_Include_ParamName_In_Exception() { // Arrange var collection = Array.Empty(); // Act & Assert var ex = Assert.Throws(() => collection.ThrowIfEmpty(TestParamName)); Assert.That(ex?.ParamName, Is.EqualTo(TestParamName)); } }