Skip to content

Commit

Permalink
Added approval tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BMV989 committed Dec 11, 2024
1 parent e401825 commit a888740
Show file tree
Hide file tree
Showing 15 changed files with 343 additions and 15 deletions.
37 changes: 37 additions & 0 deletions ObjectPrinting/ObjectPrinting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,45 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ApprovalTests" Version="7.0.0-beta.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
<None Update="Tests\ObjectPrinterAcceptanceTests.Demo.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
<None Update="Tests\ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithCustomSerializationForSpecificType.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
<None Update="Tests\ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingWithCustomCulture.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
<None Update="Tests\ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenTrimmingStringProperties.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
<None Update="Tests\ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingSpecificProperty.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
<None Update="Tests\ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenChainingSerializationRules.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
<None Update="Tests\ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenUsingExtensionMethod.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
<None Update="Tests\ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithConfiguration.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
<None Update="Tests\ObjectPrinterAcceptanceTests.PrintToString_ShouldDetectCyclicReferences.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
<None Update="Tests\ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingDictionary.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
<None Update="Tests\ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingEnumerable.approved.txt">
<DependentUpon>ObjectPrinterAcceptanceTests.cs</DependentUpon>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Person
Id = Guid
Name = Alex
Height = 1,81
Age = 19
DateOfBirth = 09.09.1985 00:00:00
Addresses = null
Children = null
Father = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Person
Id = Guid
Name = Alex
Height = 1,81
Age = 19
DateOfBirth = 01.01.0001 00:00:00
Addresses = null
Children = null
Father = Person
Id = Guid
Name = Pavel Doe
Height = 0
Age = 68
DateOfBirth = 09.09.1954 00:00:00
Addresses = null
Children = null
Father = Cyclic reference!
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Person
Name = Maxwe
Height = 1.81
DateOfBirth = понедельник, 1 января 0001 г.
Addresses = null
Children = null
Father = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Person
Id = Guid
Name = Alex
Age = 19
DateOfBirth = 01.01.0001 00:00:00
Addresses = null
Children = null
Father = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Person
Id = Guid
Name = Alex
Height = 1,81
Age = 19
Addresses = null
Children = null
Father = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Person
Id = Guid
Name = Alex
Height = 1,81
Age = 19
DateOfBirth = 01.01.0001 00:00:00
Addresses = Dictionary`2:
Key = 1
Value = London
Key = 2
Value = New York
Key = 3
Value = Moscow
Children = null
Father = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Person
Id = Guid
Name = Alex
Height = 1,81
Age = 19
DateOfBirth = 01.01.0001 00:00:00
Addresses = null
Children = List`1:
- Person
Id = Guid
Name = Natasha
Height = 1
Age = 8
DateOfBirth = 09.09.2002 00:00:00
Addresses = null
Children = null
Father = null
- Person
Id = Guid
Name = Pasha
Height = 2
Age = 9
DateOfBirth = 09.09.2004 00:00:00
Addresses = null
Children = null
Father = null
Father = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Person
Id = Guid
Name = Alex
Height = 1.81
Age = 19
DateOfBirth = 01.01.0001 00:00:00
Addresses = null
Children = null
Father = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Person
Id = Guid
Name = Al
Height = 1,81
Age = 19
DateOfBirth = 01.01.0001 00:00:00
Addresses = null
Children = null
Father = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Person
Id = Guid
Name = Alex
Height = 1,81
Age = 19
DateOfBirth = 01.01.0001 00:00:00
Addresses = null
Children = null
Father = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Person
Id = Guid
Name = Alex
Height = 1,81
DateOfBirth = 01.01.0001 00:00:00
Addresses = null
Children = null
Father = null
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Person
Id = Guid
Name = Alex
Height = 1,81
Age = 19
DateOfBirth = четверг, 9 сентября 2004 г.
Addresses = null
Children = null
Father = null
181 changes: 166 additions & 15 deletions ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,187 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using ApprovalTests;
using ApprovalTests.Reporters;
using NUnit.Framework;

namespace ObjectPrinting.Tests;

