Skip to content

Commit

Permalink
Add unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Feb 8, 2024
1 parent f359eae commit 2139a35
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
<ProjectReference Include="..\DesignPatterns.Visitor\DesignPatterns.Visitor.csproj" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>DesignPatterns.Visitor</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>DesignPatterns.AbstractFactory.UnitTests</RootNamespace>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
Expand Down Expand Up @@ -34,4 +33,4 @@
<ProjectReference Include="..\DesignPatterns.AbstractFactory\DesignPatterns.AbstractFactory.csproj" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>DesignPatterns.AbstractFactory</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand All @@ -19,4 +18,4 @@
</None>
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DesignPatterns.Singleton\DesignPatterns.Singleton.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DesignPatterns.Singleton\DesignPatterns.Singleton.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Shouldly;
using Xunit;

namespace DesignPatterns.Singleton.UnitTests;

public class SingletonWithLazyTests
{
[Fact]
public void Given_WhenCreateSingletonWithLazy_ThenSameInstancesReturned()
{
var firstInstance = SingletonWithLazy.Instance;
var secondInstance = SingletonWithLazy.Instance;

firstInstance.ShouldBe(secondInstance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Shouldly;
using Xunit;

namespace DesignPatterns.Singleton.UnitTests;

public class SingletonWithLockTests
{
[Fact]
public void Given_WhenCreateSingletonWithLock_ThenSameInstancesReturned()
{
var firstInstance = SingletonWithLock.Instance;
var secondInstance = SingletonWithLock.Instance;

firstInstance.ShouldBe(secondInstance);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions Creational/DesignPatterns.Singleton/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(ctx => ctx.AddJsonFile("appsettings.json"))
.ConfigureServices(services => { })
.Build();

Console.WriteLine();
4 changes: 2 additions & 2 deletions Creational/DesignPatterns.Singleton/SingletonWithLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class SingletonWithLock
{
private static SingletonWithLock? instance = null;
private static readonly object syncObject = new();
private static readonly object lockObj = new();

/// <summary>
/// The private constructor is declared to prevent instantiation directly, via calling a default constructor.
Expand All @@ -19,7 +19,7 @@ public static SingletonWithLock Instance
{
// The cost of executing the lock operation is significantly higher
// in comparison to the straightforward pointer check `instance != null`.
lock (syncObject)
lock (lockObj)
{
return instance ??= new SingletonWithLock();
}
Expand Down
7 changes: 7 additions & 0 deletions DesignPatterns.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Decorator",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Singleton", "Creational\DesignPatterns.Singleton\DesignPatterns.Singleton.csproj", "{658DB1C6-AC85-4E03-99C4-C79116E558F5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DesignPatterns.Singleton.UnitTests", "Creational\DesignPatterns.Singleton.UnitTests\DesignPatterns.Singleton.UnitTests.csproj", "{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -48,6 +50,10 @@ Global
{658DB1C6-AC85-4E03-99C4-C79116E558F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{658DB1C6-AC85-4E03-99C4-C79116E558F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{658DB1C6-AC85-4E03-99C4-C79116E558F5}.Release|Any CPU.Build.0 = Release|Any CPU
{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{DCE06EB6-BAB5-4573-AF67-128DBCCDB90C} = {7FF0E83F-EBEB-4103-BCD1-2F100E82FCD1}
Expand All @@ -56,5 +62,6 @@ Global
{1E56CC89-45C1-45F7-9EC8-88D648572816} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{FA3C4F09-104E-4F7D-9ADB-E7F22917ADF7} = {5961ADCC-5FA6-4076-A9F4-C1D3207ABE1A}
{658DB1C6-AC85-4E03-99C4-C79116E558F5} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
{FFB0861F-5182-40CA-BD19-33BCB0FBA4F6} = {AE005ED4-5F0B-4AF9-81AE-AAE3AD6F8901}
EndGlobalSection
EndGlobal

0 comments on commit 2139a35

Please sign in to comment.