Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions Testing/Advanced/Advanced.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -7,16 +7,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ApprovalTests" Version="6.0.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="ApprovalTests" Version="7.0.0" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.4" />
<PackageReference Include="FakeItEasy" Version="8.3.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="log4net" Version="2.0.17" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.22.0" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="131.0.6778.8700" />
<PackageReference Include="FluentAssertions" Version="8.8.0" />
<PackageReference Include="log4net" Version="3.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
<PackageReference Include="NUnit" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="5.2.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.38.0" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="142.0.7444.5900" />
</ItemGroup>

</Project>
16 changes: 8 additions & 8 deletions Testing/Basic/Basic.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -7,16 +7,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PackageReference Include="FluentAssertions" Version="8.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
<PackageReference Include="NUnit" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="5.2.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
123 changes: 106 additions & 17 deletions Testing/Basic/Homework/1. ObjectComparison/ObjectComparison.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,119 @@
using NUnit.Framework;
using System.Runtime.CompilerServices;
using FluentAssertions;
using NUnit.Framework;
using NUnit.Framework.Legacy;

namespace HomeExercise.Tasks.ObjectComparison;
public class ObjectComparison
{
[Test]
[Description("Проверка текущего царя")]
[Category("ToRefactor")]
public void CheckCurrentTsar()
#region test data
private static Person currentTsar = TsarRegistry.GetCurrentTsar();
private static Person tsarCopy = new Person("Ivan IV The Terrible", 54, 170, 70, new Person("Vasili III of Russia", 28, 170, 60, null));
private static Person tsarCopyDifferent = new Person("Ivan IV The Kind", 54, 170, 70, new Person("Vasili III of Russia", 28, 170, 60, null));

private static Person person0Parents = new Person("1", 1, 1, 1, null);
private static Person person0ParentsCopy = person0Parents.Copy()!;
private static Person person0ParentsLimits = new Person(string.Empty, int.MaxValue, int.MinValue, 0, null);
private static Person person0ParentsLimitsCopy = person0ParentsLimits.Copy()!;
private static Person person1Parents = new Person("1", 1, 1, 1, new Person("2", 2, 2, 2, null));
private static Person person1ParentsСopy = person1Parents.Copy()!;
private static Person person1ParentsDifferent = new Person("1", 1, 1, 1, new Person("3", 3, 3, 3, null));
private static Person person2Parents = new Person("1", 1, 1, 1, new Person("2", 2, 2, 2, new Person("3", 3, 3, 3, null)));
private static Person person2ParentsCopy = person2Parents.Copy()!;
private static Person person2ParentsDifferent = new Person("1", 1, 1, 1, new Person("2", 2, 2, 2, new Person("5", 5, 5, 5, null)));
#endregion

private static IEnumerable<TestCaseData> FieldCompare_SuccessCases()
{
var actualTsar = TsarRegistry.GetCurrentTsar();
yield return new TestCaseData(null, null).SetName("Пустые аргументы");
yield return new TestCaseData(currentTsar, tsarCopy).SetName("Проверка царя");
yield return new TestCaseData(person0Parents, person0ParentsCopy).SetName("Нет родителя");
yield return new TestCaseData(person1Parents, person1ParentsСopy).SetName("Есть родитель");
yield return new TestCaseData(person0Parents, person1ParentsСopy).SetName("Разные родители");
yield return new TestCaseData(person2Parents, person2ParentsDifferent).SetName("Разные прародители");
yield return new TestCaseData(person0ParentsLimits, person0ParentsLimitsCopy).SetName("Проверка с граничными значениями полей");
}

var expectedTsar = new Person("Ivan IV The Terrible", 54, 170, 70,
new Person("Vasili III of Russia", 28, 170, 60, null));
[Test, TestCaseSource(nameof(FieldCompare_SuccessCases))]
[Description("Проверка Person по полям - поля равны")]
public void AreEqual_NotThrows_OnEqualFields(Person? actual, Person? expected)
{
actual.Should().BeEquivalentTo(expected, options =>
options
.Excluding(t => t.Id)
.Excluding(t => t.Parent)
);
}

private static IEnumerable<TestCaseData> FieldCompare_FailCases()
{
yield return new TestCaseData(currentTsar, null).SetName("Сравнение с null");
yield return new TestCaseData(currentTsar, tsarCopyDifferent).SetName("Проверка неправильного царя");
yield return new TestCaseData(currentTsar, person0Parents).SetName("Полностью разные");
yield return new TestCaseData(person0ParentsLimits, person0Parents).SetName("Просто разные");
}

// Перепишите код на использование 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);
[Test, TestCaseSource(nameof(FieldCompare_FailCases))]
[Description("Проверка Person по полям - поля разные")]
public void AreEqual_Throws_OnDifferentFields(Person? actual, Person? expected)
{
actual.Should().NotBeEquivalentTo(expected, options =>
options
.Excluding(t => t.Id)
.Excluding(t => t.Parent)
);
}

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);
private static IEnumerable<TestCaseData> FullCompare_SuccessCases()
{
yield return new TestCaseData(null, null).SetName("Пустые аргументы при полном сравнении");
yield return new TestCaseData(currentTsar, tsarCopy).SetName("Проверка царя при полном сравнении");
yield return new TestCaseData(person0Parents, person0ParentsCopy).SetName("Нет родителя при полном сравнении");
yield return new TestCaseData(person1Parents, person1ParentsСopy).SetName("Есть родитель при полном сравнении");
yield return new TestCaseData(person2Parents, person2ParentsCopy).SetName("Родитель у родителя при полном сравнении");
}


