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

Степан Заколюкин #32

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
14 changes: 3 additions & 11 deletions Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NUnit.Framework;
using FluentAssertions;
using NUnit.Framework;
using NUnit.Framework.Legacy;

namespace HomeExercise.Tasks.ObjectComparison;
Expand All @@ -14,16 +15,7 @@ public void CheckCurrentTsar()
var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70,
new Person("Vasili III of Russia", 28, 170, 60, null));

// Перепишите код на использование Fluent Assertions.
ClassicAssert.AreEqual(actualTsar.Name, expectedTsar.Name);
ClassicAssert.AreEqual(actualTsar.Age, expectedTsar.Age);
ClassicAssert.AreEqual(actualTsar.Height, expectedTsar.Height);
ClassicAssert.AreEqual(actualTsar.Weight, expectedTsar.Weight);

ClassicAssert.AreEqual(expectedTsar.Parent!.Name, actualTsar.Parent!.Name);
ClassicAssert.AreEqual(expectedTsar.Parent.Age, actualTsar.Parent.Age);
ClassicAssert.AreEqual(expectedTsar.Parent.Height, actualTsar.Parent.Height);
ClassicAssert.AreEqual(expectedTsar.Parent.Parent, actualTsar.Parent.Parent);
actualTsar.Should().Be(expectedTsar);
}

[Test]
Expand Down
19 changes: 19 additions & 0 deletions Testing/Basic/Homework/1. ObjectComparison/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,23 @@ public Person(string name, int age, int height, int weight, Person parent)
Weight = weight;
Parent = parent;
}

public override bool Equals(object obj)
{
if (obj is Person person) return this.GetHashCode() == person.GetHashCode();
return false;
}

public int GetHashCode()
{
var result = 0;
var degree = 0;
foreach (var field in GetType().GetFields())
{
if (field.IsStatic || field.Name == "Id") continue;
result += (int)Math.Pow(field.GetValue(this).GetHashCode(), ++degree);
}

return result;
}
}
8 changes: 4 additions & 4 deletions Testing/Basic/Homework/2. NumberValidator/NumberValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ 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");
// 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");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

проверки нужны

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

Expand Down
88 changes: 69 additions & 19 deletions Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,74 @@ namespace HomeExercise.Tasks.NumberValidator;
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"));
public void NumberValidator_IncorrectPrecision_Exception()
{
Assert.Throws<ArgumentException>(() => new NumberValidator(-1, 2, true),
"precision должен быть положительным числом");
}

[Test]
public void NumberValidator_CorrectInitialization_NoExceptions()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В этих тестах потеряны кейсы когда onlyPositive == false (в старом коде дублирование, ты можешь воткнуть TestCase(true) и TestCase(false))

И ещё если мы конструктор валидируем, то надо докинуть проверку scale: должен быть не меньше нуля и не больше precision

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

И ещё, если мы проверяем конструктор, то нужно добавить проверку масштаба: он должен быть не меньше нуля и не больше точности

у меня это проверяется в методе NumberValidator_IncorrectScale_Exception

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А, пон, не увидел

{
Assert.DoesNotThrow(() => new NumberValidator(1, 0, true),
"При создании экземпляра класса не должно было возникнуть ошибок");
Copy link

@Inree Inree Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если мы тут за fluent assertions, то есть func.Should().Throw(exception) и func.Should().NotThrow(exception)

}

[TestCase(1, -1)]
[TestCase(1, 2)]
[TestCase(1, 1)]
public void NumberValidator_IncorrectScale_Exception(int precision, int scale = 0, bool onlyPositive = true)
{
Assert.Throws<ArgumentException>(() => new NumberValidator(precision, scale, onlyPositive),
"scale должно быть меньше precision, но больше или равно 0");
}

[Test]
public void IsValidNumber_InvalidCharacters_False()
{
ClassicAssert.IsFalse(new NumberValidator(3, 2, true).IsValidNumber("a.sd"),
"В записи числа не должны содержаться буквы");
}

[TestCase("00.00", 3, 2)]
[TestCase("+1.23", 3, 2)]
public void IsValidNumber_NotTheAppropriateLength_False(string value,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут ещё по общим правилам нейминга должно быть типа "Should_ReturnFalse_WhenUnappropriateLength" или как-то так, но видимо вы не это проходили, так что можно не править

int precision,
int scale = 0,
bool onlyPositive = true)
{
ClassicAssert.IsFalse(new NumberValidator(precision, scale, onlyPositive).IsValidNumber(value),
"Длина числа не соответствует шаблону");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ну и тут тоже new NumberValidator(precision, scale, onlyPositive).IsValidNumber(value).Should().Befalse();

}

[TestCase("+1.23", 4, 2)]
[TestCase("0", 17, 2)]
[TestCase("0.0", 17, 2)]
public void IsValidNumber_CorrectValue_True(string value, int precision, int scale = 0, bool onlyPositive = true)
{
ClassicAssert.IsTrue(new NumberValidator(precision, scale, onlyPositive).IsValidNumber(value));
}

[TestCase("-0.00", 3, 2)]
[TestCase("+0.00", 3, 2, false)]
public void IsValidNumber_TheWrongSign_False(string value, int precision, int scale = 0, bool onlyPositive = true)
{
ClassicAssert.IsFalse(new NumberValidator(precision, scale, onlyPositive).IsValidNumber(value),
"Знаки value и NumberValidator отличаются");
}

[TestCase("", 1)]
[TestCase(null, 1)]
public void IsValidNumber_NullOrEmpty_False(string value, int precision, int scale = 0, bool onlyPositive = true)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут и в IsValidNumber_CorrectValue_True стоит onlyPositive, но в кейсах не указывается

{
ClassicAssert.IsFalse(new NumberValidator(precision, scale, onlyPositive).IsValidNumber(value),
"Проверяемое значение не должно равняться null или пустой строке");
}

[Test]
public void IsValidNumber_InappropriateAccuracy_False()
{
ClassicAssert.IsFalse(new NumberValidator(17, 2, true).IsValidNumber("0.000"),
"значение не может иметь точность выше указанной в NumberValidator");
}
}