Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests #6

Merged
merged 1 commit into from
Apr 7, 2024
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

using System;

namespace VContainerAnalyzer.Test.TestData
{
public class ConstructorWithoutInjectAttributeClass : IInterface1, IInterface2, IInterface3
{
public ConstructorWithoutInjectAttributeClass(int x, int y)
{
Console.WriteLine("");
}
}
}
12 changes: 12 additions & 0 deletions VContainerAnalyzer.Test/TestData/DefaultConstructorClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

namespace VContainerAnalyzer.Test.TestData
{
public class DefaultConstructorClass
{
public DefaultConstructorClass()
{
}
}
}
17 changes: 17 additions & 0 deletions VContainerAnalyzer.Test/TestData/Interfaces.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

namespace VContainerAnalyzer.Test.TestData
{
public interface IInterface1
{
}

public interface IInterface2
{
}

public interface IInterface3
{
}
}
9 changes: 9 additions & 0 deletions VContainerAnalyzer.Test/TestData/NoConstructorClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

namespace VContainerAnalyzer.Test.TestData
{
public class NoConstructorClass : IInterface1
{
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

using VContainer.Unity;
using VContainer;

namespace VContainerAnalyzer.Test.TestData
{
// ReSharper disable once UnusedType.Global
public class FooLifetimeScope
public class RegisterConstructorWithoutInjectAttributeClassLifetimeScope
{
// ReSharper disable once UnusedMember.Global
public void Configure(IContainerBuilder builder)
{
builder.RegisterEntryPoint<ConstructorWithoutInjectAttributeClass>();
builder.RegisterEntryPoint<ConstructorWithoutInjectAttributeClass>(Lifetime.Singleton);
builder.Register<ConstructorWithoutInjectAttributeClass>(Lifetime.Singleton);
builder.Register<ConstructorWithoutInjectAttributeClass>(Lifetime.Singleton).As<IInterface1>();
builder.Register<IInterface1, ConstructorWithoutInjectAttributeClass>(Lifetime.Singleton);
builder.Register<IInterface1, IInterface2, ConstructorWithoutInjectAttributeClass>(Lifetime.Singleton);
builder.Register<IInterface1, IInterface2, IInterface3, ConstructorWithoutInjectAttributeClass>(
Lifetime.Singleton);
builder.Register<ConstructorWithoutInjectAttributeClass>(_ => new ConstructorWithoutInjectAttributeClass(),
Lifetime.Singleton);
builder.Register<ConstructorWithoutInjectAttributeClass>(Lifetime.Singleton).As<IInterface1>();
builder.RegisterEntryPoint<NoConstructorClass>();
builder.Register<IInterface4, NoConstructorClass>(Lifetime.Scoped);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

using VContainer.Unity;
using VContainer;

namespace VContainerAnalyzer.Test.TestData
{
// ReSharper disable once UnusedType.Global
public class RegisterEntryPointConstructorWithoutInjectAttributeClassLifetimeScope
{
// ReSharper disable once UnusedMember.Global
public void Configure(IContainerBuilder builder)
{
builder.RegisterEntryPoint<ConstructorWithoutInjectAttributeClass>();
builder.RegisterEntryPoint<ConstructorWithoutInjectAttributeClass>(Lifetime.Singleton);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

using VContainer;
using VContainer.Unity;

namespace VContainerAnalyzer.Test.TestData
{
public class RegisterEntryPointNoConstructorClassLifetimeScope
{
// ReSharper disable once UnusedMember.Global
public void Configure(IContainerBuilder builder)
{
builder.RegisterEntryPoint<NoConstructorClass>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2020-2024 VeyronSakai.
// This software is released under the MIT License.

using VContainer;

namespace VContainerAnalyzer.Test.TestData
{
public class RegisterNoConstructorClassLifetimeScope
{
// ReSharper disable once UnusedMember.Global
public void Configure(IContainerBuilder builder)
{
builder.Register<IInterface1, NoConstructorClass>(Lifetime.Scoped);
}
}
}
122 changes: 104 additions & 18 deletions VContainerAnalyzer.Test/VContainerAnalyzerTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2020-2023 VeyronSakai.
// This software is released under the MIT License.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -15,6 +16,28 @@ namespace VContainerAnalyzer.Test;
[TestFixture]
public class VContainerAnalyzerTest
{
private const string VContainerDirectory = "VContainer";

private static IEnumerable<string> VContainerSourcePaths { get; } =
GetVContainerFiles.Select(file => $"{Path.Combine(VContainerDirectory, file)}");

private static IEnumerable<string> GetVContainerFiles =>
[
"Container.cs",
"ContainerBuilder.cs",
"ContainerBuilderExtensions.cs",
"ContainerBuilderUnityExtensions.cs",
"RegistrationBuilder.cs"
];

private static string[] ReadCodes(params string[] sources)
{
const string TestDataDirPath = "../../../TestData";
return sources
.Concat(VContainerSourcePaths)
.Select(file => File.ReadAllText($"{TestDataDirPath}/{file}", Encoding.UTF8)).ToArray();
}

/// <summary>
/// Test analyze for empty source code
/// </summary>
Expand All @@ -29,16 +52,11 @@ public async Task EmptySourceCode_NoDiagnosticReport()
}

[Test]
public async ValueTask ConstructorWithoutInjectAttribute_ReportOneDiagnostic()
public async ValueTask AnalyzeRegisterEntryPointMethod_ConstructorDoesNotHaveInjectAttribute_ReportDiagnostics()
{
const string VContainerDirectory = "VContainer";
var source = ReadCodes("ConstructorWithoutInjectAttribute.cs",
$"{Path.Combine(VContainerDirectory, "Container.cs")}",
$"{Path.Combine(VContainerDirectory, "ContainerBuilder.cs")}",
$"{Path.Combine(VContainerDirectory, "ContainerBuilderExtensions.cs")}",
$"{Path.Combine(VContainerDirectory, "ContainerBuilderUnityExtensions.cs")}",
$"{Path.Combine(VContainerDirectory, "RegistrationBuilder.cs")}",
"FooLifetimeScope.cs");
var source = ReadCodes("ConstructorWithoutInjectAttributeClass.cs",
"Interfaces.cs",
"RegisterEntryPointConstructorWithoutInjectAttributeClassLifetimeScope.cs");

var analyzer = new VContainerAnalyzer();
var diagnostics = await DiagnosticAnalyzerRunner.Run(analyzer, source);
Expand All @@ -60,12 +78,6 @@ public async ValueTask ConstructorWithoutInjectAttribute_ReportOneDiagnostic()
{
new { Start = new LinePosition(14, 39), End = new LinePosition(14, 77) },
new { Start = new LinePosition(15, 39), End = new LinePosition(15, 77) },
new { Start = new LinePosition(16, 29), End = new LinePosition(16, 67) },
new { Start = new LinePosition(17, 42), End = new LinePosition(17, 80) },
new { Start = new LinePosition(18, 55), End = new LinePosition(18, 93) },
new { Start = new LinePosition(19, 68), End = new LinePosition(19, 106) },
new { Start = new LinePosition(21, 29), End = new LinePosition(21, 67) },
new { Start = new LinePosition(23, 29), End = new LinePosition(23, 67) },
};

Assert.That(actual, Has.Length.EqualTo(expectedPositions.Length));
Expand All @@ -80,9 +92,83 @@ public async ValueTask ConstructorWithoutInjectAttribute_ReportOneDiagnostic()
}
}

private static string[] ReadCodes(params string[] sources)
[Test]
public async ValueTask AnalyzeRegisterEntryPointMethod_ConstructorDoesNotExist_ReportNoDiagnostics()
{
var source = ReadCodes("NoConstructorClass.cs",
"Interfaces.cs",
"RegisterEntryPointNoConstructorClassLifetimeScope.cs");

var analyzer = new VContainerAnalyzer();
var diagnostics = await DiagnosticAnalyzerRunner.Run(analyzer, source);

var actual = diagnostics
.Where(x => x.Id != "CS1591") // Ignore "Missing XML comment for publicly visible type or member"
.Where(x => x.Id != "CS8019") // Ignore "Unnecessary using directive"
.ToArray();

Assert.That(actual.Length, Is.EqualTo(0));
}

[Test]
public async ValueTask AnalyzeRegisterMethod_ConstructorDoesNotHaveInjectAttribute_ReportDiagnostics()
{
var source = ReadCodes("ConstructorWithoutInjectAttributeClass.cs",
"Interfaces.cs",
"RegisterConstructorWithoutInjectAttributeClassLifetimeScope.cs");

var analyzer = new VContainerAnalyzer();
var diagnostics = await DiagnosticAnalyzerRunner.Run(analyzer, source);

var actual = diagnostics
.Where(x => x.Id != "CS1591") // Ignore "Missing XML comment for publicly visible type or member"
.Where(x => x.Id != "CS8019") // Ignore "Unnecessary using directive"
.ToArray();

Assert.Multiple(() =>
{
Assert.That(actual.First().Id, Is.EqualTo("VContainer0001"));
Assert.That(actual.First().GetMessage(),
Is.EqualTo(
"The constructor of 'ConstructorWithoutInjectAttributeClass' does not have InjectAttribute."));
});

var expectedPositions = new[]
{
new { Start = new LinePosition(12, 29), End = new LinePosition(12, 67) },
new { Start = new LinePosition(13, 29), End = new LinePosition(13, 67) },
new { Start = new LinePosition(14, 42), End = new LinePosition(14, 80) },
new { Start = new LinePosition(15, 55), End = new LinePosition(15, 93) },
new { Start = new LinePosition(16, 68), End = new LinePosition(16, 106) },
};

Assert.That(actual, Has.Length.EqualTo(expectedPositions.Length));

for (var i = 0; i < expectedPositions.Length; i++)
{
LocationAssert.HaveTheSpan(
expectedPositions[i].Start,
expectedPositions[i].End,
actual[i].Location
);
}
}

[Test]
public async ValueTask AnalyzeRegisterMethod_ConstructorDoesNotExist_ReportNoDiagnostics()
{
const string Path = "../../../TestData";
return sources.Select(file => File.ReadAllText($"{Path}/{file}", Encoding.UTF8)).ToArray();
var source = ReadCodes("NoConstructorClass.cs",
"Interfaces.cs",
"RegisterNoConstructorClassLifetimeScope.cs");

var analyzer = new VContainerAnalyzer();
var diagnostics = await DiagnosticAnalyzerRunner.Run(analyzer, source);

var actual = diagnostics
.Where(x => x.Id != "CS1591") // Ignore "Missing XML comment for publicly visible type or member"
.Where(x => x.Id != "CS8019") // Ignore "Unnecessary using directive"
.ToArray();

Assert.That(actual.Length, Is.EqualTo(0));
}
}