|
|
@ -2,128 +2,12 @@ |
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
|
|
|
|
|
using System; |
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
|
|
|
using System.Linq; |
|
|
|
|
|
using Xunit; |
|
|
using Xunit; |
|
|
|
|
|
|
|
|
namespace SixLabors.Helpers.Tests |
|
|
namespace SixLabors.Helpers.Tests |
|
|
{ |
|
|
{ |
|
|
public class GuardTests |
|
|
public class GuardTests |
|
|
{ |
|
|
{ |
|
|
[Fact] |
|
|
|
|
|
public void NotNull_TargetNotNull_ThrowsNoException() |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNull("test", "myParamName"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNull_TargetNull_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
Assert.Throws<ArgumentNullException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNull((object)null, "myParamName"); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNull_TargetNullWithMessage_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
var exception = Assert.Throws<ArgumentNullException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNull((object)null, "myParamName", "myTestMessage"); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
|
|
|
Assert.True(exception.Message.Contains("myTestMessage")); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNullOrEmpty_TargetNotNullOrEmpty_ThrowsNoException() |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNullOrEmpty("test", "myParamName"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNullOrEmpty_TargetNull_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
Assert.Throws<ArgumentNullException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNullOrEmpty(null, "myParamName"); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNullOrEmpty_TargetWhitespace_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
Assert.Throws<ArgumentException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNullOrEmpty("\n\n", "myParamName"); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNullOrEmpty_TargetEmpty_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
var exception = Assert.Throws<ArgumentException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNullOrEmpty(string.Empty, "myParamName"); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
|
|
|
Assert.True(exception.Message.Contains("Value cannot be null, empty, or cannot contain only whitespace.")); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNullOrEmpty_TargetEmptyWithMessage_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
var exception = Assert.Throws<ArgumentException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNullOrEmpty(string.Empty, "myParamName", "myTestMessage"); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
|
|
|
Assert.True(exception.Message.Contains("myTestMessage")); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNullOrEmptyIEnumerable_TargetNotNullOrEmpty_ThrowsNoException() |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNullOrEmpty(new string[] { "test" }, "myParamName"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNullOrEmptyIEnumerable_TargetNull_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
Assert.Throws<ArgumentNullException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNullOrEmpty((IEnumerable<string>)null, "myParamName"); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNullOrEmptyIEnumerable_TargetEmpty_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
var exception = Assert.Throws<ArgumentException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNullOrEmpty(new string[] { }, "myParamName"); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
|
|
|
Assert.True(exception.Message.Contains("Value cannot be empty.")); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void NotNullOrEmptyIEnumerable_TargetEmptyWithMessage_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
var exception = Assert.Throws<ArgumentException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.NotNullOrEmpty(new string[] { }, "myParamName", "myTestMessage"); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
|
|
|
Assert.True(exception.Message.Contains("myTestMessage")); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
[Fact] |
|
|
public void MustBeLessThan_IsLess_ThrowsNoException() |
|
|
public void MustBeLessThan_IsLess_ThrowsNoException() |
|
|
{ |
|
|
{ |
|
|
@ -181,7 +65,7 @@ namespace SixLabors.Helpers.Tests |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
Assert.True(exception.Message.Contains($"Value must be greater than {min}.")); |
|
|
Assert.Contains($"Value must be greater than {min}.", exception.Message); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Theory] |
|
|
[Theory] |
|
|
@ -224,43 +108,7 @@ namespace SixLabors.Helpers.Tests |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
Assert.True(exception.Message.Contains($"Value must be greater than or equal to {min} and less than or equal to {max}.")); |
|
|
Assert.Contains($"Value must be greater than or equal to {min} and less than or equal to {max}.", exception.Message); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void IsTrue_IsTrue_ThrowsNoException() |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.IsTrue(true, "myParamName", "myTestMessage"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void IsTrue_IsFalse_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
var exception = Assert.Throws<ArgumentException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.IsTrue(false, "myParamName", "myTestMessage"); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
|
|
|
Assert.True(exception.Message.Contains("myTestMessage")); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void IsFalse_IsFalse_ThrowsNoException() |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.IsFalse(false, "myParamName", "myTestMessage"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
[Fact] |
|
|
|
|
|
public void IsFalse_IsTrue_ThrowsException() |
|
|
|
|
|
{ |
|
|
|
|
|
var exception = Assert.Throws<ArgumentException>(() => |
|
|
|
|
|
{ |
|
|
|
|
|
Guard.IsFalse(true, "myParamName", "myTestMessage"); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
|
|
|
Assert.True(exception.Message.Contains("myTestMessage")); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Theory] |
|
|
[Theory] |
|
|
@ -280,7 +128,7 @@ namespace SixLabors.Helpers.Tests |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
Assert.Equal("myParamName", exception.ParamName); |
|
|
Assert.True(exception.Message.Contains($"The size must be at least 3.")); |
|
|
Assert.Contains("The size must be at least 3.", exception.Message); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|