Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,5 @@ public static class ModuleInitializer
/// Initializes the source generators.
/// </summary>
[ModuleInitializer]
public static void Init()
{
VerifySourceGenerators.Enable();
}
public static void Init() => VerifySourceGenerators.Enable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,9 @@ public RegisterLazySingletonTests(ITestOutputHelper testOutputHelper)
[InlineData(LazyThreadSafetyMode.None, "Test2")]
public Task ConstructionAndMultiplePropertyInjectionWithLazyMode(LazyThreadSafetyMode mode, string contract)
{
string arguments;
if (string.IsNullOrWhiteSpace(contract))
{
arguments = $"LazyThreadSafetyMode.{mode}";
}
else
{
arguments = $"\"{contract}\", LazyThreadSafetyMode.{mode}";
}
string arguments = string.IsNullOrWhiteSpace(contract) ?
$"LazyThreadSafetyMode.{mode}" :
$"\"{contract}\", LazyThreadSafetyMode.{mode}";

var source = @$"
using System;
Expand Down Expand Up @@ -90,15 +84,9 @@ public interface IServiceProperty3 {{ }}
[InlineData("Test2")]
public Task LazyParameterRegisteredLazy(string contract)
{
string arguments;
if (string.IsNullOrWhiteSpace(contract))
{
arguments = string.Empty;
}
else
{
arguments = $"\"{contract}\"";
}
string arguments = string.IsNullOrWhiteSpace(contract) ?
string.Empty :
$"\"{contract}\"";

var source = @$"
using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ internal static partial class SplatRegistrations
{
static partial void SetupIOCInternal( Splat.IDependencyResolver resolver)
{
Splat.Locator.CurrentMutable.Register(() => new global::Test.Service1(), typeof(global::Test.Service1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ internal static partial class SplatRegistrations
{
static partial void SetupIOCInternal( Splat.IDependencyResolver resolver)
{
Splat.Locator.CurrentMutable.Register(() => new global::Test.Service1(), typeof(global::Test.Service1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ internal static partial class SplatRegistrations
{
static partial void SetupIOCInternal( Splat.IDependencyResolver resolver)
{
Splat.Locator.CurrentMutable.Register(() => new global::Test.Service1(), typeof(global::Test.Service1));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
// ReactiveUI Association Incorporated licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;
using System.Threading.Tasks;

using VerifyXunit;

using Xunit;
using Xunit.Abstractions;

using static ICSharpCode.Decompiler.IL.Transforms.Stepper;

namespace Splat.DependencyInjection.SourceGenerator.Tests
{
[UsesVerify]
Expand All @@ -28,25 +25,13 @@ public RegisterTests(ITestOutputHelper testOutputHelper)
[InlineData("Test2")]
public Task LazyParameterConstantNotRegisteredLazyFail(string contract)
{
string arguments;
if (string.IsNullOrWhiteSpace(contract))
{
arguments = string.Empty;
}
else
{
arguments = $"\"{contract}\"";
}

string constantArguments;
if (string.IsNullOrWhiteSpace(contract))
{
constantArguments = "new Service1()";
}
else
{
constantArguments = $"new Service1(), \"{contract}\"";
}
string arguments = string.IsNullOrWhiteSpace(contract) ?
string.Empty :
$"\"{contract}\"";

string constantArguments = string.IsNullOrWhiteSpace(contract) ?
"new Service1()" :
$"new Service1(), \"{contract}\"";

var source = @$"
using System;
Expand Down Expand Up @@ -84,15 +69,9 @@ public class Service1 {{ }}
[InlineData("Test2")]
public Task LazyParameterNotRegisteredLazyFail(string contract)
{
string arguments;
if (string.IsNullOrWhiteSpace(contract))
{
arguments = string.Empty;
}
else
{
arguments = $"\"{contract}\"";
}
string arguments = string.IsNullOrWhiteSpace(contract) ?
string.Empty :
$"\"{contract}\"";

var source = @$"
using System;
Expand Down
11 changes: 4 additions & 7 deletions src/Splat.DependencyInjection.SourceGenerator.Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ protected TestBase(ITestOutputHelper testOutputHelper, string testMethod)
{
_testMethod = testMethod;

TestHelper = new TestHelper(testOutputHelper);
TestHelper = new(testOutputHelper);
}

protected TestHelper TestHelper { get; }

public Task InitializeAsync()
{
return TestHelper.InitializeAsync();
}
public Task InitializeAsync() => TestHelper.InitializeAsync();

public Task DisposeAsync()
{
TestHelper?.Dispose();
TestHelper.Dispose();
return Task.CompletedTask;
}

Expand Down Expand Up @@ -649,7 +646,7 @@ protected virtual void Dispose(bool isDisposing)
{
if (isDisposing)
{
TestHelper?.Dispose();
TestHelper.Dispose();
}
}
}
Expand Down
36 changes: 8 additions & 28 deletions src/Splat.DependencyInjection.SourceGenerator.Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
// See the LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand All @@ -23,24 +20,20 @@

using Xunit;
using Xunit.Abstractions;
using Xunit.Sdk;

namespace Splat.DependencyInjection.SourceGenerator.Tests
{
public class TestHelper : IDisposable
public sealed class TestHelper : IDisposable
{
#pragma warning disable CS0618 // Type or member is obsolete
private static readonly LibraryRange _splatLibrary = new("Splat", VersionRange.AllStableFloating, LibraryDependencyTarget.Package);
#pragma warning restore CS0618 // Type or member is obsolete

public TestHelper(ITestOutputHelper testOutput)
{
TestOutputHelper = testOutput ?? throw new ArgumentNullException(nameof(testOutput));
}
public TestHelper(ITestOutputHelper testOutput) => TestOutputHelper = testOutput ?? throw new ArgumentNullException(nameof(testOutput));

protected EventBuilderCompiler? EventCompiler { get; private set; }
private EventBuilderCompiler? EventCompiler { get; set; }

protected ITestOutputHelper TestOutputHelper { get; private set; }
private ITestOutputHelper TestOutputHelper { get; }

public async Task InitializeAsync()
{
Expand All @@ -67,16 +60,15 @@ public Task TestFail(string source, string contractParameter, [CallerFilePath] s

VerifySettings settings = new();
settings.UseParameters(contractParameter);
settings.AutoVerify();
return Verifier.Verify(driver, settings, sourceFile: file);
return Verifier.Verify(driver, settings, file);
}

public Task TestPass(string source, string contractParameter, [CallerFilePath] string file = "")
{
var driver = Generate(source);
VerifySettings settings = new();
settings.UseParameters(contractParameter);
return Verifier.Verify(driver, settings, sourceFile: file);
return Verifier.Verify(driver, settings, file);
}

public Task TestPass(string source, string contractParameter, LazyThreadSafetyMode mode, [CallerFilePath] string file = "")
Expand All @@ -85,22 +77,10 @@ public Task TestPass(string source, string contractParameter, LazyThreadSafetyMo

VerifySettings settings = new();
settings.UseParameters(contractParameter, mode);
return Verifier.Verify(driver, settings, sourceFile: file);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
return Verifier.Verify(driver, settings, file);
}

protected virtual void Dispose(bool isDisposing)
{
if (isDisposing)
{
EventCompiler?.Dispose();
}
}
public void Dispose() => EventCompiler?.Dispose();

private GeneratorDriver Generate(string source)
{
Expand Down
84 changes: 42 additions & 42 deletions src/Splat.DependencyInjection.SourceGenerator/DiagnosticWarnings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,59 +9,59 @@ namespace Splat.DependencyInjection.SourceGenerator
internal static class DiagnosticWarnings
{
internal static readonly DiagnosticDescriptor MultipleConstructorNeedAttribute = new(
id: "SPLATDI001",
title: "Can't find valid constructor",
messageFormat: "{0} has more than one constructor and one hasn't been marked with DependencyInjectionConstructorAttribute",
category: "Compiler",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
"SPLATDI001",
"Can't find valid constructor",
"{0} has more than one constructor and one hasn't been marked with DependencyInjectionConstructorAttribute",
"Compiler",
DiagnosticSeverity.Error,
true);

internal static readonly DiagnosticDescriptor PropertyMustPublicBeSettable = new(
id: "SPLATDI002",
title: "Property must be public/internal settable",
messageFormat: "{0} property marked with DependencyInjectionPropertyAttribute must have a public or internal setter",
category: "Compiler",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
"SPLATDI002",
"Property must be public/internal settable",
"{0} property marked with DependencyInjectionPropertyAttribute must have a public or internal setter",
"Compiler",
DiagnosticSeverity.Error,
true);

internal static readonly DiagnosticDescriptor MultipleConstructorsMarked = new(
id: "SPLATDI003",
title: "Multiple constructors have DependencyInjectionConstructorAttribute",
messageFormat: "{0} has multiple constructors marked with the DependencyInjectionConstructorAttribute attribute change so only one is marked",
category: "Compiler",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
"SPLATDI003",
"Multiple constructors have DependencyInjectionConstructorAttribute",
"{0} has multiple constructors marked with the DependencyInjectionConstructorAttribute attribute change so only one is marked",
"Compiler",
DiagnosticSeverity.Error,
true);

internal static readonly DiagnosticDescriptor ConstructorsMustBePublic = new(
id: "SPLATDI004",
title: "Constructors not public or internal",
messageFormat: "{0} constructor declared with DependencyInjectionConstructorAttribute attribute must be public or internal",
category: "Compiler",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
"SPLATDI004",
"Constructors not public or internal",
"{0} constructor declared with DependencyInjectionConstructorAttribute attribute must be public or internal",
"Compiler",
DiagnosticSeverity.Error,
true);

internal static readonly DiagnosticDescriptor ConstructorsMustNotHaveCircularDependency = new(
id: "SPLATDI005",
title: "Constructors must not have a circular dependency",
messageFormat: "Constructor parameters must not have a circular dependency to another registered resource",
category: "Compiler",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
"SPLATDI005",
"Constructors must not have a circular dependency",
"Constructor parameters must not have a circular dependency to another registered resource",
"Compiler",
DiagnosticSeverity.Error,
true);

internal static readonly DiagnosticDescriptor InterfaceRegisteredMultipleTimes = new(
id: "SPLATDI006",
title: "Interface has been registered before",
messageFormat: "{0} has been registered in multiple places",
category: "Compiler",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
"SPLATDI006",
"Interface has been registered before",
"{0} has been registered in multiple places",
"Compiler",
DiagnosticSeverity.Warning,
true);

internal static readonly DiagnosticDescriptor LazyParameterNotRegisteredLazy = new(
id: "SPLATDI007",
title: "Constructor has a lazy parameter",
messageFormat: "{0} constructor has a lazy parameter {1} which is not registered with RegisterLazySingleton",
category: "Compiler",
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
"SPLATDI007",
"Constructor has a lazy parameter",
"{0} constructor has a lazy parameter {1} which is not registered with RegisterLazySingleton",
"Compiler",
DiagnosticSeverity.Error,
true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected DependencyMetadata(ITypeSymbol type)
TypeName = type.ToDisplayString(RoslynCommonHelpers.TypeFormat);
}

public ITypeSymbol Type { get; init; }
public ITypeSymbol Type { get; }

public string TypeName { get; }
}
Expand Down
Loading