Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable nullability for public api #856

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions BreakingChanges.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Update target frameworks: .NET 8, .NET Standard 2.0.
* Obsolete api is removed
* CompatArg is marked as obsolete
* Nullability is enabled for public api for .NET Core TFMs

5.0.0 Release
================
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [UPDATE] Mark as obsolete api CompatArg with pre c# 7.0 support
* [NEW] Added .NET 9 to test matrix
* [UPDATE] Migrate documentation to docfx platform. https://github.com/dotnet/docfx
* [UPDATE] Nullability is enabled for public api for .NET Core TFMs

### 5.3.0 (October 2024)

Expand Down
5 changes: 0 additions & 5 deletions Gemfile

This file was deleted.

81 changes: 0 additions & 81 deletions Gemfile.lock

This file was deleted.

113 changes: 6 additions & 107 deletions src/NSubstitute/Arg.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using System.Linq.Expressions;
using NSubstitute.Core.Arguments;

// Disable nullability for client API, so it does not affect clients.
#nullable disable annotations
#pragma warning disable CS0419
using System.Linq.Expressions;

namespace NSubstitute;

/// <summary>
/// Argument matchers used for specifying calls to substitutes.
/// </summary>
public static class Arg
public static partial class Arg
{
/// <summary>
/// This type can be used with any matcher to match a generic type parameter.
Expand Down Expand Up @@ -43,7 +39,7 @@ public static ref T Is<T>(T value)
/// Match argument that satisfies <paramref name="predicate"/>.
/// If the <paramref name="predicate"/> throws an exception for an argument it will be treated as non-matching.
/// </summary>
public static ref T Is<T>(Expression<Predicate<T>> predicate)
public static ref T Is<T>(Expression<Predicate<T?>> predicate)
{
return ref ArgumentMatcher.Enqueue<T>(new ExpressionArgumentMatcher<T>(predicate));
}
Expand All @@ -52,7 +48,7 @@ public static ref T Is<T>(Expression<Predicate<T>> predicate)
/// Match argument that satisfies <paramref name="predicate"/>.
/// If the <paramref name="predicate"/> throws an exception for an argument it will be treated as non-matching.
/// </summary>
public static ref T Is<T>(Expression<Predicate<object>> predicate) where T : AnyType
public static ref T Is<T>(Expression<Predicate<object?>> predicate) where T : AnyType
{
return ref ArgumentMatcher.Enqueue<T>(new ExpressionArgumentMatcher<object>(predicate));
}
Expand Down Expand Up @@ -124,105 +120,8 @@ public static ref T Do<T>(Action<object> useArgument) where T : AnyType
return ref ArgumentMatcher.Enqueue<T>(new AnyArgumentMatcher(typeof(AnyType)), x => useArgument(x!));
}

