Skip to content

Commit

Permalink
Compiler Warning Fixes (#335)
Browse files Browse the repository at this point in the history
* StringTooLong and tests

* Fix warnings
  • Loading branch information
ardalis authored Jan 23, 2024
1 parent f1411c4 commit d96c62c
Show file tree
Hide file tree
Showing 32 changed files with 262 additions and 212 deletions.
5 changes: 5 additions & 0 deletions src/GuardClauses/GuardAgainstStringLengthExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
using Ardalis.GuardClauses;

namespace GuardClauses;

/// <summary>
/// The class containing extension methods for <see cref="IGuardClause"/>
/// for <see cref="string"/> and <see cref="System.Text.StringBuilder"/> types.
/// </summary>
public static partial class GuardClauseExtensions
{
/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions test/GuardClauses.UnitTests/GuardAgainstDefault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void ReturnsExpectedValueWhenGivenNonDefaultValue(object input, string na
[Theory]
[InlineData(null, "Parameter [parameterName] is default value for type String (Parameter 'parameterName')")]
[InlineData("Please provide correct value", "Please provide correct value (Parameter 'parameterName')")]
public void ErrorMessageMatchesExpected(string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpected(string? customMessage, string expectedMessage)
{
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.Default(default(string), "parameterName", customMessage));
Assert.NotNull(exception);
Expand All @@ -49,7 +49,7 @@ public void ErrorMessageMatchesExpected(string customMessage, string expectedMes
[Theory]
[InlineData(null, "Parameter [xyz] is default value for type String (Parameter 'xyz')")]
[InlineData("Please provide correct value", "Please provide correct value (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvided(string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvided(string? customMessage, string expectedMessage)
{
var xyz = default(string);
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.Default(xyz, message: customMessage));
Expand All @@ -63,7 +63,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvided(string cust
[InlineData(null, "Please provide correct value")]
[InlineData("SomeParameter", null)]
[InlineData("SomeOtherParameter", "Value must be correct")]
public void ExceptionParamNameMatchesExpected(string expectedParamName, string customMessage)
public void ExceptionParamNameMatchesExpected(string? expectedParamName, string? customMessage)
{
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.Default(default(string), expectedParamName, customMessage));
Assert.NotNull(exception);
Expand Down
113 changes: 58 additions & 55 deletions test/GuardClauses.UnitTests/GuardAgainstExpressionDeprecated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace GuardClauses.UnitTests;

/// <summary>
/// Tests removed to eliminate compiler warnings about obsolete method.
/// </summary>
public class GuardAgainstExpressionDeprecated
{
public struct CustomStruct
Expand All @@ -22,66 +25,66 @@ public static IEnumerable<object[]> GetCustomStruct()
};
}

[Theory]
[InlineData(10)]
public void GivenIntegerWhenTheAgainstExpressionEvaluatesToTrueDoesNothing(int test)
{
Guard.Against.AgainstExpression((x) => x == 10, test, "Value is not equal to 10");
}
//[Theory]
//[InlineData(10)]
//public void GivenIntegerWhenTheAgainstExpressionEvaluatesToTrueDoesNothing(int test)
//{
// Guard.Against.AgainstExpression((x) => x == 10, test, "Value is not equal to 10");
//}

[Theory]
[InlineData(10)]
public void GivenIntegerWhenTheAgainstExpressionEvaluatesToFalseThrowsException(int test)
{
Assert.Throws<ArgumentException>(() => Guard.Against.AgainstExpression((x) => x == 5, test, "Value is not equal to 10"));
}
//[Theory]
//[InlineData(10)]
//public void GivenIntegerWhenTheAgainstExpressionEvaluatesToFalseThrowsException(int test)
//{
// Assert.Throws<ArgumentException>(() => Guard.Against.AgainstExpression((x) => x == 5, test, "Value is not equal to 10"));
//}

[Theory]
[InlineData(1.1)]
public void GivenDoubleWhenTheAgainstExpressionEvaluatesToTrueDoesNothing(double test)
{
Guard.Against.AgainstExpression((x) => x == 1.1, test, "Value is not equal to 1.1");
}
//[Theory]
//[InlineData(1.1)]
//public void GivenDoubleWhenTheAgainstExpressionEvaluatesToTrueDoesNothing(double test)
//{
// Guard.Against.AgainstExpression((x) => x == 1.1, test, "Value is not equal to 1.1");
//}

[Theory]
[InlineData(1.1)]
public void GivenDoubleWhenTheAgainstExpressionEvaluatesToFalseThrowsException(int test)
{
Assert.Throws<ArgumentException>(() => Guard.Against.AgainstExpression((x) => x == 5.0, test, "Value is not equal to 1.1"));
}
//[Theory]
//[InlineData(1.1)]
//public void GivenDoubleWhenTheAgainstExpressionEvaluatesToFalseThrowsException(int test)
//{
// Assert.Throws<ArgumentException>(() => Guard.Against.AgainstExpression((x) => x == 5.0, test, "Value is not equal to 1.1"));
//}

[Theory]
[MemberData(nameof(GetCustomStruct))]
public void GivenCustomStructWhenTheAgainstExpressionEvaluatesToTrueDoesNothing(CustomStruct test)
{
Guard.Against.AgainstExpression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching");
}
//[Theory]
//[MemberData(nameof(GetCustomStruct))]
//public void GivenCustomStructWhenTheAgainstExpressionEvaluatesToTrueDoesNothing(CustomStruct test)
//{
// Guard.Against.AgainstExpression((x) => x.FieldName == "FieldValue", test, "FieldValue is not matching");
//}

[Theory]
[MemberData(nameof(GetCustomStruct))]
public void GivenCustomStructWhenTheAgainstExpressionEvaluatesToFalseThrowsException(CustomStruct test)
{
Assert.Throws<ArgumentException>(() => Guard.Against.AgainstExpression((x) => x.FieldName == "FailThis", test, "FieldValue is not matching"));
}
//[Theory]
//[MemberData(nameof(GetCustomStruct))]
//public void GivenCustomStructWhenTheAgainstExpressionEvaluatesToFalseThrowsException(CustomStruct test)
//{
// Assert.Throws<ArgumentException>(() => Guard.Against.AgainstExpression((x) => x.FieldName == "FailThis", test, "FieldValue is not matching"));
//}

[Theory]
[InlineData(null, "Value does not fall within the expected range.")]
[InlineData("Please provide correct value", "Please provide correct value")]
public void ErrorMessageMatchesAgainstExpected(string customMessage, string expectedMessage)
{
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.AgainstExpression(x => x == 1, 2, customMessage));
Assert.NotNull(exception);
Assert.NotNull(exception.Message);
Assert.Equal(expectedMessage, exception.Message);
}
//[Theory]
//[InlineData(null, "Value does not fall within the expected range.")]
//[InlineData("Please provide correct value", "Please provide correct value")]
//public void ErrorMessageMatchesAgainstExpected(string? customMessage, string expectedMessage)
//{
// var exception = Assert.Throws<ArgumentException>(() => Guard.Against.AgainstExpression(x => x == 1, 2, customMessage));
// Assert.NotNull(exception);
// Assert.NotNull(exception.Message);
// Assert.Equal(expectedMessage, exception.Message);
//}

[Fact]
public void ErrorIncludesParamNameIfProvidedInAgainstExpression()
{
string paramName = "testParamName";
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.AgainstExpression(x => x == 1, 2, "custom message", paramName));
Assert.NotNull(exception);
Assert.NotNull(exception.Message);
Assert.Equal(paramName, exception.ParamName);
}
//[Fact]
//public void ErrorIncludesParamNameIfProvidedInAgainstExpression()
//{
// string paramName = "testParamName";
// var exception = Assert.Throws<ArgumentException>(() => Guard.Against.AgainstExpression(x => x == 1, 2, "custom message", paramName));
// Assert.NotNull(exception);
// Assert.NotNull(exception.Message);
// Assert.Equal(paramName, exception.ParamName);
//}
}
6 changes: 3 additions & 3 deletions test/GuardClauses.UnitTests/GuardAgainstInvalidFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void ThrowsGivenGivenIncorrectFormat(string input, string regexPattern)
[Theory]
[InlineData(null, "Input parameterName was not in required format (Parameter 'parameterName')")]
[InlineData("Please provide value in a correct format", "Please provide value in a correct format (Parameter 'parameterName')")]
public void ErrorMessageMatchesExpected(string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpected(string? customMessage, string? expectedMessage)
{
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.InvalidFormat("aaa", "parameterName", "^b", customMessage));
Assert.NotNull(exception);
Expand All @@ -46,9 +46,9 @@ public void ErrorMessageMatchesExpected(string customMessage, string expectedMes
[InlineData(null, "Please provide correct value")]
[InlineData("SomeParameter", null)]
[InlineData("SomeOtherParameter", "Value must be correct")]
public void ExceptionParamNameMatchesExpected(string expectedParamName, string customMessage)
public void ExceptionParamNameMatchesExpected(string? expectedParamName, string? customMessage)
{
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.InvalidFormat("aaa", expectedParamName, "^b", customMessage));
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.InvalidFormat("aaa", expectedParamName!, "^b", customMessage));
Assert.NotNull(exception);
Assert.Equal(expectedParamName, exception.ParamName);
}
Expand Down
16 changes: 8 additions & 8 deletions test/GuardClauses.UnitTests/GuardAgainstNegative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void ReturnsExpectedValueGivenNonNegativeTimeSpanValue()
[Theory]
[InlineData(null, "Required input xyz cannot be negative. (Parameter 'xyz')")]
[InlineData("Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenIntValue(string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenIntValue(string? customMessage, string? expectedMessage)
{
var xyz = -1;

Expand All @@ -124,7 +124,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenIntValu
[Theory]
[InlineData(null, "Required input xyz cannot be negative. (Parameter 'xyz')")]
[InlineData("Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenLongValue(string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenLongValue(string? customMessage, string? expectedMessage)
{
var xyz = -1L;

Expand All @@ -138,7 +138,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenLongVal
[Theory]
[InlineData(null, "Required input xyz cannot be negative. (Parameter 'xyz')")]
[InlineData("Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDecimalValue(string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDecimalValue(string? customMessage, string? expectedMessage)
{
var xyz = -1.0M;

Expand All @@ -152,7 +152,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDecimal
[Theory]
[InlineData(null, "Required input xyz cannot be negative. (Parameter 'xyz')")]
[InlineData("Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenFloatValue(string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenFloatValue(string? customMessage, string? expectedMessage)
{
var xyz = -1.0f;

Expand All @@ -166,7 +166,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenFloatVa
[Theory]
[InlineData(null, "Required input xyz cannot be negative. (Parameter 'xyz')")]
[InlineData("Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDoubleValue(string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDoubleValue(string? customMessage, string? expectedMessage)
{
var xyz = -1.0;

Expand All @@ -180,7 +180,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDoubleV
[Theory]
[InlineData(null, "Required input xyz cannot be negative. (Parameter 'xyz')")]
[InlineData("Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenTimeSpanValue(string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenTimeSpanValue(string? customMessage, string? expectedMessage)
{
var xyz = TimeSpan.FromSeconds(-1);

Expand All @@ -194,7 +194,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenTimeSpa
[Theory]
[InlineData(null, "Required input parameterName cannot be negative. (Parameter 'parameterName')")]
[InlineData("Must be positive", "Must be positive (Parameter 'parameterName')")]
public void ErrorMessageMatchesExpected(string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpected(string? customMessage, string? expectedMessage)
{
var clausesToEvaluate = new List<Action>
{
Expand All @@ -220,7 +220,7 @@ public void ErrorMessageMatchesExpected(string customMessage, string expectedMes
[InlineData(null, "Please provide correct value")]
[InlineData("SomeParameter", null)]
[InlineData("SomeOtherParameter", "Value must be correct")]
public void ExceptionParamNameMatchesExpected(string expectedParamName, string customMessage)
public void ExceptionParamNameMatchesExpected(string? expectedParamName, string? customMessage)
{
var clausesToEvaluate = new List<Action>
{
Expand Down
18 changes: 10 additions & 8 deletions test/GuardClauses.UnitTests/GuardAgainstNegativeOrZero.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void ReturnsExpectedValueWhenGivenPositiveValue()
[InlineData(-1, "Must be positive", "Must be positive (Parameter 'xyz')")]
[InlineData(0, null, "Required input xyz cannot be zero or negative. (Parameter 'xyz')")]
[InlineData(0, "Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenIntValue(int xyz, string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenIntValue(int xyz, string? customMessage, string? expectedMessage)
{
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.NegativeOrZero(xyz, message: customMessage));

Expand All @@ -127,7 +127,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenIntValu
[InlineData(-1L, "Must be positive", "Must be positive (Parameter 'xyz')")]
[InlineData(0L, null, "Required input xyz cannot be zero or negative. (Parameter 'xyz')")]
[InlineData(0L, "Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenLongValue(long xyz, string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenLongValue(long xyz, string? customMessage, string? expectedMessage)
{
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.NegativeOrZero(xyz, message: customMessage));

Expand All @@ -141,7 +141,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenLongVal
[InlineData(-1.0, "Must be positive", "Must be positive (Parameter 'xyz')")]
[InlineData(0.0, null, "Required input xyz cannot be zero or negative. (Parameter 'xyz')")]
[InlineData(0.0, "Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDecimalValue(decimal xyz, string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDecimalValue(decimal xyz, string? customMessage, string? expectedMessage)
{
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.NegativeOrZero(xyz, message: customMessage));

Expand All @@ -155,7 +155,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDecimal
[InlineData(-1.0, "Must be positive", "Must be positive (Parameter 'xyz')")]
[InlineData(0.0, null, "Required input xyz cannot be zero or negative. (Parameter 'xyz')")]
[InlineData(0.0, "Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenFloatValue(float xyz, string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenFloatValue(float xyz, string? customMessage, string? expectedMessage)
{
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.NegativeOrZero(xyz, message: customMessage));

Expand All @@ -169,7 +169,8 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenFloatVa
[InlineData(-1.0, "Must be positive", "Must be positive (Parameter 'xyz')")]
[InlineData(0.0, null, "Required input xyz cannot be zero or negative. (Parameter 'xyz')")]
[InlineData(0.0, "Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDoubleValue(double xyz, string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDoubleValue(double xyz,
string? customMessage, string? expectedMessage)
{
var exception = Assert.Throws<ArgumentException>(() => Guard.Against.NegativeOrZero(xyz, message: customMessage));

Expand All @@ -183,7 +184,8 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenDoubleV
[InlineData(-1.0, "Must be positive", "Must be positive (Parameter 'xyz')")]
[InlineData(0.0, null, "Required input xyz cannot be zero or negative. (Parameter 'xyz')")]
[InlineData(0.0, "Must be positive", "Must be positive (Parameter 'xyz')")]
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenTimeSpanValue(double change, string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenTimeSpanValue(double change,
string? customMessage, string? expectedMessage)
{
var xyz = TimeSpan.FromSeconds(change);

Expand All @@ -199,7 +201,7 @@ public void ErrorMessageMatchesExpectedWhenNameNotExplicitlyProvidedGivenTimeSpa
[InlineData(-1, "Must be positive", "Must be positive (Parameter 'parameterName')")]
[InlineData(0, null, "Required input parameterName cannot be zero or negative. (Parameter 'parameterName')")]
[InlineData(0, "Must be positive", "Must be positive (Parameter 'parameterName')")]
public void ErrorMessageMatchesExpected(int input, string customMessage, string expectedMessage)
public void ErrorMessageMatchesExpected(int input, string? customMessage, string? expectedMessage)
{
var clausesToEvaluate = new List<Action>
{
Expand Down Expand Up @@ -229,7 +231,7 @@ public void ErrorMessageMatchesExpected(int input, string customMessage, string
[InlineData(0, null, "Please provide correct value")]
[InlineData(0, "SomeParameter", null)]
[InlineData(0, "SomeOtherParameter", "Value must be correct")]
public void ExceptionParamNameMatchesExpected(int input, string expectedParamName, string customMessage)
public void ExceptionParamNameMatchesExpected(int input, string? expectedParamName, string? customMessage)
{
var clausesToEvaluate = new List<Action>
{
Expand Down
Loading

0 comments on commit d96c62c

Please sign in to comment.