diff --git a/.editorconfig b/.editorconfig index 84059808..840e3c38 100644 --- a/.editorconfig +++ b/.editorconfig @@ -17,7 +17,9 @@ dotnet_style_qualification_for_field = false:suggestion 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 +csharp_style_namespace_declarations = file_scoped:warning +dotnet_style_prefer_collection_expression = true:warning +dotnet_style_collection_initializer = true:warning # ReSharper properties resharper_int_align_switch_expressions = true diff --git a/src/NSubstitute/Compatibility/DiagnosticsNullabilityAttributes.cs b/src/NSubstitute/Compatibility/DiagnosticsNullabilityAttributes.cs index a016b1f3..e3cdd540 100644 --- a/src/NSubstitute/Compatibility/DiagnosticsNullabilityAttributes.cs +++ b/src/NSubstitute/Compatibility/DiagnosticsNullabilityAttributes.cs @@ -90,7 +90,7 @@ internal sealed class MemberNotNullAttribute : Attribute /// /// The field or property member that is promised to be not-null. /// - public MemberNotNullAttribute(string member) => Members = new[] { member }; + public MemberNotNullAttribute(string member) => Members = [member]; /// Initializes the attribute with the list of field and property members. /// @@ -116,7 +116,7 @@ internal sealed class MemberNotNullWhenAttribute : Attribute public MemberNotNullWhenAttribute(bool returnValue, string member) { ReturnValue = returnValue; - Members = new[] { member }; + Members = [member]; } /// Initializes the attribute with the specified return value condition and list of field and property members. diff --git a/src/NSubstitute/Core/ArgumentSpecificationDequeue.cs b/src/NSubstitute/Core/ArgumentSpecificationDequeue.cs index 0c251d6d..7dfd76d8 100644 --- a/src/NSubstitute/Core/ArgumentSpecificationDequeue.cs +++ b/src/NSubstitute/Core/ArgumentSpecificationDequeue.cs @@ -5,7 +5,7 @@ namespace NSubstitute.Core; public class ArgumentSpecificationDequeue : IArgumentSpecificationDequeue { - private static readonly IArgumentSpecification[] EmptySpecifications = new IArgumentSpecification[0]; + private static readonly IArgumentSpecification[] EmptySpecifications = []; private readonly Func> _dequeueAllQueuedArgSpecs; diff --git a/src/NSubstitute/Core/Arguments/ArrayContentsArgumentMatcher.cs b/src/NSubstitute/Core/Arguments/ArrayContentsArgumentMatcher.cs index cfeb2555..ca7c66ac 100644 --- a/src/NSubstitute/Core/Arguments/ArrayContentsArgumentMatcher.cs +++ b/src/NSubstitute/Core/Arguments/ArrayContentsArgumentMatcher.cs @@ -31,7 +31,7 @@ public bool IsSatisfiedBy(object? argument) public string Format(object? argument, bool highlight) { - var argArray = argument is IEnumerable enumerableArgs ? enumerableArgs.Cast().ToArray() : new object[0]; + var argArray = argument is IEnumerable enumerableArgs ? enumerableArgs.Cast().ToArray() : []; return Format(argArray, _argumentSpecifications).Join(", "); } diff --git a/src/NSubstitute/Core/Call.cs b/src/NSubstitute/Core/Call.cs index 4bf09424..affd4f56 100644 --- a/src/NSubstitute/Core/Call.cs +++ b/src/NSubstitute/Core/Call.cs @@ -81,7 +81,7 @@ public void AssignSequenceNumber(long number) object?[] originalArray = _originalArguments; if (originalArray == _arguments && originalArray.Length > 0) { - object?[] copy = originalArray.ToArray(); + object?[] copy = [.. originalArray]; // If it happens that _originalArguments doesn't point to the `_arguments` anymore - // it might happen that other thread already created a copy and mutated the original `_arguments` array. // In this case it's unsafe to replace it with a copy. diff --git a/src/NSubstitute/Core/CustomHandlers.cs b/src/NSubstitute/Core/CustomHandlers.cs index 882e8aa5..3712e9ba 100644 --- a/src/NSubstitute/Core/CustomHandlers.cs +++ b/src/NSubstitute/Core/CustomHandlers.cs @@ -2,7 +2,7 @@ public class CustomHandlers : ICustomHandlers { - private readonly List _handlers = new(); + private readonly List _handlers = []; private readonly ISubstituteState _substituteState; public IReadOnlyCollection Handlers => _handlers; diff --git a/src/NSubstitute/Core/DependencyInjection/NSubContainer.cs b/src/NSubstitute/Core/DependencyInjection/NSubContainer.cs index 1d519ecb..0daa91c2 100644 --- a/src/NSubstitute/Core/DependencyInjection/NSubContainer.cs +++ b/src/NSubstitute/Core/DependencyInjection/NSubContainer.cs @@ -17,7 +17,7 @@ public class NSubContainer : IConfigurableNSubContainer { private readonly NSubContainer? _parentContainer; private readonly object _syncRoot; - private readonly Dictionary _registrations = new(); + private readonly Dictionary _registrations = []; public NSubContainer() { @@ -177,7 +177,7 @@ public object Resolve(Scope scope) private class Scope : INSubResolver { - private readonly Dictionary _cache = new Dictionary(); + private readonly Dictionary _cache = []; private readonly NSubContainer _mostNestedContainer; public Scope(NSubContainer mostNestedContainer) diff --git a/src/NSubstitute/Core/EventHandlerRegistry.cs b/src/NSubstitute/Core/EventHandlerRegistry.cs index 82485927..1309f912 100644 --- a/src/NSubstitute/Core/EventHandlerRegistry.cs +++ b/src/NSubstitute/Core/EventHandlerRegistry.cs @@ -6,7 +6,7 @@ public class EventHandlerRegistry : IEventHandlerRegistry // Events are not expected to be configured/raised concurrently, so simple locking should be sufficient. // List lookup is O(n), but for really small size performance is comparable to dictionary. // Given that normally a few events are configured only, it should be totally fine. - private readonly List>> _handlersForEvent = new(); + private readonly List>> _handlersForEvent = []; public void Add(string eventName, object handler) { diff --git a/src/NSubstitute/Core/Events/DelegateEventWrapper.cs b/src/NSubstitute/Core/Events/DelegateEventWrapper.cs index f2bfb372..b45308e6 100644 --- a/src/NSubstitute/Core/Events/DelegateEventWrapper.cs +++ b/src/NSubstitute/Core/Events/DelegateEventWrapper.cs @@ -64,7 +64,7 @@ private bool LooksLikeAnEventStyleCall(ParameterInfo[] parameters) sender = _providedArguments[0]; eventArgs = GetDefaultForEventArgType(eventArgsType); } - return new[] { sender, eventArgs }; + return [sender, eventArgs]; } private static bool RequiredArgsHaveBeenProvided(object?[] providedArgs, ParameterInfo[] requiredArgs) diff --git a/src/NSubstitute/Core/Events/EventHandlerWrapper.cs b/src/NSubstitute/Core/Events/EventHandlerWrapper.cs index 767784b9..29d4f599 100644 --- a/src/NSubstitute/Core/Events/EventHandlerWrapper.cs +++ b/src/NSubstitute/Core/Events/EventHandlerWrapper.cs @@ -35,6 +35,6 @@ protected override object[] WorkOutRequiredArguments(ICall call) { var sender = _sender ?? call.Target(); var eventArgs = _eventArgs ?? GetDefaultForEventArgType(typeof(TEventArgs)); - return new[] { sender, eventArgs }; + return [sender, eventArgs]; } } \ No newline at end of file diff --git a/src/NSubstitute/Core/Events/RaiseEventWrapper.cs b/src/NSubstitute/Core/Events/RaiseEventWrapper.cs index 4b762b7d..efe56e1c 100644 --- a/src/NSubstitute/Core/Events/RaiseEventWrapper.cs +++ b/src/NSubstitute/Core/Events/RaiseEventWrapper.cs @@ -21,7 +21,7 @@ protected EventArgs GetDefaultForEventArgType(Type type) , type.Name, RaiseMethodName); throw new CannotCreateEventArgsException(message); } - return (EventArgs)defaultConstructor.Invoke(new object[0]); + return (EventArgs)defaultConstructor.Invoke([]); } private static ConstructorInfo? GetDefaultConstructor(Type type) => type.GetConstructor(Type.EmptyTypes); diff --git a/src/NSubstitute/Core/Query.cs b/src/NSubstitute/Core/Query.cs index 81b76a57..bc523f8e 100644 --- a/src/NSubstitute/Core/Query.cs +++ b/src/NSubstitute/Core/Query.cs @@ -2,7 +2,7 @@ public class Query : IQuery, IQueryResults { - private readonly List _querySpec = new(); + private readonly List _querySpec = []; private readonly HashSet _matchingCalls = new(new CallSequenceNumberComparer()); private readonly ICallSpecificationFactory _callSpecificationFactory; diff --git a/src/NSubstitute/Core/ReflectionExtensions.cs b/src/NSubstitute/Core/ReflectionExtensions.cs index 96292774..4672f34f 100644 --- a/src/NSubstitute/Core/ReflectionExtensions.cs +++ b/src/NSubstitute/Core/ReflectionExtensions.cs @@ -41,8 +41,6 @@ private static bool CanBePropertySetterCall(MethodInfo call) private static PropertyInfo[] GetAllProperties(Type? type) { - return type != null - ? type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) - : new PropertyInfo[0]; + return type?.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ?? []; } } diff --git a/src/NSubstitute/Core/ThreadLocalContext.cs b/src/NSubstitute/Core/ThreadLocalContext.cs index 59756f52..3fbbb4a4 100644 --- a/src/NSubstitute/Core/ThreadLocalContext.cs +++ b/src/NSubstitute/Core/ThreadLocalContext.cs @@ -6,7 +6,7 @@ namespace NSubstitute.Core; public class ThreadLocalContext : IThreadLocalContext { - private static readonly IArgumentSpecification[] EmptySpecifications = new IArgumentSpecification[0]; + private static readonly IArgumentSpecification[] EmptySpecifications = []; private readonly RobustThreadLocal _lastCallRouter; private readonly RobustThreadLocal> _argumentSpecifications; diff --git a/src/NSubstitute/Extensions/ExceptionExtensions.cs b/src/NSubstitute/Extensions/ExceptionExtensions.cs index ced66769..d9ad6731 100644 --- a/src/NSubstitute/Extensions/ExceptionExtensions.cs +++ b/src/NSubstitute/Extensions/ExceptionExtensions.cs @@ -236,7 +236,7 @@ private static object FromException(object value, Exception exception) { var fromExceptionMethodInfo = typeof(Task).GetMethods(BindingFlags.Static | BindingFlags.Public).Single(m => m.Name == "FromException" && m.ContainsGenericParameters); var specificFromExceptionMethod = fromExceptionMethodInfo.MakeGenericMethod(valueType.GenericTypeArguments); - return specificFromExceptionMethod.Invoke(null, new object[] { exception }); + return specificFromExceptionMethod.Invoke(null, [exception]); } return Task.FromException(exception); diff --git a/src/NSubstitute/Proxies/CastleDynamicProxy/CastleDynamicProxyFactory.cs b/src/NSubstitute/Proxies/CastleDynamicProxy/CastleDynamicProxyFactory.cs index dab85200..97d20354 100644 --- a/src/NSubstitute/Proxies/CastleDynamicProxy/CastleDynamicProxyFactory.cs +++ b/src/NSubstitute/Proxies/CastleDynamicProxy/CastleDynamicProxyFactory.cs @@ -40,7 +40,7 @@ private object GenerateTypeProxy(ICallRouter callRouter, Type typeToProxy, Type[ typeToProxy, additionalInterfaces, constructorArguments, - new IInterceptor[] { proxyIdInterceptor, forwardingInterceptor }, + [proxyIdInterceptor, forwardingInterceptor], proxyGenerationOptions); forwardingInterceptor.SwitchToFullDispatchMode(); @@ -63,7 +63,7 @@ private object GenerateDelegateProxy(ICallRouter callRouter, Type delegateType, typeToProxy: typeof(object), additionalInterfaces: null, constructorArguments: null, - interceptors: new IInterceptor[] { proxyIdInterceptor, forwardingInterceptor }, + interceptors: [proxyIdInterceptor, forwardingInterceptor], proxyGenerationOptions); forwardingInterceptor.SwitchToFullDispatchMode(); diff --git a/src/NSubstitute/Raise.cs b/src/NSubstitute/Raise.cs index f0fc2580..d247db62 100644 --- a/src/NSubstitute/Raise.cs +++ b/src/NSubstitute/Raise.cs @@ -81,7 +81,7 @@ private static object[] FixParamsArrayAmbiguity(object[] arguments, Type delegat if (singleParameterType.IsInstanceOfType(arguments)) { - return new object[] { arguments }; + return [arguments]; } return arguments; diff --git a/src/NSubstitute/Routing/AutoValues/AutoObservableProvider.cs b/src/NSubstitute/Routing/AutoValues/AutoObservableProvider.cs index bdf1ecd0..177ff40f 100644 --- a/src/NSubstitute/Routing/AutoValues/AutoObservableProvider.cs +++ b/src/NSubstitute/Routing/AutoValues/AutoObservableProvider.cs @@ -1,5 +1,5 @@ -using System.Reflection; -using NSubstitute.Core; +using NSubstitute.Core; +using System.Reflection; namespace NSubstitute.Routing.AutoValues; @@ -23,9 +23,7 @@ public bool CanProvideValueFor(Type type) => Type innerType = type.GetGenericArguments()[0]; var valueProvider = _autoValueProviders.Value.FirstOrDefault(vp => vp.CanProvideValueFor(innerType)); var value = valueProvider == null ? GetDefault(type) : valueProvider.GetValue(innerType); - return Activator.CreateInstance( - typeof(ReturnObservable<>).MakeGenericType(innerType) - , new object?[] { value }); + return Activator.CreateInstance(typeof(ReturnObservable<>).MakeGenericType(innerType), [value]); } private static object? GetDefault(Type type) diff --git a/src/NSubstitute/Routing/AutoValues/AutoSubstituteProvider.cs b/src/NSubstitute/Routing/AutoValues/AutoSubstituteProvider.cs index e3806e1d..eaa8ac0a 100644 --- a/src/NSubstitute/Routing/AutoValues/AutoSubstituteProvider.cs +++ b/src/NSubstitute/Routing/AutoValues/AutoSubstituteProvider.cs @@ -21,7 +21,7 @@ public bool CanProvideValueFor(Type type) public object GetValue(Type type) { - return _substituteFactory.Create(new[] { type }, new object[0]); + return _substituteFactory.Create([type], []); } private bool IsPureVirtualClassWithParameterlessConstructor(Type type) diff --git a/src/NSubstitute/Routing/AutoValues/AutoTaskProvider.cs b/src/NSubstitute/Routing/AutoValues/AutoTaskProvider.cs index 239b6506..e106df3e 100644 --- a/src/NSubstitute/Routing/AutoValues/AutoTaskProvider.cs +++ b/src/NSubstitute/Routing/AutoValues/AutoTaskProvider.cs @@ -26,7 +26,7 @@ public object GetValue(Type type) var value = valueProvider == null ? GetDefault(type) : valueProvider.GetValue(taskType); var taskCompletionSourceType = typeof(TaskCompletionSource<>).MakeGenericType(taskType); var taskCompletionSource = Activator.CreateInstance(taskCompletionSourceType); - taskCompletionSourceType.GetMethod(nameof(TaskCompletionSource.SetResult))!.Invoke(taskCompletionSource, new[] { value }); + taskCompletionSourceType.GetMethod(nameof(TaskCompletionSource.SetResult))!.Invoke(taskCompletionSource, [value]); return taskCompletionSourceType.GetProperty(nameof(TaskCompletionSource.Task))!.GetValue(taskCompletionSource, null)!; } else diff --git a/src/NSubstitute/Routing/AutoValues/AutoValueProvidersFactory.cs b/src/NSubstitute/Routing/AutoValues/AutoValueProvidersFactory.cs index 815c41c0..5dc7fe7c 100644 --- a/src/NSubstitute/Routing/AutoValues/AutoValueProvidersFactory.cs +++ b/src/NSubstitute/Routing/AutoValues/AutoValueProvidersFactory.cs @@ -12,15 +12,15 @@ public IReadOnlyCollection CreateProviders(ISubstituteFactor () => result ?? throw new SubstituteInternalException("Value was not constructed yet."), LazyThreadSafetyMode.PublicationOnly); - result = new IAutoValueProvider[] - { + result = + [ new AutoObservableProvider(lazyResult), new AutoQueryableProvider(), new AutoSubstituteProvider(substituteFactory), new AutoStringProvider(), new AutoArrayProvider(), new AutoTaskProvider(lazyResult) - }; + ]; return result; } diff --git a/src/NSubstitute/Routing/RouteFactory.cs b/src/NSubstitute/Routing/RouteFactory.cs index ac84ed24..4808bb62 100644 --- a/src/NSubstitute/Routing/RouteFactory.cs +++ b/src/NSubstitute/Routing/RouteFactory.cs @@ -30,85 +30,85 @@ public RouteFactory(SequenceNumberGenerator sequenceNumberGenerator, public IRoute CallQuery(ISubstituteState state) { - return new Route(new ICallHandler[] { - new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification) - , new AddCallToQueryResultHandler(_threadLocalContext) - , new ReturnAutoValue(AutoValueBehaviour.UseValueForSubsequentCalls, state.AutoValueProviders, state.AutoValuesCallResults, _callSpecificationFactory) - , ReturnDefaultForReturnTypeHandler() - }); + return new Route([ + new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification), + new AddCallToQueryResultHandler(_threadLocalContext), + new ReturnAutoValue(AutoValueBehaviour.UseValueForSubsequentCalls, state.AutoValueProviders, state.AutoValuesCallResults, _callSpecificationFactory), + ReturnDefaultForReturnTypeHandler() + ]); } public IRoute CheckReceivedCalls(ISubstituteState state, MatchArgs matchArgs, Quantity requiredQuantity) { - return new Route(new ICallHandler[] { - new ClearLastCallRouterHandler(_threadLocalContext) - , new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification) - , new CheckReceivedCallsHandler(state.ReceivedCalls, _callSpecificationFactory, _receivedCallsExceptionThrower, matchArgs, requiredQuantity) - , new ReturnAutoValue(AutoValueBehaviour.ReturnAndForgetValue, state.AutoValueProviders, state.AutoValuesCallResults, _callSpecificationFactory) - , ReturnDefaultForReturnTypeHandler() - }); + return new Route([ + new ClearLastCallRouterHandler(_threadLocalContext), + new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification), + new CheckReceivedCallsHandler(state.ReceivedCalls, _callSpecificationFactory, _receivedCallsExceptionThrower, matchArgs, requiredQuantity), + new ReturnAutoValue(AutoValueBehaviour.ReturnAndForgetValue, state.AutoValueProviders, state.AutoValuesCallResults, _callSpecificationFactory), + ReturnDefaultForReturnTypeHandler() + ]); } public IRoute DoWhenCalled(ISubstituteState state, Action doAction, MatchArgs matchArgs) { - return new Route(new ICallHandler[] { - new ClearLastCallRouterHandler(_threadLocalContext) - , new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification) - , new SetActionForCallHandler(_callSpecificationFactory, state.CallActions, doAction, matchArgs) - , ReturnDefaultForReturnTypeHandler() - }); + return new Route([ + new ClearLastCallRouterHandler(_threadLocalContext), + new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification), + new SetActionForCallHandler(_callSpecificationFactory, state.CallActions, doAction, matchArgs), + ReturnDefaultForReturnTypeHandler() + ]); } public IRoute DoNotCallBase(ISubstituteState state, MatchArgs matchArgs) { - return new Route(new ICallHandler[] { - new ClearLastCallRouterHandler(_threadLocalContext) - , new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification) - , new DoNotCallBaseForCallHandler(_callSpecificationFactory, state.CallBaseConfiguration, matchArgs) - , ReturnDefaultForReturnTypeHandler() - }); + return new Route([ + new ClearLastCallRouterHandler(_threadLocalContext), + new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification), + new DoNotCallBaseForCallHandler(_callSpecificationFactory, state.CallBaseConfiguration, matchArgs), + ReturnDefaultForReturnTypeHandler() + ]); } public IRoute CallBase(ISubstituteState state, MatchArgs matchArgs) { - return new Route(new ICallHandler[] { - new ClearLastCallRouterHandler(_threadLocalContext) - , new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification) - , new CallBaseForCallHandler(_callSpecificationFactory, state.CallBaseConfiguration, matchArgs) - , ReturnDefaultForReturnTypeHandler() - }); + return new Route([ + new ClearLastCallRouterHandler(_threadLocalContext), + new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification), + new CallBaseForCallHandler(_callSpecificationFactory, state.CallBaseConfiguration, matchArgs), + ReturnDefaultForReturnTypeHandler() + ]); } public IRoute RaiseEvent(ISubstituteState state, Func getEventArguments) { - return new Route(new ICallHandler[] { - new ClearLastCallRouterHandler(_threadLocalContext) - , new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification) - , new RaiseEventHandler(state.EventHandlerRegistry, getEventArguments) - , ReturnDefaultForReturnTypeHandler() - }); + return new Route([ + new ClearLastCallRouterHandler(_threadLocalContext), + new ClearUnusedCallSpecHandler(_threadLocalContext.PendingSpecification), + new RaiseEventHandler(state.EventHandlerRegistry, getEventArguments), + ReturnDefaultForReturnTypeHandler() + ]); } public IRoute RecordCallSpecification(ISubstituteState state) { - return new Route(new ICallHandler[] { - new RecordCallSpecificationHandler(_threadLocalContext.PendingSpecification, _callSpecificationFactory, state.CallActions) - , new PropertySetterHandler(_propertyHelper, state.ConfigureCall) - , new ReturnAutoValue(AutoValueBehaviour.UseValueForSubsequentCalls, state.AutoValueProviders, state.AutoValuesCallResults, _callSpecificationFactory) - , new ReturnFromAndConfigureDynamicCall(state.ConfigureCall) - , ReturnDefaultForReturnTypeHandler() - }); + return new Route([ + new RecordCallSpecificationHandler(_threadLocalContext.PendingSpecification, _callSpecificationFactory, state.CallActions), + new PropertySetterHandler(_propertyHelper, state.ConfigureCall), + new ReturnAutoValue(AutoValueBehaviour.UseValueForSubsequentCalls, state.AutoValueProviders, state.AutoValuesCallResults, _callSpecificationFactory), + new ReturnFromAndConfigureDynamicCall(state.ConfigureCall), + ReturnDefaultForReturnTypeHandler() + ]); } public IRoute RecordReplay(ISubstituteState state) { - return new Route(new ICallHandler[] { - new TrackLastCallHandler(_threadLocalContext.PendingSpecification) - , new RecordCallHandler(state.ReceivedCalls, _sequenceNumberGenerator) - , new EventSubscriptionHandler(state.EventHandlerRegistry) - , new PropertySetterHandler(_propertyHelper, state.ConfigureCall) - , new DoActionsCallHandler(state.CallActions) - , new ReturnConfiguredResultHandler(state.CallResults) - , new ReturnResultForTypeHandler(state.ResultsForType) - , new ReturnFromBaseIfRequired(state.CallBaseConfiguration) - , new ReturnFromCustomHandlers(state.CustomHandlers) - , new ReturnAutoValue(AutoValueBehaviour.UseValueForSubsequentCalls, state.AutoValueProviders, state.AutoValuesCallResults, _callSpecificationFactory) - , new ReturnFromAndConfigureDynamicCall(state.ConfigureCall) - , ReturnDefaultForReturnTypeHandler() - }); + return new Route([ + new TrackLastCallHandler(_threadLocalContext.PendingSpecification), + new RecordCallHandler(state.ReceivedCalls, _sequenceNumberGenerator), + new EventSubscriptionHandler(state.EventHandlerRegistry), + new PropertySetterHandler(_propertyHelper, state.ConfigureCall), + new DoActionsCallHandler(state.CallActions), + new ReturnConfiguredResultHandler(state.CallResults), + new ReturnResultForTypeHandler(state.ResultsForType), + new ReturnFromBaseIfRequired(state.CallBaseConfiguration), + new ReturnFromCustomHandlers(state.CustomHandlers), + new ReturnAutoValue(AutoValueBehaviour.UseValueForSubsequentCalls, state.AutoValueProviders, state.AutoValuesCallResults, _callSpecificationFactory), + new ReturnFromAndConfigureDynamicCall(state.ConfigureCall), + ReturnDefaultForReturnTypeHandler() + ]); } private ReturnDefaultForReturnTypeHandler ReturnDefaultForReturnTypeHandler() => new(_defaultForType); diff --git a/src/NSubstitute/Substitute.cs b/src/NSubstitute/Substitute.cs index 9efd2223..f1d471e6 100644 --- a/src/NSubstitute/Substitute.cs +++ b/src/NSubstitute/Substitute.cs @@ -21,7 +21,7 @@ public static class Substitute public static T For(params object[] constructorArguments) where T : class { - return (T)For(new[] { typeof(T) }, constructorArguments); + return (T)For([typeof(T)], constructorArguments); } /// @@ -37,7 +37,7 @@ public static T1 For(params object[] constructorArguments) where T1 : class where T2 : class { - return (T1)For(new[] { typeof(T1), typeof(T2) }, constructorArguments); + return (T1)For([typeof(T1), typeof(T2)], constructorArguments); } /// @@ -56,7 +56,7 @@ public static T1 For(params object[] constructorArguments) where T2 : class where T3 : class { - return (T1)For(new[] { typeof(T1), typeof(T2), typeof(T3) }, constructorArguments); + return (T1)For([typeof(T1), typeof(T2), typeof(T3)], constructorArguments); } /// @@ -87,6 +87,6 @@ public static T ForPartsOf(params object[] constructorArguments) where T : class { var substituteFactory = SubstitutionContext.Current.SubstituteFactory; - return (T)substituteFactory.CreatePartial(new[] { typeof(T) }, constructorArguments); + return (T)substituteFactory.CreatePartial([typeof(T)], constructorArguments); } } \ No newline at end of file diff --git a/src/NSubstitute/SubstituteExtensions.Returns.cs b/src/NSubstitute/SubstituteExtensions.Returns.cs index e63b673d..39ecb82e 100644 --- a/src/NSubstitute/SubstituteExtensions.Returns.cs +++ b/src/NSubstitute/SubstituteExtensions.Returns.cs @@ -54,7 +54,7 @@ private static ConfiguredCall ConfigureReturn(MatchArgs matchArgs, T? returnT } else { - returnValue = new ReturnMultipleValues(new[] { returnThis }.Concat(returnThese).ToArray()); + returnValue = new ReturnMultipleValues([returnThis, .. returnThese]); } return SubstitutionContext .Current @@ -71,7 +71,7 @@ private static ConfiguredCall ConfigureReturn(MatchArgs matchArgs, Func(new[] { returnThis }.Concat(returnThese).ToArray()); + returnValue = new ReturnMultipleFuncsValues([returnThis, .. returnThese]); } return SubstitutionContext diff --git a/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs b/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs index 63468c3d..517ed831 100644 --- a/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs +++ b/tests/NSubstitute.Acceptance.Specs/ArgumentMatching.cs @@ -200,7 +200,7 @@ public void Received_should_compare_elements_for_params_arguments() _something.Received().WithParams(1, first, second); _something.Received().WithParams(1, Arg.Any(), second); _something.Received().WithParams(1, first, Arg.Any()); - _something.Received().WithParams(1, new[] { first, second }); + _something.Received().WithParams(1, [first, second]); _something.Received().WithParams(1, Arg.Any()); _something.Received().WithParams(1, Arg.Is(x => x.Length == 2)); _something.DidNotReceive().WithParams(2, first, second); diff --git a/tests/NSubstitute.Acceptance.Specs/ArgumentMatchingCompat.cs b/tests/NSubstitute.Acceptance.Specs/ArgumentMatchingCompat.cs index 08f68aad..d6c43c2e 100644 --- a/tests/NSubstitute.Acceptance.Specs/ArgumentMatchingCompat.cs +++ b/tests/NSubstitute.Acceptance.Specs/ArgumentMatchingCompat.cs @@ -141,7 +141,7 @@ public void Received_should_compare_elements_for_params_arguments() _something.Received().WithParams(1, first, second); _something.Received().WithParams(1, Arg.Compat.Any(), second); _something.Received().WithParams(1, first, Arg.Compat.Any()); - _something.Received().WithParams(1, new[] { first, second }); + _something.Received().WithParams(1, [first, second]); _something.Received().WithParams(1, Arg.Compat.Any()); _something.Received().WithParams(1, Arg.Compat.Is(x => x.Length == 2)); _something.DidNotReceive().WithParams(2, first, second); diff --git a/tests/NSubstitute.Acceptance.Specs/FieldReports/ExceptionsThrownFromCustomArgumentMatchers.cs b/tests/NSubstitute.Acceptance.Specs/FieldReports/ExceptionsThrownFromCustomArgumentMatchers.cs index 8ef1126d..79b6d9dd 100644 --- a/tests/NSubstitute.Acceptance.Specs/FieldReports/ExceptionsThrownFromCustomArgumentMatchers.cs +++ b/tests/NSubstitute.Acceptance.Specs/FieldReports/ExceptionsThrownFromCustomArgumentMatchers.cs @@ -81,10 +81,10 @@ public void Multiple_conditions_where_one_requires_an_array_index_available() { var emptyArray = new string[0]; var request = Substitute.For(); - request.GetMultiple(Arg.Is(x => x[0] == "greeting")).Returns(new[] { "hello", "bye" }); - request.GetMultiple(emptyArray).Returns(new[] { "?" }); + request.GetMultiple(Arg.Is(x => x[0] == "greeting")).Returns(["hello", "bye"]); + request.GetMultiple(emptyArray).Returns(["?"]); - Assert.That(request.GetMultiple(new[] { "greeting" }), Is.EqualTo(new[] { "hello", "bye" })); + Assert.That(request.GetMultiple(["greeting"]), Is.EqualTo(new[] { "hello", "bye" })); Assert.That(request.GetMultiple(emptyArray), Is.EqualTo(new[] { "?" })); } } \ No newline at end of file diff --git a/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue47_RaisingEventsWithNullArg.cs b/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue47_RaisingEventsWithNullArg.cs index 78b9fa23..a4953be6 100644 --- a/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue47_RaisingEventsWithNullArg.cs +++ b/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue47_RaisingEventsWithNullArg.cs @@ -23,6 +23,6 @@ public void Pass_null_when_raising_delegate_event() public void Pass_null_when_raising_eventhandlerish_event() { var sub = Substitute.For(); - sub.OnEventishThing += Raise.Event(new object[] { null }); + sub.OnEventishThing += Raise.Event([null]); } } \ No newline at end of file diff --git a/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs b/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs index 7a52bc71..58e9cf68 100644 --- a/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs +++ b/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs @@ -18,7 +18,7 @@ static Issue500_SpecialMethodsWithoutAttribute() [Test] public void ShouldCorrectlyConfigureProperty() { - var substitute = Substitute.For(new[] { TypeWithMissingSpecialNameMethodAttributes }, new object[0]); + var substitute = Substitute.For([TypeWithMissingSpecialNameMethodAttributes], []); var fixture = new GeneratedTypeFixture(substitute); fixture.MyProperty = "42"; @@ -30,7 +30,7 @@ public void ShouldCorrectlyConfigureProperty() [Test] public void ShouldCorrectlyConfigureEvent() { - object substitute = Substitute.For(new[] { TypeWithMissingSpecialNameMethodAttributes }, new object[0]); + object substitute = Substitute.For([TypeWithMissingSpecialNameMethodAttributes], []); var fixture = new GeneratedTypeFixture(substitute); bool wasCalled = false; @@ -58,13 +58,13 @@ private static Type GenerateTypeWithMissingSpecialNameAttributes() MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Abstract | MethodAttributes.HideBySig | MethodAttributes.NewSlot /* | MethodAttributes.SpecialName */, typeof(void), - new[] { typeof(EventHandler) }); + [typeof(EventHandler)]); var evRemover = typeBuilder.DefineMethod( $"remove_{EventName}", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Abstract | MethodAttributes.HideBySig | MethodAttributes.NewSlot /* | MethodAttributes.SpecialName */, typeof(void), - new[] { typeof(EventHandler) }); + [typeof(EventHandler)]); evBuilder.SetAddOnMethod(evAdder); evBuilder.SetRemoveOnMethod(evRemover); @@ -81,7 +81,7 @@ private static Type GenerateTypeWithMissingSpecialNameAttributes() MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Abstract | MethodAttributes.HideBySig | MethodAttributes.NewSlot /* | MethodAttributes.SpecialName */, typeof(void), - new[] { typeof(object) }); + [typeof(object)]); propBuilder.SetGetMethod(propGetter); propBuilder.SetSetMethod(propSetter); diff --git a/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue560_RaiseEventWithArrayArg.cs b/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue560_RaiseEventWithArrayArg.cs index 21076ff5..ebcaf1f0 100644 --- a/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue560_RaiseEventWithArrayArg.cs +++ b/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue560_RaiseEventWithArrayArg.cs @@ -56,7 +56,7 @@ public void Should_raise_event_for_object_array_arg_provided_without_using_param var eventSamples = Substitute.For(); eventSamples.ActionWithParamOfObjectArray += x => capturedArg = x; - eventSamples.ActionWithParamOfObjectArray += Raise.Event>(new object[] { arg1 }); + eventSamples.ActionWithParamOfObjectArray += Raise.Event>([arg1]); Assert.That(capturedArg, Is.EqualTo(arg1)); } @@ -69,7 +69,7 @@ public void Should_raise_event_for_object_array_arg_provided_without_using_param var eventSamples = Substitute.For(); eventSamples.ActionWithParamOfObjectArray += x => capturedArg = x; - eventSamples.ActionWithParamOfObjectArray += Raise.Event>(new object[] { arg1 }); + eventSamples.ActionWithParamOfObjectArray += Raise.Event>([arg1]); Assert.That(capturedArg, Is.EqualTo(arg1)); } diff --git a/tests/NSubstitute.Acceptance.Specs/FormattingCallsWhenThrowingReceivedCallsExceptions.cs b/tests/NSubstitute.Acceptance.Specs/FormattingCallsWhenThrowingReceivedCallsExceptions.cs index 5fea58f9..70a72a0b 100644 --- a/tests/NSubstitute.Acceptance.Specs/FormattingCallsWhenThrowingReceivedCallsExceptions.cs +++ b/tests/NSubstitute.Acceptance.Specs/FormattingCallsWhenThrowingReceivedCallsExceptions.cs @@ -265,7 +265,7 @@ public class When_checking_call_to_method_with_params : Context protected override void ConfigureContext() { Sample.ParamsMethod(2, "hello", "everybody"); - Sample.ParamsMethod(1, new[] { "hello", "everybody" }); + Sample.ParamsMethod(1, ["hello", "everybody"]); Sample.ParamsMethod(1, "hello"); Sample.ParamsMethod(3, "1", "2", "3"); Sample.ParamsMethod(1); @@ -298,7 +298,7 @@ public class When_checking_call_to_method_with_valuetype_params : Context protected override void ConfigureContext() { Sample.IntParamsMethod(1, 2, 3); - Sample.IntParamsMethod(new[] { 4, 5 }); + Sample.IntParamsMethod([4, 5]); Sample.IntParamsMethod(); } diff --git a/tests/NSubstitute.Acceptance.Specs/PartialSubExamples.cs b/tests/NSubstitute.Acceptance.Specs/PartialSubExamples.cs index dee80035..3e643679 100644 --- a/tests/NSubstitute.Acceptance.Specs/PartialSubExamples.cs +++ b/tests/NSubstitute.Acceptance.Specs/PartialSubExamples.cs @@ -82,9 +82,9 @@ public class UnderlyingListExample { public class TaskList { - readonly List list = new List(); + readonly List list = []; public virtual void Add(string s) { list.Add(s); } - public virtual string[] ToArray() { return list.ToArray(); } + public virtual string[] ToArray() { return [.. list]; } } public class TaskView diff --git a/tests/NSubstitute.Acceptance.Specs/ReceivedCalls.cs b/tests/NSubstitute.Acceptance.Specs/ReceivedCalls.cs index cbc74635..3edbe6ff 100644 --- a/tests/NSubstitute.Acceptance.Specs/ReceivedCalls.cs +++ b/tests/NSubstitute.Acceptance.Specs/ReceivedCalls.cs @@ -10,8 +10,8 @@ public class ReceivedCalls { private ICar _car; const int Rpm = 7000; - private static readonly object[] Luggage = new[] { new object(), new object() }; - private static readonly DateTime[] ServiceDates = new[] { new DateTime(2001, 01, 01), new DateTime(2002, 02, 02) }; + private static readonly object[] Luggage = [new object(), new object()]; + private static readonly DateTime[] ServiceDates = [new DateTime(2001, 01, 01), new DateTime(2002, 02, 02)]; [SetUp] public void SetUp()