From dddcb303f3337739cfed92544baa6b776b4a302d Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Fri, 13 Dec 2024 11:33:36 +0500 Subject: [PATCH 01/11] first part --- ...r_ShouldExcludePropertyOfType.verified.txt | 7 ++ ....ObjectPrinter_ShouldFindLoop.verified.txt | 16 ++++ ...bjectPrinter_ShouldSetCulture.verified.txt | 8 ++ ...r_ShouldSetPrinterForProperty.verified.txt | 8 ++ ...inter_ShouldSetPrinterForType.verified.txt | 8 ++ ...tPrinter_ShouldTrimStringType.verified.txt | 8 ++ .../ObjectPrinting.Tests.csproj | 30 ++++++ .../Tests => ObjectPrinting.Tests}/Person.cs | 3 + ObjectPrinting.Tests/Tests.cs | 95 +++++++++++++++++++ ObjectPrinting/FinalTypes.cs | 16 ++++ ObjectPrinting/ObjectExtensions.cs | 11 +++ ObjectPrinting/ObjectPrinting.csproj | 1 + ObjectPrinting/PrintingConfig.cs | 95 +++++++++++++++---- ObjectPrinting/PropertyPrintingConfig.cs | 39 ++++++++ .../PropertyPrintingConfigExtensions.cs | 27 ++++++ ObjectPrinting/Solved/PrintingConfig.cs | 2 +- .../Tests/ObjectPrinterAcceptanceTests.cs | 27 ------ .../FluentMapping.Tests.csproj | 1 + Samples/FluentMapper/FluentMapping.csproj | 4 + Samples/Spectacle/Spectacle.csproj | 4 + fluent-api.sln | 18 ++-- fluent-api.sln.DotSettings | 3 + 22 files changed, 381 insertions(+), 50 deletions(-) create mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt create mode 100644 ObjectPrinting.Tests/ObjectPrinting.Tests.csproj rename {ObjectPrinting/Tests => ObjectPrinting.Tests}/Person.cs (64%) create mode 100644 ObjectPrinting.Tests/Tests.cs create mode 100644 ObjectPrinting/FinalTypes.cs create mode 100644 ObjectPrinting/ObjectExtensions.cs create mode 100644 ObjectPrinting/PropertyPrintingConfig.cs create mode 100644 ObjectPrinting/PropertyPrintingConfigExtensions.cs delete mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt new file mode 100644 index 00000000..d0f332a8 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt @@ -0,0 +1,7 @@ +Person + Name = Dima + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = + Wife = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt new file mode 100644 index 00000000..ca95e5d5 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt @@ -0,0 +1,16 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dima + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = + Wife = Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3302 + Name = Alla + Height = 170 + Age = 35 + BirthDate = 19.02.1989 0:00:00 + Husband = Loop found + Wife = + diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt new file mode 100644 index 00000000..f1616670 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt @@ -0,0 +1,8 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dima + Height = 176 + Age = 39 + BirthDate = 7/27/1985 12:00:00 AM + Husband = + Wife = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt new file mode 100644 index 00000000..df5a39b8 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt @@ -0,0 +1,8 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = XX + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = + Wife = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt new file mode 100644 index 00000000..9efa6a05 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt @@ -0,0 +1,8 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dima + Height = 176 + Age = XX + BirthDate = 27.07.1985 0:00:00 + Husband = + Wife = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt new file mode 100644 index 00000000..d864559c --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt @@ -0,0 +1,8 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dim + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = + Wife = diff --git a/ObjectPrinting.Tests/ObjectPrinting.Tests.csproj b/ObjectPrinting.Tests/ObjectPrinting.Tests.csproj new file mode 100644 index 00000000..90b49ac7 --- /dev/null +++ b/ObjectPrinting.Tests/ObjectPrinting.Tests.csproj @@ -0,0 +1,30 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + + + diff --git a/ObjectPrinting/Tests/Person.cs b/ObjectPrinting.Tests/Person.cs similarity index 64% rename from ObjectPrinting/Tests/Person.cs rename to ObjectPrinting.Tests/Person.cs index f9555955..ff45eb9d 100644 --- a/ObjectPrinting/Tests/Person.cs +++ b/ObjectPrinting.Tests/Person.cs @@ -8,5 +8,8 @@ public class Person public string Name { get; set; } public double Height { get; set; } public int Age { get; set; } + public DateTime BirthDate { get; set; } + public Person Husband { get; set; } + public Person Wife { get; set; } } } \ No newline at end of file diff --git a/ObjectPrinting.Tests/Tests.cs b/ObjectPrinting.Tests/Tests.cs new file mode 100644 index 00000000..5d488df8 --- /dev/null +++ b/ObjectPrinting.Tests/Tests.cs @@ -0,0 +1,95 @@ +using System.Globalization; + +namespace ObjectPrinting.Tests +{ + [TestFixture] + public class Tests + { + private static readonly VerifySettings DefaultSettings = new(); + private Person person; + + [SetUp] + public void Setup() + { + DefaultSettings.UseDirectory("ForVerifier"); + person = new Person + { + Id = new Guid("3f2504e0-4f89-11d3-9a0c-0305e82c3301"), + Age = 39, + Name = "Dima", + Height = 176.0, + BirthDate = new DateTime(1985, 07, 27) + }; + } + + [Test] + public Task ObjectPrinter_ShouldExcludePropertyOfType() + { + var result = ObjectPrinter.For() + .Excluding() + .PrintToString(person); + + return Verifier.Verify(result, DefaultSettings); + } + + [Test] + public Task ObjectPrinter_ShouldSetPrinterForType() + { + var result = ObjectPrinter.For() + .Printing().Using(i => "XX") + .PrintToString(person); + + return Verifier.Verify(result, DefaultSettings); + } + + [Test] + public Task ObjectPrinter_ShouldSetPrinterForProperty() + { + var result = ObjectPrinter.For() + .Printing(p => p.Name).Using(i => "XX") + .PrintToString(person); + + return Verifier.Verify(result, DefaultSettings); + } + + [Test] + public Task ObjectPrinter_ShouldSetCulture() + { + var result = ObjectPrinter.For() + .Printing().SetCulture(new CultureInfo("en")) + .PrintToString(person); + + return Verifier.Verify(result, DefaultSettings); + } + + [Test] + public Task ObjectPrinter_ShouldTrimStringType() + { + var result = ObjectPrinter.For() + .Printing().TrimmedToLength(3) + .PrintToString(person); + + return Verifier.Verify(result, DefaultSettings); + } + + [Test] + public Task ObjectPrinter_ShouldFindLoop() + { + var spouse = new Person() + { + Id = new Guid("3f2504e0-4f89-11d3-9a0c-0305e82c3302"), + Age = 35, + Name = "Alla", + Height = 170.0, + BirthDate = new DateTime(1989, 2, 19), + Husband = person + }; + person.Wife = spouse; + + var result = ObjectPrinter.For() + .PrintToString(person); + + return Verifier.Verify(result, DefaultSettings); + } + } +} \ No newline at end of file diff --git a/ObjectPrinting/FinalTypes.cs b/ObjectPrinting/FinalTypes.cs new file mode 100644 index 00000000..a77137f8 --- /dev/null +++ b/ObjectPrinting/FinalTypes.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ObjectPrinting +{ + public class FinalTypes + { + public static readonly HashSet Set = [ + typeof(int), typeof(double), typeof(float), typeof(string), + typeof(DateTime), typeof(TimeSpan), typeof(Guid) + ]; + } +} diff --git a/ObjectPrinting/ObjectExtensions.cs b/ObjectPrinting/ObjectExtensions.cs new file mode 100644 index 00000000..4ff79945 --- /dev/null +++ b/ObjectPrinting/ObjectExtensions.cs @@ -0,0 +1,11 @@ +namespace ObjectPrinting +{ + + public static class ObjectExtensions + { + public static string PrintToString(this T obj) + { + return ObjectPrinter.For().PrintToString(obj); + } + } +} \ No newline at end of file diff --git a/ObjectPrinting/ObjectPrinting.csproj b/ObjectPrinting/ObjectPrinting.csproj index c5db392f..6cd512b5 100644 --- a/ObjectPrinting/ObjectPrinting.csproj +++ b/ObjectPrinting/ObjectPrinting.csproj @@ -8,5 +8,6 @@ + diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index a9e08211..663ed6d4 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -1,41 +1,104 @@ -using System; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; using System.Linq; +using System.Linq.Expressions; using System.Text; +using NUnit.Framework.Internal; namespace ObjectPrinting { public class PrintingConfig { + private readonly HashSet typesToExclude = []; + private readonly HashSet propertiesToExclude = []; + private readonly Dictionary> typePrinters = []; + private readonly Dictionary> propertyPrinters = []; + private readonly HashSet visitedObjects = []; + + public PropertyPrintingConfig Printing() + { + return new PropertyPrintingConfig(this); + } + + public PropertyPrintingConfig Printing(Expression> memberSelector) + { + return new PropertyPrintingConfig(this); + } + + public PrintingConfig Excluding(Expression> memberSelector) + { + if (memberSelector.Body is not MemberExpression memberExpression) + throw new ArgumentException("Wrong Expression"); + propertiesToExclude.Add(memberExpression.Member.Name); + return this; + } + + public PrintingConfig Excluding() + { + typesToExclude.Add(typeof(TPropType)); + return this; + } + public string PrintToString(TOwner obj) { return PrintToString(obj, 0); } - private string PrintToString(object obj, int nestingLevel) { - //TODO apply configurations if (obj == null) - return "null" + Environment.NewLine; + return string.Empty; - var finalTypes = new[] - { - typeof(int), typeof(double), typeof(float), typeof(string), - typeof(DateTime), typeof(TimeSpan) - }; - if (finalTypes.Contains(obj.GetType())) - return obj + Environment.NewLine; + var type = obj.GetType(); - var identation = new string('\t', nestingLevel + 1); + if (visitedObjects.Contains(obj)) + return "Loop found"; + + if (typesToExclude.Contains(type)) + return string.Empty; + + visitedObjects.Add(obj); + + if (typePrinters.TryGetValue(type, out var serializer)) + return serializer(obj); + + if (FinalTypes.Set.Contains(type)) + return obj.ToString(); + + var indentation = new string('\t', nestingLevel + 1); var sb = new StringBuilder(); - var type = obj.GetType(); sb.AppendLine(type.Name); + foreach (var propertyInfo in type.GetProperties()) { - sb.Append(identation + propertyInfo.Name + " = " + - PrintToString(propertyInfo.GetValue(obj), - nestingLevel + 1)); + var propertyType = propertyInfo.PropertyType; + var propertyName = propertyInfo.Name; + var propertyValue = propertyInfo.GetValue(obj); + + if (propertyPrinters.TryGetValue(propertyName, out var propertySerializer)) + { + _ = sb.AppendLine(propertySerializer(propertyValue)); + continue; + } + + if (typesToExclude.Contains(propertyType) || propertiesToExclude.Contains(propertyName)) + continue; + + sb.Append(indentation + propertyName + " = "); + _ = sb.AppendLine(PrintToString(propertyValue, nestingLevel + 1)); } return sb.ToString(); } + + public void AddTypePrinter(Func print) + { + typePrinters[typeof(TPropType)] = obj => print((TPropType)obj); + } + + internal void AddPropertryPrinter(string propName, Func print) + { + propertyPrinters[propName] = print; + } } } \ No newline at end of file diff --git a/ObjectPrinting/PropertyPrintingConfig.cs b/ObjectPrinting/PropertyPrintingConfig.cs new file mode 100644 index 00000000..127ea063 --- /dev/null +++ b/ObjectPrinting/PropertyPrintingConfig.cs @@ -0,0 +1,39 @@ +using System; +using System.Globalization; + +namespace ObjectPrinting +{ + public class PropertyPrintingConfig : IPropertyPrintingConfig + { + private readonly PrintingConfig printingConfig; + private readonly string propName; + + public PropertyPrintingConfig(PrintingConfig printingConfig, string propName = null) + { + this.printingConfig = printingConfig; + this.propName = propName; + } + + public PrintingConfig Using(Func print) + { + if(propName == null) + this.printingConfig.AddTypePrinter(print); + else + printingConfig.AddPropertryPrinter(propName, x => print((TPropType)x)); + return printingConfig; + } + + //public PrintingConfig Using(CultureInfo culture) + //{ + // return printingConfig; + //} + + PrintingConfig IPropertyPrintingConfig.ParentConfig => printingConfig; + } + + public interface IPropertyPrintingConfig + { + PrintingConfig ParentConfig { get; } + public PrintingConfig Using(Func print); + } +} \ No newline at end of file diff --git a/ObjectPrinting/PropertyPrintingConfigExtensions.cs b/ObjectPrinting/PropertyPrintingConfigExtensions.cs new file mode 100644 index 00000000..97656d53 --- /dev/null +++ b/ObjectPrinting/PropertyPrintingConfigExtensions.cs @@ -0,0 +1,27 @@ +using System; +using System.Globalization; + +namespace ObjectPrinting +{ + public static class PropertyPrintingConfigExtensions + { + public static string PrintToString(this T obj, Func, PrintingConfig> config) + { + return config(ObjectPrinter.For()).PrintToString(obj); + } + + public static PrintingConfig SetCulture + (this IPropertyPrintingConfig config, + CultureInfo culture) + where TPropType : IFormattable + { + return config.Using(x => x.ToString(null, culture)); + } + + public static PrintingConfig TrimmedToLength(this PropertyPrintingConfig propConfig, int maxLen) + { + return propConfig.Using(x => x[..Math.Min(x.Length, maxLen)]); + } + } +} \ No newline at end of file diff --git a/ObjectPrinting/Solved/PrintingConfig.cs b/ObjectPrinting/Solved/PrintingConfig.cs index 0ec5aeb2..1cdc571a 100644 --- a/ObjectPrinting/Solved/PrintingConfig.cs +++ b/ObjectPrinting/Solved/PrintingConfig.cs @@ -32,7 +32,7 @@ public string PrintToString(TOwner obj) return PrintToString(obj, 0); } - private string PrintToString(object obj, int nestingLevel) + private static string PrintToString(object obj, int nestingLevel) { //TODO apply configurations if (obj == null) diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs deleted file mode 100644 index 4c8b2445..00000000 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs +++ /dev/null @@ -1,27 +0,0 @@ -using NUnit.Framework; - -namespace ObjectPrinting.Tests -{ - [TestFixture] - public class ObjectPrinterAcceptanceTests - { - [Test] - public void Demo() - { - var person = new Person { Name = "Alex", Age = 19 }; - - var printer = ObjectPrinter.For(); - //1. Исключить из сериализации свойства определенного типа - //2. Указать альтернативный способ сериализации для определенного типа - //3. Для числовых типов указать культуру - //4. Настроить сериализацию конкретного свойства - //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) - //6. Исключить из сериализации конкретного свойства - - string s1 = printer.PrintToString(person); - - //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию - //8. ...с конфигурированием - } - } -} \ No newline at end of file diff --git a/Samples/FluentMapper.Tests/FluentMapping.Tests.csproj b/Samples/FluentMapper.Tests/FluentMapping.Tests.csproj index 3828c566..a580ad54 100644 --- a/Samples/FluentMapper.Tests/FluentMapping.Tests.csproj +++ b/Samples/FluentMapper.Tests/FluentMapping.Tests.csproj @@ -9,6 +9,7 @@ + diff --git a/Samples/FluentMapper/FluentMapping.csproj b/Samples/FluentMapper/FluentMapping.csproj index fa71b7ae..a4f2550a 100644 --- a/Samples/FluentMapper/FluentMapping.csproj +++ b/Samples/FluentMapper/FluentMapping.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/Samples/Spectacle/Spectacle.csproj b/Samples/Spectacle/Spectacle.csproj index fa71b7ae..a4f2550a 100644 --- a/Samples/Spectacle/Spectacle.csproj +++ b/Samples/Spectacle/Spectacle.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/fluent-api.sln b/fluent-api.sln index 69c8db9e..b4a001bb 100644 --- a/fluent-api.sln +++ b/fluent-api.sln @@ -1,17 +1,19 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.25123.0 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPrinting", "ObjectPrinting\ObjectPrinting.csproj", "{07B8C9B7-8289-46CB-9875-048A57758EEE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ObjectPrinting", "ObjectPrinting\ObjectPrinting.csproj", "{07B8C9B7-8289-46CB-9875-048A57758EEE}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{6D308E4A-CEC7-4536-9B87-81CD337A87AD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMapping", "Samples\FluentMapper\FluentMapping.csproj", "{FEEA5AFE-459A-4D13-81D0-252E1A2E6F4E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentMapping", "Samples\FluentMapper\FluentMapping.csproj", "{FEEA5AFE-459A-4D13-81D0-252E1A2E6F4E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMapping.Tests", "Samples\FluentMapper.Tests\FluentMapping.Tests.csproj", "{8A7BB3EA-3E6A-4D04-A801-D5CD1620DA0D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FluentMapping.Tests", "Samples\FluentMapper.Tests\FluentMapping.Tests.csproj", "{8A7BB3EA-3E6A-4D04-A801-D5CD1620DA0D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spectacle", "Samples\Spectacle\Spectacle.csproj", "{EFA9335C-411B-4597-B0B6-5438D1AE04C3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spectacle", "Samples\Spectacle\Spectacle.csproj", "{EFA9335C-411B-4597-B0B6-5438D1AE04C3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPrinting.Tests", "ObjectPrinting.Tests\ObjectPrinting.Tests.csproj", "{13FB7EA2-3C1E-442A-A43A-8246005FD7BE}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -35,6 +37,10 @@ Global {EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.ActiveCfg = Release|Any CPU {EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.Build.0 = Release|Any CPU + {13FB7EA2-3C1E-442A-A43A-8246005FD7BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {13FB7EA2-3C1E-442A-A43A-8246005FD7BE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {13FB7EA2-3C1E-442A-A43A-8246005FD7BE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {13FB7EA2-3C1E-442A-A43A-8246005FD7BE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/fluent-api.sln.DotSettings b/fluent-api.sln.DotSettings index 135b83ec..229f449d 100644 --- a/fluent-api.sln.DotSettings +++ b/fluent-api.sln.DotSettings @@ -1,6 +1,9 @@  <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + <Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></Policy> + <Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Types and namespaces"><ElementKinds><Kind Name="NAMESPACE" /><Kind Name="CLASS" /><Kind Name="STRUCT" /><Kind Name="ENUM" /><Kind Name="DELEGATE" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy> + True True True Imported 10.10.2016 From a7d6f253ba83723271760bdc21dceaf68c23aa86 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Fri, 13 Dec 2024 14:36:22 +0500 Subject: [PATCH 02/11] add collection printer --- ...r_ShouldExcludePropertyOfType.verified.txt | 3 ++ ....ObjectPrinter_ShouldFindLoop.verified.txt | 6 +++ ...bjectPrinter_ShouldPrintArray.verified.txt | 35 +++++++++++++ ...Printer_ShouldPrintCollection.verified.txt | 31 +++++++++++ ...Printer_ShouldPrintDictionary.verified.txt | 23 +++++++++ ...ObjectPrinter_ShouldPrintList.verified.txt | 15 ++++++ ...bjectPrinter_ShouldSetCulture.verified.txt | 3 ++ ...r_ShouldSetPrinterForProperty.verified.txt | 3 ++ ...inter_ShouldSetPrinterForType.verified.txt | 3 ++ ...tPrinter_ShouldTrimStringType.verified.txt | 3 ++ ObjectPrinting.Tests/Person.cs | 4 ++ ObjectPrinting.Tests/Tests.cs | 51 +++++++++++++++++++ ObjectPrinting/PrintingConfig.cs | 25 ++++++++- ObjectPrinting/PropertyPrintingConfig.cs | 2 +- 14 files changed, 204 insertions(+), 3 deletions(-) create mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintArray.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintCollection.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintList.verified.txt diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt index d0f332a8..d482f1e9 100644 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt @@ -5,3 +5,6 @@ BirthDate = 27.07.1985 0:00:00 Husband = Wife = + HomeworksCompleted = + Friends = + EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt index ca95e5d5..cff2abfa 100644 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt @@ -13,4 +13,10 @@ BirthDate = 19.02.1989 0:00:00 Husband = Loop found Wife = + HomeworksCompleted = + Friends = + EmergensyList = + HomeworksCompleted = + Friends = + EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintArray.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintArray.verified.txt new file mode 100644 index 00000000..ba92449b --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintArray.verified.txt @@ -0,0 +1,35 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dima + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = + Wife = + HomeworksCompleted = + Friends = + Person + Id = 00000000-0000-0000-0000-000000000000 + Name = Nikita + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 + Husband = + Wife = + HomeworksCompleted = + Friends = + EmergensyList = + + Person + Id = Loop found + Name = Pasha + Height = Loop found + Age = Loop found + BirthDate = Loop found + Husband = + Wife = + HomeworksCompleted = + Friends = + EmergensyList = + + EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintCollection.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintCollection.verified.txt new file mode 100644 index 00000000..99ec8f6f --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintCollection.verified.txt @@ -0,0 +1,31 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dima + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = + Wife = + Friends = + Person + Id = 00000000-0000-0000-0000-000000000000 + Name = Nikita + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 + Husband = + Wife = + Friends = + EmergensyList = + Person + Id = Loop found + Name = Pasha + Height = Loop found + Age = Loop found + BirthDate = Loop found + Husband = + Wife = + Friends = + EmergensyList = + + EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt new file mode 100644 index 00000000..5279a14a --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt @@ -0,0 +1,23 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dima + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = + Wife = + HomeworksCompleted = + Friends = + EmergensyList = + KeyValuePair`2 + Key = wife + Value = 5-5-5-3-5-3-5 + + KeyValuePair`2 + Key = father + Value = 8-800-2000-600 + + KeyValuePair`2 + Key = mother + Value = +7 922 22 222 22 + diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintList.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintList.verified.txt new file mode 100644 index 00000000..c10da4d2 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintList.verified.txt @@ -0,0 +1,15 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dima + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = + Wife = + HomeworksCompleted = + Testing + Tag cloud + Markdown + Object Printer + Friends = + EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt index f1616670..681b9af9 100644 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt @@ -6,3 +6,6 @@ BirthDate = 7/27/1985 12:00:00 AM Husband = Wife = + HomeworksCompleted = + Friends = + EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt index df5a39b8..bd5f7da3 100644 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt @@ -6,3 +6,6 @@ BirthDate = 27.07.1985 0:00:00 Husband = Wife = + HomeworksCompleted = + Friends = + EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt index 9efa6a05..2cf765d1 100644 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt @@ -6,3 +6,6 @@ BirthDate = 27.07.1985 0:00:00 Husband = Wife = + HomeworksCompleted = + Friends = + EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt index d864559c..da53b566 100644 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt @@ -6,3 +6,6 @@ BirthDate = 27.07.1985 0:00:00 Husband = Wife = + HomeworksCompleted = + Friends = + EmergensyList = diff --git a/ObjectPrinting.Tests/Person.cs b/ObjectPrinting.Tests/Person.cs index ff45eb9d..cdfd0945 100644 --- a/ObjectPrinting.Tests/Person.cs +++ b/ObjectPrinting.Tests/Person.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; namespace ObjectPrinting.Tests { @@ -11,5 +12,8 @@ public class Person public DateTime BirthDate { get; set; } public Person Husband { get; set; } public Person Wife { get; set; } + public List HomeworksCompleted { get; set; } + public Person[] Friends { get; set; } + public Dictionary EmergensyList { get; set; } } } \ No newline at end of file diff --git a/ObjectPrinting.Tests/Tests.cs b/ObjectPrinting.Tests/Tests.cs index 5d488df8..f207aba4 100644 --- a/ObjectPrinting.Tests/Tests.cs +++ b/ObjectPrinting.Tests/Tests.cs @@ -91,5 +91,56 @@ public Task ObjectPrinter_ShouldFindLoop() return Verifier.Verify(result, DefaultSettings); } + + [Test] + public Task ObjectPrinter_ShouldPrintList() + { + var homeworks = new List() + { + "Testing", + "Tag cloud", + "Markdown", + "Object Printer" + }; + person.HomeworksCompleted = homeworks; + + var result = ObjectPrinter.For() + .PrintToString(person); + + return Verifier.Verify(result, DefaultSettings); + } + + [Test] + public Task ObjectPrinter_ShouldPrintArray() + { + var friends = new[] + { + new Person() { Name = "Nikita" }, + new Person() { Name = "Pasha" } + }; + person.Friends = friends; + + var result = ObjectPrinter.For() + .PrintToString(person); + + return Verifier.Verify(result, DefaultSettings); + } + + [Test] + public Task ObjectPrinter_ShouldPrintDictionary() + { + var emergencyCallList = new Dictionary() + { + {"wife","5-5-5-3-5-3-5"}, + {"father", "8-800-2000-600"}, + {"mother", "+7 922 22 222 22"} + }; + person.EmergensyList = emergencyCallList; + + var result = ObjectPrinter.For() + .PrintToString(person); + + return Verifier.Verify(result, DefaultSettings); + } } } \ No newline at end of file diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 663ed6d4..6814e939 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -60,6 +60,13 @@ private string PrintToString(object obj, int nestingLevel) visitedObjects.Add(obj); + if (obj is ICollection) + { + var collection = (IEnumerable) obj; + return PrintCollection(collection, nestingLevel); + } + + if (typePrinters.TryGetValue(type, out var serializer)) return serializer(obj); @@ -81,7 +88,7 @@ private string PrintToString(object obj, int nestingLevel) _ = sb.AppendLine(propertySerializer(propertyValue)); continue; } - + if (typesToExclude.Contains(propertyType) || propertiesToExclude.Contains(propertyName)) continue; @@ -91,12 +98,26 @@ private string PrintToString(object obj, int nestingLevel) return sb.ToString(); } + private string PrintCollection(IEnumerable collection, int nestingLevel) + { + var indentation = new string('\t', nestingLevel + 1); + var sb = new StringBuilder(); + + foreach (var element in collection) + { + sb.Append("\n"); + sb.Append(indentation); + _ = sb.Append(PrintToString(element, nestingLevel + 1)); + } + return sb.ToString(); + } + public void AddTypePrinter(Func print) { typePrinters[typeof(TPropType)] = obj => print((TPropType)obj); } - internal void AddPropertryPrinter(string propName, Func print) + internal void AddPropertyPrinter(string propName, Func print) { propertyPrinters[propName] = print; } diff --git a/ObjectPrinting/PropertyPrintingConfig.cs b/ObjectPrinting/PropertyPrintingConfig.cs index 127ea063..bcf4112e 100644 --- a/ObjectPrinting/PropertyPrintingConfig.cs +++ b/ObjectPrinting/PropertyPrintingConfig.cs @@ -19,7 +19,7 @@ public PrintingConfig Using(Func print) if(propName == null) this.printingConfig.AddTypePrinter(print); else - printingConfig.AddPropertryPrinter(propName, x => print((TPropType)x)); + printingConfig.AddPropertyPrinter(propName, x => print((TPropType)x)); return printingConfig; } From 8c5a9a9bc0d58e236527c2fbb7bcb4d75f89b367 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Mon, 16 Dec 2024 21:07:18 +0500 Subject: [PATCH 03/11] string trimming with test --- ObjectPrinting.Tests/Tests.cs | 7 ++++--- ObjectPrinting/FinalTypes.cs | 12 +++++++++--- ObjectPrinting/PropertyPrintingConfig.cs | 11 ++--------- ObjectPrinting/PropertyPrintingConfigExtensions.cs | 5 ++++- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/ObjectPrinting.Tests/Tests.cs b/ObjectPrinting.Tests/Tests.cs index f207aba4..bd5a7551 100644 --- a/ObjectPrinting.Tests/Tests.cs +++ b/ObjectPrinting.Tests/Tests.cs @@ -62,11 +62,12 @@ public Task ObjectPrinter_ShouldSetCulture() return Verifier.Verify(result, DefaultSettings); } - [Test] - public Task ObjectPrinter_ShouldTrimStringType() + [TestCase(10, TestName = "Print original string when its length smaller than needed") ] + [TestCase(3, TestName = "Print trimmed string in right way")] + public Task ObjectPrinter_ShouldTrimStringType(int maxLen) { var result = ObjectPrinter.For() - .Printing().TrimmedToLength(3) + .Printing().TrimmedToLength(maxLen) .PrintToString(person); return Verifier.Verify(result, DefaultSettings); diff --git a/ObjectPrinting/FinalTypes.cs b/ObjectPrinting/FinalTypes.cs index a77137f8..80842774 100644 --- a/ObjectPrinting/FinalTypes.cs +++ b/ObjectPrinting/FinalTypes.cs @@ -8,9 +8,15 @@ namespace ObjectPrinting { public class FinalTypes { - public static readonly HashSet Set = [ - typeof(int), typeof(double), typeof(float), typeof(string), - typeof(DateTime), typeof(TimeSpan), typeof(Guid) + public static readonly HashSet Set = + [ + typeof(int), + typeof(double), + typeof(float), + typeof(string), + typeof(DateTime), + typeof(TimeSpan), + typeof(Guid) ]; } } diff --git a/ObjectPrinting/PropertyPrintingConfig.cs b/ObjectPrinting/PropertyPrintingConfig.cs index bcf4112e..1281f118 100644 --- a/ObjectPrinting/PropertyPrintingConfig.cs +++ b/ObjectPrinting/PropertyPrintingConfig.cs @@ -3,7 +3,8 @@ namespace ObjectPrinting { - public class PropertyPrintingConfig : IPropertyPrintingConfig + public class PropertyPrintingConfig + : IPropertyPrintingConfig { private readonly PrintingConfig printingConfig; private readonly string propName; @@ -22,18 +23,10 @@ public PrintingConfig Using(Func print) printingConfig.AddPropertyPrinter(propName, x => print((TPropType)x)); return printingConfig; } - - //public PrintingConfig Using(CultureInfo culture) - //{ - // return printingConfig; - //} - - PrintingConfig IPropertyPrintingConfig.ParentConfig => printingConfig; } public interface IPropertyPrintingConfig { - PrintingConfig ParentConfig { get; } public PrintingConfig Using(Func print); } } \ No newline at end of file diff --git a/ObjectPrinting/PropertyPrintingConfigExtensions.cs b/ObjectPrinting/PropertyPrintingConfigExtensions.cs index 97656d53..2005ee51 100644 --- a/ObjectPrinting/PropertyPrintingConfigExtensions.cs +++ b/ObjectPrinting/PropertyPrintingConfigExtensions.cs @@ -21,7 +21,10 @@ public static PrintingConfig SetCulture public static PrintingConfig TrimmedToLength(this PropertyPrintingConfig propConfig, int maxLen) { - return propConfig.Using(x => x[..Math.Min(x.Length, maxLen)]); + return propConfig.Using(x => + (x.Length > maxLen) + ? x[..maxLen] + : x); } } } \ No newline at end of file From 4a5af90029db1cd263ced505a66521bd36fc307e Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Mon, 16 Dec 2024 21:35:07 +0500 Subject: [PATCH 04/11] add dictionary printer --- ...Printer_ShouldPrintDictionary.verified.txt | 18 ++++-------- ObjectPrinting/PrintingConfig.cs | 28 +++++++++++++++++-- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt index 5279a14a..2f91fd85 100644 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt @@ -9,15 +9,9 @@ HomeworksCompleted = Friends = EmergensyList = - KeyValuePair`2 - Key = wife - Value = 5-5-5-3-5-3-5 - - KeyValuePair`2 - Key = father - Value = 8-800-2000-600 - - KeyValuePair`2 - Key = mother - Value = +7 922 22 222 22 - + key = wife + value = 5-5-5-3-5-3-5 + key = father + value = 8-800-2000-600 + key = mother + value = +7 922 22 222 22 diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 6814e939..3a48b254 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -85,7 +85,7 @@ private string PrintToString(object obj, int nestingLevel) if (propertyPrinters.TryGetValue(propertyName, out var propertySerializer)) { - _ = sb.AppendLine(propertySerializer(propertyValue)); + sb.AppendLine(propertySerializer(propertyValue)); continue; } @@ -93,13 +93,16 @@ private string PrintToString(object obj, int nestingLevel) continue; sb.Append(indentation + propertyName + " = "); - _ = sb.AppendLine(PrintToString(propertyValue, nestingLevel + 1)); + sb.AppendLine(PrintToString(propertyValue, nestingLevel + 1)); } return sb.ToString(); } private string PrintCollection(IEnumerable collection, int nestingLevel) { + if (collection is IDictionary) + return PrintDictionary( (IDictionary)collection, nestingLevel); + var indentation = new string('\t', nestingLevel + 1); var sb = new StringBuilder(); @@ -107,7 +110,26 @@ private string PrintCollection(IEnumerable collection, int nestingLevel) { sb.Append("\n"); sb.Append(indentation); - _ = sb.Append(PrintToString(element, nestingLevel + 1)); + sb.Append(PrintToString(element, nestingLevel + 1)); + } + return sb.ToString(); + } + + private string PrintDictionary(IDictionary collection, int nestingLevel) + { + var indentation = new string('\t', nestingLevel + 1); + var sb = new StringBuilder(); + + foreach (var key in collection.Keys) + { + sb.Append("\n"); + sb.Append(indentation); + sb.Append("key = "); + sb.Append(PrintToString(key, nestingLevel + 1)); + sb.Append("\n"); + sb.Append(indentation); + sb.Append("value = "); + sb.Append(PrintToString(collection[key], nestingLevel + 1)); } return sb.ToString(); } From 9918d0ee2e4c64fb57744b81a5e3965eda32602c Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Wed, 18 Dec 2024 18:00:17 +0500 Subject: [PATCH 05/11] add printing fields --- ObjectPrinting.Tests/Person.cs | 1 + ObjectPrinting/PrintingConfig.cs | 44 ++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/ObjectPrinting.Tests/Person.cs b/ObjectPrinting.Tests/Person.cs index cdfd0945..aaac9851 100644 --- a/ObjectPrinting.Tests/Person.cs +++ b/ObjectPrinting.Tests/Person.cs @@ -5,6 +5,7 @@ namespace ObjectPrinting.Tests { public class Person { + public string LastName; public Guid Id { get; set; } public string Name { get; set; } public double Height { get; set; } diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 3a48b254..6e249b57 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -4,6 +4,7 @@ using System.Globalization; using System.Linq; using System.Linq.Expressions; +using System.Reflection; using System.Text; using NUnit.Framework.Internal; @@ -48,7 +49,7 @@ public string PrintToString(TOwner obj) private string PrintToString(object obj, int nestingLevel) { if (obj == null) - return string.Empty; + return "null" ; var type = obj.GetType(); @@ -60,40 +61,49 @@ private string PrintToString(object obj, int nestingLevel) visitedObjects.Add(obj); - if (obj is ICollection) - { - var collection = (IEnumerable) obj; - return PrintCollection(collection, nestingLevel); - } - - if (typePrinters.TryGetValue(type, out var serializer)) return serializer(obj); if (FinalTypes.Set.Contains(type)) return obj.ToString(); + if (obj is ICollection) + { + var collection = (IEnumerable) obj; + return PrintCollection(collection, nestingLevel); + } + var indentation = new string('\t', nestingLevel + 1); var sb = new StringBuilder(); sb.AppendLine(type.Name); - foreach (var propertyInfo in type.GetProperties()) + foreach (var memberInfo in type + .GetProperties() + .Cast() + .Concat(type.GetFields(BindingFlags.Instance | BindingFlags.Public))) { - var propertyType = propertyInfo.PropertyType; - var propertyName = propertyInfo.Name; - var propertyValue = propertyInfo.GetValue(obj); + + var name = memberInfo.Name; + var value = memberInfo.GetValue(obj); + if (value == null) + { + sb.Append(indentation + name + " = null\n"); + continue; + } + + var propertyType = value.GetType(); - if (propertyPrinters.TryGetValue(propertyName, out var propertySerializer)) + if (propertyPrinters.TryGetValue(name, out var propertyPrinter)) { - sb.AppendLine(propertySerializer(propertyValue)); + sb.AppendLine(propertyPrinter(value)); continue; } - if (typesToExclude.Contains(propertyType) || propertiesToExclude.Contains(propertyName)) + if (typesToExclude.Contains(propertyType) || propertiesToExclude.Contains(name)) continue; - sb.Append(indentation + propertyName + " = "); - sb.AppendLine(PrintToString(propertyValue, nestingLevel + 1)); + sb.Append(indentation + name + " = "); + sb.AppendLine(PrintToString(value, nestingLevel + 1)); } return sb.ToString(); } From f412ddd2b94fc01282c87fbf5d59c05adfeae4f6 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Wed, 18 Dec 2024 19:27:25 +0500 Subject: [PATCH 06/11] cleaning unused packages --- ObjectPrinting.Tests/ObjectPrinting.Tests.csproj | 5 ++++- ObjectPrinting/ObjectPrinting.csproj | 4 ---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ObjectPrinting.Tests/ObjectPrinting.Tests.csproj b/ObjectPrinting.Tests/ObjectPrinting.Tests.csproj index 90b49ac7..23528238 100644 --- a/ObjectPrinting.Tests/ObjectPrinting.Tests.csproj +++ b/ObjectPrinting.Tests/ObjectPrinting.Tests.csproj @@ -1,4 +1,4 @@ - + net8.0 @@ -12,7 +12,10 @@ + + + diff --git a/ObjectPrinting/ObjectPrinting.csproj b/ObjectPrinting/ObjectPrinting.csproj index 6cd512b5..9a86d3d3 100644 --- a/ObjectPrinting/ObjectPrinting.csproj +++ b/ObjectPrinting/ObjectPrinting.csproj @@ -5,9 +5,5 @@ - - - - From 4988c4c9229668e8a6fc0dc4aa5806b099d9727c Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Wed, 18 Dec 2024 19:31:28 +0500 Subject: [PATCH 07/11] extensions according to its meaning --- ObjectPrinting/MemberInfoExtensions.cs | 21 +++++++++++++++++++ ObjectPrinting/ObjectExtensions.cs | 7 +++++++ .../PropertyPrintingConfigExtensions.cs | 5 ----- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 ObjectPrinting/MemberInfoExtensions.cs diff --git a/ObjectPrinting/MemberInfoExtensions.cs b/ObjectPrinting/MemberInfoExtensions.cs new file mode 100644 index 00000000..773da682 --- /dev/null +++ b/ObjectPrinting/MemberInfoExtensions.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace ObjectPrinting +{ + public static class MemberInfoExtensions + { public static object GetValue(this MemberInfo memberInfo, object forObject) + { + return memberInfo.MemberType switch + { + MemberTypes.Field => ((FieldInfo)memberInfo).GetValue(forObject), + MemberTypes.Property => ((PropertyInfo)memberInfo).GetValue(forObject), + _ => throw new NotSupportedException($"Unsupported member type {memberInfo.MemberType}"), + }; + } + } +} diff --git a/ObjectPrinting/ObjectExtensions.cs b/ObjectPrinting/ObjectExtensions.cs index 4ff79945..0d140304 100644 --- a/ObjectPrinting/ObjectExtensions.cs +++ b/ObjectPrinting/ObjectExtensions.cs @@ -1,3 +1,5 @@ +using System; + namespace ObjectPrinting { @@ -7,5 +9,10 @@ public static string PrintToString(this T obj) { return ObjectPrinter.For().PrintToString(obj); } + + public static string PrintToString(this T obj, Func, PrintingConfig> config) + { + return config(ObjectPrinter.For()).PrintToString(obj); + } } } \ No newline at end of file diff --git a/ObjectPrinting/PropertyPrintingConfigExtensions.cs b/ObjectPrinting/PropertyPrintingConfigExtensions.cs index 2005ee51..e00c191e 100644 --- a/ObjectPrinting/PropertyPrintingConfigExtensions.cs +++ b/ObjectPrinting/PropertyPrintingConfigExtensions.cs @@ -5,11 +5,6 @@ namespace ObjectPrinting { public static class PropertyPrintingConfigExtensions { - public static string PrintToString(this T obj, Func, PrintingConfig> config) - { - return config(ObjectPrinter.For()).PrintToString(obj); - } - public static PrintingConfig SetCulture (this IPropertyPrintingConfig config, CultureInfo culture) From ff94060181759d209d3f04898e5ba9ff8fcbdaa3 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Wed, 18 Dec 2024 20:01:14 +0500 Subject: [PATCH 08/11] tests refactoring --- .../ObjectPrinterTests.Demo.verified.txt | 48 ++++++ ...ShouldExcludeConcreteProperty.verified.txt | 48 ++++++ ...g_ShouldExcludePropertyOfType.verified.txt | 48 ++++++ ....ObjectPrinter_ShouldFindLoop.verified.txt | 2 + ...s.SetCulture_ShouldSetCulture.verified.txt | 51 ++++++ ...houldTrimStringType_maxLen=10.verified.txt | 51 ++++++ ...ShouldTrimStringType_maxLen=3.verified.txt | 51 ++++++ ...g_ShouldSetPrinterForProperty.verified.txt | 51 ++++++ ...Using_ShouldSetPrinterForType.verified.txt | 51 ++++++ ...r_ShouldExcludePropertyOfType.verified.txt | 10 -- ....ObjectPrinter_ShouldFindLoop.verified.txt | 22 --- ...bjectPrinter_ShouldPrintArray.verified.txt | 35 ----- ...Printer_ShouldPrintCollection.verified.txt | 31 ---- ...Printer_ShouldPrintDictionary.verified.txt | 17 -- ...ObjectPrinter_ShouldPrintList.verified.txt | 15 -- ...bjectPrinter_ShouldSetCulture.verified.txt | 11 -- ...r_ShouldSetPrinterForProperty.verified.txt | 11 -- ...inter_ShouldSetPrinterForType.verified.txt | 11 -- ...tPrinter_ShouldTrimStringType.verified.txt | 11 -- .../{Tests.cs => ObjectPrinterTests.cs} | 146 +++++++++--------- ObjectPrinting.Tests/Person.cs | 1 + ObjectPrinting/PrintingConfig.cs | 18 +-- .../Tests/ObjectPrinterAcceptanceTests.cs | 40 ----- ObjectPrinting/Solved/Tests/Person.cs | 12 -- 24 files changed, 483 insertions(+), 309 deletions(-) create mode 100644 ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Demo.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludeConcreteProperty.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludePropertyOfType.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.ObjectPrinter_ShouldFindLoop.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.SetCulture_ShouldSetCulture.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=10.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=3.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForProperty.verified.txt create mode 100644 ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForType.verified.txt delete mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt delete mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt delete mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintArray.verified.txt delete mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintCollection.verified.txt delete mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt delete mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintList.verified.txt delete mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt delete mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt delete mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt delete mode 100644 ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt rename ObjectPrinting.Tests/{Tests.cs => ObjectPrinterTests.cs} (53%) delete mode 100644 ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs delete mode 100644 ObjectPrinting/Solved/Tests/Person.cs diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Demo.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Demo.verified.txt new file mode 100644 index 00000000..f82e7157 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Demo.verified.txt @@ -0,0 +1,48 @@ +Person + Name = Dima + Height = 176 + Age = Hidden + BirthDate = 7/27/1985 12:00:00 AM + Husband = null + Wife = null + HomeworksCompleted = + Testi + Tag c + Markd + Objec + Friends = + Person + Name = Nikit + Height = 0 + Age = Hidden + BirthDate = 1/1/0001 12:00:00 AM + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + Person + Name = Pasha + Height = Loop found + Age = Loop found + BirthDate = Loop found + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + EmergensyList = + key = wife + value = 8-800 + key = fathe + value = 3-10- + key = mothe + value = 03 + LastName = null + ToExclude = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludeConcreteProperty.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludeConcreteProperty.verified.txt new file mode 100644 index 00000000..3f6ee095 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludeConcreteProperty.verified.txt @@ -0,0 +1,48 @@ +Person + Name = Dima + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = + Testing + Tag cloud + Markdown + Object Printer + Friends = + Person + Name = Nikita + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + Person + Name = Pasha + Height = Loop found + Age = Loop found + BirthDate = Loop found + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + EmergensyList = + key = wife + value = 8-800-5-5-5-3-5-3-5 + key = father + value = 3-10-10-10 + key = mother + value = 03 + LastName = null + ToExclude = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludePropertyOfType.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludePropertyOfType.verified.txt new file mode 100644 index 00000000..db44b7f7 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludePropertyOfType.verified.txt @@ -0,0 +1,48 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = + + + + + Friends = + Person + Id = 00000000-0000-0000-0000-000000000000 + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + Person + Id = Loop found + Height = Loop found + Age = Loop found + BirthDate = Loop found + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + EmergensyList = + key = + value = + key = + value = + key = + value = + LastName = null + ToExclude = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.ObjectPrinter_ShouldFindLoop.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.ObjectPrinter_ShouldFindLoop.verified.txt new file mode 100644 index 00000000..9c118afb --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.ObjectPrinter_ShouldFindLoop.verified.txt @@ -0,0 +1,2 @@ +Foo + Self = Loop found diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.SetCulture_ShouldSetCulture.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.SetCulture_ShouldSetCulture.verified.txt new file mode 100644 index 00000000..18104a3a --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.SetCulture_ShouldSetCulture.verified.txt @@ -0,0 +1,51 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dima + Height = 176 + Age = 39 + BirthDate = 7/27/1985 12:00:00 AM + Husband = null + Wife = null + HomeworksCompleted = + Testing + Tag cloud + Markdown + Object Printer + Friends = + Person + Id = 00000000-0000-0000-0000-000000000000 + Name = Nikita + Height = 0 + Age = 0 + BirthDate = 1/1/0001 12:00:00 AM + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + Person + Id = Loop found + Name = Pasha + Height = Loop found + Age = Loop found + BirthDate = Loop found + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + EmergensyList = + key = wife + value = 8-800-5-5-5-3-5-3-5 + key = father + value = 3-10-10-10 + key = mother + value = 03 + LastName = null + ToExclude = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=10.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=10.verified.txt new file mode 100644 index 00000000..41c5c9f9 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=10.verified.txt @@ -0,0 +1,51 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dima + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = + Testing + Tag cloud + Markdown + Object Pri + Friends = + Person + Id = 00000000-0000-0000-0000-000000000000 + Name = Nikita + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + Person + Id = Loop found + Name = Pasha + Height = Loop found + Age = Loop found + BirthDate = Loop found + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + EmergensyList = + key = wife + value = 8-800-5-5- + key = father + value = 3-10-10-10 + key = mother + value = 03 + LastName = null + ToExclude = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=3.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=3.verified.txt new file mode 100644 index 00000000..00a62b5c --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=3.verified.txt @@ -0,0 +1,51 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dim + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = + Tes + Tag + Mar + Obj + Friends = + Person + Id = 00000000-0000-0000-0000-000000000000 + Name = Nik + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + Person + Id = Loop found + Name = Pas + Height = Loop found + Age = Loop found + BirthDate = Loop found + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + EmergensyList = + key = wif + value = 8-8 + key = fat + value = 3-1 + key = mot + value = 03 + LastName = null + ToExclude = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForProperty.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForProperty.verified.txt new file mode 100644 index 00000000..f19b0ca5 --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForProperty.verified.txt @@ -0,0 +1,51 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = XX + Height = 176 + Age = 39 + BirthDate = 27.07.1985 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = + XX + XX + XX + XX + Friends = + Person + Id = 00000000-0000-0000-0000-000000000000 + Name = XX + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + Person + Id = Loop found + Name = XX + Height = Loop found + Age = Loop found + BirthDate = Loop found + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + EmergensyList = + key = XX + value = XX + key = XX + value = XX + key = XX + value = XX + LastName = null + ToExclude = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForType.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForType.verified.txt new file mode 100644 index 00000000..21a789fb --- /dev/null +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForType.verified.txt @@ -0,0 +1,51 @@ +Person + Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 + Name = Dima + Height = 176 + Age = XX + BirthDate = 27.07.1985 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = + Testing + Tag cloud + Markdown + Object Printer + Friends = + Person + Id = 00000000-0000-0000-0000-000000000000 + Name = Nikita + Height = 0 + Age = XX + BirthDate = 01.01.0001 0:00:00 + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + Person + Id = Loop found + Name = Pasha + Height = Loop found + Age = Loop found + BirthDate = Loop found + Husband = null + Wife = null + HomeworksCompleted = null + Friends = null + EmergensyList = null + LastName = null + ToExclude = null + + EmergensyList = + key = wife + value = 8-800-5-5-5-3-5-3-5 + key = father + value = 3-10-10-10 + key = mother + value = 03 + LastName = null + ToExclude = null diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt deleted file mode 100644 index d482f1e9..00000000 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldExcludePropertyOfType.verified.txt +++ /dev/null @@ -1,10 +0,0 @@ -Person - Name = Dima - Height = 176 - Age = 39 - BirthDate = 27.07.1985 0:00:00 - Husband = - Wife = - HomeworksCompleted = - Friends = - EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt deleted file mode 100644 index cff2abfa..00000000 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldFindLoop.verified.txt +++ /dev/null @@ -1,22 +0,0 @@ -Person - Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 - Name = Dima - Height = 176 - Age = 39 - BirthDate = 27.07.1985 0:00:00 - Husband = - Wife = Person - Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3302 - Name = Alla - Height = 170 - Age = 35 - BirthDate = 19.02.1989 0:00:00 - Husband = Loop found - Wife = - HomeworksCompleted = - Friends = - EmergensyList = - - HomeworksCompleted = - Friends = - EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintArray.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintArray.verified.txt deleted file mode 100644 index ba92449b..00000000 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintArray.verified.txt +++ /dev/null @@ -1,35 +0,0 @@ -Person - Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 - Name = Dima - Height = 176 - Age = 39 - BirthDate = 27.07.1985 0:00:00 - Husband = - Wife = - HomeworksCompleted = - Friends = - Person - Id = 00000000-0000-0000-0000-000000000000 - Name = Nikita - Height = 0 - Age = 0 - BirthDate = 01.01.0001 0:00:00 - Husband = - Wife = - HomeworksCompleted = - Friends = - EmergensyList = - - Person - Id = Loop found - Name = Pasha - Height = Loop found - Age = Loop found - BirthDate = Loop found - Husband = - Wife = - HomeworksCompleted = - Friends = - EmergensyList = - - EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintCollection.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintCollection.verified.txt deleted file mode 100644 index 99ec8f6f..00000000 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintCollection.verified.txt +++ /dev/null @@ -1,31 +0,0 @@ -Person - Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 - Name = Dima - Height = 176 - Age = 39 - BirthDate = 27.07.1985 0:00:00 - Husband = - Wife = - Friends = - Person - Id = 00000000-0000-0000-0000-000000000000 - Name = Nikita - Height = 0 - Age = 0 - BirthDate = 01.01.0001 0:00:00 - Husband = - Wife = - Friends = - EmergensyList = - Person - Id = Loop found - Name = Pasha - Height = Loop found - Age = Loop found - BirthDate = Loop found - Husband = - Wife = - Friends = - EmergensyList = - - EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt deleted file mode 100644 index 2f91fd85..00000000 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintDictionary.verified.txt +++ /dev/null @@ -1,17 +0,0 @@ -Person - Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 - Name = Dima - Height = 176 - Age = 39 - BirthDate = 27.07.1985 0:00:00 - Husband = - Wife = - HomeworksCompleted = - Friends = - EmergensyList = - key = wife - value = 5-5-5-3-5-3-5 - key = father - value = 8-800-2000-600 - key = mother - value = +7 922 22 222 22 diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintList.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintList.verified.txt deleted file mode 100644 index c10da4d2..00000000 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldPrintList.verified.txt +++ /dev/null @@ -1,15 +0,0 @@ -Person - Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 - Name = Dima - Height = 176 - Age = 39 - BirthDate = 27.07.1985 0:00:00 - Husband = - Wife = - HomeworksCompleted = - Testing - Tag cloud - Markdown - Object Printer - Friends = - EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt deleted file mode 100644 index 681b9af9..00000000 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetCulture.verified.txt +++ /dev/null @@ -1,11 +0,0 @@ -Person - Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 - Name = Dima - Height = 176 - Age = 39 - BirthDate = 7/27/1985 12:00:00 AM - Husband = - Wife = - HomeworksCompleted = - Friends = - EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt deleted file mode 100644 index bd5f7da3..00000000 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForProperty.verified.txt +++ /dev/null @@ -1,11 +0,0 @@ -Person - Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 - Name = XX - Height = 176 - Age = 39 - BirthDate = 27.07.1985 0:00:00 - Husband = - Wife = - HomeworksCompleted = - Friends = - EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt deleted file mode 100644 index 2cf765d1..00000000 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldSetPrinterForType.verified.txt +++ /dev/null @@ -1,11 +0,0 @@ -Person - Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 - Name = Dima - Height = 176 - Age = XX - BirthDate = 27.07.1985 0:00:00 - Husband = - Wife = - HomeworksCompleted = - Friends = - EmergensyList = diff --git a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt b/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt deleted file mode 100644 index da53b566..00000000 --- a/ObjectPrinting.Tests/ForVerifier/Tests.ObjectPrinter_ShouldTrimStringType.verified.txt +++ /dev/null @@ -1,11 +0,0 @@ -Person - Id = 3f2504e0-4f89-11d3-9a0c-0305e82c3301 - Name = Dim - Height = 176 - Age = 39 - BirthDate = 27.07.1985 0:00:00 - Husband = - Wife = - HomeworksCompleted = - Friends = - EmergensyList = diff --git a/ObjectPrinting.Tests/Tests.cs b/ObjectPrinting.Tests/ObjectPrinterTests.cs similarity index 53% rename from ObjectPrinting.Tests/Tests.cs rename to ObjectPrinting.Tests/ObjectPrinterTests.cs index bd5a7551..44149875 100644 --- a/ObjectPrinting.Tests/Tests.cs +++ b/ObjectPrinting.Tests/ObjectPrinterTests.cs @@ -3,13 +3,14 @@ namespace ObjectPrinting.Tests { [TestFixture] - public class Tests + public class ObjectPrinterTests { private static readonly VerifySettings DefaultSettings = new(); private Person person; + private static Task Verify(string printedObject) => Verifier.Verify(printedObject, DefaultSettings); - [SetUp] - public void Setup() + [OneTimeSetUp] + public void OneTimeSetup() { DefaultSettings.UseDirectory("ForVerifier"); person = new Person @@ -20,128 +21,129 @@ public void Setup() Height = 176.0, BirthDate = new DateTime(1985, 07, 27) }; + + var homeworks = new List() + { + "Testing", + "Tag cloud", + "Markdown", + "Object Printer" + }; + person.HomeworksCompleted = homeworks; + + var friends = new[] + { + new Person() { Name = "Nikita" }, + new Person() { Name = "Pasha" } + }; + person.Friends = friends; + + var emergencyCallList = new Dictionary() + { + {"wife","8-800-5-5-5-3-5-3-5"}, + {"father", "3-10-10-10"}, + {"mother", "03"} + }; + person.EmergensyList = emergencyCallList; } [Test] - public Task ObjectPrinter_ShouldExcludePropertyOfType() + public Task Excluding_ShouldExcludePropertyOfType() { var result = ObjectPrinter.For() - .Excluding() + .Excluding() .PrintToString(person); - - return Verifier.Verify(result, DefaultSettings); - } + return Verify(result); + } + [Test] - public Task ObjectPrinter_ShouldSetPrinterForType() + public Task Excluding_ShouldExcludeConcreteProperty() { var result = ObjectPrinter.For() - .Printing().Using(i => "XX") + .Excluding(p => p.ToExclude) + .Excluding(p => p.Id) .PrintToString(person); - - return Verifier.Verify(result, DefaultSettings); + + return Verify(result); } [Test] - public Task ObjectPrinter_ShouldSetPrinterForProperty() + public Task Using_ShouldSetPrinterForType() { var result = ObjectPrinter.For() - .Printing(p => p.Name).Using(i => "XX") + .Printing().Using(_ => "XX") .PrintToString(person); - return Verifier.Verify(result, DefaultSettings); + return Verify(result); } [Test] - public Task ObjectPrinter_ShouldSetCulture() + public Task Using_ShouldSetPrinterForProperty() { var result = ObjectPrinter.For() - .Printing().SetCulture(new CultureInfo("en")) + .Printing(p => p.Name).Using(_ => "XX") .PrintToString(person); - return Verifier.Verify(result, DefaultSettings); + return Verify(result); } - [TestCase(10, TestName = "Print original string when its length smaller than needed") ] - [TestCase(3, TestName = "Print trimmed string in right way")] - public Task ObjectPrinter_ShouldTrimStringType(int maxLen) + [Test] + public Task SetCulture_ShouldSetCulture() { var result = ObjectPrinter.For() - .Printing().TrimmedToLength(maxLen) + .Printing().SetCulture(new CultureInfo("en")) .PrintToString(person); - return Verifier.Verify(result, DefaultSettings); + return Verify(result); } - [Test] - public Task ObjectPrinter_ShouldFindLoop() + [TestCase(10, TestName = "Print original string when its length smaller than needed") ] + [TestCase(3, TestName = "Print trimmed string in right way")] + public Task TrimmedToLength_ShouldTrimStringType(int maxLen) { - var spouse = new Person() - { - Id = new Guid("3f2504e0-4f89-11d3-9a0c-0305e82c3302"), - Age = 35, - Name = "Alla", - Height = 170.0, - BirthDate = new DateTime(1989, 2, 19), - Husband = person - }; - person.Wife = spouse; - var result = ObjectPrinter.For() + .Printing().TrimmedToLength(maxLen) .PrintToString(person); - return Verifier.Verify(result, DefaultSettings); + return Verify(result); } - [Test] - public Task ObjectPrinter_ShouldPrintList() + private class Foo { - var homeworks = new List() - { - "Testing", - "Tag cloud", - "Markdown", - "Object Printer" - }; - person.HomeworksCompleted = homeworks; - - var result = ObjectPrinter.For() - .PrintToString(person); - - return Verifier.Verify(result, DefaultSettings); + public Foo Self { get; set; } } [Test] - public Task ObjectPrinter_ShouldPrintArray() + public Task ObjectPrinter_ShouldFindLoop() { - var friends = new[] - { - new Person() { Name = "Nikita" }, - new Person() { Name = "Pasha" } - }; - person.Friends = friends; + var foo = new Foo(); + foo.Self = foo; - var result = ObjectPrinter.For() - .PrintToString(person); + var result = foo.PrintToString(); - return Verifier.Verify(result, DefaultSettings); + return Verify(result); } [Test] - public Task ObjectPrinter_ShouldPrintDictionary() + public Task Demo() { - var emergencyCallList = new Dictionary() - { - {"wife","5-5-5-3-5-3-5"}, - {"father", "8-800-2000-600"}, - {"mother", "+7 922 22 222 22"} - }; - person.EmergensyList = emergencyCallList; - var result = ObjectPrinter.For() + //1. + .Excluding() + //2. + .Printing().Using(_ => "XX") + //3. + .Printing().SetCulture(new CultureInfo("en")) + //4. + .Printing(p => p.Age).Using(_ => "Hidden") + //5. ( ) + .Printing().TrimmedToLength(5) + //6. + .Excluding(p => p.ToExclude) .PrintToString(person); - return Verifier.Verify(result, DefaultSettings); + return Verify(result); } } } \ No newline at end of file diff --git a/ObjectPrinting.Tests/Person.cs b/ObjectPrinting.Tests/Person.cs index aaac9851..254e305c 100644 --- a/ObjectPrinting.Tests/Person.cs +++ b/ObjectPrinting.Tests/Person.cs @@ -6,6 +6,7 @@ namespace ObjectPrinting.Tests public class Person { public string LastName; + public string ToExclude; public Guid Id { get; set; } public string Name { get; set; } public double Height { get; set; } diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 6e249b57..b474398f 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -6,7 +6,6 @@ using System.Linq.Expressions; using System.Reflection; using System.Text; -using NUnit.Framework.Internal; namespace ObjectPrinting { @@ -61,8 +60,8 @@ private string PrintToString(object obj, int nestingLevel) visitedObjects.Add(obj); - if (typePrinters.TryGetValue(type, out var serializer)) - return serializer(obj); + if (typePrinters.TryGetValue(type, out var printer)) + return printer(obj); if (FinalTypes.Set.Contains(type)) return obj.ToString(); @@ -110,16 +109,15 @@ private string PrintToString(object obj, int nestingLevel) private string PrintCollection(IEnumerable collection, int nestingLevel) { - if (collection is IDictionary) - return PrintDictionary( (IDictionary)collection, nestingLevel); + if (collection is IDictionary dictionary) + return PrintDictionary( dictionary, nestingLevel); var indentation = new string('\t', nestingLevel + 1); var sb = new StringBuilder(); foreach (var element in collection) { - sb.Append("\n"); - sb.Append(indentation); + sb.Append("\n" + indentation); sb.Append(PrintToString(element, nestingLevel + 1)); } return sb.ToString(); @@ -132,12 +130,10 @@ private string PrintDictionary(IDictionary collection, int nestingLevel) foreach (var key in collection.Keys) { - sb.Append("\n"); - sb.Append(indentation); + sb.Append("\n" + indentation); sb.Append("key = "); sb.Append(PrintToString(key, nestingLevel + 1)); - sb.Append("\n"); - sb.Append(indentation); + sb.Append("\n" + indentation); sb.Append("value = "); sb.Append(PrintToString(collection[key], nestingLevel + 1)); } diff --git a/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs deleted file mode 100644 index ac52d5ee..00000000 --- a/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Globalization; -using NUnit.Framework; - -namespace ObjectPrinting.Solved.Tests -{ - [TestFixture] - public class ObjectPrinterAcceptanceTests - { - [Test] - public void Demo() - { - var person = new Person { Name = "Alex", Age = 19 }; - - var printer = ObjectPrinter.For() - //1. Исключить из сериализации свойства определенного типа - .Excluding() - //2. Указать альтернативный способ сериализации для определенного типа - .Printing().Using(i => i.ToString("X")) - //3. Для числовых типов указать культуру - .Printing().Using(CultureInfo.InvariantCulture) - //4. Настроить сериализацию конкретного свойства - //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) - .Printing(p => p.Name).TrimmedToLength(10) - //6. Исключить из сериализации конкретного свойства - .Excluding(p => p.Age); - - string s1 = printer.PrintToString(person); - - //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию - string s2 = person.PrintToString(); - - //8. ...с конфигурированием - string s3 = person.PrintToString(s => s.Excluding(p => p.Age)); - Console.WriteLine(s1); - Console.WriteLine(s2); - Console.WriteLine(s3); - } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Solved/Tests/Person.cs b/ObjectPrinting/Solved/Tests/Person.cs deleted file mode 100644 index 858ebbf8..00000000 --- a/ObjectPrinting/Solved/Tests/Person.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace ObjectPrinting.Solved.Tests -{ - public class Person - { - public Guid Id { get; set; } - public string Name { get; set; } - public double Height { get; set; } - public int Age { get; set; } - } -} \ No newline at end of file From 572bceea70888ded7312890b3b31721e31ae4b5b Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Wed, 18 Dec 2024 20:21:01 +0500 Subject: [PATCH 09/11] PrintingConfig methods refactoring --- ObjectPrinting/PrintingConfig.cs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index b474398f..739ad473 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -66,12 +66,14 @@ private string PrintToString(object obj, int nestingLevel) if (FinalTypes.Set.Contains(type)) return obj.ToString(); - if (obj is ICollection) - { - var collection = (IEnumerable) obj; - return PrintCollection(collection, nestingLevel); - } + if (obj is not ICollection) return PrintObj(obj, nestingLevel); + var collection = (IEnumerable) obj; + return PrintCollection(collection, nestingLevel); + } + private string PrintObj( object obj, int nestingLevel) + { + var type = obj.GetType(); var indentation = new string('\t', nestingLevel + 1); var sb = new StringBuilder(); sb.AppendLine(type.Name); @@ -81,12 +83,12 @@ private string PrintToString(object obj, int nestingLevel) .Cast() .Concat(type.GetFields(BindingFlags.Instance | BindingFlags.Public))) { - + var name = memberInfo.Name; var value = memberInfo.GetValue(obj); if (value == null) { - sb.Append(indentation + name + " = null\n"); + sb.Append(indentation + name + " = null\n"); continue; } From 0b077514bb758868910b01a723507a33a64ad0a1 Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Wed, 18 Dec 2024 23:12:02 +0500 Subject: [PATCH 10/11] the check of visited has been updated --- .../ForVerifier/ObjectPrinterTests.Demo.verified.txt | 6 +++--- ...s.Excluding_ShouldExcludeConcreteProperty.verified.txt | 6 +++--- ...sts.Excluding_ShouldExcludePropertyOfType.verified.txt | 8 ++++---- ...tPrinterTests.SetCulture_ShouldSetCulture.verified.txt | 8 ++++---- ...edToLength_ShouldTrimStringType_maxLen=10.verified.txt | 8 ++++---- ...medToLength_ShouldTrimStringType_maxLen=3.verified.txt | 8 ++++---- ...erTests.Using_ShouldSetPrinterForProperty.verified.txt | 8 ++++---- ...rinterTests.Using_ShouldSetPrinterForType.verified.txt | 8 ++++---- ObjectPrinting/PrintingConfig.cs | 2 +- 9 files changed, 31 insertions(+), 31 deletions(-) diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Demo.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Demo.verified.txt index f82e7157..290414ba 100644 --- a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Demo.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Demo.verified.txt @@ -26,9 +26,9 @@ Person Name = Pasha - Height = Loop found - Age = Loop found - BirthDate = Loop found + Height = 0 + Age = Hidden + BirthDate = 1/1/0001 12:00:00 AM Husband = null Wife = null HomeworksCompleted = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludeConcreteProperty.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludeConcreteProperty.verified.txt index 3f6ee095..ba1ad4d5 100644 --- a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludeConcreteProperty.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludeConcreteProperty.verified.txt @@ -26,9 +26,9 @@ Person Name = Pasha - Height = Loop found - Age = Loop found - BirthDate = Loop found + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 Husband = null Wife = null HomeworksCompleted = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludePropertyOfType.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludePropertyOfType.verified.txt index db44b7f7..049bc8c7 100644 --- a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludePropertyOfType.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Excluding_ShouldExcludePropertyOfType.verified.txt @@ -25,10 +25,10 @@ ToExclude = null Person - Id = Loop found - Height = Loop found - Age = Loop found - BirthDate = Loop found + Id = 00000000-0000-0000-0000-000000000000 + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 Husband = null Wife = null HomeworksCompleted = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.SetCulture_ShouldSetCulture.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.SetCulture_ShouldSetCulture.verified.txt index 18104a3a..bbaffb82 100644 --- a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.SetCulture_ShouldSetCulture.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.SetCulture_ShouldSetCulture.verified.txt @@ -27,11 +27,11 @@ ToExclude = null Person - Id = Loop found + Id = 00000000-0000-0000-0000-000000000000 Name = Pasha - Height = Loop found - Age = Loop found - BirthDate = Loop found + Height = 0 + Age = 0 + BirthDate = 1/1/0001 12:00:00 AM Husband = null Wife = null HomeworksCompleted = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=10.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=10.verified.txt index 41c5c9f9..a64cde8b 100644 --- a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=10.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=10.verified.txt @@ -27,11 +27,11 @@ ToExclude = null Person - Id = Loop found + Id = 00000000-0000-0000-0000-000000000000 Name = Pasha - Height = Loop found - Age = Loop found - BirthDate = Loop found + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 Husband = null Wife = null HomeworksCompleted = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=3.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=3.verified.txt index 00a62b5c..ab25797f 100644 --- a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=3.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.TrimmedToLength_ShouldTrimStringType_maxLen=3.verified.txt @@ -27,11 +27,11 @@ ToExclude = null Person - Id = Loop found + Id = 00000000-0000-0000-0000-000000000000 Name = Pas - Height = Loop found - Age = Loop found - BirthDate = Loop found + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 Husband = null Wife = null HomeworksCompleted = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForProperty.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForProperty.verified.txt index f19b0ca5..79b6349d 100644 --- a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForProperty.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForProperty.verified.txt @@ -27,11 +27,11 @@ ToExclude = null Person - Id = Loop found + Id = 00000000-0000-0000-0000-000000000000 Name = XX - Height = Loop found - Age = Loop found - BirthDate = Loop found + Height = 0 + Age = 0 + BirthDate = 01.01.0001 0:00:00 Husband = null Wife = null HomeworksCompleted = null diff --git a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForType.verified.txt b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForType.verified.txt index 21a789fb..40896106 100644 --- a/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForType.verified.txt +++ b/ObjectPrinting.Tests/ForVerifier/ObjectPrinterTests.Using_ShouldSetPrinterForType.verified.txt @@ -27,11 +27,11 @@ ToExclude = null Person - Id = Loop found + Id = 00000000-0000-0000-0000-000000000000 Name = Pasha - Height = Loop found - Age = Loop found - BirthDate = Loop found + Height = 0 + Age = XX + BirthDate = 01.01.0001 0:00:00 Husband = null Wife = null HomeworksCompleted = null diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 739ad473..5e4c81ed 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -52,7 +52,7 @@ private string PrintToString(object obj, int nestingLevel) var type = obj.GetType(); - if (visitedObjects.Contains(obj)) + if (visitedObjects.Contains(obj, ReferenceEqualityComparer.Instance)) return "Loop found"; if (typesToExclude.Contains(type)) From 3b1e02daac5cf0fdf4e53c2fffb7290fbc73f90d Mon Sep 17 00:00:00 2001 From: Dmitriy Bessarab Date: Thu, 19 Dec 2024 11:29:41 +0500 Subject: [PATCH 11/11] final mistake correction --- ObjectPrinting/PrintingConfig.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 5e4c81ed..2cd0e754 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -24,7 +24,7 @@ public PropertyPrintingConfig Printing() public PropertyPrintingConfig Printing(Expression> memberSelector) { - return new PropertyPrintingConfig(this); + return new PropertyPrintingConfig(this, memberSelector.Name); } public PrintingConfig Excluding(Expression> memberSelector)