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

Савельев Григорий #234

Open
wants to merge 6 commits 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
20 changes: 0 additions & 20 deletions cs/HomeExercises/HomeExercises.csproj

This file was deleted.

37 changes: 37 additions & 0 deletions cs/HomeExercises/HomeExercises.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HomeExercises", "HomeExercises\HomeExercises.csproj", "{217E7357-A6CA-4CBF-8C96-9F42B0EE4265}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Person.Tests", "Person.Tests\Person.Tests.csproj", "{CB57B3E5-40D3-46DC-8A72-4663207B0631}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NumberValidator.Tests", "NumberValidator.Tests\NumberValidator.Tests.csproj", "{28D5585A-51C7-4353-8F4F-F86887F0576E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{217E7357-A6CA-4CBF-8C96-9F42B0EE4265}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{217E7357-A6CA-4CBF-8C96-9F42B0EE4265}.Debug|Any CPU.Build.0 = Debug|Any CPU
{217E7357-A6CA-4CBF-8C96-9F42B0EE4265}.Release|Any CPU.ActiveCfg = Release|Any CPU
{217E7357-A6CA-4CBF-8C96-9F42B0EE4265}.Release|Any CPU.Build.0 = Release|Any CPU
{CB57B3E5-40D3-46DC-8A72-4663207B0631}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB57B3E5-40D3-46DC-8A72-4663207B0631}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB57B3E5-40D3-46DC-8A72-4663207B0631}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB57B3E5-40D3-46DC-8A72-4663207B0631}.Release|Any CPU.Build.0 = Release|Any CPU
{28D5585A-51C7-4353-8F4F-F86887F0576E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28D5585A-51C7-4353-8F4F-F86887F0576E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28D5585A-51C7-4353-8F4F-F86887F0576E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28D5585A-51C7-4353-8F4F-F86887F0576E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5E766B1C-5638-4495-AC79-F7F4EC79629C}
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions cs/HomeExercises/HomeExercises/HomeExercises.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<RootNamespace>HomeExercises</RootNamespace>
<AssemblyName>ObjectComparison</AssemblyName>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.0.0"/>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions cs/HomeExercises/HomeExercises/NumberValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Text.RegularExpressions;

namespace HomeExercises;

public class NumberValidator
{
private readonly Regex numberRegex;
private readonly bool onlyPositive;
private readonly int precision;
private readonly int scale;

public NumberValidator(int precision, int scale = 0, bool onlyPositive = false)
{
this.precision = precision;
this.scale = scale;
this.onlyPositive = onlyPositive;

if (precision <= 0)
throw new ArgumentException("precision must be a positive number");

if (scale < 0 || scale >= precision)
throw new ArgumentException("precision must be a non-negative number less or equal than precision");

numberRegex = new Regex(@"^([+-]?)(\d+)([.,](\d+))?$", RegexOptions.IgnoreCase);
}

public bool IsValidNumber(string value)
{
if (string.IsNullOrEmpty(value))
return false;

var match = numberRegex.Match(value);

if (!match.Success)
return false;

var intPart = match.Groups[1].Value.Length + match.Groups[2].Value.Length;
var fracPart = match.Groups[4].Value.Length;

if (intPart + fracPart > precision || fracPart > scale)
return false;

if (onlyPositive && match.Groups[1].Value == "-")
return false;

return true;
}
}
20 changes: 20 additions & 0 deletions cs/HomeExercises/HomeExercises/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace HomeExercises;

