-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
343 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.Demo.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
17 changes: 17 additions & 0 deletions
17
...ests/ObjectPrinterAcceptanceTests.PrintToString_ShouldDetectCyclicReferences.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
7 changes: 7 additions & 0 deletions
7
...ptanceTests.PrintToString_ShouldWorkCorrectly_WhenChainingSerializationRules.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
8 changes: 8 additions & 0 deletions
8
...eptanceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingSpecificProperty.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
8 changes: 8 additions & 0 deletions
8
...ceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingTypeFromSerialization.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
15 changes: 15 additions & 0 deletions
15
...rAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingDictionary.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
27 changes: 27 additions & 0 deletions
27
...rAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingEnumerable.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
...anceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingWithCustomCulture.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
...ceptanceTests.PrintToString_ShouldWorkCorrectly_WhenTrimmingStringProperties.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
...erAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenUsingExtensionMethod.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
8 changes: 8 additions & 0 deletions
8
...ctPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithConfiguration.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
...sts.PrintToString_ShouldWorkCorrectly_WithCustomSerializationForSpecificType.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.