/// <summary>
/// Alternate version of <see cref="Arg"/> matchers for compatibility with pre-C#7 compilers
/// which do not support <c>ref</c> return types. Do not use unless you are unable to use <see cref="Arg"/>.
///
/// For more information see <see href="https://nsubstitute.github.io/help/compat-args">Compatibility Argument
/// Matchers</see> in the NSubstitute documentation.
/// </summary>
public static class Compat
{
/// <summary>
/// Match any argument value compatible with type <typeparamref name="T"/>.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Any{T}" /> instead.
/// </summary>
public static T Any<T>() => Arg.Any<T>();

/// <summary>
/// Match argument that is equal to <paramref name="value"/>.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Is{T}(T)" /> instead.
/// </summary>
public static T Is<T>(T value) => Arg.Is(value);

/// <summary>
/// Match argument that satisfies <paramref name="predicate"/>.
/// If the <paramref name="predicate"/> throws an exception for an argument it will be treated as non-matching.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Is{T}(Expression{Predicate{T}})" /> instead.
/// </summary>
public static T Is<T>(Expression<Predicate<T>> predicate) => Arg.Is(predicate);

/// <summary>
/// Match argument that satisfies <paramref name="predicate"/>.
/// If the <paramref name="predicate"/> throws an exception for an argument it will be treated as non-matching.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Is{T}(Expression{Predicate{T}})" /> instead.
/// </summary>
public static AnyType Is<T>(Expression<Predicate<object>> predicate) where T : AnyType => Arg.Is<T>(predicate);

/// <summary>
/// Invoke any <see cref="Action"/> argument whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Invoke" /> instead.
/// </summary>
public static Action Invoke() => Arg.Invoke();

/// <summary>
/// Invoke any <see cref="Action&lt;T&gt;"/> argument with specified argument whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Invoke{T}" /> instead.
/// </summary>
public static Action<T> Invoke<T>(T arg) => Arg.Invoke(arg);

/// <summary>
/// Invoke any <see cref="Action&lt;T1,T2&gt;"/> argument with specified arguments whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Invoke{T1,T2}" /> instead.
/// </summary>
public static Action<T1, T2> Invoke<T1, T2>(T1 arg1, T2 arg2) => Arg.Invoke(arg1, arg2);

/// <summary>
/// Invoke any <see cref="Action&lt;T1,T2,T3&gt;"/> argument with specified arguments whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Invoke{T1,T2,T3}" /> instead.
/// </summary>
public static Action<T1, T2, T3> Invoke<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3) => Arg.Invoke(arg1, arg2, arg3);

/// <summary>
/// Invoke any <see cref="Action&lt;T1,T2,T3,T4&gt;"/> argument with specified arguments whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Invoke{T1,T2,T3,T4}" /> instead.
/// </summary>
public static Action<T1, T2, T3, T4> Invoke<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4) => Arg.Invoke(arg1, arg2, arg3, arg4);

/// <summary>
/// Invoke any <typeparamref name="TDelegate"/> argument with specified arguments whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.InvokeDelegate{TDelegate}" /> instead.
/// </summary>
/// <param name="arguments">Arguments to pass to delegate.</param>
public static TDelegate InvokeDelegate<TDelegate>(params object[] arguments) => Arg.InvokeDelegate<TDelegate>(arguments);

/// <summary>
/// Capture any argument compatible with type <typeparamref name="T"/> and use it to call the <paramref name="useArgument"/> function
/// whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Do{T}" /> instead.
/// </summary>
public static T Do<T>(Action<T> useArgument) => Arg.Do<T>(useArgument);

/// <summary>
/// Capture any argument compatible with type <typeparamref name="T"/> and use it to call the <paramref name="useArgument"/> function
/// whenever a matching call is made to the substitute.
/// </summary>
public static AnyType Do<T>(Action<object> useArgument) where T : AnyType => Arg.Do<T>(useArgument);
}

private static Action<object> InvokeDelegateAction(params object[] arguments)
private static Action<object?> InvokeDelegateAction(params object?[] arguments)
{
return x => ((Delegate)x).DynamicInvoke(arguments);
return x => ((Delegate)x!).DynamicInvoke(arguments);
}
}
3 changes: 0 additions & 3 deletions src/NSubstitute/Callback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
using NSubstitute.Core;
using System.Collections.Concurrent;

// Disable nullability for client API, so it does not affect clients.
#nullable disable annotations

namespace NSubstitute;

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions src/NSubstitute/Callbacks/ConfiguredCallback.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using NSubstitute.Core;

// Disable nullability for client API, so it does not affect clients.
#nullable disable annotations

namespace NSubstitute.Callbacks;

public class ConfiguredCallback : EndCallbackChain
Expand Down
108 changes: 108 additions & 0 deletions src/NSubstitute/Compatibility/Arg.Compat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System.Linq.Expressions;

// Disable nullability for client API, so it does not affect clients.
#nullable disable annotations
#pragma warning disable CS0419

namespace NSubstitute;

