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

Шмаков Данил #223

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
56 changes: 38 additions & 18 deletions cs/HomeExercises/NumberValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,46 @@ namespace HomeExercises
{
public class NumberValidatorTests
{
[Test]
public void Test()
[TestCase(1, 0,"0", TestName = "int zero")]

This comment was marked as outdated.

[TestCase(2, 0,"+0", TestName = "positive int zero")]
[TestCase(2, 0,"-0", false , TestName = "negative int zero")]
[TestCase(2, 1,"0.0", TestName = "zero with fractional part")]
[TestCase(3, 2,"1.23", TestName = "positive non zero without sign with fractional part")]
[TestCase(4, 2,"+1.23", TestName = "positive non zero with sign and fractional part")]
[TestCase(4, 2,"-1.23", false, TestName = "negative non zero with sign and fractional part")]
public void IsValid(int precision, int scale, string validatingString, bool onlyPositive = true)
{
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));
new NumberValidator(precision, scale, onlyPositive)
.IsValidNumber(validatingString)
.Should()
.BeTrue();
}

[TestCase(2, 1,"0.00", TestName = "zero intPart + fracPart should be less than precesion")]
[TestCase(3, 2, "+0.00", TestName = "zero intPart + fracPart + \"+\" should be less than precesion")]

This comment was marked as outdated.

[TestCase(3, 2, "-0.00", false, TestName = "zero intPart + fracPart + \"-\" should be less than precesion")]
[TestCase(3, 2,"+1.23", TestName = "positive non zero intPart + fracPart + \"+\" should be less than precesion")]
[TestCase(3, 2,"-1.23", false, TestName = "negative non zero intPart + fracPart + \"+\" should be less than precesion")]
[TestCase(3, 2, "a.sd", TestName = "non digit symbols")]
[TestCase(2, 1, ".0", TestName = "must have digits before point")]
[TestCase(1, 0, "0.", TestName = "must have digits after point (if exist)")]
public void IsNotValid(int precision, int scale, string validatingString, bool onlyPositive = true)

This comment was marked as outdated.

{
new NumberValidator(precision, scale, onlyPositive)
.IsValidNumber(validatingString)
.Should()
.BeFalse();
}

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"));
[TestCase(-1, 1, TestName = "negative precision")]
[TestCase(1, -1, TestName = "negative scale")]
[TestCase(-1, -1, TestName = "negative precision and scale")]
[TestCase(1, 1, TestName = "precision equals scale")]
[TestCase(1, 2, TestName = "precision less than scale")]
public void ShouldThrow(int precision, int scale, bool onlyPositive = true)
{
Action act = () => new NumberValidator(precision, scale, onlyPositive);
act.Should().Throw<ArgumentException>();

Choose a reason for hiding this comment

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

это можно проще написать через Assert.Throws<>( () => ...)

}
}

Expand Down
26 changes: 16 additions & 10 deletions cs/HomeExercises/ObjectComparison.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using FluentAssertions;
using System;
using FluentAssertions;
using FluentAssertions.Equivalency;
using NUnit.Framework;

namespace HomeExercises
Expand All @@ -16,15 +18,13 @@ public void CheckCurrentTsar()
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);

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);
actualTsar.Should().BeEquivalentTo(expectedTsar, options =>
options
.IgnoringCyclicReferences()
.Excluding((IMemberInfo info) =>
info.SelectedMemberInfo.DeclaringType == typeof(Person)
&& info.SelectedMemberInfo.Name == nameof(Person.Id))
);
}

[Test]
Expand All @@ -35,7 +35,13 @@ public void CheckCurrentTsar_WithCustomEquality()
var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70,
new Person("Vasili III of Russia", 28, 170, 60, null));


// Какие недостатки у такого подхода?

// 1. Вероятность StackOverflow из-за бесконечной рекурсии
// 2. При каждом изменении свойств или добавлении новых функцию придётся переписывать
// 3. Данный код не выполняет никакой полезной функции кроме сравнения полей двух объектов

This comment was marked as outdated.

// 4. Нарушение SRP, ответственность за сравнение объетов должна быть инкапсулирована в класс
Assert.True(AreEqual(actualTsar, expectedTsar));

This comment was marked as outdated.

}

Expand Down