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

Merged
merged 9 commits into from
Mar 9, 2025
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
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
csharp_style_namespace_declarations = file_scoped:warning
dotnet_style_prefer_collection_expression = true:warning
dotnet_style_prefer_collection_expression = true:suggestion
dotnet_style_collection_initializer = true:warning
csharp_style_prefer_primary_constructors = true:warning

Expand Down
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 8+. Workaround: you can disable nullability `<Nullable>disable</Nullable>` on project if you don't want to use it. more info https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references

5.0.0 Release
================
Expand Down
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
### 6.x (work in progress)

* [UPDATE] Update target frameworks: .NET8, .NET Standard 2.0
* [UPDATE][BREAKING] Update target frameworks: .NET8, .NET Standard 2.0
* [UPDATE] Drop EOL .NET 6/7 platforms from testing matrix
* [UPDATE] Update github actions steps versions
* [UPDATE] Remove legacy obsolete API
* [UPDATE] Mark as obsolete api CompatArg with pre c# 7.0 support
* [UPDATE][BREAKING] Remove legacy obsolete API
* [UPDATE][BREAKING] 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][BREAKING] 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)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that it's a bit too late to jump into the train, as it was merged, but I find this change a bit questionable and am concerned about usability of it. For example here now T is nullable, but that's not the case at all. It depends solely on the method we invoke whether the value is null or not. I understand that we don't have a chance to infer that somehow - but yet now people will be forced to use null-forgiving operator. That isn't convenient at all. It's like .ToString()! you have to write all the time - it's annoying and it frankly sucks 😟

The primary purpose of this library is to enable unit testing. I would find it quite surprising that people would have nullability enabled in the unit tests, as.. well, it's unit tests, not the production code and you want to have flexibility there. I could totally see that some are enabling, but yet I wouldn't imagine it being the default.

My primary argument is that we cannot promise nullability or describe anything meaningful - shall we just not enable it for classes like Arg? Like if you don't know the answer to the question - you just say "I don't know" instead of pretending you know something. After this change the compilation could break for the existing test projects (if they have the nullability enabled) and people will have to go and manually suppress it everywhere - isn't it annoying?

@Romfos @dtchepak What are you thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zvirja Thanks for the thoughts. I haven't used C# since nullability changes (mainly Kotlin at the moment), so not across this. This hasn't made it to release so it's fine to revert.

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