public static partial class Arg
{
/// <summary>
/// Alternate version of <see cref="Arg"/> matchers for compatibility with pre-C#7 compilers
/// which do not support <c>ref</c> return types. Do not use unless you are unable to use <see cref="Arg"/>.
///
/// For more information see <see href="https://nsubstitute.github.io/help/compat-args">Compatibility Argument
/// Matchers</see> in the NSubstitute documentation.
/// </summary>
[Obsolete("This api is deprecated and will be removed in future versions of product.")]
public static class Compat
{
/// <summary>
/// Match any argument value compatible with type <typeparamref name="T"/>.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Any{T}" /> instead.
/// </summary>
public static T Any<T>() => Arg.Any<T>();

/// <summary>
/// Match argument that is equal to <paramref name="value"/>.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Is{T}(T)" /> instead.
/// </summary>
public static T Is<T>(T value) => Arg.Is(value);

/// <summary>
/// Match argument that satisfies <paramref name="predicate"/>.
/// If the <paramref name="predicate"/> throws an exception for an argument it will be treated as non-matching.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Is{T}(Expression{Predicate{T}})" /> instead.
/// </summary>
public static T Is<T>(Expression<Predicate<T>> predicate) => Arg.Is(predicate);

/// <summary>
/// Match argument that satisfies <paramref name="predicate"/>.
/// If the <paramref name="predicate"/> throws an exception for an argument it will be treated as non-matching.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Is{T}(Expression{Predicate{T}})" /> instead.
/// </summary>
public static AnyType Is<T>(Expression<Predicate<object>> predicate) where T : AnyType => Arg.Is<T>(predicate);

/// <summary>
/// Invoke any <see cref="Action"/> argument whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Invoke" /> instead.
/// </summary>
public static Action Invoke() => Arg.Invoke();

/// <summary>
/// Invoke any <see cref="Action&lt;T&gt;"/> argument with specified argument whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Invoke{T}" /> instead.
/// </summary>
public static Action<T> Invoke<T>(T arg) => Arg.Invoke(arg);

/// <summary>
/// Invoke any <see cref="Action&lt;T1,T2&gt;"/> argument with specified arguments whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Invoke{T1,T2}" /> instead.
/// </summary>
public static Action<T1, T2> Invoke<T1, T2>(T1 arg1, T2 arg2) => Arg.Invoke(arg1, arg2);

/// <summary>
/// Invoke any <see cref="Action&lt;T1,T2,T3&gt;"/> argument with specified arguments whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Invoke{T1,T2,T3}" /> instead.
/// </summary>
public static Action<T1, T2, T3> Invoke<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3) => Arg.Invoke(arg1, arg2, arg3);

/// <summary>
/// Invoke any <see cref="Action&lt;T1,T2,T3,T4&gt;"/> argument with specified arguments whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Invoke{T1,T2,T3,T4}" /> instead.
/// </summary>
public static Action<T1, T2, T3, T4> Invoke<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4) => Arg.Invoke(arg1, arg2, arg3, arg4);

/// <summary>
/// Invoke any <typeparamref name="TDelegate"/> argument with specified arguments whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.InvokeDelegate{TDelegate}" /> instead.
/// </summary>
/// <param name="arguments">Arguments to pass to delegate.</param>
public static TDelegate InvokeDelegate<TDelegate>(params object[] arguments) => Arg.InvokeDelegate<TDelegate>(arguments);

/// <summary>
/// Capture any argument compatible with type <typeparamref name="T"/> and use it to call the <paramref name="useArgument"/> function
/// whenever a matching call is made to the substitute.
/// This is provided for compatibility with older compilers --
/// if possible use <see cref="Arg.Do{T}" /> instead.
/// </summary>
public static T Do<T>(Action<T> useArgument) => Arg.Do<T>(useArgument);

/// <summary>
/// Capture any argument compatible with type <typeparamref name="T"/> and use it to call the <paramref name="useArgument"/> function
/// whenever a matching call is made to the substitute.
/// </summary>
public static AnyType Do<T>(Action<object> useArgument) where T : AnyType => Arg.Do<T>(useArgument);
}
}
Loading
Loading