Skip to content

Commit

Permalink
Collection expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
pengweiqhca committed Mar 17, 2024
1 parent 75b9beb commit 6cebe87
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/Xunit.DependencyInjection.Analyzer/Rules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ public static class Rules
.Where(p => p.PropertyType == typeof(DiagnosticDescriptor))
.Select(p => p.GetMethod)
.Where(m => m != null)
.Select(m => (DiagnosticDescriptor)m!.Invoke(null, Array.Empty<object?>())!));
.Select(m => (DiagnosticDescriptor)m!.Invoke(null, [])!));
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static IHostBuilder GetHostBuilder<TEntryPoint>(Action<IWebHostBuilder>?

ArgumentNullException.ThrowIfNull(factory);

SetHostFactory.Invoke(deferredHostBuilder, BindingFlags.DoNotWrapExceptions, null, new[] { factory }, null);
SetHostFactory.Invoke(deferredHostBuilder, BindingFlags.DoNotWrapExceptions, null, [factory], null);

var setContentRoot = typeof(WebApplicationFactory<TEntryPoint>).GetMethod("SetContentRoot",
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) ?? throw NotSupported();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public class DependencyInjectionTestClassRunner(
protected override object?[] CreateTestClassConstructorArguments()
{
if ((!Class.Type.GetTypeInfo().IsAbstract ? 0 : Class.Type.GetTypeInfo().IsSealed ? 1 : 0) != 0)
return Array.Empty<object?>();
return [];

var constructor = SelectTestClassConstructor();

if (constructor == null) return Array.Empty<object?>();
if (constructor == null) return [];

var parameters = constructor.GetParameters();

Expand Down
6 changes: 3 additions & 3 deletions src/Xunit.DependencyInjection/HostManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace Xunit.DependencyInjection;
namespace Xunit.DependencyInjection;

internal sealed class HostManager(AssemblyName assemblyName, IMessageSink diagnosticMessageSink)
: IHostedService, IDisposable
{
private readonly Dictionary<Type, DependencyInjectionContext> _hostMap = new();
private readonly List<IHost> _hosts = new();
private readonly Dictionary<Type, DependencyInjectionContext> _hostMap = [];
private readonly List<IHost> _hosts = [];

private Type? _defaultStartupType;
private DependencyInjectionContext? _defaultHost;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.Internal;
Expand All @@ -12,8 +12,8 @@ namespace Microsoft.Extensions.Internal;
MethodInfo getAwaiterMethod)
{
private const BindingFlags Everything = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
private static readonly MethodInfo OnCompleted = typeof(INotifyCompletion).GetMethod(nameof(INotifyCompletion.OnCompleted), Everything, null, new[] { typeof(Action) }, null)!;
private static readonly MethodInfo UnsafeOnCompleted = typeof(ICriticalNotifyCompletion).GetMethod(nameof(ICriticalNotifyCompletion.UnsafeOnCompleted), Everything, null, new[] { typeof(Action) }, null)!;
private static readonly MethodInfo OnCompleted = typeof(INotifyCompletion).GetMethod(nameof(INotifyCompletion.OnCompleted), Everything, null, [typeof(Action)], null)!;
private static readonly MethodInfo UnsafeOnCompleted = typeof(ICriticalNotifyCompletion).GetMethod(nameof(ICriticalNotifyCompletion.UnsafeOnCompleted), Everything, null, [typeof(Action)], null)!;

public Type AwaiterType { get; } = awaiterType;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Diagnostics;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;

Expand All @@ -12,14 +12,14 @@ internal sealed class ObjectMethodExecutor
private readonly MethodExecutor _executor;

private static readonly ConstructorInfo ObjectMethodExecutorAwaitableConstructor =
typeof(ObjectMethodExecutorAwaitable).GetConstructor(new[] {
typeof(ObjectMethodExecutorAwaitable).GetConstructor([
typeof(object), // customAwaitable
typeof(Func<object, object>), // getAwaiterMethod
typeof(Func<object, bool>), // isCompletedMethod
typeof(Func<object, object>), // getResultMethod
typeof(Action<object, Action>), // onCompletedMethod
typeof(Action<object, Action>) // unsafeOnCompletedMethod
})!;
])!;

private ObjectMethodExecutor(MethodInfo methodInfo, TypeInfo targetTypeInfo, object?[]? parameterDefaultValues)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Xunit.DependencyInjection/StartupLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ internal static class StartupLoader

var parameters = method.GetParameters();
if (parameters.Length == 0)
return (IHostBuilder?)method.Invoke(method.IsStatic ? null : startup, Array.Empty<object>());
return (IHostBuilder?)method.Invoke(method.IsStatic ? null : startup, []);

if (parameters.Length > 1 || parameters[0].ParameterType != typeof(AssemblyName))
throw new InvalidOperationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ public static Task VerifyAnalyzerAsync(string source, CancellationToken cancella
var test = new Test
{
TestCode = source,
ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.Extensions.Hosting", "2.1.0"),
new PackageIdentity("Xunit.DependencyInjection", "7.1.0"))),
ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages([
new("Microsoft.Extensions.Hosting", "2.1.0"),
new("Xunit.DependencyInjection", "7.1.0")
]),
};

test.ExpectedDiagnostics.AddRange(expected);

return test.RunAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public static Task VerifyAnalyzerAsync(string source, CancellationToken cancella
var test = new Test
{
TestCode = source,
ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.Extensions.Hosting", "2.1.0"),
new PackageIdentity("Xunit.DependencyInjection", "7.1.0"))),
ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages([
new("Microsoft.Extensions.Hosting", "2.1.0"),
new("Xunit.DependencyInjection", "7.1.0")
]),
};

test.ExpectedDiagnostics.AddRange(expected);
Expand All @@ -47,13 +48,14 @@ public static Task VerifyCodeFixAsync(string source, string fixedSource, Cancell
{
TestCode = source,
FixedCode = fixedSource,
ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.Extensions.Hosting", "2.1.0"),
new PackageIdentity("Xunit.DependencyInjection", "7.1.0"))),
ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages([
new("Microsoft.Extensions.Hosting", "2.1.0"),
new("Xunit.DependencyInjection", "7.1.0")
]),
};

test.ExpectedDiagnostics.AddRange(expected);

return test.RunAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ public static Task VerifyRefactoringAsync(string source, CancellationToken cance
{
TestCode = source,
FixedCode = fixedSource,
ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages(ImmutableArray.Create(
new PackageIdentity("Microsoft.Extensions.Hosting", "2.1.0"),
new PackageIdentity("Xunit.DependencyInjection", "7.1.0"))),
ReferenceAssemblies = ReferenceAssemblies.Default.AddPackages([
new("Microsoft.Extensions.Hosting", "2.1.0"),
new("Xunit.DependencyInjection", "7.1.0")
]),
};

test.ExpectedDiagnostics.AddRange(expected);

return test.RunAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static class CSharpVerifierHelper

private static ImmutableDictionary<string, ReportDiagnostic> GetNullableWarningsFromCompiler()
{
string[] args = { "/warnaserror:nullable" };
string[] args = ["/warnaserror:nullable"];
var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory);
var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Xunit.DependencyInjection.Test.CollectionFixture;
using Xunit.DependencyInjection.Test.CollectionFixture;

namespace Xunit.DependencyInjection.Test.ClassFixture;

public class AsyncLifetimeFixtureWithDisposableDependency : IAsyncLifetime
public class AsyncLifetimeFixtureWithDisposableDependency(
IDependencyWithManagedLifetime dependency,
CollectionFixtureWithDependency collectionFixtureWithDependency) : IAsyncLifetime
{
public AsyncLifetimeFixtureWithDisposableDependency(IDependencyWithManagedLifetime dependency, CollectionFixtureWithDependency _) =>
Dependency = dependency;
public IDependencyWithManagedLifetime Dependency { get; } = dependency;

public IDependencyWithManagedLifetime Dependency { get; }
public CollectionFixtureWithDependency CollectionFixtureWithDependency { get; } = collectionFixtureWithDependency;

public IList<string> Journal { get; } = new List<string>();

Expand Down
2 changes: 1 addition & 1 deletion test/Xunit.DependencyInjection.Test/SkippableFactTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class TestAwaitable
{
private bool _isCompleted;

private readonly List<Action> _onCompletedCallbacks = new();
private readonly List<Action> _onCompletedCallbacks = [];

// Simulate a brief delay before completion
public TestAwaitable() => ThreadPool.QueueUserWorkItem(_ =>
Expand Down

0 comments on commit 6cebe87

Please sign in to comment.