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

Рыбин Леонид #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bin/
obj/
.vs/
Basic.sln
Basic.sln.DotSettings.user

This comment was marked as resolved.

27 changes: 16 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,8 @@ 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().BeEquivalentTo(expectedTsar, options =>
options.Excluding(o => o.Name == "Id" && o.DeclaringType == typeof(Person)));

This comment was marked as resolved.

}

[Test]
Expand All @@ -35,6 +28,18 @@ public void CheckCurrentTsar_WithCustomEquality()
new Person("Vasili III of Russia", 28, 170, 60, null));

// Какие недостатки у такого подхода?
/*
1) Метод AreEqual проверяет только фиксированые параметры в Person, поэтому
если класс Person будет измненён, то тесты предётся переписывать,
добавляя новые или удаляя старые поля.
2) Метод AreEqual возвращает True или False, поэтому даже если только одно поле
не пройдёт проверку, всё что будет понятно, что проверка не прошла, а
что именно было не так, узнать не получится.
3) Даже если переписывание тестов не является проблемой (см. 1)), то при добавлении
большего количества полей, особенно если типом новых полей будут другие классы
со своими полями, метод AreEqual рискует разрастись до монструозных масштабов,
из-за чего его станет очень сложно читать.
*/
ClassicAssert.True(AreEqual(actualTsar, expectedTsar));
}

Expand Down
61 changes: 41 additions & 20 deletions Testing/Basic/Homework/2. NumberValidator/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@

using FluentAssertions;
using NUnit.Framework;
using NUnit.Framework.Legacy;

namespace HomeExercise.Tasks.NumberValidator;

[TestFixture]
public class NumberValidatorTests
{
[Test]
public void Test()
[TestCase(-1, 2, true, TestName = "negative precision")]
[TestCase(0, 2, false, TestName = "precision == 0")]

Choose a reason for hiding this comment

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

Всё конечно ситуативно, но лучше прям словами такие кейсы прописывать.

[TestCase(1, -2, true, TestName = "negative scale")]
[TestCase(1, 2, false, TestName = "scale > precision")]
[TestCase(1, 1, true, TestName = "scale == precision")]
public void NumberValidation_ConstructorHasArgumentException(int precision, int scale, bool onlyPositive)

This comment was marked as resolved.

{
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));
var action = () => new NumberValidator(precision, scale, onlyPositive);
action.Should().Throw<ArgumentException>();

This comment was marked as resolved.

}

[TestCase(1, 0, true, TestName = "default")]

This comment was marked as resolved.

public void NumberValidation_ConstructorDoesNotHaveArgumentException(int precision, int scale, bool onlyPositive)
{
var action = () => new NumberValidator(precision, scale, onlyPositive);
action.Should().NotThrow();
}

[TestCase(1, 0, true, "0",ExpectedResult = true)]
[TestCase(2, 1, true, "0.0", ExpectedResult = true)]
[TestCase(2, 1, true, "0.1", ExpectedResult = true)]
[TestCase(2, 1, true, "0,1", ExpectedResult = true)]
[TestCase(2, 0, true, "+1", ExpectedResult = true)]
[TestCase(3, 1, true, "+1.1", ExpectedResult = true)]
[TestCase(3, 1, true, "+1,1", ExpectedResult = true)]
[TestCase(2, 0, false, "-1", ExpectedResult = true)]
[TestCase(3, 1, false, "-1.1", ExpectedResult = true)]
[TestCase(3, 1, false, "-1,1", ExpectedResult = true)]
[TestCase(7, 2, true, "671,23", ExpectedResult = 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(2, 0, true, "", ExpectedResult = false)]
[TestCase(2, 1, true, ".0", ExpectedResult = false)]
[TestCase(2, 0, true, "0.", ExpectedResult = false)]
[TestCase(2, 0, true, "-1", ExpectedResult = false)]
[TestCase(10, 2, true, "abcde.f", ExpectedResult = false)]
[TestCase(10, 9, true, "127.0.0.1", ExpectedResult = false)]
[TestCase(3, 2, true, "4321.5", ExpectedResult = false)]
[TestCase(3, 0, true, "+145", ExpectedResult = false)]
[TestCase(3, 0, false, "-124", ExpectedResult = false)]

This comment was marked as resolved.

public bool IsValidNumber(int precision, int scale, bool onlyPositive, string value)

This comment was marked as resolved.

{
return new NumberValidator(precision, scale, onlyPositive).IsValidNumber(value);
}
}