-
Notifications
You must be signed in to change notification settings - Fork 282
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
Трофимов Никита #239
Open
WoeromProg
wants to merge
4
commits into
kontur-courses:master
Choose a base branch
from
WoeromProg: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
Трофимов Никита #239
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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,45 @@ | ||
using System; | ||
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; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 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,56 @@ | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using System; | ||
|
||
namespace HomeExercises.tests | ||
{ | ||
public class NumberValidator_Should | ||
{ | ||
[TestCase(-1, 2, false, TestName = "NegativePrecision")] | ||
[TestCase(0, 0, false, TestName = "PrecisionIsZero")] | ||
[TestCase(3, -1, false, TestName = "NegativeScale")] | ||
[TestCase(1, 2, false, TestName = "ScaleIsGreaterThanPrecision")] | ||
[TestCase(1, 1, true, TestName = "PrecisionEqualsScale")] | ||
public void Creation_ShouldThrowArgumentException(int precision, int scale, bool onlyPositive) | ||
{ | ||
Action creation = () => new NumberValidator(precision, scale, onlyPositive); | ||
creation.Should().Throw<ArgumentException>(); | ||
} | ||
[Test] | ||
public void Creation_ShouldNotThrow() | ||
{ | ||
Action creation = () => new NumberValidator(1, 0); | ||
creation | ||
.Should() | ||
.NotThrow<Exception>(); | ||
|
||
} | ||
|
||
[TestCase(3, 2, false, "-+0.0", TestName = "TwoSigns")] | ||
[TestCase(3, 2, false, " -0.0", TestName = "SpaceFirstDigitInNumber")] | ||
[TestCase(3, 2, false, "0,", TestName ="Aren'tDigitsAfterCommas")] | ||
[TestCase(3, 2, false, "0.0.0", TestName = "ThreeDigitsInNumberByTwoDots")] | ||
[TestCase(3, 2, false, "0,0,0", TestName = "ThreeDigitsInNumberByTwoCommas")] | ||
[TestCase(3, 2, false, "00.00", TestName = "NumberPrecisionGreaterThanValidatorPrecision")] | ||
[TestCase(3, 2, false, null, TestName = "NumberNull")] | ||
[TestCase(3, 2, false, "", TestName = "NumberEmpty")] | ||
[TestCase(3, 2, false, "+1.23", TestName = "NumberPrecisionGreaterThanValidatorPrecision")] | ||
[TestCase(3, 2, false, "-1.23", TestName = "NumberNegativeInNumberOnlyPositive")] | ||
[TestCase(17, 2, true, "0.000", TestName = "NumberScaleGreaterThanValidatorScale")] | ||
[TestCase(17, 2, true, "a.sd", TestName = "IncorrectNumberFormatBecauseLettersArePresent")] | ||
public void IsValidNumber_ShouldFalse_When(int precision, int scale, bool onlyPositive, string number) | ||
{ | ||
var validator = new NumberValidator(precision, scale, onlyPositive); | ||
validator.IsValidNumber(number).Should().BeFalse(); | ||
} | ||
|
||
[TestCase(17, 2, false, "0.0", TestName = "NumberWithFractionalPart")] | ||
[TestCase(17, 2, false, "0", TestName = "OnlyInteger")] | ||
[TestCase(4, 2, true, "+1.23", TestName = "NumberWithSign")] | ||
public void IsValidNumber_ShouldTrue_When(int precision, int scale, bool onlyPositive, string number) | ||
{ | ||
var validator = new NumberValidator(precision, scale); | ||
validator.IsValidNumber(number).Should().BeTrue(); | ||
} | ||
} | ||
} |
This file contains 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,61 @@ | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
|
||
namespace HomeExercises.tests | ||
{ | ||
public class ObjectComparison_Should | ||
{ | ||
[Test] | ||
[Description("Проверка текущего царя")] | ||
[Category("ToRefactor")] | ||
public void CheckValuesOfTheCurrentTsar() | ||
{ | ||
var actualTsar = TsarRegistry.GetCurrentTsar(); | ||
|
||
var expectedTsar = new ObjectComparison("Ivan IV The Terrible", 54, 170, 70, | ||
new ObjectComparison("Vasili III of Russia", 28, 170, 60, null)); | ||
|
||
actualTsar.Should().BeEquivalentTo(expectedTsar, options => options | ||
.Excluding(tsar => | ||
tsar.SelectedMemberInfo.Name == nameof(ObjectComparison.Id) | ||
&& tsar.SelectedMemberInfo.DeclaringType == typeof(ObjectComparison))); | ||
} | ||
|
||
[Test] | ||
[Description("Альтернативное решение. Какие у него недостатки?")] | ||
public void CheckCurrentTsar_WithCustomEquality() | ||
{ | ||
var actualTsar = TsarRegistry.GetCurrentTsar(); | ||
var expectedTsar = new ObjectComparison("Ivan IV The Terrible", 54, 170, 70, | ||
new ObjectComparison("Vasili III of Russia", 28, 170, 60, null)); | ||
|
||
//Какие недостатки у такого подхода? | ||
//Нужно будет переписывать AreEqual в случае изменения полей ObjectComparison | ||
//Возможна ошибка при написании метода | ||
//Если тест упадет, сообщение об ошибке будет малоинформативным | ||
Assert.True(AreEqual(actualTsar, expectedTsar)); | ||
} | ||
|
||
private bool AreEqual(ObjectComparison? actual, ObjectComparison? 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); | ||
} | ||
} | ||
|
||
public class TsarRegistry | ||
{ | ||
public static ObjectComparison GetCurrentTsar() | ||
{ | ||
return new ObjectComparison( | ||
"Ivan IV The Terrible", 54, 170, 70, | ||
new ObjectComparison("Vasili III of Russia", 28, 170, 60, null)); | ||
} | ||
} | ||
} |
This file contains 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.
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.
Лишняя строка пустая