Skip to content

Commit ee276f3

Browse files
author
Mihnea Rădulescu
committed
Merge branch 'main' into bug/issue-788-Raise-EventWith-default-constructor
2 parents a524189 + b7ff02c commit ee276f3

File tree

107 files changed

+460
-1206
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+460
-1206
lines changed

Diff for: src/NSubstitute/Compatibility/DiagnosticsNullabilityAttributes.cs

+25-33
Original file line numberDiff line numberDiff line change
@@ -22,64 +22,56 @@ internal sealed class MaybeNullAttribute : Attribute { }
2222
internal sealed class NotNullAttribute : Attribute { }
2323

2424
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.</summary>
25+
/// <remarks>Initializes the attribute with the specified return value condition.</remarks>
26+
/// <param name="returnValue">
27+
/// The return value condition. If the method returns this value, the associated parameter may be null.
28+
/// </param>
2529
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
26-
internal sealed class MaybeNullWhenAttribute : Attribute
30+
internal sealed class MaybeNullWhenAttribute(bool returnValue) : Attribute
2731
{
28-
/// <summary>Initializes the attribute with the specified return value condition.</summary>
29-
/// <param name="returnValue">
30-
/// The return value condition. If the method returns this value, the associated parameter may be null.
31-
/// </param>
32-
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
33-
3432
/// <summary>Gets the return value condition.</summary>
35-
public bool ReturnValue { get; }
33+
public bool ReturnValue { get; } = returnValue;
3634
}
3735

3836
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
37+
/// <remarks>Initializes the attribute with the specified return value condition.</remarks>
38+
/// <param name="returnValue">
39+
/// The return value condition. If the method returns this value, the associated parameter will not be null.
40+
/// </param>
3941
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
40-
internal sealed class NotNullWhenAttribute : Attribute
42+
internal sealed class NotNullWhenAttribute(bool returnValue) : Attribute
4143
{
42-
/// <summary>Initializes the attribute with the specified return value condition.</summary>
43-
/// <param name="returnValue">
44-
/// The return value condition. If the method returns this value, the associated parameter will not be null.
45-
/// </param>
46-
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
47-
4844
/// <summary>Gets the return value condition.</summary>
49-
public bool ReturnValue { get; }
45+
public bool ReturnValue { get; } = returnValue;
5046
}
5147

5248
/// <summary>Specifies that the output will be non-null if the named parameter is non-null.</summary>
49+
/// <remarks>Initializes the attribute with the associated parameter name.</remarks>
50+
/// <param name="parameterName">
51+
/// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
52+
/// </param>
5353
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
54-
internal sealed class NotNullIfNotNullAttribute : Attribute
54+
internal sealed class NotNullIfNotNullAttribute(string parameterName) : Attribute
5555
{
56-
/// <summary>Initializes the attribute with the associated parameter name.</summary>
57-
/// <param name="parameterName">
58-
/// The associated parameter name. The output will be non-null if the argument to the parameter specified is non-null.
59-
/// </param>
60-
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
61-
6256
/// <summary>Gets the associated parameter name.</summary>
63-
public string ParameterName { get; }
57+
public string ParameterName { get; } = parameterName;
6458
}
6559

6660
/// <summary>Applied to a method that will never return under any circumstance.</summary>
6761
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
6862
internal sealed class DoesNotReturnAttribute : Attribute { }
6963

7064
/// <summary>Specifies that the method will not return if the associated Boolean parameter is passed the specified value.</summary>
65+
/// <remarks>Initializes the attribute with the specified parameter value.</remarks>
66+
/// <param name="parameterValue">
67+
/// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
68+
/// the associated parameter matches this value.
69+
/// </param>
7170
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
72-
internal sealed class DoesNotReturnIfAttribute : Attribute
71+
internal sealed class DoesNotReturnIfAttribute(bool parameterValue) : Attribute
7372
{
74-
/// <summary>Initializes the attribute with the specified parameter value.</summary>
75-
/// <param name="parameterValue">
76-
/// The condition parameter value. Code after the method will be considered unreachable by diagnostics if the argument to
77-
/// the associated parameter matches this value.
78-
/// </param>
79-
public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
80-
8173
/// <summary>Gets the condition parameter value.</summary>
82-
public bool ParameterValue { get; }
74+
public bool ParameterValue { get; } = parameterValue;
8375
}
8476

