Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,42 @@
OfType(type, typeof(TExpected), paramName);
}

/// <summary>
/// Ensures that the argument is of the expected type.
/// </summary>
/// <param name="value">The argument.</param>
/// <param name="expectedType">The expected <see cref="Type"/>.</param>
/// <param name="paramName">The parameter name.</param>
/// <exception cref="ArgumentException">The type argument is not of the expected type.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[StackTraceHidden]
public static void OfType(
object? value,
Type expectedType,
[CallerArgumentExpression("value")] string? paramName = null)
{
if (value != null)
{
OfType(value.GetType(), expectedType, paramName);
}
}

/// <summary>
/// Ensures that the argument is of the expected type.
/// </summary>
/// <typeparam name="TExpected">The expected <see cref="Type"/>.</typeparam>
/// <param name="value">The argument.</param>
/// <param name="paramName">The parameter name.</param>
/// <exception cref="ArgumentException">The type argument is not of the expected type.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[StackTraceHidden]
public static void OfType<TExpected>(
object? value,
[CallerArgumentExpression("value")] string? paramName = null)
{
OfType(value, typeof(TExpected), paramName);
}

/// <summary>
/// Ensures that the type argument is of the expected type. Supports testing for
/// open generic types.
Expand Down Expand Up @@ -303,6 +339,46 @@
OfGenericType(type, typeof(TExpected), paramName);
}

