From c7c30a00f41b58f80b6c1df05384a36fc7536fc0 Mon Sep 17 00:00:00 2001 From: vahid Date: Fri, 19 Jan 2024 15:51:43 +0330 Subject: [PATCH] unit tests added --- .../Jinget.Core.DiScanner.csproj | 8 + 01-Core/Jinget.Core.DiScanner/README.md | 74 +++++ Jinget.sln | 7 + .../DiScannerTest.cs | 307 ++++++++++++++++++ .../GlobalUsings.cs | 7 + .../Jinget.Core.DiScanner.Tests.csproj | 24 ++ .../SampleTypes/Contracts/ICustomInterface.cs | 6 + .../SampleTypes/Sample.cs | 8 + 8 files changed, 441 insertions(+) create mode 100644 01-Core/Jinget.Core.DiScanner/README.md create mode 100644 Tests/Jinget.Core.DiScanner.Tests/DiScannerTest.cs create mode 100644 Tests/Jinget.Core.DiScanner.Tests/GlobalUsings.cs create mode 100644 Tests/Jinget.Core.DiScanner.Tests/Jinget.Core.DiScanner.Tests.csproj create mode 100644 Tests/Jinget.Core.DiScanner.Tests/SampleTypes/Contracts/ICustomInterface.cs create mode 100644 Tests/Jinget.Core.DiScanner.Tests/SampleTypes/Sample.cs diff --git a/01-Core/Jinget.Core.DiScanner/Jinget.Core.DiScanner.csproj b/01-Core/Jinget.Core.DiScanner/Jinget.Core.DiScanner.csproj index a924a39..730ccbc 100644 --- a/01-Core/Jinget.Core.DiScanner/Jinget.Core.DiScanner.csproj +++ b/01-Core/Jinget.Core.DiScanner/Jinget.Core.DiScanner.csproj @@ -15,6 +15,7 @@ Jinget Core https://github.com/VahidFarahmandian/Jinget jinget, di, dependency injection, asp.net core, .net core, .net + README.md @@ -30,4 +31,11 @@ + + + \ + True + + + diff --git a/01-Core/Jinget.Core.DiScanner/README.md b/01-Core/Jinget.Core.DiScanner/README.md new file mode 100644 index 0000000..0721a33 --- /dev/null +++ b/01-Core/Jinget.Core.DiScanner/README.md @@ -0,0 +1,74 @@ +# Jinget.Core.DiScanner +Using this library, you can easily register your source code dependencies automatically without the need to write repetitive and annoying codes. + + +## How to Use: + +Download the package from NuGet using Package Manager: +`Install-Package Jinget.Core.DiScanner` +You can also use other methods supported by NuGet. Check [Here](https://www.nuget.org/packages/Jinget.Core.DiScanner"Here") for more information. + +## Samples + +***FromCallingAssembly:*** + +Will scan for types from the calling assembly. + +```csharp +_services.Scan( + s => s.FromCallingAssembly() + .AddClasses() + .AsImplementedInterfaces() + .WithTransientLifetime()); +``` + +The above code will scan the calling assembly and registers each matching concrete type as all of its implemented interfaces. `WithTransientLifetime` indicates that the services should have `Transient` lifetime. + +***FromExecutingAssembly:*** + +Will scan for types from the calling assembly. + +```csharp +_services.Scan( + s => s.FromExecutingAssembly() + .AddClasses() + .AsImplementedInterfaces() + .WithTransientLifetime()); +``` + +The above code will scan for types from the currently executing assembly, and registers each matching concrete type as all of its implemented interfaces. + +***FromAssembliesOf:*** + +```csharp +_services.Scan( + s => s.FromAssembliesOf(typeof(Sample)) + .AddClasses() + .AsSelfWithInterfaces() + .WithSingletonLifetime()); +``` + +The above code will scan the assembly containing the `Sample` type and registers each matching concrete type as all of its implemented interfaces, by returning an instance of the main type + +***FromAssemblies:*** + +```csharp +_services.Scan( + s => s.FromAssemblies(typeof(ICustomInterface).Assembly, typeof(ISelector).Assembly) + .AddClasses() + .AsImplementedInterfaces() + .WithTransientLifetime()); +``` + +The above code will scan for types in each assembly in assemblies, and registers each matching concrete type as all of its implemented interfaces, by returning an instance of the main type + +------------ +## How to install +In order to install Jinget.Core.DiScanner please refer to [nuget.org](https://www.nuget.org/packages/Jinget.Core.DiScanner "nuget.org") + +## Contact Me +👨‍💻 Twitter: https://twitter.com/_jinget + +📧 Email: farahmandian2011@gmail.com + +📣 Instagram: https://www.instagram.com/vahidfarahmandian diff --git a/Jinget.sln b/Jinget.sln index 9ef2ff0..551e571 100644 --- a/Jinget.sln +++ b/Jinget.sln @@ -36,6 +36,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jinget.Blazor.Test", "Tests EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Jinget.Core.DiScanner", "01-Core\Jinget.Core.DiScanner\Jinget.Core.DiScanner.csproj", "{9411D2C7-AF8E-4C8A-A11E-DA52E3BEB6C9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jinget.Core.DiScanner.Tests", "Tests\Jinget.Core.DiScanner.Tests\Jinget.Core.DiScanner.Tests.csproj", "{F464F7DD-38F2-412C-9CD1-28D53853BFE8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -78,6 +80,10 @@ Global {9411D2C7-AF8E-4C8A-A11E-DA52E3BEB6C9}.Debug|Any CPU.Build.0 = Debug|Any CPU {9411D2C7-AF8E-4C8A-A11E-DA52E3BEB6C9}.Release|Any CPU.ActiveCfg = Release|Any CPU {9411D2C7-AF8E-4C8A-A11E-DA52E3BEB6C9}.Release|Any CPU.Build.0 = Release|Any CPU + {F464F7DD-38F2-412C-9CD1-28D53853BFE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F464F7DD-38F2-412C-9CD1-28D53853BFE8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F464F7DD-38F2-412C-9CD1-28D53853BFE8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F464F7DD-38F2-412C-9CD1-28D53853BFE8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -92,6 +98,7 @@ Global {A3E2F74D-AEB2-4D7A-8B75-F8198BF5FBE0} = {18874D4F-0257-4DF2-A97F-F532A36BAD0E} {4BFE23D5-F786-4349-9481-A7D4A884CBEC} = {F1B06C67-988D-4E24-8110-92C9B6D4CBC0} {9411D2C7-AF8E-4C8A-A11E-DA52E3BEB6C9} = {115DD383-ACB4-48AA-A6C2-C6105314F051} + {F464F7DD-38F2-412C-9CD1-28D53853BFE8} = {F1B06C67-988D-4E24-8110-92C9B6D4CBC0} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {8690B4B4-1E8A-4C1D-9A63-123EAD0492EE} diff --git a/Tests/Jinget.Core.DiScanner.Tests/DiScannerTest.cs b/Tests/Jinget.Core.DiScanner.Tests/DiScannerTest.cs new file mode 100644 index 0000000..7ee40b2 --- /dev/null +++ b/Tests/Jinget.Core.DiScanner.Tests/DiScannerTest.cs @@ -0,0 +1,307 @@ +namespace Jinget.Core.DiScanner.Tests +{ + [TestClass] + public class DiScannerTest + { + private ServiceCollection? _services; + [TestInitialize] + public void Initialize() + { + _services = []; + } + + #region using assembly + + [TestMethod] + public void Should_register_and_resolve_transient_di_using_calling_assembly() + { + _services.RemoveAll(typeof(ICustomInterface)); + _services.Scan( + s => s.FromCallingAssembly() + .AddClasses() + .AsImplementedInterfaces() + .WithTransientLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_scoped_di_using_calling_assembly() + { + _services.RemoveAll(typeof(ICustomInterface)); + _services.Scan( + s => s.FromCallingAssembly() + .AddClasses() + .AsImplementedInterfaces() + .WithScopedLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_singleton_di_using_calling_assembly() + { + _services.RemoveAll(typeof(ICustomInterface)); + _services.Scan( + s => s.FromCallingAssembly() + .AddClasses() + .AsImplementedInterfaces() + .WithSingletonLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_transient_di_using_executing_assembly() + { + _services.RemoveAll(typeof(ISelector)); + _services.Scan( + s => s.FromExecutingAssembly() + .AddClasses() + .AsImplementedInterfaces() + .WithTransientLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_scoped_di_using_executing_assembly() + { + _services.RemoveAll(typeof(ISelector)); + _services.Scan( + s => s.FromExecutingAssembly() + .AddClasses() + .AsImplementedInterfaces() + .WithScopedLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_singleton_di_using_executing_assembly() + { + _services.RemoveAll(typeof(ISelector)); + _services.Scan( + s => s.FromExecutingAssembly() + .AddClasses() + .AsImplementedInterfaces() + .WithSingletonLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_transient_di_using_from_assemblies() + { + _services.RemoveAll(typeof(ISelector)); + _services.RemoveAll(typeof(ICustomInterface)); + + _services.Scan( + s => s.FromAssemblies(typeof(ICustomInterface).Assembly, typeof(ISelector).Assembly) + .AddClasses() + .AsImplementedInterfaces() + .WithTransientLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService1 = provider.GetRequiredService(); + var resolvedService2 = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService1); + Assert.IsNotNull(resolvedService2); + } + + [TestMethod] + public void Should_register_and_resolve_scoped_di_using_from_assemblies() + { + _services.RemoveAll(typeof(ISelector)); + _services.RemoveAll(typeof(ICustomInterface)); + + _services.Scan( + s => s.FromAssemblies(typeof(ICustomInterface).Assembly, typeof(ISelector).Assembly).AddClasses() + .AsImplementedInterfaces() + .WithScopedLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService1 = provider.GetRequiredService(); + var resolvedService2 = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService1); + Assert.IsNotNull(resolvedService2); + } + + [TestMethod] + public void Should_register_and_resolve_singleton_di_using_from_assemblies() + { + _services.RemoveAll(typeof(ISelector)); + _services.RemoveAll(typeof(ICustomInterface)); + + _services.Scan( + s => s.FromAssemblies(typeof(ICustomInterface).Assembly, typeof(ISelector).Assembly) + .AddClasses() + .AsImplementedInterfaces() + .WithSingletonLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService1 = provider.GetRequiredService(); + var resolvedService2 = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService1); + Assert.IsNotNull(resolvedService2); + } + + [TestMethod] + public void Should_register_and_resolve_transient_di_using_from_assembly() + { + _services.RemoveAll(typeof(ISelector)); + _services.Scan( + s => s.FromAssembliesOf(typeof(TypeSourceSelector)) + .AddClasses() + .AsImplementedInterfaces() + .WithTransientLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_scoped_di_using_from_assembly() + { + _services.RemoveAll(typeof(ISelector)); + _services.Scan( + s => s.FromAssembliesOf(typeof(TypeSourceSelector)) + .AddClasses() + .AsImplementedInterfaces() + .WithScopedLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_singleton_di_using_from_assembly() + { + _services.RemoveAll(typeof(ISelector)); + _services.Scan( + s => s.FromAssembliesOf(typeof(TypeSourceSelector)) + .AddClasses() + .AsImplementedInterfaces() + .WithSingletonLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + #endregion + + #region as self + + [TestMethod] + public void Should_register_and_resolve_di_as_self() + { + _services.RemoveAll(typeof(Sample)); + _services.Scan( + s => s.FromAssembliesOf(typeof(Sample)) + .AddClasses() + .AsSelf() + .WithSingletonLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_di_as_custom() + { + _services.RemoveAll(typeof(ICustomInterface)); + _services.Scan( + s => s.FromAssembliesOf(typeof(Sample)) + .AddClasses(x => x.AssignableTo(typeof(ICustomInterface))) + .As() + .WithSingletonLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_di_as_self_and_impl_interfaces() + { + _services.RemoveAll(typeof(ICustomInterface)); + _services.RemoveAll(typeof(Sample)); + _services.Scan( + s => s.FromAssembliesOf(typeof(Sample)) + .AddClasses() + .AsSelfWithInterfaces() + .WithSingletonLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService1 = provider.GetRequiredService(); + var resolvedService2 = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService1); + Assert.IsNotNull(resolvedService2); + } + + #endregion + + [TestMethod] + public void Should_register_and_resolve_di__using_addtype() + { + _services.RemoveAll(typeof(Sample)); + _services.Scan( + s => s.AddType() + .AsImplementedInterfaces() + .WithSingletonLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService); + } + + [TestMethod] + public void Should_register_and_resolve_di__using_addtypes() + { + _services.RemoveAll(typeof(Sample)); + _services.Scan( + s => s.AddTypes(typeof(Sample), typeof(TypeSourceSelector)) + .AsImplementedInterfaces() + .WithSingletonLifetime()); + + var provider = _services.BuildServiceProvider(); + var resolvedService1 = provider.GetRequiredService(); + var resolvedService2 = provider.GetRequiredService(); + + Assert.IsNotNull(resolvedService1); + Assert.IsNotNull(resolvedService2); + } + } +} \ No newline at end of file diff --git a/Tests/Jinget.Core.DiScanner.Tests/GlobalUsings.cs b/Tests/Jinget.Core.DiScanner.Tests/GlobalUsings.cs new file mode 100644 index 0000000..349345f --- /dev/null +++ b/Tests/Jinget.Core.DiScanner.Tests/GlobalUsings.cs @@ -0,0 +1,7 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.DependencyInjection.Extensions; + +global using Jinget.Core.DiScanner.Contracts; +global using Jinget.Core.DiScanner.Tests.SampleTypes; +global using Jinget.Core.DiScanner.Tests.SampleTypes.Contracts; \ No newline at end of file diff --git a/Tests/Jinget.Core.DiScanner.Tests/Jinget.Core.DiScanner.Tests.csproj b/Tests/Jinget.Core.DiScanner.Tests/Jinget.Core.DiScanner.Tests.csproj new file mode 100644 index 0000000..d25bf4f --- /dev/null +++ b/Tests/Jinget.Core.DiScanner.Tests/Jinget.Core.DiScanner.Tests.csproj @@ -0,0 +1,24 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + diff --git a/Tests/Jinget.Core.DiScanner.Tests/SampleTypes/Contracts/ICustomInterface.cs b/Tests/Jinget.Core.DiScanner.Tests/SampleTypes/Contracts/ICustomInterface.cs new file mode 100644 index 0000000..9f042b7 --- /dev/null +++ b/Tests/Jinget.Core.DiScanner.Tests/SampleTypes/Contracts/ICustomInterface.cs @@ -0,0 +1,6 @@ +namespace Jinget.Core.DiScanner.Tests.SampleTypes.Contracts +{ + public interface ICustomInterface + { + } +} diff --git a/Tests/Jinget.Core.DiScanner.Tests/SampleTypes/Sample.cs b/Tests/Jinget.Core.DiScanner.Tests/SampleTypes/Sample.cs new file mode 100644 index 0000000..cc76f81 --- /dev/null +++ b/Tests/Jinget.Core.DiScanner.Tests/SampleTypes/Sample.cs @@ -0,0 +1,8 @@ +using Jinget.Core.DiScanner.Tests.SampleTypes.Contracts; + +namespace Jinget.Core.DiScanner.Tests.SampleTypes +{ + public class Sample : ICustomInterface + { + } +}