-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
/// <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); | ||||||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. StyleCop? |
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> |
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)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TestCaseData? |
||
} |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Пространство имён должно совпадать с именем проекта