/// <summary>
/// Ensures that the argument is of the expected type. Supports testing for
/// open generic types.
/// </summary>
/// <param name="value">The argument.</param>
/// <param name="expectedType">The expected <see cref="Type"/>.</param>
/// <param name="paramName">The parameter name.</param>
/// <exception cref="ArgumentException">The type argument is not of the expected type.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[StackTraceHidden]
public static void OfGenericType(

Check warning on line 352 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (windows-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 352 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (windows-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 352 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (windows-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 352 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (ubuntu-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 352 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (ubuntu-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 352 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (ubuntu-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 352 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (macOS-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 352 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (macOS-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 352 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (macOS-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
object? value,
Type expectedType,
[CallerArgumentExpression("value")] string? paramName = null)
{
if (value != null)
{
OfGenericType(value.GetType(), expectedType, paramName);

Check warning on line 360 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (windows-latest)

'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Type, Type, String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 360 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (windows-latest)

'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Type, Type, String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 360 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (windows-latest)

'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Type, Type, String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 360 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (ubuntu-latest)

'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Type, Type, String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 360 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (ubuntu-latest)

'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Type, Type, String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 360 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (ubuntu-latest)

'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Type, Type, String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 360 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (macOS-latest)

'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Type, Type, String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 360 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (macOS-latest)

'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Type, Type, String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 360 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (macOS-latest)

'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Type, Type, String)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
}
}

/// <summary>
/// Ensures that the argument is of the expected type. Supports testing for
/// open generic types.
/// </summary>
/// <typeparam name="TExpected">The expected <see cref="Type"/>.</typeparam>
/// <param name="value">The argument.</param>
/// <param name="paramName">The parameter name.</param>
/// <exception cref="ArgumentException">The type argument is not of the expected type.</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[StackTraceHidden]
public static void OfGenericType<TExpected>(

Check warning on line 374 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (windows-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType<TExpected>(Object, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 374 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (windows-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType<TExpected>(Object, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 374 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (windows-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType<TExpected>(Object, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 374 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (ubuntu-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType<TExpected>(Object, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 374 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (ubuntu-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType<TExpected>(Object, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 374 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (ubuntu-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType<TExpected>(Object, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 374 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (macOS-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType<TExpected>(Object, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 374 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (macOS-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType<TExpected>(Object, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.

Check warning on line 374 in src/AppCoreNet.Diagnostics.Sources/Ensure.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (macOS-latest)

Parameter 'value' of method 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType<TExpected>(Object, String)' has 'DynamicallyAccessedMembersAttribute', but that attribute can only be applied to parameters of type 'System.Type' or 'System.String'.
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
object? value,
[CallerArgumentExpression("value")] string? paramName = null)
{
OfGenericType(value, typeof(TExpected), paramName);
}

[StackTraceHidden]
private static bool IsAssignableTo(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
Expand Down
55 changes: 55 additions & 0 deletions test/AppCoreNet.Diagnostics.Tests/EnsureTests.Arg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,39 @@
.Be("param");
}

[Theory]
[InlineData(typeof(IEnumerable))]
[InlineData(typeof(IEnumerable<string>))]
[InlineData(typeof(ExpectedArgBaseType))]
[InlineData(typeof(ExpectedArgType))]
public void OfTypeDoesNotThrowForRelatedValue(Type expectedType)
{
object value = new ExpectedArgType();
Ensure.Arg.OfType(value, expectedType, "param");
}

[Fact]
public void OfTypeThrowsForUnrelatedValue()
{
var exception = Assert.Throws<ArgumentException>(
() => Ensure.Arg.OfType(string.Empty, typeof(ExpectedArgType), "param"));

exception.ParamName.Should()
.Be("param");
}

[Fact]
public void OfTypeDoesNotThrowForNullType()
{
Ensure.Arg.OfType(null, typeof(string));
}

[Fact]
public void OfTypeDoesNotThrowForNullValue()
{
Ensure.Arg.OfType((object?)null, typeof(string));
}

[Theory]
[InlineData(typeof(IEnumerable))]
[InlineData(typeof(IEnumerable<string>))]
Expand All @@ -199,12 +226,30 @@
Ensure.Arg.OfGenericType(type, expectedType, "param");
}

[Theory]
[InlineData(typeof(IEnumerable))]
[InlineData(typeof(IEnumerable<string>))]
[InlineData(typeof(IEnumerable<>))]
[InlineData(typeof(ExpectedArgBaseType))]
[InlineData(typeof(ExpectedArgType))]
public void OfGenericTypeDoesNotThrowForRelatedValue(Type expectedType)
{
object value = new ExpectedArgType();
Ensure.Arg.OfGenericType(value, expectedType, "param");

Check warning on line 238 in test/AppCoreNet.Diagnostics.Tests/EnsureTests.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (windows-latest)

'value' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)'. The return value of method 'AppCoreNet.Diagnostics.EnsureTests.Arg.ExpectedArgType.ExpectedArgType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 238 in test/AppCoreNet.Diagnostics.Tests/EnsureTests.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (ubuntu-latest)

'value' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)'. The return value of method 'AppCoreNet.Diagnostics.EnsureTests.Arg.ExpectedArgType.ExpectedArgType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Check warning on line 238 in test/AppCoreNet.Diagnostics.Tests/EnsureTests.Arg.cs

View workflow job for this annotation

GitHub Actions / .NET Build (macOS-latest)

'value' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'AppCoreNet.Diagnostics.Ensure.Arg.OfGenericType(Object, Type, String)'. The return value of method 'AppCoreNet.Diagnostics.EnsureTests.Arg.ExpectedArgType.ExpectedArgType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
}

[Fact]
public void OfGenericTypeDoesNotThrowForNullType()
{
Ensure.Arg.OfGenericType(null, typeof(string));
}

[Fact]
public void OfGenericTypeDoesNotThrowForNullValue()
{
Ensure.Arg.OfGenericType((object?)null, typeof(string));
}

[Fact]
public void OfGenericTypeThrowsForUnrelatedType()
{
Expand All @@ -214,5 +259,15 @@
exception.ParamName.Should()
.Be("param");
}

[Fact]
public void OfGenericTypeThrowsForUnrelatedValue()
{
var exception = Assert.Throws<ArgumentException>(
() => Ensure.Arg.OfGenericType(string.Empty, typeof(ExpectedArgType), "param"));

exception.ParamName.Should()
.Be("param");
}
}
}