From a88874081a44ee8d287de2cd2b64552fb65154a8 Mon Sep 17 00:00:00 2001 From: Maxim Date: Wed, 11 Dec 2024 21:44:29 +0500 Subject: [PATCH] Added approval tests --- ObjectPrinting/ObjectPrinting.csproj | 37 ++++ ...ctPrinterAcceptanceTests.Demo.approved.txt | 9 + ..._ShouldDetectCyclicReferences.approved.txt | 17 ++ ...henChainingSerializationRules.approved.txt | 7 + ...WhenExcludingSpecificProperty.approved.txt | 8 + ...xcludingTypeFromSerialization.approved.txt | 8 + ...tly_WhenSerializingDictionary.approved.txt | 15 ++ ...tly_WhenSerializingEnumerable.approved.txt | 27 +++ ...nSerializingWithCustomCulture.approved.txt | 9 + ..._WhenTrimmingStringProperties.approved.txt | 9 + ...ctly_WhenUsingExtensionMethod.approved.txt | 9 + ...rkCorrectly_WithConfiguration.approved.txt | 8 + ...mSerializationForSpecificType.approved.txt | 9 + .../Tests/ObjectPrinterAcceptanceTests.cs | 181 ++++++++++++++++-- ObjectPrinting/Tests/Person.cs | 5 + 15 files changed, 343 insertions(+), 15 deletions(-) create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.Demo.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldDetectCyclicReferences.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenChainingSerializationRules.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingSpecificProperty.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingTypeFromSerialization.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingDictionary.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingEnumerable.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingWithCustomCulture.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenTrimmingStringProperties.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenUsingExtensionMethod.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithConfiguration.approved.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithCustomSerializationForSpecificType.approved.txt diff --git a/ObjectPrinting/ObjectPrinting.csproj b/ObjectPrinting/ObjectPrinting.csproj index c5db392f..38f21d13 100644 --- a/ObjectPrinting/ObjectPrinting.csproj +++ b/ObjectPrinting/ObjectPrinting.csproj @@ -5,8 +5,45 @@ + + + + + ObjectPrinterAcceptanceTests.cs + + + ObjectPrinterAcceptanceTests.cs + + + ObjectPrinterAcceptanceTests.cs + + + ObjectPrinterAcceptanceTests.cs + + + ObjectPrinterAcceptanceTests.cs + + + ObjectPrinterAcceptanceTests.cs + + + ObjectPrinterAcceptanceTests.cs + + + ObjectPrinterAcceptanceTests.cs + + + ObjectPrinterAcceptanceTests.cs + + + ObjectPrinterAcceptanceTests.cs + + + ObjectPrinterAcceptanceTests.cs + + diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.Demo.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.Demo.approved.txt new file mode 100644 index 00000000..7720eadd --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.Demo.approved.txt @@ -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 diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldDetectCyclicReferences.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldDetectCyclicReferences.approved.txt new file mode 100644 index 00000000..3d662ca0 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldDetectCyclicReferences.approved.txt @@ -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! diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenChainingSerializationRules.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenChainingSerializationRules.approved.txt new file mode 100644 index 00000000..2d80b15d --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenChainingSerializationRules.approved.txt @@ -0,0 +1,7 @@ +Person + Name = Maxwe + Height = 1.81 + DateOfBirth = понедельник, 1 января 0001 г. + Addresses = null + Children = null + Father = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingSpecificProperty.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingSpecificProperty.approved.txt new file mode 100644 index 00000000..e41aa8de --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingSpecificProperty.approved.txt @@ -0,0 +1,8 @@ +Person + Id = Guid + Name = Alex + Age = 19 + DateOfBirth = 01.01.0001 00:00:00 + Addresses = null + Children = null + Father = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingTypeFromSerialization.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingTypeFromSerialization.approved.txt new file mode 100644 index 00000000..34bd8b56 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenExcludingTypeFromSerialization.approved.txt @@ -0,0 +1,8 @@ +Person + Id = Guid + Name = Alex + Height = 1,81 + Age = 19 + Addresses = null + Children = null + Father = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingDictionary.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingDictionary.approved.txt new file mode 100644 index 00000000..5a3341db --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingDictionary.approved.txt @@ -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 diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingEnumerable.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingEnumerable.approved.txt new file mode 100644 index 00000000..843105cf --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingEnumerable.approved.txt @@ -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 diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingWithCustomCulture.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingWithCustomCulture.approved.txt new file mode 100644 index 00000000..898e63a0 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenSerializingWithCustomCulture.approved.txt @@ -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 diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenTrimmingStringProperties.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenTrimmingStringProperties.approved.txt new file mode 100644 index 00000000..43777d58 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenTrimmingStringProperties.approved.txt @@ -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 diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenUsingExtensionMethod.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenUsingExtensionMethod.approved.txt new file mode 100644 index 00000000..ed3539cb --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WhenUsingExtensionMethod.approved.txt @@ -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 diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithConfiguration.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithConfiguration.approved.txt new file mode 100644 index 00000000..9599a2a8 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithConfiguration.approved.txt @@ -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 diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithCustomSerializationForSpecificType.approved.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithCustomSerializationForSpecificType.approved.txt new file mode 100644 index 00000000..40937f92 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PrintToString_ShouldWorkCorrectly_WithCustomSerializationForSpecificType.approved.txt @@ -0,0 +1,9 @@ +Person + Id = Guid + Name = Alex + Height = 1,81 + Age = 19 + DateOfBirth = четверг, 9 сентября 2004 г. + Addresses = null + Children = null + Father = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs index 36e9d9bf..58d16232 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; using System.Globalization; +using ApprovalTests; +using ApprovalTests.Reporters; using NUnit.Framework; namespace ObjectPrinting.Tests; @@ -7,30 +10,178 @@ 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(); + var s = printer.PrintToString(person); - var printer = ObjectPrinter.For() - //1. Исключить из сериализации свойства определенного типа + Approvals.Verify(s); + } + + [Test] + [UseReporter(typeof(DiffReporter))] + public void PrintToString_ShouldWorkCorrectly_WhenExcludingTypeFromSerialization() + { + person.DateOfBirth = new DateTime(2004, 9, 9); + var printer = ObjectPrinter + .For() + .Excluding(); //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() + .Printing().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() + .Printing().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() + .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() + .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() .Excluding() - //2. Указать альтернативный способ сериализации для определенного типа - .Printing().Using(i => i.ToString("X")) - //3. Для числовых типов указать культуру + .Printing().Using(i => i.ToLongDateString()) .Printing().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 + { + { 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); } } \ No newline at end of file diff --git a/ObjectPrinting/Tests/Person.cs b/ObjectPrinting/Tests/Person.cs index 9fab80f4..7d2b19f2 100644 --- a/ObjectPrinting/Tests/Person.cs +++ b/ObjectPrinting/Tests/Person.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace ObjectPrinting.Tests; @@ -8,4 +9,8 @@ public class Person public string Name { get; set; } public double Height { get; set; } public int Age { get; set; } + public DateTime DateOfBirth { get; set; } + public Dictionary Addresses { get; set; } + public List Children { get; set; } + public Person Father { get; set; } } \ No newline at end of file