Skip to content

Commit

Permalink
Apply IDE0032 use auto-properties
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Adapter/MSTestAdapter.PlatformServices/Services/SettingsProvider.cs
  • Loading branch information
Evangelink committed Nov 15, 2024
1 parent 429fd76 commit c9ad687
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 138 deletions.
25 changes: 10 additions & 15 deletions src/Adapter/MSTest.TestAdapter/Execution/TestAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public class TestAssemblyInfo
{
private readonly object _assemblyInfoExecuteSyncObject;

private MethodInfo? _assemblyInitializeMethod;
private MethodInfo? _assemblyCleanupMethod;

/// <summary>
/// Initializes a new instance of the <see cref="TestAssemblyInfo"/> class.
/// </summary>
Expand All @@ -40,18 +37,17 @@ internal TestAssemblyInfo(Assembly assembly)
/// </summary>
public MethodInfo? AssemblyInitializeMethod
{
get => _assemblyInitializeMethod;

get;
internal set
{
if (_assemblyInitializeMethod != null)
if (field != null)
{
DebugEx.Assert(_assemblyInitializeMethod.DeclaringType?.FullName is not null, "AssemblyInitializeMethod.DeclaringType.FullName is null");
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorMultiAssemblyInit, _assemblyInitializeMethod.DeclaringType.FullName);
DebugEx.Assert(field.DeclaringType?.FullName is not null, "AssemblyInitializeMethod.DeclaringType.FullName is null");
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorMultiAssemblyInit, field.DeclaringType.FullName);
throw new TypeInspectionException(message);
}

_assemblyInitializeMethod = value;
field = value;
}
}

Expand All @@ -70,18 +66,17 @@ internal set
/// </summary>
public MethodInfo? AssemblyCleanupMethod
{
get => _assemblyCleanupMethod;

get;
internal set
{
if (_assemblyCleanupMethod != null)
if (field != null)
{
DebugEx.Assert(_assemblyCleanupMethod.DeclaringType?.FullName is not null, "AssemblyCleanupMethod.DeclaringType.FullName is null");
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorMultiAssemblyClean, _assemblyCleanupMethod.DeclaringType.FullName);
DebugEx.Assert(field.DeclaringType?.FullName is not null, "AssemblyCleanupMethod.DeclaringType.FullName is null");
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorMultiAssemblyClean, field.DeclaringType.FullName);
throw new TypeInspectionException(message);
}

_assemblyCleanupMethod = value;
field = value;
}
}

Expand Down
31 changes: 12 additions & 19 deletions src/Adapter/MSTest.TestAdapter/Execution/TestClassInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
public class TestClassInfo
{
private readonly object _testClassExecuteSyncObject = new();
private MethodInfo? _classCleanupMethod;
private MethodInfo? _classInitializeMethod;
private MethodInfo? _testCleanupMethod;
private MethodInfo? _testInitializeMethod;

/// <summary>
/// Initializes a new instance of the <see cref="TestClassInfo"/> class.
Expand Down Expand Up @@ -84,17 +80,16 @@ internal TestClassInfo(
/// </summary>
public MethodInfo? ClassInitializeMethod
{
get => _classInitializeMethod;

get;
internal set
{
if (_classInitializeMethod != null)
if (field != null)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorMultiClassInit, ClassType.FullName);
throw new TypeInspectionException(message);
}

_classInitializeMethod = value;
field = value;
}
}

Expand Down Expand Up @@ -157,17 +152,16 @@ internal set
/// </summary>
public MethodInfo? ClassCleanupMethod
{
get => _classCleanupMethod;

get;
internal set
{
if (_classCleanupMethod != null)
if (field != null)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorMultiClassClean, ClassType.FullName);
throw new TypeInspectionException(message);
}

_classCleanupMethod = value;
field = value;
}
}

Expand Down Expand Up @@ -200,17 +194,17 @@ public bool HasExecutableCleanupMethod
/// </summary>
public MethodInfo? TestInitializeMethod
{
get => _testInitializeMethod;
get;

internal set
{
if (_testInitializeMethod != null)
if (field != null)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorMultiInit, ClassType.FullName);
throw new TypeInspectionException(message);
}

_testInitializeMethod = value;
field = value;
}
}

