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

Add unit tests for Factory pattern #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions DesignPatternsDotNetCore.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29920.165
# Visual Studio Version 17
VisualStudioVersion = 17.3.32825.248
MinimumVisualStudioVersion = 15.0.26124.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdapterPattern", "AdapterPattern\AdapterPattern.csproj", "{79553F75-E8DC-4988-B511-A79CC6A9CDF7}"
EndProject
Expand Down Expand Up @@ -40,6 +40,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuilderPattern", "BuilderPa
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProxyPattern", "ProxyPattern\ProxyPattern.csproj", "{0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FactoryPattern.Tests", "FactoryPattern.Tests\FactoryPattern.Tests.csproj", "{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -278,6 +280,18 @@ Global
{0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|x64.Build.0 = Release|Any CPU
{0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|x86.ActiveCfg = Release|Any CPU
{0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|x86.Build.0 = Release|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Debug|x64.ActiveCfg = Debug|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Debug|x64.Build.0 = Debug|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Debug|x86.ActiveCfg = Debug|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Debug|x86.Build.0 = Debug|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Release|Any CPU.Build.0 = Release|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Release|x64.ActiveCfg = Release|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Release|x64.Build.0 = Release|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Release|x86.ActiveCfg = Release|Any CPU
{010CD3AB-55CD-479A-B4EC-AAA2D64A0EDC}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
92 changes: 92 additions & 0 deletions FactoryPattern.Tests/AbstractFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
namespace FactoryPattern.Tests
{
[TestFixture]
public class AbstractFactoryTests
{
[TestCaseSource(nameof(_cheeseCases))]
public void CreateCheese_CheeseFromIngredientsFactories_ReturnsCertainCheeseType(object ingredientsFactory, Type expected)
{
// Arrange, Act
var actual = ((IIngredientsFactory)ingredientsFactory).CreateCheese();

// Assert
Assert.That(actual, Is.InstanceOf(expected));
}

[TestCaseSource(nameof(_clamCases))]
public void CreateClam_ClamFromIngredientsFactories_ReturnsCertainClamType(object ingredientsFactory, Type expected)
{
// Arrange, Act
var actual = ((IIngredientsFactory)ingredientsFactory).CreateClam();

// Assert
Assert.That(actual, Is.InstanceOf(expected));
}

[TestCaseSource(nameof(_doughCases))]
public void CreateDough_DoughFromIngredientsFactories_ReturnsCertainDoughType(object ingredientsFactory, Type expected)
{
// Arrange, Act
var actual = ((IIngredientsFactory)ingredientsFactory).CreateDough();

// Assert
Assert.That(actual, Is.InstanceOf(expected));
}

[TestCaseSource(nameof(_sauceCases))]
public void CreateSauce_SauceFromIngredientsFactories_ReturnsCertainSauceType(object ingredientsFactory, Type expected)
{
// Arrange, Act
var actual = ((IIngredientsFactory)ingredientsFactory).CreateSauce();

// Assert
Assert.That(actual, Is.InstanceOf(expected));
}

[TestCaseSource(nameof(_veggiesCases))]
public void CreateVeggies_VeggiesFromIngredientsFactories_ReturnsCertainVeggiesSet(object ingredientsFactory, string[] expected)
{
// Arrange, Act
var veggies = ((IIngredientsFactory)ingredientsFactory).CreateVeggies();
var actual = veggies.Select(v => v.Name);

// Assert
Assert.That(actual, Is.EqualTo(expected));
}

private static readonly List<TestCaseData> _cheeseCases =
new(new[]
{
new TestCaseData(new NyIngredientsFactory(), typeof(Mozarella)),
new TestCaseData(new ChicagoIngredientsFactory(), typeof(Parmesan))
});

private static readonly List<TestCaseData> _clamCases =
new(new[]
{
new TestCaseData(new NyIngredientsFactory(), typeof(FrozenClam)),
new TestCaseData(new ChicagoIngredientsFactory(), typeof(FreshClam))
});

private static readonly List<TestCaseData> _doughCases =
new(new[]
{
new TestCaseData(new NyIngredientsFactory(), typeof(ThinCrust)),
new TestCaseData(new ChicagoIngredientsFactory(), typeof(DeepDish))
});

private static readonly List<TestCaseData> _sauceCases =
new(new[]
{
new TestCaseData(new NyIngredientsFactory(), typeof(CherryTomato)),
new TestCaseData(new ChicagoIngredientsFactory(), typeof(PlumTomato))
});

private static readonly List<TestCaseData> _veggiesCases =
new(new[]
{
new TestCaseData(new NyIngredientsFactory(), new string[] { "Onions", "Bell Peppers", "Olives" }),
new TestCaseData(new ChicagoIngredientsFactory(), new string[] { "Onions", "Cucumber", "Bell Peppers" })
});
}
}
36 changes: 36 additions & 0 deletions FactoryPattern.Tests/FactoryMethodTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace FactoryPattern.Tests
{
[TestFixture]
public class FactoryMethodTests
{
[TestCase(typeof(ClamPizza), "Clam")]
[TestCase(typeof(CheesePizza), "Cheese")]
[TestCase(typeof(VeggiePizza), "Veggie")]
public void Create_PizzaFromNyPizzaFactory_ReturnsPizzaOfCertainType(Type expected, string pizzaType)
{
// Arrange
var nyPizzaFactory = new NyPizzaFactory();

// Act
var actual = nyPizzaFactory.Order(pizzaType);

// Assert
Assert.That(actual, Is.InstanceOf(expected));
}

[TestCase(typeof(ClamPizza), "Clam")]
[TestCase(typeof(CheesePizza), "Cheese")]
[TestCase(typeof(VeggiePizza), "Veggie")]
public void Create_PizzaFromChicagoPizzaFactory_ReturnsPizzaOfCertainType(Type expected, string pizzaType)
{
// Arrange
var chicagoPizzaFactory = new ChicagoPizzaFactory();

// Act
var actual = chicagoPizzaFactory.Order(pizzaType);

// Assert
Assert.That(actual, Is.InstanceOf(expected));
}
}
}
23 changes: 23 additions & 0 deletions FactoryPattern.Tests/FactoryPattern.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FactoryPattern\FactoryPattern.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions FactoryPattern.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
3 changes: 3 additions & 0 deletions FactoryPattern/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("FactoryPattern.Tests")]