[TestFixture]
public class ObjectPrinterAcceptanceTests
{
private Person person;

[SetUp]
public void Setup() => person = new Person { Name = "Alex", Age = 19, Height = 1.81 };

[Test]
[UseReporter(typeof(DiffReporter))]
public void Demo()
{
var person = new Person { Name = "Alex", Age = 19 };
person.DateOfBirth = new DateTime(1985, 9, 9);
var printer = ObjectPrinter.For<Person>();
var s = printer.PrintToString(person);

var printer = ObjectPrinter.For<Person>()
//1. Исключить из сериализации свойства определенного типа
Approvals.Verify(s);
}

[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldWorkCorrectly_WhenExcludingTypeFromSerialization()
{
person.DateOfBirth = new DateTime(2004, 9, 9);
var printer = ObjectPrinter
.For<Person>()
.Excluding<DateTime>(); //1. Исключить из сериализации свойства определенного типа

var s = printer.PrintToString(person);

Approvals.Verify(s);
}

[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldWorkCorrectly_WithCustomSerializationForSpecificType()
{
person.DateOfBirth = new DateTime(2004, 9, 9);
var printer = ObjectPrinter
.For<Person>()
.Printing<DateTime>().Using(i => i.ToLongDateString());
//2. Указать альтернативный способ сериализации для определенного типа

var s = printer.PrintToString(person);

Approvals.Verify(s);
Console.WriteLine(s);
}

[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldWorkCorrectly_WhenSerializingWithCustomCulture()
{
var printer = ObjectPrinter
.For<Person>()
.Printing<double>().Using(CultureInfo.InvariantCulture);
//3. Для числовых типов указать культуру

var s = printer.PrintToString(person);

Approvals.Verify(s);
}

[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldWorkCorrectly_WhenTrimmingStringProperties()
{
var printer = ObjectPrinter
.For<Person>()
.Printing(p => p.Name) //4. Настроить сериализацию конкретного свойства
.TrimmedToLength(2);
//5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств)

var s = printer.PrintToString(person);

Approvals.Verify(s);
}

[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldWorkCorrectly_WhenExcludingSpecificProperty()
{
var printer = ObjectPrinter
.For<Person>()
.Excluding(p => p.Height);
//6. Исключение из сериализации конкретного свойства/поля

var s = printer.PrintToString(person);

Approvals.Verify(s);
}
[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldWorkCorrectly_WhenChainingSerializationRules()
{
person.Name = "Maxwell";
var printer = ObjectPrinter
.For<Person>()
.Excluding<Guid>()
//2. Указать альтернативный способ сериализации для определенного типа
.Printing<int>().Using(i => i.ToString("X"))
//3. Для числовых типов указать культуру
.Printing<DateTime>().Using(i => i.ToLongDateString())
.Printing<double>().Using(CultureInfo.InvariantCulture)
//4. Настроить сериализацию конкретного свойства
.Printing(p => p.Name)
//5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств)
.TrimmedToLength(10)
//6. Исключить из сериализации конкретного свойства
.Printing(p => p.Name).TrimmedToLength(5)
.Excluding(p => p.Age);

var s1 = printer.PrintToString(person);
var s = printer.PrintToString(person);

Approvals.Verify(s);
}

[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldWorkCorrectly_WhenUsingExtensionMethod()
{
var s = person.PrintToString();
//7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию

//7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию
var s2 = person.PrintToString();
Approvals.Verify(s);
}

[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldWorkCorrectly_WithConfiguration()
{
var s = person.PrintToString(s => s.Excluding(p => p.Age));
//8. ...с конфигурированием
var s3 = person.PrintToString(s => s.Excluding(p => p.Age));

Approvals.Verify(s);
}

[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldDetectCyclicReferences()
{
var father = new Person
{
Name = "Pavel Doe",
Age = 68,
DateOfBirth = new DateTime(1954, 9, 9),
Father = person
};
person.Father = father;

var s = person.PrintToString();

Approvals.Verify(s);
}

[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldWorkCorrectly_WhenSerializingDictionary()
{
person.Addresses = new Dictionary<int, string>
{
{ 1, "London" },
{ 2, "New York" },
{ 3, "Moscow" }
};

var s = person.PrintToString();

Approvals.Verify(s);
}

[Test]
[UseReporter(typeof(DiffReporter))]
public void PrintToString_ShouldWorkCorrectly_WhenSerializingEnumerable()
{
person.Children =
[
new Person { Name = "Natasha", Age = 8, Height = 1, DateOfBirth = new DateTime(2002, 9 , 9) },
new Person { Name = "Pasha", Age = 9, Height = 2, DateOfBirth = new DateTime(2004, 9 , 9) },
];

var s = person.PrintToString();

Approvals.Verify(s);
}
}
Loading

0 comments on commit a888740

Please sign in to comment.