public class Person
{
public static int IdCounter;
public int Age, Height, Weight;
public int Id;
public string Name;
public Person? Parent;

public Person(string name, int age, int height, int weight, Person? parent)
{
Id = IdCounter++;
Name = name;
Age = age;
Height = height;
Weight = weight;
Parent = parent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Сравнение объектов

Изучите тест в классе ObjectComparison.
Затем изучите [документацию FluentAssertions](http://fluentassertions.com/documentation.html).
Затем изучите [документацию FluentAssertions](http://fluentassertions.com/documentation.html).

Перепишите тест с использованием наиболее подходящего метода FluentAssertions так чтобы:

Expand All @@ -17,7 +17,7 @@

Изучите код теста в классе NumberValidatorTests.

Перепишите тест так, чтобы
Перепишите тест так, чтобы

* найти и удалить повторяющиеся проверки,
* найти недостающие проверки,
Expand Down
11 changes: 11 additions & 0 deletions cs/HomeExercises/HomeExercises/TsarRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace HomeExercises;

public class TsarRegistry
{
public static Person GetCurrentTsar()
{
return new Person(
"Ivan IV The Terrible", 54, 170, 70,
new Person("Vasili III of Russia", 28, 170, 60, null));
}
}
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>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.0.0"/>
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
</ItemGroup>

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

</Project>
73 changes: 73 additions & 0 deletions cs/HomeExercises/NumberValidator.Tests/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using FluentAssertions;
using NUnit.Framework;

namespace NumberValidator.Tests;

[TestFixture]
public class NumberValidatorTests
{
[TestCase(-5, 5, TestName = "Precision < 0")]
[TestCase(0, 5, TestName = "Precision = 0")]
[TestCase(5, -5, TestName = "Scale < 0")]
[TestCase(5, 5, TestName = "Precision = Scale")]
[TestCase(5, 10, TestName = "Precision < Scale")]
public void Constructor_Should_ThrowException_When_ArgumentsIncorrect(int precision, int scale)
{
var action = new Action(() => _ = new HomeExercises.NumberValidator(precision, scale));
action.Should().Throw<ArgumentException>();
}

[TestCase(10, 5, false, TestName = "Precision > 0, Scale >= 0, Precision > Scale")]
public void Constructor_ShouldNot_ThrowException_When_ArgumentsAreCorrect(int precision, int scale,
bool onlyPositive)
{
var action = new Action(() => _ = new HomeExercises.NumberValidator(precision, scale, onlyPositive));
action.Should().NotThrow();
}

[TestCase(15, TestName = "Only precision is given")]
public void Constructor_ShouldNot_ThrowException_When_OneArgumentGiven(int precision)
{
var action = new Action(() => _ = new HomeExercises.NumberValidator(precision));
action.Should().NotThrow();
}

[TestCase(1, 0, true, "5", TestName = "No fractional part")]
[TestCase(3, 0, true, "+99", TestName = "Sign and no fractional part")]
[TestCase(4, 2, false, "-1.25", TestName = "Negative number")]
public void IsValid_Should_ReturnTrue_When_CorrectValuesGiven(int precision, int scale,
bool onlyPositive, string value)
{
var validator = new HomeExercises.NumberValidator(precision, scale, onlyPositive);
validator.IsValidNumber(value).Should().BeTrue();
}

[TestCase(4, 2, true, "+5,33", TestName = "Works with dot as separator")]
[TestCase(4, 2, true, "+5.33", TestName = "Works with comma as separator")]
public void IsValid_Should_Work_When_DotOrCommaAreSeparators(int precision, int scale,
bool onlyPositive, string value)
{
var validator = new HomeExercises.NumberValidator(precision, scale, onlyPositive);
validator.IsValidNumber(value).Should().BeTrue();
}

[TestCase(3, 2, true, "00.00", TestName = "Actual precision < expected")]
[TestCase(3, 2, true, "+0.00",
TestName = "Sign was not taken into account when forming precision value")]
[TestCase(17, 2, true, "0.000", TestName = "Actual scale < expected")]
[TestCase(3, 2, true, "a.sd", TestName = "Letters instead of digits")]
[TestCase(3, 2, true, "-1.25", TestName = "Negative number when (onlyPositive = true)")]
[TestCase(10, 5, true, "+", TestName = "No number given")]
[TestCase(10, 5, true, "+ 5, 956", TestName = "Spaces are forbidden")]
[TestCase(10, 5, true, "45!34", TestName = "Incorrect separator")]
[TestCase(10, 5, true, "++3.45", TestName = "Two signs")]
[TestCase(10, 5, true, "2,,66", TestName = "Two separators")]
[TestCase(10, 5, true, "", TestName = "Empty string as number")]
[TestCase(10, 5, true, null, TestName = "Null instead of string")]
public void IsValid_Should_ReturnFalse_When_IncorrectValuesGiven(int precision, int scale,
bool onlyPositive, string value)
{
var validator = new HomeExercises.NumberValidator(precision, scale, onlyPositive);
validator.IsValidNumber(value).Should().BeFalse();
}
}
80 changes: 0 additions & 80 deletions cs/HomeExercises/NumberValidatorTests.cs

This file was deleted.

Loading