-
Notifications
You must be signed in to change notification settings - Fork 274
Горбатов Александр #227
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
Open
SilversShade
wants to merge
5
commits into
kontur-courses:master
Choose a base branch
from
SilversShade:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Горбатов Александр #227
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a6ccffc
ObjectComparison.CheckCurrentTsar test rewritten to use FluentAssertions
e521a3c
NumberValidatorTests refactored
f9d2b34
refactor
29addbf
ObjectComparisonTests.CheckCurrentTsar method refactored
8a56f80
ObjectComparisonTests.CheckCurrentTsar refactored
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| global using NUnit.Framework; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net7.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.1"/> | ||
| <PackageReference Include="NUnit" Version="3.13.3"/> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.4.2"/> | ||
| <PackageReference Include="NUnit.Analyzers" Version="3.6.1"/> | ||
| <PackageReference Include="coverlet.collector" Version="3.2.0"/> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\HomeExercises\HomeExercises.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| using FluentAssertions; | ||
| using HomeExercises; | ||
|
|
||
| namespace HomeExercisesTests | ||
| { | ||
| public class NumberValidatorTests | ||
| { | ||
| [TestCase(-1, 0, Description = "Precision should be a positive number")] | ||
| [TestCase(1, -1, Description = "Scale must be a non-negative number")] | ||
| [TestCase(1, 2, Description = "Scale must be less than precision")] | ||
| public void Constructor_ThrowsArgumentException_OnIncorrectInput(int precision, int scale) | ||
| { | ||
| Assert.Throws<ArgumentException>(() => new NumberValidator(precision, scale, true)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Constructor_SuccessfullyCreatesObject_OnCorrectInput() | ||
| { | ||
| Assert.DoesNotThrow(() => new NumberValidator(1, 0, true)); | ||
| } | ||
|
|
||
| [TestCase(null)] | ||
| [TestCase(" ")] | ||
| [TestCase(" ")] | ||
| [TestCase("")] | ||
| [TestCase("\n")] | ||
| public void IsValidNumber_ReturnsFalse_WhenValueIsNullOrEmpty(string value) | ||
| { | ||
| var numberValidator = new NumberValidator(1, 0, true); | ||
|
|
||
| numberValidator.IsValidNumber(value).Should().BeFalse(); | ||
| } | ||
|
|
||
| [TestCase(".")] | ||
| [TestCase("a.sd")] | ||
| [TestCase("+-5")] | ||
| [TestCase("1..3")] | ||
| [TestCase("1.")] | ||
| [TestCase(".3")] | ||
| public void IsValidNumber_ReturnsFalse_WhenValueIsNaN(string value) | ||
| { | ||
| var numberValidator = new NumberValidator(2, 1, true); | ||
|
|
||
| numberValidator.IsValidNumber(value).Should().BeFalse(); | ||
| } | ||
|
|
||
| [TestCase("00.00", 3, 2)] | ||
| [TestCase("+1.00", 3, 2)] | ||
| [TestCase("-1.00", 3, 2)] | ||
| [TestCase("-1", 1, 0)] | ||
| public void IsValidNumber_ReturnsFalse_WhenIntAndFracPartsAreGreaterThanPrecision(string value, int precision, | ||
| int scale) | ||
| { | ||
| var numberValidator = new NumberValidator(precision, scale); | ||
|
|
||
| numberValidator.IsValidNumber(value).Should().BeFalse(); | ||
| } | ||
|
|
||
| [TestCase("0.00", 3, 1)] | ||
| [TestCase("-123.12", 6, 0)] | ||
| public void IsValidNumber_ReturnsFalse_WhenFracPartIsGreaterThanScale(string value, int precision, int scale) | ||
| { | ||
| var numberValidator = new NumberValidator(precision, scale); | ||
|
|
||
| numberValidator.IsValidNumber(value).Should().BeFalse(); | ||
| } | ||
|
|
||
| [TestCase("-1.2", 3, 1)] | ||
| [TestCase("-0.00", 4, 2)] | ||
| public void IsValidNumber_ReturnsFalse_WhenValueIsNegativeButOnlyPositiveIsTrue(string value, int precision, | ||
| int scale) | ||
| { | ||
| var numberValidator = new NumberValidator(precision, scale, true); | ||
|
|
||
| numberValidator.IsValidNumber(value).Should().BeFalse(); | ||
| } | ||
|
|
||
| [TestCase("0.0", 17, 2, true)] | ||
| [TestCase("0.0", 2, 1, false)] | ||
| [TestCase("+1.23", 4, 2, true)] | ||
| [TestCase("-1.23", 4, 2, false)] | ||
| [TestCase("0,1", 2, 1, true)] | ||
| [TestCase("0", 1, 0, true)] | ||
| public void IsValidNumber_ReturnsTrue_OnCorrectInputData(string value, int precision, int scale, | ||
| bool onlyPositive) | ||
| { | ||
| var numberValidator = new NumberValidator(precision, scale, onlyPositive); | ||
|
|
||
| numberValidator.IsValidNumber(value).Should().BeTrue(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| using FluentAssertions; | ||
| using HomeExercises; | ||
|
|
||
| namespace HomeExercisesTests | ||
| { | ||
| public class ObjectComparisonTests | ||
| { | ||
| [Test] | ||
| [Description("Проверка текущего царя")] | ||
| [Category("ToRefactor")] | ||
| public void CheckCurrentTsar() | ||
| { | ||
| var actualTsar = TsarRegistry.GetCurrentTsar(); | ||
|
|
||
| var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, | ||
| new Person("Vasili III of Russia", 28, 170, 60, null)); | ||
|
|
||
| /* | ||
| * Чем это решение лучше решения в CheckCurrentTsar_WithCustomEquality: | ||
| * 1. Это решение не нужно будет переписывать при добавлении/удалении в класс/из класса Person полей. | ||
| * 2. В этом решении наглядно видно, какое именно поле не учитывается при сравнении объектов. | ||
| * 3. При падении теста мы увидим, какие именно поля объектов не прошли проверку на равенство. | ||
| * 4. Это решение игнорирует циклические ссылки, поэтому этот тест не выбросит StackOverflowException | ||
| * при циклических зависимостях. | ||
| */ | ||
| actualTsar | ||
| .Should() | ||
| .BeEquivalentTo(expectedTsar, options => options | ||
| .Excluding(info => | ||
| info.SelectedMemberInfo.Name.Equals(nameof(Person.Id)) | ||
| && info.SelectedMemberInfo.DeclaringType == typeof(Person)) | ||
| .IgnoringCyclicReferences() | ||
| .AllowingInfiniteRecursion()); | ||
| } | ||
|
|
||
| [Test] | ||
| [Description("Альтернативное решение. Какие у него недостатки?")] | ||
| public void CheckCurrentTsar_WithCustomEquality() | ||
| { | ||
| var actualTsar = TsarRegistry.GetCurrentTsar(); | ||
| var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, | ||
| new Person("Vasili III of Russia", 28, 170, 60, null)); | ||
|
|
||
| // Какие недостатки у такого подхода? | ||
|
|
||
| /* | ||
| * 1. Если мы решим изменить класс Person (например, добавить или удалить какие-то поля), | ||
| * то тогда придется переписывать метод AreEqual | ||
| * 2. Если тест не пройдет, то мы не увидим, какие именно поля у двух объектов не совпали. | ||
| * Нам просто выдаст сообщение 'Expected: True, But was: False' | ||
LevShisterov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * 3. Если передать зацикленного царя, тест выбросит Stack Overflow Exception. | ||
| */ | ||
|
|
||
| Assert.True(AreEqual(actualTsar, expectedTsar)); | ||
| } | ||
|
|
||
| private static bool AreEqual(Person? actual, Person? expected) | ||
| { | ||
| if (actual == expected) return true; | ||
| if (actual == null || expected == null) return false; | ||
| return | ||
| actual.Name == expected.Name | ||
| && actual.Age == expected.Age | ||
| && actual.Height == expected.Height | ||
| && actual.Weight == expected.Weight | ||
| && AreEqual(actual.Parent, expected.Parent); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.