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

Null objects task #16

Merged
merged 1 commit into from
Oct 10, 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
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Пространство имён должно совпадать с именем проекта


/// <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>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public interface INullChecker<T>
public interface INullChecker<in 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;
}
Comment on lines +13 to +16

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public bool IsNull(int value)
{
return value == 0;
}
public bool IsNull(int value)
=> 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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Без вариантности тут слишком жёсткое ограничение на типы

{
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>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StyleCop?

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));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestCaseData?

}
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