Skip to content

Commit

Permalink
Null objects task
Browse files Browse the repository at this point in the history
  • Loading branch information
kamendov-maxim committed May 30, 2024
1 parent 2e7b4c9 commit 9f5e5bc
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 0 deletions.
15 changes: 15 additions & 0 deletions NullObjects/NullObjects.Src/INullChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Zachet;

/// <summary>
/// Interface for null checker used in NullCounter
/// </summary>
/// <typeparam name="T">Type of which will be elements you check for null</typeparam>
public interface INullChecker<T>
{
/// <summary>
/// Check if element is null
/// </summary>
/// <param name="element">element to check for null</param>
/// <returns>True if element is null, false if not null</returns>
bool IsNull(T element);
}
17 changes: 17 additions & 0 deletions NullObjects/NullObjects.Src/IntNullChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Zachet;

/// <summary>
/// Null checker for ints which considers zeroes as nulls
/// </summary>
public class IntNullChecker : INullChecker<int>
{
/// <summary>
/// check if element is null
/// </summary>
/// <param name="value">element to check</param>
/// <returns></returns>
public bool IsNull(int value)
{
return value == 0;
}
}
27 changes: 27 additions & 0 deletions NullObjects/NullObjects.Src/NullCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Zachet;

/// <summary>
/// Class with method to count null objects in IEnumerable
/// </summary>
public static class NullCounter
{
/// <summary>
/// Count null objects int collection
/// </summary>
/// <typeparam name="T">Any type for which you have null checker</typeparam>
/// <param name="list">Collection implementing IEnumerable (to iterate through it)</param>
/// <param name="nullChecker">Object which can check if element of T type is null</param>
/// <returns>Count of null objects</returns>
public static int CountNullObjects<T>(IEnumerable<T> list, INullChecker<T> nullChecker)
{
int count = 0;
foreach (var item in list)
{
if (nullChecker.IsNull(item))
{
++count;
}
}
return count;
}
}
9 changes: 9 additions & 0 deletions NullObjects/NullObjects.Src/NullObjects.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

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

</Project>
28 changes: 28 additions & 0 deletions NullObjects/NullObjects.Tests/NullObjects.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<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="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NullObjects.Src\NullObjects.csproj" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions NullObjects/NullObjects.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace NullObjects.Tests;
using Zachet;

public class Tests
{
readonly List<int> emptyList = [];
readonly List<int> noNullObjectsList = [1, 2, 3, 4, 5, 6, 7, 8];
readonly List<int> listWithNulls = [1, 5, 0, 2, 4, 0, 0, 5, 2];
readonly IntNullChecker intNullChecker = new();

[Test]
public void EmptyListTest()
{
Assert.That(NullCounter.CountNullObjects<int>(emptyList, intNullChecker), Is.EqualTo(0));
}

[Test]
public void NoNullObjectsTest()
{
Assert.That(NullCounter.CountNullObjects<int>(noNullObjectsList, intNullChecker), Is.EqualTo(0));
}

[Test]
public void ListWithNullsTest()
{
Assert.That(NullCounter.CountNullObjects<int>(listWithNulls, intNullChecker), Is.EqualTo(3));
}
}
28 changes: 28 additions & 0 deletions NullObjects/NullObjects.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NullObjects", "NullObjects.Src\NullObjects.csproj", "{536D1FA1-4750-4984-9BB1-4339F97B77B0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NullObjects.Tests", "NullObjects.Tests\NullObjects.Tests.csproj", "{A627F83F-5227-4E1B-9251-247EE85157ED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{536D1FA1-4750-4984-9BB1-4339F97B77B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{536D1FA1-4750-4984-9BB1-4339F97B77B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{536D1FA1-4750-4984-9BB1-4339F97B77B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{536D1FA1-4750-4984-9BB1-4339F97B77B0}.Release|Any CPU.Build.0 = Release|Any CPU
{A627F83F-5227-4E1B-9251-247EE85157ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A627F83F-5227-4E1B-9251-247EE85157ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A627F83F-5227-4E1B-9251-247EE85157ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A627F83F-5227-4E1B-9251-247EE85157ED}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 9f5e5bc

Please sign in to comment.