[Test, TestCaseSource(nameof(FullCompare_SuccessCases))]
[Description("Проверка Person по Parent - Parent равны")]
public void AreEqual_NotThrows_OnEqualPersons(Person? actual, Person? expected)
{
actual.Should().BeEquivalentTo(expected, options =>
options
.Excluding(t => t.Id)
.Excluding(t => t.Path.EndsWith("Id"))
);
}

private static IEnumerable<TestCaseData> FullCompare_FailCases()
{
yield return new TestCaseData(currentTsar, null).SetName("Сравнение с null при полном сравнении");
yield return new TestCaseData(currentTsar, tsarCopyDifferent).SetName("Проверка неправильного царя при полном сравнении");
yield return new TestCaseData(currentTsar, person1Parents).SetName("Полностью разные при полном сравнении");
yield return new TestCaseData(person1Parents, person0Parents).SetName("Родитель null при полном сравнении");
yield return new TestCaseData(person1Parents, person1ParentsDifferent).SetName("Разные родители при полном сравнении");
yield return new TestCaseData(person2Parents, person2ParentsDifferent).SetName("Разные прародители при полном сравнении");
}

[Test, TestCaseSource(nameof(FullCompare_FailCases))]
[Description("Проверка Person по Parent - Parent различны")]
public void AreEqual_Throw_OnDifferentPersons(Person? actual, Person? expected)
{
actual.Should().NotBeEquivalentTo(expected, options =>
options
.Excluding(t => t.Id)
.Excluding(t => t.Path.EndsWith("Id"))
);
}

// А что так можно было что ли?

// При добавлении новых полей в Person придется расширять условие в return еще больше;
// Тест должен пройти по всем Parent прежде чем дать результат,
// в моем тесте при различии полей, он сразу прервется выкинув исключение;
// Я добавил TestCaseSource для добавления новых кейсов, в данном тесте данные собираются в нем;
// Использование FluentAssertions упрощает прочтение кода в моем тесте;

[Test]
[Description("Альтернативное решение. Какие у него недостатки?")]
public void CheckCurrentTsar_WithCustomEquality()
Expand All @@ -42,6 +130,7 @@ private bool AreEqual(Person? actual, Person? expected)
{
if (actual == expected) return true;
if (actual == null || expected == null) return false;
// Большой return
return
actual.Name == expected.Name
&& actual.Age == expected.Age
Expand Down
16 changes: 14 additions & 2 deletions Testing/Basic/Homework/1. ObjectComparison/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ public class Person
public static int IdCounter = 0;
public int Age, Height, Weight;
public string Name;
public Person Parent;
public Person? Parent;
public int Id;

public Person(string name, int age, int height, int weight, Person parent)
public Person(string name, int age, int height, int weight, Person? parent)
{
Id = IdCounter++;
Name = name;
Expand All @@ -18,4 +18,16 @@ public Person(string name, int age, int height, int weight, Person parent)
Weight = weight;
Parent = parent;
}

public Person? Copy() => Copy(this)!;

public static Person? Copy(Person? source)
{
if (source is null)
{
return null;
}
return new Person(source.Name, source.Age, source.Height, source.Weight, Person.Copy(source.Parent!)!);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public NumberValidator(int precision, int scale = 0, bool onlyPositive = false)
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");
throw new ArgumentException("scale must be a non-negative number less or equal than precision");
numberRegex = new Regex(@"^([+-]?)(\d+)([.,](\d+))?$", RegexOptions.IgnoreCase);
}

Expand Down
Loading