-
Notifications
You must be signed in to change notification settings - Fork 274
Яценко Ирина #235
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?
Яценко Ирина #235
Changes from 3 commits
2c1d986
3938cbe
d229179
09348a8
5f3e0e4
6eb6ff0
2146ede
8832ca8
def3bbb
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,34 +1,109 @@ | ||
| using System; | ||
| using System.Text.RegularExpressions; | ||
| using FluentAssertions; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace HomeExercises | ||
| { | ||
| public class NumberValidatorTests | ||
|
||
| { | ||
| [Test] | ||
| public void Test() | ||
| public void NumberValidatorCtor_WhenPassNegativePrecision_ShouldThrowsArgumentException() | ||
|
||
| { | ||
| 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)); | ||
|
|
||
| Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); | ||
| Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0")); | ||
| Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); | ||
| Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("00.00")); | ||
| Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-0.00")); | ||
| Assert.IsTrue(new NumberValidator(17, 2, true).IsValidNumber("0.0")); | ||
| Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+0.00")); | ||
| Assert.IsTrue(new NumberValidator(4, 2, true).IsValidNumber("+1.23")); | ||
| Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("+1.23")); | ||
| Assert.IsFalse(new NumberValidator(17, 2, true).IsValidNumber("0.000")); | ||
| Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("-1.23")); | ||
| Assert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("a.sd")); | ||
| TestDelegate testDelegate = () => new NumberValidator(-1, 2, true); | ||
|
|
||
| Assert.Throws<ArgumentException>(testDelegate); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void NumberValidatorCtor_WhenPassNegativeScale_ShouldThrowsArgumentException() | ||
| { | ||
| TestDelegate testDelegate = () => new NumberValidator(1, -2); | ||
|
|
||
| Assert.Throws<ArgumentException>(testDelegate); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NumberValidatorCtor_WhenPassValidArguments_ShouldDoesNotThrows() | ||
| { | ||
| TestDelegate testDelegate = () => new NumberValidator(1, 0, true); | ||
|
|
||
| Assert.DoesNotThrow(testDelegate); | ||
|
||
| } | ||
|
|
||
| [Test] | ||
| public void NumberValidatorCtor_WhenPrecisionIsEqualToTheScale_ShouldThrowsArgumentException() | ||
| { | ||
| TestDelegate testDelegate = () => new NumberValidator(2, 2, true); | ||
|
|
||
| Assert.Throws<ArgumentException>(testDelegate); | ||
| } | ||
|
|
||
| public void IsValidNumberTest(int precision, int scale, bool onlyPositive, | ||
| string number, bool expectedResult) | ||
| { | ||
| var validator = new NumberValidator(precision, scale, onlyPositive); | ||
|
|
||
| var actualResult = validator.IsValidNumber(number); | ||
|
|
||
| Assert.AreEqual(expectedResult, actualResult); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestOf(nameof(NumberValidator.IsValidNumber))] | ||
| public void WhenFractionalPartIsMissing_ShouldReturnTrue() | ||
| { | ||
| IsValidNumberTest(17,2,true,"0", true); | ||
|
||
| } | ||
|
|
||
| [Test] | ||
| [TestOf(nameof(NumberValidator.IsValidNumber))] | ||
| public void WhenLettersInsteadOfNumber_ShouldReturnFalse() | ||
| { | ||
| IsValidNumberTest(3, 2, true, "a.sd", false); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestOf(nameof(NumberValidator.IsValidNumber))] | ||
| public void WhenSymbolsInsteadOfNumber_ShouldReturnFalse() | ||
| { | ||
| IsValidNumberTest(3, 2, true, "2.!", false); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestOf(nameof(NumberValidator.IsValidNumber))] | ||
| public void WhenNumberIsNull_ShouldReturnFalse() | ||
| { | ||
| IsValidNumberTest(17,2,true, null!, false); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestOf(nameof(NumberValidator.IsValidNumber))] | ||
| public void WhenPassNumberIsEmpty_ShouldReturnFalse() | ||
| { | ||
| IsValidNumberTest(3,2,true,"", false); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestOf(nameof(NumberValidator.IsValidNumber))] | ||
| public void WhenIntPartWithNegativeSignMoreThanPrecision_ShouldReturnFalse() | ||
| { | ||
| IsValidNumberTest(3,2,true,"-0.00", false); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestOf(nameof(NumberValidator.IsValidNumber))] | ||
| public void WhenIntPartWithPositiveSignMoreThanPrecision_ShouldReturnFalse() | ||
| { | ||
| IsValidNumberTest(3,2,true,"+1.23", false); | ||
| } | ||
|
|
||
| [Test] | ||
| [TestOf(nameof(NumberValidator.IsValidNumber))] | ||
| public void WhenFractionalPartMoreThanScale_ShouldReturnFalse() | ||
| { | ||
| IsValidNumberTest(17,2,true, "0.000", false); | ||
| } | ||
| } | ||
|
|
||
| public class NumberValidator | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,81 +3,82 @@ | |
|
|
||
| namespace HomeExercises | ||
| { | ||
| public class ObjectComparison | ||
| { | ||
| [Test] | ||
| [Description("Проверка текущего царя")] | ||
| [Category("ToRefactor")] | ||
| public void CheckCurrentTsar() | ||
| { | ||
| var actualTsar = TsarRegistry.GetCurrentTsar(); | ||
| public class ObjectComparison | ||
| { | ||
| [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)); | ||
| var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70, | ||
| new Person("Vasili III of Russia", 28, 170, 60, null)); | ||
|
|
||
| // Перепишите код на использование Fluent Assertions. | ||
| Assert.AreEqual(actualTsar.Name, expectedTsar.Name); | ||
| Assert.AreEqual(actualTsar.Age, expectedTsar.Age); | ||
| Assert.AreEqual(actualTsar.Height, expectedTsar.Height); | ||
| Assert.AreEqual(actualTsar.Weight, expectedTsar.Weight); | ||
| actualTsar.Should() | ||
| .BeEquivalentTo(expectedTsar, x => x | ||
| .Excluding(x => x.Id) | ||
| .Excluding(x => x.Parent!.Id)); | ||
| } | ||
|
|
||
| Assert.AreEqual(expectedTsar.Parent!.Name, actualTsar.Parent!.Name); | ||
| Assert.AreEqual(expectedTsar.Parent.Age, actualTsar.Parent.Age); | ||
| Assert.AreEqual(expectedTsar.Parent.Height, actualTsar.Parent.Height); | ||
| Assert.AreEqual(expectedTsar.Parent.Parent, actualTsar.Parent.Parent); | ||
| } | ||
| [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)); | ||
|
|
||
| [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)); | ||
| // Какие недостатки у такого подхода? | ||
| // Данный подход делает класс труднорасширяемым, ведь при добавлении новых полей в класс Person | ||
| // нужно переписывать и метод сравнения для всех этих полей, так же новые поля cможет добавить | ||
| // другой разработчик, который не знает о таком методе сравнения, что приведет к новым, неотловоенным ошибкам - | ||
| // новые поля не будут сравниваться. | ||
| // В моем решении (CheckCurrentTsar), такой ошибки не возникнет и класс Person сможет без ошибок расширяться | ||
| // при условии, что сравниваться будут все поля, кроме Id (так же и их Parent), так как код написан не перебиранием | ||
| // всех полей для сравнения, а сравнением объекта в целом с исключением его Id. | ||
| Assert.True(AreEqual(actualTsar, expectedTsar)); | ||
| } | ||
|
|
||
| // Какие недостатки у такого подхода? | ||
| Assert.True(AreEqual(actualTsar, expectedTsar)); | ||
| } | ||
| private 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); | ||
|
||
| } | ||
| } | ||
|
|
||
| private 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); | ||
| } | ||
| } | ||
| 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)); | ||
| } | ||
| } | ||
|
|
||
| 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)); | ||
| } | ||
| } | ||
| public class Person | ||
| { | ||
| public static int IdCounter = 0; | ||
| public int Age, Height, Weight; | ||
| public string Name; | ||
| public Person? Parent; | ||
| public int Id; | ||
|
|
||
| public class Person | ||
| { | ||
| public static int IdCounter = 0; | ||
| public int Age, Height, Weight; | ||
| public string Name; | ||
| public Person? Parent; | ||
| public int Id; | ||
|
|
||
| public Person(string name, int age, int height, int weight, Person? parent) | ||
| { | ||
| Id = IdCounter++; | ||
| Name = name; | ||
| Age = age; | ||
| Height = height; | ||
| Weight = weight; | ||
| Parent = 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; | ||
| } | ||
| } | ||
| } | ||
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.
Круто, что коммиты логически разбиты