8577
/// <summary>Specifies that the method or property will ensure that the listed field and property members have not-null values.</summary>

Diff for: src/NSubstitute/Core/ArgumentSpecificationDequeue.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,10 @@
33

44
namespace NSubstitute.Core;
55

6-
public class ArgumentSpecificationDequeue : IArgumentSpecificationDequeue
6+
public class ArgumentSpecificationDequeue(Func<IList<IArgumentSpecification>> dequeueAllQueuedArgSpecs) : IArgumentSpecificationDequeue
77
{
88
private static readonly IArgumentSpecification[] EmptySpecifications = [];
99

10-
private readonly Func<IList<IArgumentSpecification>> _dequeueAllQueuedArgSpecs;
11-
12-
public ArgumentSpecificationDequeue(Func<IList<IArgumentSpecification>> dequeueAllQueuedArgSpecs)
13-
{
14-
_dequeueAllQueuedArgSpecs = dequeueAllQueuedArgSpecs;
15-
}
16-
1710
public IList<IArgumentSpecification> DequeueAllArgumentSpecificationsForMethod(int parametersCount)
1811
{
1912
if (parametersCount == 0)
@@ -24,7 +17,7 @@ public IList<IArgumentSpecification> DequeueAllArgumentSpecificationsForMethod(i
2417
return EmptySpecifications;
2518
}
2619

27-
var queuedArgSpecifications = _dequeueAllQueuedArgSpecs.Invoke();
20+
var queuedArgSpecifications = dequeueAllQueuedArgSpecs.Invoke();
2821
return queuedArgSpecifications;
2922
}
3023

Diff for: src/NSubstitute/Core/Arguments/AnyArgumentMatcher.cs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
namespace NSubstitute.Core.Arguments;
22

3-
public class AnyArgumentMatcher : IArgumentMatcher
3+
public class AnyArgumentMatcher(Type typeArgMustBeCompatibleWith) : IArgumentMatcher
44
{
5-
private readonly Type _typeArgMustBeCompatibleWith;
5+
public override string ToString() => "any " + typeArgMustBeCompatibleWith.GetNonMangledTypeName();
66

7-
public AnyArgumentMatcher(Type typeArgMustBeCompatibleWith)
8-
{
9-
_typeArgMustBeCompatibleWith = typeArgMustBeCompatibleWith;
10-
}
11-
12-
public override string ToString() => "any " + _typeArgMustBeCompatibleWith.GetNonMangledTypeName();
13-
14-
public bool IsSatisfiedBy(object? argument) => argument.IsCompatibleWith(_typeArgMustBeCompatibleWith);
7+
public bool IsSatisfiedBy(object? argument) => argument.IsCompatibleWith(typeArgMustBeCompatibleWith);
158
}

Diff for: src/NSubstitute/Core/Arguments/ArgumentMatchInfo.cs

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
namespace NSubstitute.Core.Arguments;
22

3-
public class ArgumentMatchInfo
3+
public class ArgumentMatchInfo(int index, object? argument, IArgumentSpecification specification)
44
{
5-
public ArgumentMatchInfo(int index, object? argument, IArgumentSpecification specification)
6-
{
7-
Index = index;
8-
_argument = argument;
9-
_specification = specification;
10-
}
11-
12-
private readonly object? _argument;
13-
private readonly IArgumentSpecification _specification;
14-
public int Index { get; }
5+
private readonly object? _argument = argument;
6+
private readonly IArgumentSpecification _specification = specification;
7+
public int Index { get; } = index;
158

169
public bool IsMatch => _specification.IsSatisfiedBy(_argument);
1710

Diff for: src/NSubstitute/Core/Arguments/ArgumentMatcher.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@ public static class ArgumentMatcher
3333
return ref new DefaultValueContainer<T>().Value;
3434
}
3535

36-
private class GenericToNonGenericMatcherProxy<T> : IArgumentMatcher
36+
private class GenericToNonGenericMatcherProxy<T>(IArgumentMatcher<T> matcher) : IArgumentMatcher
3737
{
38-
protected readonly IArgumentMatcher<T> _matcher;
39-
40-
public GenericToNonGenericMatcherProxy(IArgumentMatcher<T> matcher)
41-
{
42-
_matcher = matcher;
43-
}
38+
protected readonly IArgumentMatcher<T> _matcher = matcher;
4439

4540
public bool IsSatisfiedBy(object? argument) => _matcher.IsSatisfiedBy((T?)argument!);
4641
}

Diff for: src/NSubstitute/Core/Arguments/ArgumentSpecification.cs

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
namespace NSubstitute.Core.Arguments;
22

3-
public class ArgumentSpecification : IArgumentSpecification
3+
public class ArgumentSpecification(Type forType, IArgumentMatcher matcher, Action<object?> action) : IArgumentSpecification
44
{
55
private static readonly Action<object?> NoOpAction = _ => { };
6-
7-
private readonly IArgumentMatcher _matcher;
8-
private readonly Action<object?> _action;
9-
public Type ForType { get; }
10-
public bool HasAction => _action != NoOpAction;
6+
public Type ForType { get; } = forType;
7+
public bool HasAction => action != NoOpAction;
118

129
public ArgumentSpecification(Type forType, IArgumentMatcher matcher) : this(forType, matcher, NoOpAction) { }
1310

14-
public ArgumentSpecification(Type forType, IArgumentMatcher matcher, Action<object?> action)
15-
{
16-
ForType = forType;
17-
_matcher = matcher;
18-
_action = action;
19-
}
20-
2111
public bool IsSatisfiedBy(object? argument)
2212
{
2313
if (!IsCompatibleWith(argument))
@@ -27,7 +17,7 @@ public bool IsSatisfiedBy(object? argument)
2717

2818
try
2919
{
30-
return _matcher.IsSatisfiedBy(argument);
20+
return matcher.IsSatisfiedBy(argument);
3121
}
3222
catch
3323
{
@@ -42,7 +32,7 @@ public string DescribeNonMatch(object? argument)
4232
return GetIncompatibleTypeMessage(argument);
4333
}
4434

45-
return _matcher is IDescribeNonMatches describe
35+
return matcher is IDescribeNonMatches describe
4636
? describe.DescribeFor(argument)
4737
: string.Empty;
4838
}
@@ -51,12 +41,12 @@ public string FormatArgument(object? argument)
5141
{
5242
var isSatisfiedByArg = IsSatisfiedBy(argument);
5343

54-
return _matcher is IArgumentFormatter matcherFormatter
44+
return matcher is IArgumentFormatter matcherFormatter
5545
? matcherFormatter.Format(argument, highlight: !isSatisfiedByArg)
5646
: ArgumentFormatter.Default.Format(argument, highlight: !isSatisfiedByArg);
5747
}
5848

59-
public override string ToString() => _matcher.ToString() ?? string.Empty;
49+
public override string ToString() => matcher.ToString() ?? string.Empty;
6050

6151
public IArgumentSpecification CreateCopyMatchingAnyArgOfType(Type requiredType)
6252
{
@@ -65,19 +55,19 @@ public IArgumentSpecification CreateCopyMatchingAnyArgOfType(Type requiredType)
6555
return new ArgumentSpecification(
6656
requiredType,
6757
new AnyArgumentMatcher(requiredType),
68-
_action == NoOpAction ? NoOpAction : RunActionIfTypeIsCompatible);
58+
action == NoOpAction ? NoOpAction : RunActionIfTypeIsCompatible);
6959
}
7060

7161
public void RunAction(object? argument)
7262
{
73-
_action(argument);
63+
action(argument);
7464
}
7565

7666
private void RunActionIfTypeIsCompatible(object? argument)
7767
{
7868
if (argument.IsCompatibleWith(ForType))
7969
{
80-
_action(argument);
70+
action(argument);
8171
}
8272
}
8373

Diff for: src/NSubstitute/Core/Arguments/ArgumentSpecificationCompatibilityTester.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
namespace NSubstitute.Core.Arguments;
22

3-
public class ArgumentSpecificationCompatibilityTester : IArgumentSpecificationCompatibilityTester
3+
public class ArgumentSpecificationCompatibilityTester(IDefaultChecker defaultChecker) : IArgumentSpecificationCompatibilityTester
44
{
5-
private readonly IDefaultChecker _defaultChecker;
6-
7-
public ArgumentSpecificationCompatibilityTester(IDefaultChecker defaultChecker)
8-
{
9-
_defaultChecker = defaultChecker;
10-
}
11-
125
public bool IsSpecificationCompatible(IArgumentSpecification specification, object? argumentValue, Type argumentType)
136
{
147
var typeArgSpecIsFor = specification.ForType;
@@ -18,7 +11,7 @@ public bool IsSpecificationCompatible(IArgumentSpecification specification, obje
1811

1912
private bool IsProvidedArgumentTheOneWeWouldGetUsingAnArgSpecForThisType(object? argument, Type typeArgSpecIsFor)
2013
{
21-
return _defaultChecker.IsDefault(argument, typeArgSpecIsFor);
14+
return defaultChecker.IsDefault(argument, typeArgSpecIsFor);
2215
}
2316

2417
private bool AreTypesCompatible(Type argumentType, Type typeArgSpecIsFor)

Diff for: src/NSubstitute/Core/Arguments/ArgumentSpecificationFactory.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,9 @@ private IEnumerable<IArgumentSpecification> UnwrapParamsArguments(IEnumerable<ob
8080
return result;
8181
}
8282

83-
private class ParameterInfoFromType : IParameterInfo
83+
private class ParameterInfoFromType(Type parameterType) : IParameterInfo
8484
{
85-
public ParameterInfoFromType(Type parameterType)
86-
{
87-
ParameterType = parameterType;
88-
}
89-
90-
public Type ParameterType { get; }
85+
public Type ParameterType { get; } = parameterType;
9186

9287
public bool IsParams => false;
9388

Diff for: src/NSubstitute/Core/Arguments/ArgumentSpecificationsFactory.cs

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1-
using System.Reflection;
2-
using NSubstitute.Exceptions;
1+
using NSubstitute.Exceptions;
2+
using System.Reflection;
33

44
namespace NSubstitute.Core.Arguments;
55

6-
public class ArgumentSpecificationsFactory : IArgumentSpecificationsFactory
6+
public class ArgumentSpecificationsFactory(
7+
IArgumentSpecificationFactory argumentSpecificationFactory,
8+
ISuppliedArgumentSpecificationsFactory suppliedArgumentSpecificationsFactory) : IArgumentSpecificationsFactory
79
{
8-
private readonly IArgumentSpecificationFactory _argumentSpecificationFactory;
9-
private readonly ISuppliedArgumentSpecificationsFactory _suppliedArgumentSpecificationsFactory;
10-
11-
public ArgumentSpecificationsFactory(IArgumentSpecificationFactory argumentSpecificationFactory, ISuppliedArgumentSpecificationsFactory suppliedArgumentSpecificationsFactory)
12-
{
13-
_argumentSpecificationFactory = argumentSpecificationFactory;
14-
_suppliedArgumentSpecificationsFactory = suppliedArgumentSpecificationsFactory;
15-
}
16-
1710
public IEnumerable<IArgumentSpecification> Create(IList<IArgumentSpecification> argumentSpecs, object?[] arguments, IParameterInfo[] parameterInfos, MethodInfo methodInfo, MatchArgs matchArgs)
1811
{
19-
var suppliedArgumentSpecifications = _suppliedArgumentSpecificationsFactory.Create(argumentSpecs);
12+
var suppliedArgumentSpecifications = suppliedArgumentSpecificationsFactory.Create(argumentSpecs);
2013

2114
var result = new List<IArgumentSpecification>();
2215
for (var i = 0; i < arguments.Length; i++)
@@ -26,7 +19,7 @@ public IEnumerable<IArgumentSpecification> Create(IList<IArgumentSpecification>
2619

2720
try
2821
{
29-
result.Add(_argumentSpecificationFactory.Create(arg, paramInfo, suppliedArgumentSpecifications));
22+
result.Add(argumentSpecificationFactory.Create(arg, paramInfo, suppliedArgumentSpecifications));
3023
}
3124
catch (AmbiguousArgumentsException ex) when (ex.ContainsDefaultMessage)
3225
{

Diff for: src/NSubstitute/Core/Arguments/ArrayContentsArgumentMatcher.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22

33
namespace NSubstitute.Core.Arguments;
44

5-
public class ArrayContentsArgumentMatcher : IArgumentMatcher, IArgumentFormatter
5+
public class ArrayContentsArgumentMatcher(IEnumerable<IArgumentSpecification> argumentSpecifications) : IArgumentMatcher, IArgumentFormatter
66
{
7-
private readonly IArgumentSpecification[] _argumentSpecifications;
8-
9-
public ArrayContentsArgumentMatcher(IEnumerable<IArgumentSpecification> argumentSpecifications)
10-
{
11-
_argumentSpecifications = argumentSpecifications.ToArray();
12-
}
7+
private readonly IArgumentSpecification[] _argumentSpecifications = argumentSpecifications.ToArray();
138

149
public bool IsSatisfiedBy(object? argument)
1510
{

Diff for: src/NSubstitute/Core/Arguments/DefaultChecker.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
namespace NSubstitute.Core.Arguments;
22

3-
public class DefaultChecker : IDefaultChecker
3+
public class DefaultChecker(IDefaultForType defaultForType) : IDefaultChecker
44
{
5-
private readonly IDefaultForType _defaultForType;
6-
7-
public DefaultChecker(IDefaultForType defaultForType)
8-
{
9-
_defaultForType = defaultForType;
10-
}
11-
125
public bool IsDefault(object? value, Type forType)
136
{
14-
return EqualityComparer<object>.Default.Equals(value, _defaultForType.GetDefaultFor(forType));
7+
return EqualityComparer<object>.Default.Equals(value, defaultForType.GetDefaultFor(forType));
158
}
169
}
+3-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
namespace NSubstitute.Core.Arguments;
22

3-
public class EqualsArgumentMatcher : IArgumentMatcher
3+
public class EqualsArgumentMatcher(object? value) : IArgumentMatcher
44
{
5-
private readonly object? _value;
5+
public override string ToString() => ArgumentFormatter.Default.Format(value, false);
66

7-
public EqualsArgumentMatcher(object? value)
8-
{
9-
_value = value;
10-
}
11-
12-
public override string ToString() => ArgumentFormatter.Default.Format(_value, false);
13-
14-
public bool IsSatisfiedBy(object? argument) => EqualityComparer<object>.Default.Equals(_value, argument);
7+
public bool IsSatisfiedBy(object? argument) => EqualityComparer<object>.Default.Equals(value, argument);
158
}

0 commit comments

Comments
 (0)