Expand All @@ -219,17 +213,16 @@ internal set
/// </summary>
public MethodInfo? TestCleanupMethod
{
get => _testCleanupMethod;

get;
internal set
{
if (_testCleanupMethod != null)
if (field != null)
{
string message = string.Format(CultureInfo.CurrentCulture, Resource.UTA_ErrorMultiClean, ClassType.FullName);
throw new TypeInspectionException(message);
}

_testCleanupMethod = value;
field = value;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ public class TestRunCancellationToken
/// </summary>
private readonly ConcurrentBag<Action> _registeredCallbacks = new();

/// <summary>
/// Stores whether the test run is canceled or not.
/// </summary>
private bool _canceled;

public TestRunCancellationToken()
: this(CancellationToken.None)
{
Expand All @@ -35,12 +30,11 @@ public TestRunCancellationToken()
/// </summary>
public bool Canceled
{
get => _canceled;

get;
private set
{
bool previousValue = _canceled;
_canceled = value;
bool previousValue = field;
field = value;

if (!previousValue && value)
{
Expand Down
81 changes: 60 additions & 21 deletions src/Adapter/MSTest.TestAdapter/PlatformServiceProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics.CodeAnalysis;

#if !WINDOWS_UWP
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.SourceGeneration;
#endif
Expand All @@ -15,17 +17,6 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter;
/// </summary>
internal class PlatformServiceProvider : IPlatformServiceProvider
{
private static IPlatformServiceProvider? s_instance;

private ITestSource? _testSource;
private IFileOperations? _fileOperations;
private IAdapterTraceLogger? _traceLogger;
private ITestDeployment? _testDeployment;
private ISettingsProvider? _settingsProvider;
private ITestDataSource? _testDataSource;
private IThreadOperations? _threadOperations;
private IReflectionOperations2? _reflectionOperations;

/// <summary>
/// Initializes a new instance of the <see cref="PlatformServiceProvider"/> class - a singleton.
/// </summary>
Expand All @@ -43,58 +34,104 @@ private PlatformServiceProvider() =>
/// <summary>
/// Gets an instance to the platform service validator for test sources.
/// </summary>
public ITestSource TestSource => _testSource ??= new TestSource();
[field: AllowNull]
[field: MaybeNull]
public ITestSource TestSource
{
get => field ??= new TestSource();
private set;
}

/// <summary>
/// Gets an instance to the platform service validator for data sources for tests.
/// </summary>
public ITestDataSource TestDataSource => _testDataSource ??= new TestDataSource();
[field: AllowNull]
[field: MaybeNull]
public ITestDataSource TestDataSource
{
get => field ??= new TestDataSource();
private set;
}

/// <summary>
/// Gets an instance to the platform service for file operations.
/// </summary>
[field: AllowNull]
[field: MaybeNull]
public IFileOperations FileOperations
=> _fileOperations ??=
{
get => field ??=
#if !WINDOWS_UWP
SourceGeneratorToggle.UseSourceGenerator
? new SourceGeneratedFileOperations()
: new FileOperations();
#else
new FileOperations();
#endif
private set;
}

/// <summary>
/// Gets an instance to the platform service for trace logging.
/// </summary>
public IAdapterTraceLogger AdapterTraceLogger => _traceLogger ??= new AdapterTraceLogger();
[field: AllowNull]
[field: MaybeNull]
public IAdapterTraceLogger AdapterTraceLogger
{
get => field ??= new AdapterTraceLogger();
private set;
}

/// <summary>
/// Gets an instance of the test deployment service.
/// </summary>
public ITestDeployment TestDeployment => _testDeployment ??= new TestDeployment();
[field: AllowNull]
[field: MaybeNull]
public ITestDeployment TestDeployment
{
get => field ??= new TestDeployment();
private set;
}

/// <summary>
/// Gets an instance to the platform service for a Settings Provider.
/// </summary>
public ISettingsProvider SettingsProvider => _settingsProvider ??= new MSTestSettingsProvider();
[field: AllowNull]
[field: MaybeNull]
public ISettingsProvider SettingsProvider
{
get => field ??= new MSTestSettingsProvider();
private set;
}

/// <summary>
/// Gets an instance to the platform service for thread operations.
/// </summary>
public IThreadOperations ThreadOperations => _threadOperations ??= new ThreadOperations();
[field: AllowNull]
[field: MaybeNull]
public IThreadOperations ThreadOperations
{
get => field ??= new ThreadOperations();
private set;
}

/// <summary>
/// Gets an instance to the platform service for reflection operations specific to a platform.
/// </summary>
[field: AllowNull]
[field: MaybeNull]
public IReflectionOperations2 ReflectionOperations
=> _reflectionOperations ??=
{
get => field ??=
#if !WINDOWS_UWP
SourceGeneratorToggle.UseSourceGenerator
? new SourceGeneratedReflectionOperations()
: new ReflectionOperations2();
#else
new ReflectionOperations2();
#endif
private set;
}

/// <summary>
/// Gets or sets an instance to the platform service for cancellation token supporting cancellation of a test run.
Expand All @@ -104,10 +141,12 @@ public IReflectionOperations2 ReflectionOperations
/// <summary>
/// Gets or sets the instance for the platform service.
/// </summary>
[field: AllowNull]
[field: MaybeNull]
internal static IPlatformServiceProvider Instance
{
get => s_instance ??= new PlatformServiceProvider();
set => s_instance = value;
get => field ??= new PlatformServiceProvider();
set;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#if !WINDOWS_UWP
using System.Diagnostics.CodeAnalysis;
#endif
using System.Xml;

using Microsoft.Testing.Platform.Configurations;
Expand All @@ -14,28 +17,22 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
public class MSTestSettingsProvider : ISettingsProvider
{
#if !WINDOWS_UWP
/// <summary>
/// Member variable for Adapter settings.
/// </summary>
private static MSTestAdapterSettings? s_settings;

/// <summary>
/// Gets settings provided to the adapter.
/// </summary>
[field: AllowNull]
[field: MaybeNull]
[AllowNull]
public static MSTestAdapterSettings Settings
{
get
{
s_settings ??= new MSTestAdapterSettings();

return s_settings;
}
get => field ??= new MSTestAdapterSettings();
private set;
}

/// <summary>
/// Reset the settings to its default.
/// </summary>
public static void Reset() => s_settings = null;
public static void Reset() => Settings = null;
#endif

internal static void Load(IConfiguration configuration)
Expand All @@ -55,7 +52,7 @@ public void Load(XmlReader reader)
{
#if !WINDOWS_UWP
Guard.NotNull(reader);
s_settings = MSTestAdapterSettings.ToSettings(reader);
Settings = MSTestAdapterSettings.ToSettings(reader);
#endif
}

Expand Down
Loading

0 comments on commit c9ad687

Please sign in to comment.