-
Notifications
You must be signed in to change notification settings - Fork 57
Русинов Матвей #48
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
base: master
Are you sure you want to change the base?
Русинов Матвей #48
Changes from all commits
7a57337
f458052
8df83ef
766f520
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 |
|---|---|---|
| @@ -1,31 +1,93 @@ | ||
| | ||
| using NUnit.Framework; | ||
| using NUnit.Framework.Legacy; | ||
| using NUnit.Framework; | ||
| using FluentAssertions; | ||
|
|
||
| namespace HomeExercise.Tasks.NumberValidator; | ||
|
|
||
| [TestFixture] | ||
| public class NumberValidatorTests | ||
| { | ||
| [Test] | ||
| public void Test() | ||
| { | ||
| Assert.Throws<ArgumentException>(() => new NumberValidator(-1, 2, true)); | ||
| Assert.DoesNotThrow(() => new NumberValidator(1, 0, true)); | ||
| Assert.Throws<ArgumentException>(() => new NumberValidator(-1, 2, false)); | ||
| Assert.DoesNotThrow(() => new NumberValidator(1, 0, true)); | ||
|
|
||
| ClassicAssert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); | ||
| ClassicAssert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0")); | ||
| ClassicAssert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); | ||
| ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("00.00")); | ||
| ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-0.00")); | ||
| ClassicAssert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); | ||
| ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+0.00")); | ||
| ClassicAssert.IsTrue(new NumberValidator(4, 2, true).IsValidNumber("+1.23")); | ||
| ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+1.23")); | ||
| ClassicAssert.IsFalse(new NumberValidator(17, 2, true).IsValidNumber("0.000")); | ||
| ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-1.23")); | ||
| ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("a.sd")); | ||
| [TestCase(0, 2, "positive", TestName = "Precision is zero")] | ||
| [TestCase(1, -1, "non-negative", TestName = "Scale is negative")] | ||
| [TestCase(1, 2, "less or equal", TestName = "Precision is less than scale")] | ||
| [TestCase(1, 1, "less or equal", TestName = "Precision equals scale")] | ||
| public void Constructor_ThrowsException_OnInvalidArguments_Test(int precision, int scale, string expectedMessage) | ||
| { | ||
| var constructor = () => new NumberValidator(precision, scale); | ||
| constructor.Should().Throw<ArgumentException>().WithMessage($"*{expectedMessage}*"); | ||
| } | ||
|
|
||
| [TestCase(1, 0, TestName = "Precision and scale are minimum possible")] | ||
| [TestCase(2, 1, TestName = "Highest valid scale below precision")] | ||
| public void Constructor_DoesNotThrowException_OnBoundaryValidArguments_Test(int precision, int scale) | ||
| { | ||
| var constructor = () => new NumberValidator(precision, scale, true); | ||
| constructor.Should().NotThrow(); | ||
| } | ||
|
Comment on lines
19
to
25
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. С одной стороны, под рефакторингом понимается изменение кода без изменения его поведения. С этой точки зрения тесты действительно должны быть - ведь они были в исходной версии.
Author
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. комментарий обработал, не стал удалять, сделал логику теста в том, что он проверяет какие-то граничные значения, вместо обычного пессимистичного теста 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. Это хороший подход |
||
|
|
||
| [TestCase("a.sd", TestName = "Input contains letters")] | ||
| [TestCase("", TestName = "Input is empty string")] | ||
| [TestCase(null, TestName = "Input is null")] | ||
| [TestCase("12+3", TestName = "Input contains invalid characters")] | ||
| public void Validator_ReturnsFalse_OnInvalidInputFormats_Test(string input) | ||
| { | ||
| var validator = new NumberValidator(5); | ||
| validator.IsValidNumber(input).Should().BeFalse(); | ||
| } | ||
|
|
||
| [TestCase(1, 0, "0", TestName = "One zero")] | ||
| [TestCase(2, 1, "0.0", TestName = "One zero with fraction part")] | ||
| [TestCase(3, 1, "00.0", TestName = "Two leading zeros with fraction part")] | ||
| public void Validator_ReturnsTrue_WithProperZeros_Test(int precision, int scale, string input) | ||
| { | ||
| var validator = new NumberValidator(precision, scale); | ||
| validator.IsValidNumber(input).Should().BeTrue(); | ||
| } | ||
|
|
||
| [TestCase(4, 0, "1234", TestName = "Simple integer")] | ||
| [TestCase(5, 2, "123.54", TestName = "Number with fraction part")] | ||
| public void Validator_ReturnsTrue_WithProperNumbers_Test(int precision, int scale, string input) | ||
| { | ||
| var validator = new NumberValidator(precision, scale); | ||
| validator.IsValidNumber(input).Should().BeTrue(); | ||
| } | ||
|
|
||
| [TestCase("123", TestName = "Precision overflow with numerals")] | ||
| [TestCase("000", TestName = "Precision overflow with zeros")] | ||
| public void Validator_ReturnsFalse_WithPrecisionOverflow_Test(string input) | ||
| { | ||
| var validator = new NumberValidator(2); | ||
| validator.IsValidNumber(input).Should().BeFalse(); | ||
| } | ||
|
|
||
| [TestCase("1.23456", TestName = "Scale overflow with numerals")] | ||
| [TestCase("0.00000", TestName = "Scale overflow with zeros")] | ||
| public void Validator_ReturnsFalse_WithScaleOverflow_Test(string input) | ||
| { | ||
| var validator = new NumberValidator(10, 4); | ||
| validator.IsValidNumber(input).Should().BeFalse(); | ||
| } | ||
|
|
||
| [TestCase("-123", TestName = "Negative integer")] | ||
| [TestCase("-0", TestName = "Negative zero")] | ||
| public void Validator_ReturnsFalse_WithNegativeNumberWhenNotAllowed_Test(string input) | ||
| { | ||
| var validator = new NumberValidator(10, 0, true); | ||
| validator.IsValidNumber(input).Should().BeFalse(); | ||
| } | ||
|
|
||
| [TestCase("-1.23", TestName = "Negative float")] | ||
| [TestCase("+1.23", TestName = "Positive float")] | ||
| public void Validator_ReturnsFalse_WithSignPrecisionOverflow_Test(string input) | ||
| { | ||
| var validator = new NumberValidator(3, 2); | ||
| validator.IsValidNumber(input).Should().BeFalse(); | ||
| } | ||
|
|
||
| [TestCase("12,34", TestName = "Comma separator")] | ||
| [TestCase("12.34", TestName = "Dot separator")] | ||
| public void Validator_ReturnsTrue_WithCommaSeparator_Test(string input) | ||
| { | ||
| var validator = new NumberValidator(5, 2); | ||
| validator.IsValidNumber(input).Should().BeTrue(); | ||
| } | ||
| } | ||
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.
Оставлю комментарий здесь, т.к. github не дает мне выбрать неизмененные строки кода
Обычно имена тестов следуют некоторым правилам (соглашениям). В разных компаниях (да и командах внутри компании) они могут различаться. Например, один из способов именования такой:
<ClassName>_<ShouldBe...>_[When...]_Test.В данном случае можно ограничиться добавлением суффикса _Test в конец имени тестов. Так мы явно выделим эти методы от "обычных" методов.
К слову, почти такой же способ ты использовал в NumberValidatorTests
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.
поменял названия, которые совсем не подходили по структуре
обновил названия, которые не полностью совпадали со структурой