From 441f072cd1e19167ff3b0d03af1ead61666bffad Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Sun, 8 Dec 2024 18:29:47 +0500 Subject: [PATCH 01/21] stuff done in class --- ObjectPrinting/ITypeSerializer.cs | 8 ++++++ ObjectPrinting/ITypeSerializerInternal.cs | 6 +++++ ObjectPrinting/ObjectPrinting.csproj | 1 + ObjectPrinting/PrintingConfig.cs | 24 ++++++++++++++++++ .../Tests/ObjectPrinterAcceptanceTests.cs | 15 ++++++++++- ObjectPrinting/TypeSerializerExtensions.cs | 25 +++++++++++++++++++ ObjectPrinting/TypeSerializerImpl.cs | 19 ++++++++++++++ 7 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 ObjectPrinting/ITypeSerializer.cs create mode 100644 ObjectPrinting/ITypeSerializerInternal.cs create mode 100644 ObjectPrinting/TypeSerializerExtensions.cs create mode 100644 ObjectPrinting/TypeSerializerImpl.cs diff --git a/ObjectPrinting/ITypeSerializer.cs b/ObjectPrinting/ITypeSerializer.cs new file mode 100644 index 00000000..25a76eb9 --- /dev/null +++ b/ObjectPrinting/ITypeSerializer.cs @@ -0,0 +1,8 @@ +using System; + +namespace ObjectPrinting; + +public interface ITypeSerializer +{ + public PrintingConfig Use(Func serializer); +} \ No newline at end of file diff --git a/ObjectPrinting/ITypeSerializerInternal.cs b/ObjectPrinting/ITypeSerializerInternal.cs new file mode 100644 index 00000000..27825212 --- /dev/null +++ b/ObjectPrinting/ITypeSerializerInternal.cs @@ -0,0 +1,6 @@ +namespace ObjectPrinting; + +internal interface ITypeSerializerInternal +{ + public PrintingConfig Config { get; } +} \ 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..28aa80bc 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -1,16 +1,40 @@ using System; +using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Text; +using NUnit.Framework.Internal; namespace ObjectPrinting { public class PrintingConfig { + private List _excludedTypes = new(); + private Dictionary _typeConverters = new(); + internal CultureInfo DoubleCultureInfo { get; set; } = CultureInfo.CurrentCulture; + internal CultureInfo FloatCultureInfo { get; set; } = CultureInfo.CurrentCulture; + public string PrintToString(TOwner obj) { return PrintToString(obj, 0); } + internal void AddConverter(Type type, Func converter) + { + _typeConverters.Add(type, converter); + } + + public PrintingConfig ExceptType() + { + _excludedTypes.Add(typeof(T)); + return this; + } + + public ITypeSerializer ForType() + { + return new TypeSerializerImpl(this); + } + private string PrintToString(object obj, int nestingLevel) { //TODO apply configurations diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs index 4c8b2445..c6847d47 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs @@ -1,4 +1,6 @@ -using NUnit.Framework; +using System; +using System.Globalization; +using NUnit.Framework; namespace ObjectPrinting.Tests { @@ -12,13 +14,24 @@ public void Demo() var printer = ObjectPrinter.For(); //1. Исключить из сериализации свойства определенного типа + printer.ExceptType(); //2. Указать альтернативный способ сериализации для определенного типа + printer.ForType() + .Use(o => o.ToString()); //3. Для числовых типов указать культуру + printer.ForType() + .UseCulture(CultureInfo.InvariantCulture); //4. Настроить сериализацию конкретного свойства + printer.ForProperty(x => x.Age) // TODO use Expression> + .Use(o => o.ToString()); //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) + printer.ForType() + .UseMaxLength(15); //6. Исключить из сериализации конкретного свойства + printer.ExceptProperty(nameof(Person.Name)); string s1 = printer.PrintToString(person); + // TODO use Verify.NUnit //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию //8. ...с конфигурированием diff --git a/ObjectPrinting/TypeSerializerExtensions.cs b/ObjectPrinting/TypeSerializerExtensions.cs new file mode 100644 index 00000000..eccc1363 --- /dev/null +++ b/ObjectPrinting/TypeSerializerExtensions.cs @@ -0,0 +1,25 @@ +using System; +using System.Globalization; + +namespace ObjectPrinting; + +public static class TypeSerializerExtensions +{ + public static PrintingConfig UseCulture( + this ITypeSerializer typeSerializer, + CultureInfo cultureInfo) + { + var config = ((ITypeSerializerInternal)typeSerializer).Config; + config.DoubleCultureInfo = cultureInfo; + return config; + } + + public static PrintingConfig UseCulture( + this ITypeSerializer typeSerializer, + CultureInfo cultureInfo) + { + var config = ((ITypeSerializerInternal)typeSerializer).Config; + config.FloatCultureInfo = cultureInfo; + return config; + } +} \ No newline at end of file diff --git a/ObjectPrinting/TypeSerializerImpl.cs b/ObjectPrinting/TypeSerializerImpl.cs new file mode 100644 index 00000000..06de14d3 --- /dev/null +++ b/ObjectPrinting/TypeSerializerImpl.cs @@ -0,0 +1,19 @@ +using System; + +namespace ObjectPrinting; + +internal class TypeSerializerImpl : ITypeSerializer, ITypeSerializerInternal +{ + public PrintingConfig Config { get; } + + internal TypeSerializerImpl(PrintingConfig config) + { + Config = config; + } + + public PrintingConfig Use(Func serializer) + { + Config.AddConverter(typeof(TParam), serializer); + return Config; + } +} \ No newline at end of file From bdadc67a3f1c4bfb9d889ad72bfc3f9c9ceedbf7 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Sun, 8 Dec 2024 18:55:59 +0500 Subject: [PATCH 02/21] added max string length configuration --- ObjectPrinting/PrintingConfig.cs | 1 + ObjectPrinting/TypeSerializerExtensions.cs | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 28aa80bc..a1b33ed5 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -13,6 +13,7 @@ public class PrintingConfig private Dictionary _typeConverters = new(); internal CultureInfo DoubleCultureInfo { get; set; } = CultureInfo.CurrentCulture; internal CultureInfo FloatCultureInfo { get; set; } = CultureInfo.CurrentCulture; + internal int MaxStringLength { get; set; } public string PrintToString(TOwner obj) { diff --git a/ObjectPrinting/TypeSerializerExtensions.cs b/ObjectPrinting/TypeSerializerExtensions.cs index eccc1363..f1cbc0b3 100644 --- a/ObjectPrinting/TypeSerializerExtensions.cs +++ b/ObjectPrinting/TypeSerializerExtensions.cs @@ -22,4 +22,13 @@ public static PrintingConfig UseCulture( config.FloatCultureInfo = cultureInfo; return config; } + + public static PrintingConfig UseMaxLength( + this ITypeSerializer typeSerializer, + int maxLength) + { + var config = ((ITypeSerializerInternal)typeSerializer).Config; + config.MaxStringLength = maxLength; + return config; + } } \ No newline at end of file From 0a315e13c0a7dc9c1673d07582c0d14e136760ab Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Sun, 8 Dec 2024 19:47:12 +0500 Subject: [PATCH 03/21] renamed a variable and changed Use() method signature --- ObjectPrinting/ITypeSerializer.cs | 2 +- ObjectPrinting/TypeSerializerImpl.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ObjectPrinting/ITypeSerializer.cs b/ObjectPrinting/ITypeSerializer.cs index 25a76eb9..85f24866 100644 --- a/ObjectPrinting/ITypeSerializer.cs +++ b/ObjectPrinting/ITypeSerializer.cs @@ -4,5 +4,5 @@ namespace ObjectPrinting; public interface ITypeSerializer { - public PrintingConfig Use(Func serializer); + public PrintingConfig Use(Func converter); } \ No newline at end of file diff --git a/ObjectPrinting/TypeSerializerImpl.cs b/ObjectPrinting/TypeSerializerImpl.cs index 06de14d3..afe9e3fd 100644 --- a/ObjectPrinting/TypeSerializerImpl.cs +++ b/ObjectPrinting/TypeSerializerImpl.cs @@ -11,9 +11,9 @@ internal TypeSerializerImpl(PrintingConfig config) Config = config; } - public PrintingConfig Use(Func serializer) + public PrintingConfig Use(Func converter) { - Config.AddConverter(typeof(TParam), serializer); + Config.AddConverter(typeof(TParam), converter); return Config; } } \ No newline at end of file From 9605d4ade7466556e2ad6b474cacff668d366ac4 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Sun, 8 Dec 2024 20:06:02 +0500 Subject: [PATCH 04/21] added ForProperty() and ExceptProperty() --- ObjectPrinting/IPropertySerializer.cs | 8 +++++ ObjectPrinting/PrintingConfig.cs | 39 ++++++++++++++++++++++++ ObjectPrinting/PropertySerializerImpl.cs | 22 +++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 ObjectPrinting/IPropertySerializer.cs create mode 100644 ObjectPrinting/PropertySerializerImpl.cs diff --git a/ObjectPrinting/IPropertySerializer.cs b/ObjectPrinting/IPropertySerializer.cs new file mode 100644 index 00000000..456c5633 --- /dev/null +++ b/ObjectPrinting/IPropertySerializer.cs @@ -0,0 +1,8 @@ +using System; + +namespace ObjectPrinting; + +public interface IPropertySerializer +{ + public PrintingConfig Use(Func converter); +} \ No newline at end of file diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index a1b33ed5..35387b80 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Linq.Expressions; +using System.Reflection; using System.Text; using NUnit.Framework.Internal; @@ -10,7 +12,9 @@ namespace ObjectPrinting public class PrintingConfig { private List _excludedTypes = new(); + private List _excludedProperties = new(); private Dictionary _typeConverters = new(); + private Dictionary _propertyConverters = new(); internal CultureInfo DoubleCultureInfo { get; set; } = CultureInfo.CurrentCulture; internal CultureInfo FloatCultureInfo { get; set; } = CultureInfo.CurrentCulture; internal int MaxStringLength { get; set; } @@ -25,17 +29,52 @@ internal void AddConverter(Type type, Func converter) _typeConverters.Add(type, converter); } + internal void AddPropertyConverter(Func converter, MemberInfo propertyInfo) + { + _propertyConverters.Add(propertyInfo, converter); + } + public PrintingConfig ExceptType() { _excludedTypes.Add(typeof(T)); return this; } + public PrintingConfig ExceptProperty(Expression> propertyExpression) + { + if (propertyExpression == null) + throw new ArgumentNullException($"{nameof(propertyExpression)} cannot be null"); + + _excludedProperties.Add(GetMemberInfo(propertyExpression)); + return this; + } + public ITypeSerializer ForType() { return new TypeSerializerImpl(this); } + public IPropertySerializer ForProperty( + Expression> propertyExpression) + { + if (propertyExpression == null) + throw new ArgumentNullException($"{nameof(propertyExpression)} cannot be null"); + + return new PropertySerializerImpl(this, GetMemberInfo(propertyExpression)); + } + + private static MemberInfo GetMemberInfo(Expression> propertyExpression) + { + if (propertyExpression.Body is MemberExpression memberExpression) + return memberExpression.Member; + + if (propertyExpression.Body is UnaryExpression unaryExpression + && unaryExpression.Operand is MemberExpression unaryMemberExpression) + return unaryMemberExpression.Member; + + throw new ArgumentException("Expression does not refer to a property or field."); + } + private string PrintToString(object obj, int nestingLevel) { //TODO apply configurations diff --git a/ObjectPrinting/PropertySerializerImpl.cs b/ObjectPrinting/PropertySerializerImpl.cs new file mode 100644 index 00000000..714ca584 --- /dev/null +++ b/ObjectPrinting/PropertySerializerImpl.cs @@ -0,0 +1,22 @@ +using System; +using System.Reflection; + +namespace ObjectPrinting; + +internal class PropertySerializerImpl : IPropertySerializer +{ + public PrintingConfig Config { get; } + private readonly MemberInfo _memberInfo; + + internal PropertySerializerImpl(PrintingConfig config, MemberInfo memberInfo) + { + Config = config; + _memberInfo = memberInfo; + } + + public PrintingConfig Use(Func converter) + { + Config.AddPropertyConverter(converter, _memberInfo); + return Config; + } +} \ No newline at end of file From 54a970859ce3c8d8d0a87e3f47d3d0a145b7b621 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Sun, 8 Dec 2024 21:08:45 +0500 Subject: [PATCH 05/21] refactored ForType<>() related stuff --- ObjectPrinting/ITypeSerializerInternal.cs | 6 ------ ObjectPrinting/PrintingConfig.cs | 3 +-- ObjectPrinting/TypeSerializerExtensions.cs | 15 ++++++++++++--- ObjectPrinting/TypeSerializerImpl.cs | 6 ++++-- 4 files changed, 17 insertions(+), 13 deletions(-) delete mode 100644 ObjectPrinting/ITypeSerializerInternal.cs diff --git a/ObjectPrinting/ITypeSerializerInternal.cs b/ObjectPrinting/ITypeSerializerInternal.cs deleted file mode 100644 index 27825212..00000000 --- a/ObjectPrinting/ITypeSerializerInternal.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace ObjectPrinting; - -internal interface ITypeSerializerInternal -{ - public PrintingConfig Config { get; } -} \ No newline at end of file diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 35387b80..4c2b043b 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -5,7 +5,6 @@ using System.Linq.Expressions; using System.Reflection; using System.Text; -using NUnit.Framework.Internal; namespace ObjectPrinting { @@ -24,7 +23,7 @@ public string PrintToString(TOwner obj) return PrintToString(obj, 0); } - internal void AddConverter(Type type, Func converter) + internal void AddTypeConverter(Type type, Func converter) { _typeConverters.Add(type, converter); } diff --git a/ObjectPrinting/TypeSerializerExtensions.cs b/ObjectPrinting/TypeSerializerExtensions.cs index f1cbc0b3..60072f8e 100644 --- a/ObjectPrinting/TypeSerializerExtensions.cs +++ b/ObjectPrinting/TypeSerializerExtensions.cs @@ -5,11 +5,18 @@ namespace ObjectPrinting; public static class TypeSerializerExtensions { + private static PrintingConfig GetConfig(ITypeSerializer typeSerializer) + { + return ((TypeSerializerImpl)typeSerializer).Config; + } + public static PrintingConfig UseCulture( this ITypeSerializer typeSerializer, CultureInfo cultureInfo) { - var config = ((ITypeSerializerInternal)typeSerializer).Config; + if (cultureInfo == null) + throw new ArgumentNullException($"{nameof(cultureInfo)} cannot be null"); + var config = GetConfig(typeSerializer); config.DoubleCultureInfo = cultureInfo; return config; } @@ -18,7 +25,9 @@ public static PrintingConfig UseCulture( this ITypeSerializer typeSerializer, CultureInfo cultureInfo) { - var config = ((ITypeSerializerInternal)typeSerializer).Config; + if (cultureInfo == null) + throw new ArgumentNullException($"{nameof(cultureInfo)} cannot be null"); + var config = GetConfig(typeSerializer); config.FloatCultureInfo = cultureInfo; return config; } @@ -27,7 +36,7 @@ public static PrintingConfig UseMaxLength( this ITypeSerializer typeSerializer, int maxLength) { - var config = ((ITypeSerializerInternal)typeSerializer).Config; + var config = GetConfig(typeSerializer); config.MaxStringLength = maxLength; return config; } diff --git a/ObjectPrinting/TypeSerializerImpl.cs b/ObjectPrinting/TypeSerializerImpl.cs index afe9e3fd..88a91eda 100644 --- a/ObjectPrinting/TypeSerializerImpl.cs +++ b/ObjectPrinting/TypeSerializerImpl.cs @@ -2,7 +2,7 @@ namespace ObjectPrinting; -internal class TypeSerializerImpl : ITypeSerializer, ITypeSerializerInternal +internal class TypeSerializerImpl : ITypeSerializer { public PrintingConfig Config { get; } @@ -13,7 +13,9 @@ internal TypeSerializerImpl(PrintingConfig config) public PrintingConfig Use(Func converter) { - Config.AddConverter(typeof(TParam), converter); + if (converter == null) + throw new ArgumentNullException($"{nameof(converter)} cannot be null"); + Config.AddTypeConverter(typeof(TParam), converter); return Config; } } \ No newline at end of file From 360534d1fb8e6e29106ae78272b75061a9888c95 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Mon, 9 Dec 2024 00:10:26 +0500 Subject: [PATCH 06/21] implemented basic functionality --- ObjectPrinting/PrintingConfig.cs | 47 +++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 4c2b043b..028331af 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -10,19 +10,26 @@ namespace ObjectPrinting { public class PrintingConfig { - private List _excludedTypes = new(); - private List _excludedProperties = new(); - private Dictionary _typeConverters = new(); - private Dictionary _propertyConverters = new(); + private readonly List _excludedTypes = new(); + private readonly List _excludedProperties = new(); + private readonly Dictionary _typeConverters = new(); + private readonly Dictionary _propertyConverters = new(); internal CultureInfo DoubleCultureInfo { get; set; } = CultureInfo.CurrentCulture; internal CultureInfo FloatCultureInfo { get; set; } = CultureInfo.CurrentCulture; internal int MaxStringLength { get; set; } + internal int MaxRecursionDepth { get; set; } = 16; public string PrintToString(TOwner obj) { return PrintToString(obj, 0); } + public PrintingConfig WithMaxRecursionDepth(int maxRecursionDepth) + { + MaxRecursionDepth = maxRecursionDepth; + return this; + } + internal void AddTypeConverter(Type type, Func converter) { _typeConverters.Add(type, converter); @@ -76,8 +83,7 @@ private static MemberInfo GetMemberInfo(Expression MaxRecursionDepth) return "null" + Environment.NewLine; var finalTypes = new[] @@ -86,19 +92,40 @@ private string PrintToString(object obj, int nestingLevel) typeof(DateTime), typeof(TimeSpan) }; if (finalTypes.Contains(obj.GetType())) + { + if (obj is string stringValue) + return string.Concat( + stringValue.AsSpan(0, Math.Min(MaxStringLength, stringValue.Length)), + Environment.NewLine); return obj + Environment.NewLine; + } - var identation = new string('\t', nestingLevel + 1); + 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)); + if (!_excludedProperties.Contains(propertyInfo) && !_excludedTypes.Contains(propertyInfo.PropertyType)) + { + var propertyValue = propertyInfo.GetValue(obj); + if (propertyValue == null || !TryConvert(propertyInfo, propertyValue, out var valueString)) + valueString = PrintToString(propertyValue, nestingLevel + 1); + + sb.Append($"{indentation}{propertyInfo.Name} = {valueString}"); + } } return sb.ToString(); } + + private bool TryConvert(PropertyInfo propertyInfo, object? propertyValue, out string value) + { + value = String.Empty; + if (_propertyConverters.TryGetValue(propertyInfo, out var converter)) + value = $"{converter.DynamicInvoke(propertyValue) as string ?? "null"}{Environment.NewLine}"; + else if (_typeConverters.TryGetValue(propertyInfo.PropertyType, out var typeConverter)) + value = $"{typeConverter.DynamicInvoke(propertyValue) as string ?? "null"}{Environment.NewLine}"; + return value != String.Empty; + } } } \ No newline at end of file From 12332f205fb935316dcc9525eace886ac3df0cd5 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Mon, 9 Dec 2024 00:20:35 +0500 Subject: [PATCH 07/21] implemented culture support --- ObjectPrinting/PrintingConfig.cs | 11 +++++++++++ ObjectPrinting/TypeSerializerExtensions.cs | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 028331af..13fa2e54 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -16,6 +16,7 @@ public class PrintingConfig private readonly Dictionary _propertyConverters = new(); internal CultureInfo DoubleCultureInfo { get; set; } = CultureInfo.CurrentCulture; internal CultureInfo FloatCultureInfo { get; set; } = CultureInfo.CurrentCulture; + internal CultureInfo DateTimeCultureInfo { get; set; } = CultureInfo.CurrentCulture; internal int MaxStringLength { get; set; } internal int MaxRecursionDepth { get; set; } = 16; @@ -97,6 +98,16 @@ private string PrintToString(object obj, int nestingLevel) return string.Concat( stringValue.AsSpan(0, Math.Min(MaxStringLength, stringValue.Length)), Environment.NewLine); + + if (obj is double doubleValue) + return doubleValue.ToString(DoubleCultureInfo) + Environment.NewLine; + + if (obj is float floatValue) + return floatValue.ToString(FloatCultureInfo) + Environment.NewLine; + + if (obj is DateTime dateTimeValue) + return dateTimeValue.ToString(DateTimeCultureInfo) + Environment.NewLine; + return obj + Environment.NewLine; } diff --git a/ObjectPrinting/TypeSerializerExtensions.cs b/ObjectPrinting/TypeSerializerExtensions.cs index 60072f8e..b3a22bef 100644 --- a/ObjectPrinting/TypeSerializerExtensions.cs +++ b/ObjectPrinting/TypeSerializerExtensions.cs @@ -10,6 +10,17 @@ private static PrintingConfig GetConfig(ITypeSerializer< return ((TypeSerializerImpl)typeSerializer).Config; } + public static PrintingConfig UseCulture( + this ITypeSerializer typeSerializer, + CultureInfo cultureInfo) + { + if (cultureInfo == null) + throw new ArgumentNullException($"{nameof(cultureInfo)} cannot be null"); + var config = GetConfig(typeSerializer); + config.DateTimeCultureInfo = cultureInfo; + return config; + } + public static PrintingConfig UseCulture( this ITypeSerializer typeSerializer, CultureInfo cultureInfo) From 9e2a9507f4a97d098d99552d983fb2e00e65707e Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Mon, 9 Dec 2024 00:34:37 +0500 Subject: [PATCH 08/21] fixed a bug with MaxStringLength --- ObjectPrinting/PrintingConfig.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 13fa2e54..12a2d604 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -17,7 +17,7 @@ public class PrintingConfig internal CultureInfo DoubleCultureInfo { get; set; } = CultureInfo.CurrentCulture; internal CultureInfo FloatCultureInfo { get; set; } = CultureInfo.CurrentCulture; internal CultureInfo DateTimeCultureInfo { get; set; } = CultureInfo.CurrentCulture; - internal int MaxStringLength { get; set; } + internal int MaxStringLength { get; set; } = int.MaxValue; internal int MaxRecursionDepth { get; set; } = 16; public string PrintToString(TOwner obj) From c7b2f79233897d2576b5f24db013e17a5c275d64 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Mon, 9 Dec 2024 00:35:21 +0500 Subject: [PATCH 09/21] added object extension methods for printing --- ObjectPrinting/ObjectExtensions.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ObjectPrinting/ObjectExtensions.cs diff --git a/ObjectPrinting/ObjectExtensions.cs b/ObjectPrinting/ObjectExtensions.cs new file mode 100644 index 00000000..1ac1f44a --- /dev/null +++ b/ObjectPrinting/ObjectExtensions.cs @@ -0,0 +1,17 @@ +using System; + +namespace ObjectPrinting; + +public static class ObjectExtensions +{ + public static string PrintToString(this T obj) + { + return ObjectPrinter.For().PrintToString(obj); + } + + public static string PrintToString(this T obj, Func, PrintingConfig> configurer) + { + var config = configurer(ObjectPrinter.For()); + return config.PrintToString(obj); + } +} \ No newline at end of file From 02918de93e54e8761a1ae1490f099477ae985507 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Mon, 9 Dec 2024 01:23:30 +0500 Subject: [PATCH 10/21] added support for collections --- ObjectPrinting/PrintingConfig.cs | 36 +++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 12a2d604..3fecf28f 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -114,21 +115,46 @@ private string PrintToString(object obj, int nestingLevel) var indentation = new string('\t', nestingLevel + 1); var sb = new StringBuilder(); var type = obj.GetType(); - sb.AppendLine(type.Name); + sb.AppendLine($"{type.Name}:"); + + if (obj is IEnumerable enumerable) + { + var bracketIndentation = new string('\t', nestingLevel); + sb.AppendLine($"{bracketIndentation}["); + foreach (var element in enumerable) + { + sb.Append($"{bracketIndentation}-\t"); + var valueString = String.Empty; + if (_typeConverters.TryGetValue(element.GetType(), out var typeConverter)) + valueString = + $"{typeConverter.DynamicInvoke(element) as string ?? "null"}{Environment.NewLine}"; + else + valueString = PrintToString(element, nestingLevel + 1); + sb.Append($"{valueString}"); + } + sb.AppendLine($"{bracketIndentation}]"); + return sb.ToString(); + } + foreach (var propertyInfo in type.GetProperties()) { if (!_excludedProperties.Contains(propertyInfo) && !_excludedTypes.Contains(propertyInfo.PropertyType)) { - var propertyValue = propertyInfo.GetValue(obj); - if (propertyValue == null || !TryConvert(propertyInfo, propertyValue, out var valueString)) - valueString = PrintToString(propertyValue, nestingLevel + 1); - + var valueString = GetValueString(propertyInfo, obj, nestingLevel); sb.Append($"{indentation}{propertyInfo.Name} = {valueString}"); } } return sb.ToString(); } + private string GetValueString(PropertyInfo propertyInfo, object obj, int nestingLevel) + { + var propertyValue = propertyInfo.GetValue(obj); + if (propertyValue == null || !TryConvert(propertyInfo, propertyValue, out var valueString)) + valueString = PrintToString(propertyValue, nestingLevel + 1); + return valueString; + } + private bool TryConvert(PropertyInfo propertyInfo, object? propertyValue, out string value) { value = String.Empty; From c87c05795f05adc4a3afefb3102683d6de65d0ef Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Wed, 11 Dec 2024 21:26:24 +0500 Subject: [PATCH 11/21] fix for recursion depth --- ObjectPrinting/PrintingConfig.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 3fecf28f..e4b3f385 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -85,7 +85,7 @@ private static MemberInfo GetMemberInfo(Expression MaxRecursionDepth) + if (obj == null) return "null" + Environment.NewLine; var finalTypes = new[] @@ -111,6 +111,9 @@ private string PrintToString(object obj, int nestingLevel) return obj + Environment.NewLine; } + + if (nestingLevel > MaxRecursionDepth) + return "null" + Environment.NewLine; var indentation = new string('\t', nestingLevel + 1); var sb = new StringBuilder(); From 92dbb2a1e03977e5e70549092ae71a48903bdcfc Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Wed, 11 Dec 2024 21:50:10 +0500 Subject: [PATCH 12/21] added tests --- .gitignore | 3 + ...ests.CollectionSupport_Arrays.verified.txt | 8 + ...ollectionSupport_Dictionaries.verified.txt | 9 + ...Tests.CollectionSupport_Lists.verified.txt | 5 + ...nceTests.CultureSpecification.verified.txt | 7 + ...eTests.CyclicReferenceSupport.verified.txt | 25 +++ ...ethod_ForDefaultSerialization.verified.txt | 7 + ...thod_WithConfigurationSupport.verified.txt | 6 + ...ptanceTests.PropertyExclusion.verified.txt | 6 + ...rtySerializationSpecification.verified.txt | 7 + ...cceptanceTests.StringTrimming.verified.txt | 7 + ...AcceptanceTests.TypeExclusion.verified.txt | 5 + ...ypeSerializationSpecification.verified.txt | 7 + .../Tests/ObjectPrinterAcceptanceTests.cs | 202 +++++++++++++++--- ObjectPrinting/Tests/Person.cs | 3 + 15 files changed, 276 insertions(+), 31 deletions(-) create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Arrays.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Dictionaries.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Lists.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CultureSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CyclicReferenceSupport.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_ForDefaultSerialization.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_WithConfigurationSupport.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertyExclusion.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertySerializationSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringTrimming.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeExclusion.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeSerializationSpecification.verified.txt diff --git a/.gitignore b/.gitignore index a1f46668..4c1e8fdf 100644 --- a/.gitignore +++ b/.gitignore @@ -236,3 +236,6 @@ _Pvt_Extensions .fake/ .idea/ + +# Verify.NUnit +*.received.* \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Arrays.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Arrays.verified.txt new file mode 100644 index 00000000..a6d4b4ec --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Arrays.verified.txt @@ -0,0 +1,8 @@ +Double[]: +[ +- 1 +- 2 +- 3.14 +- 4 +- 5 +] diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Dictionaries.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Dictionaries.verified.txt new file mode 100644 index 00000000..ecfae574 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Dictionaries.verified.txt @@ -0,0 +1,9 @@ +Dictionary`2: +[ +- KeyValuePair`2: + Key = a + Value = 1.1 +- KeyValuePair`2: + Key = b + Value = 2.2 +] diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Lists.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Lists.verified.txt new file mode 100644 index 00000000..c27220a5 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Lists.verified.txt @@ -0,0 +1,5 @@ +List`1: +[ +- Alex +- John +] diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CultureSpecification.verified.txt new file mode 100644 index 00000000..13b2853d --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CultureSpecification.verified.txt @@ -0,0 +1,7 @@ +Person: + Id = Guid: + Name = Alex + Surname = Smith + Height = 177.4 + Age = 19 + OtherPerson = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CyclicReferenceSupport.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CyclicReferenceSupport.verified.txt new file mode 100644 index 00000000..11148c21 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CyclicReferenceSupport.verified.txt @@ -0,0 +1,25 @@ +Person: + Id = Guid: + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = Person: + Id = Guid: + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = Person: + Id = Guid: + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = Person: + Id = null + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_ForDefaultSerialization.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_ForDefaultSerialization.verified.txt new file mode 100644 index 00000000..82b7833b --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_ForDefaultSerialization.verified.txt @@ -0,0 +1,7 @@ +Person: + Id = Guid: + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_WithConfigurationSupport.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_WithConfigurationSupport.verified.txt new file mode 100644 index 00000000..ff2da16e --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_WithConfigurationSupport.verified.txt @@ -0,0 +1,6 @@ +Person: + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertyExclusion.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertyExclusion.verified.txt new file mode 100644 index 00000000..ff2da16e --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertyExclusion.verified.txt @@ -0,0 +1,6 @@ +Person: + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertySerializationSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertySerializationSpecification.verified.txt new file mode 100644 index 00000000..3b06ffa1 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertySerializationSpecification.verified.txt @@ -0,0 +1,7 @@ +Person: + Id = Guid: + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = John diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringTrimming.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringTrimming.verified.txt new file mode 100644 index 00000000..0ee6ea0f --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringTrimming.verified.txt @@ -0,0 +1,7 @@ +Person: + Id = Guid: + Name = Al + Surname = Sm + Height = 177,4 + Age = 19 + OtherPerson = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeExclusion.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeExclusion.verified.txt new file mode 100644 index 00000000..d537721a --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeExclusion.verified.txt @@ -0,0 +1,5 @@ +Person: + Id = Guid: + Height = 177,4 + Age = 19 + OtherPerson = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeSerializationSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeSerializationSpecification.verified.txt new file mode 100644 index 00000000..a934f6d3 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeSerializationSpecification.verified.txt @@ -0,0 +1,7 @@ +Person: + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs index c6847d47..15e625dd 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs @@ -1,40 +1,180 @@ using System; +using System.Collections.Generic; using System.Globalization; +using System.Threading.Tasks; using NUnit.Framework; +using VerifyNUnit; -namespace ObjectPrinting.Tests +namespace ObjectPrinting.Tests; + +[TestFixture] +public class ObjectPrinterAcceptanceTests { - [TestFixture] - public class ObjectPrinterAcceptanceTests + private Person _person; + private PrintingConfig _config; + + [SetUp] + public void SetUp() + { + _person = new Person + { + Id = new Guid(), Name = "Alex", Surname = "Smith", + Age = 19, Height = 177.4 + }; + _config = ObjectPrinter.For(); + } + + [Test] + [Description("Возможность исключения из сериализации свойств определенного типа")] + public Task TypeExclusion() + { + var str = _config + .ExceptType() + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + [Description("Возможность указать альтернативный способ сериализации для определенного типа")] + public Task TypeSerializationSpecification() + { + var str = _config + .ForType() + .Use(o => o.ToString()) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + [Description("Возможность для числовых типов указать культуру")] + public Task CultureSpecification() + { + var str = _config + .ForType() + .UseCulture(CultureInfo.InvariantCulture) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + [Description("Возможность настройки сериализации конкретного свойства")] + public Task PropertySerializationSpecification() + { + _person.OtherPerson = new Person { Id = new Guid(), Name = "John", Surname = "Doe" }; + + var str = _config + .ForProperty(x => x.OtherPerson!) + .Use(o => o.Name) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + [Description("Возможность обрезания строк")] + public Task StringTrimming() + { + var str = _config + .ForType() + .UseMaxLength(2) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + [Description("Возможность исключения из сериализации конкретного свойства/поля")] + public Task PropertyExclusion() + { + var str = _config + .ExceptProperty(x => x.Id) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + [Description("Обработка циклических ссылок между объектами")] + public Task CyclicReferenceSupport() + { + _person.OtherPerson = _person; + + var str = _config + .WithMaxRecursionDepth(3) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + [Description("Метод-расширение для сериализации по умолчанию")] + public Task ExtensionMethod_ForDefaultSerialization() { - [Test] - public void Demo() + var str = _person.PrintToString(); + + return Verifier.Verify(str); + } + + [Test] + [Description("Метод-расширение, позволяющий проводить настройку сериализации")] + public Task ExtensionMethod_WithConfigurationSupport() + { + var str = _person.PrintToString( + config => config.ExceptProperty(x => x.Id)); + + return Verifier.Verify(str); + } + + [Test] + [Description("Сериализация массивов")] + public Task CollectionSupport_Arrays() + { + var arr = new double[] {1, 2, 3.14, 4, 5}; + + var str = arr.PrintToString( + config => config + .ForType() + .UseCulture(CultureInfo.InvariantCulture)); + + return Verifier.Verify(str); + } + + [Test] + [Description("Сериализация списков")] + public Task CollectionSupport_Lists() + { + var list = new List + { + _person, + new Person { Id = new Guid(), Name = "John", Surname = "Doe" } + }; + + var str = list.PrintToString( + config => config + .ForType() + .Use(o => o.Name)); + + return Verifier.Verify(str); + } + + [Test] + [Description("Сериализация словарей")] + public Task CollectionSupport_Dictionaries() + { + var dictionary = new Dictionary { - var person = new Person { Name = "Alex", Age = 19 }; - - var printer = ObjectPrinter.For(); - //1. Исключить из сериализации свойства определенного типа - printer.ExceptType(); - //2. Указать альтернативный способ сериализации для определенного типа - printer.ForType() - .Use(o => o.ToString()); - //3. Для числовых типов указать культуру - printer.ForType() - .UseCulture(CultureInfo.InvariantCulture); - //4. Настроить сериализацию конкретного свойства - printer.ForProperty(x => x.Age) // TODO use Expression> - .Use(o => o.ToString()); - //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) - printer.ForType() - .UseMaxLength(15); - //6. Исключить из сериализации конкретного свойства - printer.ExceptProperty(nameof(Person.Name)); - - string s1 = printer.PrintToString(person); - // TODO use Verify.NUnit - - //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию - //8. ...с конфигурированием - } + { "a", 1.1 }, + { "b", 2.2 } + }; + + var str = dictionary.PrintToString( + config => config + .ForType() + .UseCulture(CultureInfo.InvariantCulture)); + + return Verifier.Verify(str); } } \ No newline at end of file diff --git a/ObjectPrinting/Tests/Person.cs b/ObjectPrinting/Tests/Person.cs index f9555955..16f94ff6 100644 --- a/ObjectPrinting/Tests/Person.cs +++ b/ObjectPrinting/Tests/Person.cs @@ -6,7 +6,10 @@ public class Person { public Guid Id { get; set; } public string Name { get; set; } + public string Surname { get; set; } public double Height { get; set; } public int Age { get; set; } + + public Person? OtherPerson { get; set; } } } \ No newline at end of file From d6aeaa892ba52be1151b30e04bbf981f7aa0fb30 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Wed, 11 Dec 2024 22:09:37 +0500 Subject: [PATCH 13/21] added bounds check for WithMaxRecursionDepth() --- ObjectPrinting/PrintingConfig.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index e4b3f385..5008cbb1 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -28,6 +28,8 @@ public string PrintToString(TOwner obj) public PrintingConfig WithMaxRecursionDepth(int maxRecursionDepth) { + if (maxRecursionDepth < 0) + throw new ArgumentOutOfRangeException($"{nameof(maxRecursionDepth)} must not be less than 0"); MaxRecursionDepth = maxRecursionDepth; return this; } From 50334f7a97c3a7f9acd31771c2fd41eae70ed76f Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Wed, 11 Dec 2024 22:23:22 +0500 Subject: [PATCH 14/21] refactored PrintToString() --- ObjectPrinting/PrintingConfig.cs | 74 +++++++++++++++++--------------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 5008cbb1..645bd41b 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -96,23 +96,7 @@ private string PrintToString(object obj, int nestingLevel) typeof(DateTime), typeof(TimeSpan) }; if (finalTypes.Contains(obj.GetType())) - { - if (obj is string stringValue) - return string.Concat( - stringValue.AsSpan(0, Math.Min(MaxStringLength, stringValue.Length)), - Environment.NewLine); - - if (obj is double doubleValue) - return doubleValue.ToString(DoubleCultureInfo) + Environment.NewLine; - - if (obj is float floatValue) - return floatValue.ToString(FloatCultureInfo) + Environment.NewLine; - - if (obj is DateTime dateTimeValue) - return dateTimeValue.ToString(DateTimeCultureInfo) + Environment.NewLine; - - return obj + Environment.NewLine; - } + return SerializeFinalType(obj); if (nestingLevel > MaxRecursionDepth) return "null" + Environment.NewLine; @@ -123,23 +107,7 @@ private string PrintToString(object obj, int nestingLevel) sb.AppendLine($"{type.Name}:"); if (obj is IEnumerable enumerable) - { - var bracketIndentation = new string('\t', nestingLevel); - sb.AppendLine($"{bracketIndentation}["); - foreach (var element in enumerable) - { - sb.Append($"{bracketIndentation}-\t"); - var valueString = String.Empty; - if (_typeConverters.TryGetValue(element.GetType(), out var typeConverter)) - valueString = - $"{typeConverter.DynamicInvoke(element) as string ?? "null"}{Environment.NewLine}"; - else - valueString = PrintToString(element, nestingLevel + 1); - sb.Append($"{valueString}"); - } - sb.AppendLine($"{bracketIndentation}]"); - return sb.ToString(); - } + return SerializeEnumerable(sb, enumerable, nestingLevel); foreach (var propertyInfo in type.GetProperties()) { @@ -152,6 +120,44 @@ private string PrintToString(object obj, int nestingLevel) return sb.ToString(); } + private string SerializeEnumerable(StringBuilder sb, IEnumerable enumerable, int nestingLevel) + { + var bracketIndentation = new string('\t', nestingLevel); + sb.AppendLine($"{bracketIndentation}["); + foreach (var element in enumerable) + { + sb.Append($"{bracketIndentation}-\t"); + var valueString = String.Empty; + if (_typeConverters.TryGetValue(element.GetType(), out var typeConverter)) + valueString = + $"{typeConverter.DynamicInvoke(element) as string ?? "null"}{Environment.NewLine}"; + else + valueString = PrintToString(element, nestingLevel + 1); + sb.Append($"{valueString}"); + } + sb.AppendLine($"{bracketIndentation}]"); + return sb.ToString(); + } + + private string SerializeFinalType(object obj) + { + if (obj is string stringValue) + return string.Concat( + stringValue.AsSpan(0, Math.Min(MaxStringLength, stringValue.Length)), + Environment.NewLine); + + if (obj is double doubleValue) + return doubleValue.ToString(DoubleCultureInfo) + Environment.NewLine; + + if (obj is float floatValue) + return floatValue.ToString(FloatCultureInfo) + Environment.NewLine; + + if (obj is DateTime dateTimeValue) + return dateTimeValue.ToString(DateTimeCultureInfo) + Environment.NewLine; + + return obj + Environment.NewLine; + } + private string GetValueString(PropertyInfo propertyInfo, object obj, int nestingLevel) { var propertyValue = propertyInfo.GetValue(obj); From 743b6a86ba1a9cee817f591389385d60e047ecb6 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Thu, 12 Dec 2024 14:44:27 +0500 Subject: [PATCH 15/21] improved culture support --- ObjectPrinting/PrintingConfig.cs | 52 +++++++++------------- ObjectPrinting/TypeSerializerExtensions.cs | 29 ++---------- 2 files changed, 24 insertions(+), 57 deletions(-) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 645bd41b..f6236da3 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -15,17 +15,20 @@ public class PrintingConfig private readonly List _excludedProperties = new(); private readonly Dictionary _typeConverters = new(); private readonly Dictionary _propertyConverters = new(); - internal CultureInfo DoubleCultureInfo { get; set; } = CultureInfo.CurrentCulture; - internal CultureInfo FloatCultureInfo { get; set; } = CultureInfo.CurrentCulture; - internal CultureInfo DateTimeCultureInfo { get; set; } = CultureInfo.CurrentCulture; + private readonly Dictionary _cultureSpecs = new(); internal int MaxStringLength { get; set; } = int.MaxValue; - internal int MaxRecursionDepth { get; set; } = 16; + private int MaxRecursionDepth { get; set; } = 16; public string PrintToString(TOwner obj) { return PrintToString(obj, 0); } + internal void AddCultureSpec(Type type, CultureInfo cultureInfo) + { + _cultureSpecs.Add(type, cultureInfo); + } + public PrintingConfig WithMaxRecursionDepth(int maxRecursionDepth) { if (maxRecursionDepth < 0) @@ -88,18 +91,22 @@ private static MemberInfo GetMemberInfo(Expression MaxRecursionDepth) - return "null" + Environment.NewLine; + return $"null{Environment.NewLine}"; var indentation = new string('\t', nestingLevel + 1); var sb = new StringBuilder(); @@ -139,25 +146,6 @@ private string SerializeEnumerable(StringBuilder sb, IEnumerable enumerable, int return sb.ToString(); } - private string SerializeFinalType(object obj) - { - if (obj is string stringValue) - return string.Concat( - stringValue.AsSpan(0, Math.Min(MaxStringLength, stringValue.Length)), - Environment.NewLine); - - if (obj is double doubleValue) - return doubleValue.ToString(DoubleCultureInfo) + Environment.NewLine; - - if (obj is float floatValue) - return floatValue.ToString(FloatCultureInfo) + Environment.NewLine; - - if (obj is DateTime dateTimeValue) - return dateTimeValue.ToString(DateTimeCultureInfo) + Environment.NewLine; - - return obj + Environment.NewLine; - } - private string GetValueString(PropertyInfo propertyInfo, object obj, int nestingLevel) { var propertyValue = propertyInfo.GetValue(obj); diff --git a/ObjectPrinting/TypeSerializerExtensions.cs b/ObjectPrinting/TypeSerializerExtensions.cs index b3a22bef..677966d7 100644 --- a/ObjectPrinting/TypeSerializerExtensions.cs +++ b/ObjectPrinting/TypeSerializerExtensions.cs @@ -9,37 +9,16 @@ private static PrintingConfig GetConfig(ITypeSerializer< { return ((TypeSerializerImpl)typeSerializer).Config; } - - public static PrintingConfig UseCulture( - this ITypeSerializer typeSerializer, - CultureInfo cultureInfo) - { - if (cultureInfo == null) - throw new ArgumentNullException($"{nameof(cultureInfo)} cannot be null"); - var config = GetConfig(typeSerializer); - config.DateTimeCultureInfo = cultureInfo; - return config; - } - - public static PrintingConfig UseCulture( - this ITypeSerializer typeSerializer, - CultureInfo cultureInfo) - { - if (cultureInfo == null) - throw new ArgumentNullException($"{nameof(cultureInfo)} cannot be null"); - var config = GetConfig(typeSerializer); - config.DoubleCultureInfo = cultureInfo; - return config; - } - public static PrintingConfig UseCulture( - this ITypeSerializer typeSerializer, + public static PrintingConfig UseCulture( + this ITypeSerializer typeSerializer, CultureInfo cultureInfo) + where TParam : IFormattable { if (cultureInfo == null) throw new ArgumentNullException($"{nameof(cultureInfo)} cannot be null"); var config = GetConfig(typeSerializer); - config.FloatCultureInfo = cultureInfo; + config.AddCultureSpec(typeof(TParam), cultureInfo); return config; } From 69eca2aad4000dd168ae878b5140a74f5346a6cb Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Thu, 12 Dec 2024 15:07:22 +0500 Subject: [PATCH 16/21] made trimming individual string fields possible --- ObjectPrinting/PrintingConfig.cs | 8 ++++++++ ObjectPrinting/PropertySerializerExtensions.cs | 18 ++++++++++++++++++ ObjectPrinting/PropertySerializerImpl.cs | 6 +++--- 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 ObjectPrinting/PropertySerializerExtensions.cs diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index f6236da3..c38730d5 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -16,6 +16,7 @@ public class PrintingConfig private readonly Dictionary _typeConverters = new(); private readonly Dictionary _propertyConverters = new(); private readonly Dictionary _cultureSpecs = new(); + private readonly Dictionary _stringPropertyLengths = new(); internal int MaxStringLength { get; set; } = int.MaxValue; private int MaxRecursionDepth { get; set; } = 16; @@ -29,6 +30,11 @@ internal void AddCultureSpec(Type type, CultureInfo cultureInfo) _cultureSpecs.Add(type, cultureInfo); } + internal void AddStringPropertyLength(MemberInfo propertyInfo, int length) + { + _stringPropertyLengths.Add(propertyInfo, length); + } + public PrintingConfig WithMaxRecursionDepth(int maxRecursionDepth) { if (maxRecursionDepth < 0) @@ -159,6 +165,8 @@ private bool TryConvert(PropertyInfo propertyInfo, object? propertyValue, out st value = String.Empty; if (_propertyConverters.TryGetValue(propertyInfo, out var converter)) value = $"{converter.DynamicInvoke(propertyValue) as string ?? "null"}{Environment.NewLine}"; + else if (propertyValue is string str && _stringPropertyLengths.TryGetValue(propertyInfo, out var length)) + value = $"{str.Substring(0, Math.Min(length, str.Length))}{Environment.NewLine}{length}"; else if (_typeConverters.TryGetValue(propertyInfo.PropertyType, out var typeConverter)) value = $"{typeConverter.DynamicInvoke(propertyValue) as string ?? "null"}{Environment.NewLine}"; return value != String.Empty; diff --git a/ObjectPrinting/PropertySerializerExtensions.cs b/ObjectPrinting/PropertySerializerExtensions.cs new file mode 100644 index 00000000..8ae83171 --- /dev/null +++ b/ObjectPrinting/PropertySerializerExtensions.cs @@ -0,0 +1,18 @@ +using System; + +namespace ObjectPrinting; + +public static class PropertySerializerExtensions +{ + public static PrintingConfig UseMaxLength( + this IPropertySerializer serializer, + int maxLength) + { + if (maxLength < 0) + throw new ArgumentOutOfRangeException($"{nameof(maxLength)} cannot be negative"); + var typeSerializer = (PropertySerializerImpl)serializer; + var config = typeSerializer.Config; + config.AddStringPropertyLength(typeSerializer.MemberInfo, maxLength); + return config; + } +} \ No newline at end of file diff --git a/ObjectPrinting/PropertySerializerImpl.cs b/ObjectPrinting/PropertySerializerImpl.cs index 714ca584..c247950a 100644 --- a/ObjectPrinting/PropertySerializerImpl.cs +++ b/ObjectPrinting/PropertySerializerImpl.cs @@ -6,17 +6,17 @@ namespace ObjectPrinting; internal class PropertySerializerImpl : IPropertySerializer { public PrintingConfig Config { get; } - private readonly MemberInfo _memberInfo; + public readonly MemberInfo MemberInfo; internal PropertySerializerImpl(PrintingConfig config, MemberInfo memberInfo) { Config = config; - _memberInfo = memberInfo; + MemberInfo = memberInfo; } public PrintingConfig Use(Func converter) { - Config.AddPropertyConverter(converter, _memberInfo); + Config.AddPropertyConverter(converter, MemberInfo); return Config; } } \ No newline at end of file From 90176f4165a9c6eab431b4a753c1f6e515ba4b93 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Thu, 12 Dec 2024 15:08:15 +0500 Subject: [PATCH 17/21] added bounds check for maxLength parameter --- ObjectPrinting/TypeSerializerExtensions.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ObjectPrinting/TypeSerializerExtensions.cs b/ObjectPrinting/TypeSerializerExtensions.cs index 677966d7..85417610 100644 --- a/ObjectPrinting/TypeSerializerExtensions.cs +++ b/ObjectPrinting/TypeSerializerExtensions.cs @@ -26,6 +26,9 @@ public static PrintingConfig UseMaxLength( this ITypeSerializer typeSerializer, int maxLength) { + if (maxLength < 0) + throw new ArgumentOutOfRangeException($"{nameof(maxLength)} cannot be negative"); + var config = GetConfig(typeSerializer); config.MaxStringLength = maxLength; return config; From d79b36a1cf462061c10e1709d04596f2d634395b Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Thu, 12 Dec 2024 21:52:39 +0500 Subject: [PATCH 18/21] improved collections support and overall serialization style --- ObjectPrinting/PrintingConfig.cs | 95 +++++++++++++++++++++++--------- 1 file changed, 69 insertions(+), 26 deletions(-) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index c38730d5..8a3761d2 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -22,7 +22,7 @@ public class PrintingConfig public string PrintToString(TOwner obj) { - return PrintToString(obj, 0); + return PrintToString(obj, 0, 0); } internal void AddCultureSpec(Type type, CultureInfo cultureInfo) @@ -94,69 +94,112 @@ private static MemberInfo GetMemberInfo(Expression MaxRecursionDepth) - return $"null{Environment.NewLine}"; + if (recursionDepth > MaxRecursionDepth) + return "null"; var indentation = new string('\t', nestingLevel + 1); var sb = new StringBuilder(); var type = obj.GetType(); - sb.AppendLine($"{type.Name}:"); + + if (obj is IDictionary dictionary) + return SerializeDictionary(sb, dictionary, nestingLevel, recursionDepth); if (obj is IEnumerable enumerable) - return SerializeEnumerable(sb, enumerable, nestingLevel); + return SerializeEnumerable(sb, enumerable, nestingLevel, recursionDepth); - foreach (var propertyInfo in type.GetProperties()) + var bracketIndentation = new string('\t', nestingLevel); + sb.AppendLine($"{type.Name}:"); + sb.AppendLine($"{bracketIndentation}{{"); + var properties = type.GetProperties(); + foreach (var propertyInfo in properties) { if (!_excludedProperties.Contains(propertyInfo) && !_excludedTypes.Contains(propertyInfo.PropertyType)) { - var valueString = GetValueString(propertyInfo, obj, nestingLevel); - sb.Append($"{indentation}{propertyInfo.Name} = {valueString}"); + var valueString = GetValueString(propertyInfo, obj, nestingLevel, recursionDepth); + sb.AppendLine($"{indentation}{propertyInfo.Name} = {valueString}"); } } + sb.Append($"{bracketIndentation}}}"); return sb.ToString(); } - private string SerializeEnumerable(StringBuilder sb, IEnumerable enumerable, int nestingLevel) + private string SerializeEnumerable(StringBuilder sb, IEnumerable enumerable, int nestingLevel, int recursionDepth) { var bracketIndentation = new string('\t', nestingLevel); sb.AppendLine($"{bracketIndentation}["); + if (!enumerable.GetEnumerator().MoveNext()) + { + return "[]"; + } + enumerable.GetEnumerator().Reset(); foreach (var element in enumerable) { - sb.Append($"{bracketIndentation}-\t"); + sb.Append($"{bracketIndentation}\t"); var valueString = String.Empty; if (_typeConverters.TryGetValue(element.GetType(), out var typeConverter)) valueString = - $"{typeConverter.DynamicInvoke(element) as string ?? "null"}{Environment.NewLine}"; + $"{typeConverter.DynamicInvoke(element) as string ?? "null"}"; + else + valueString = PrintToString(element, nestingLevel + 1, recursionDepth + 1); + sb.AppendLine($"{valueString},"); + } + sb.AppendLine($"{bracketIndentation}]"); + return sb.ToString(); + } + + private string SerializeDictionary(StringBuilder sb, IDictionary dictionary, int nestingLevel, int recursionDepth) + { + var bracketIndentation = new string('\t', nestingLevel); + sb.AppendLine($"{bracketIndentation}["); + foreach (DictionaryEntry element in dictionary) + { + var key = element.Key; + var value = element.Value; + + var keyValueIndentation = new string('\t', nestingLevel + 1); + sb.AppendLine($"{bracketIndentation}{{"); + sb.Append($"{keyValueIndentation}key: "); + var keyString = String.Empty; + if (_typeConverters.TryGetValue(key.GetType(), out var typeConverter)) + keyString = + $"{typeConverter.DynamicInvoke(key) as string ?? "null"}"; + else + keyString = PrintToString(key, nestingLevel + 2, recursionDepth + 1); + sb.AppendLine($"{keyString}"); + sb.Append($"{keyValueIndentation}value: "); + var valueString = String.Empty; + if (_typeConverters.TryGetValue(value.GetType(), out typeConverter)) + valueString = + $"{typeConverter.DynamicInvoke(value) as string ?? "null"}"; else - valueString = PrintToString(element, nestingLevel + 1); - sb.Append($"{valueString}"); + valueString = PrintToString(value, nestingLevel + 2, recursionDepth + 1); + sb.AppendLine($"{valueString}"); + sb.AppendLine($"{bracketIndentation}}},"); } sb.AppendLine($"{bracketIndentation}]"); return sb.ToString(); } - private string GetValueString(PropertyInfo propertyInfo, object obj, int nestingLevel) + private string GetValueString(PropertyInfo propertyInfo, object obj, int nestingLevel, int recursionDepth) { var propertyValue = propertyInfo.GetValue(obj); if (propertyValue == null || !TryConvert(propertyInfo, propertyValue, out var valueString)) - valueString = PrintToString(propertyValue, nestingLevel + 1); + valueString = PrintToString(propertyValue, nestingLevel + 1, recursionDepth + 1); return valueString; } @@ -164,11 +207,11 @@ private bool TryConvert(PropertyInfo propertyInfo, object? propertyValue, out st { value = String.Empty; if (_propertyConverters.TryGetValue(propertyInfo, out var converter)) - value = $"{converter.DynamicInvoke(propertyValue) as string ?? "null"}{Environment.NewLine}"; + value = $"{converter.DynamicInvoke(propertyValue) as string ?? "null"}"; else if (propertyValue is string str && _stringPropertyLengths.TryGetValue(propertyInfo, out var length)) - value = $"{str.Substring(0, Math.Min(length, str.Length))}{Environment.NewLine}{length}"; + value = $"{str.Substring(0, Math.Min(length, str.Length))}"; else if (_typeConverters.TryGetValue(propertyInfo.PropertyType, out var typeConverter)) - value = $"{typeConverter.DynamicInvoke(propertyValue) as string ?? "null"}{Environment.NewLine}"; + value = $"{typeConverter.DynamicInvoke(propertyValue) as string ?? "null"}"; return value != String.Empty; } } From 436cdd4b9efa8ee3b069407feb0be01a28fa8080 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Thu, 12 Dec 2024 21:53:54 +0500 Subject: [PATCH 19/21] updated basic tests --- ...ests.CollectionSupport_Arrays.verified.txt | 13 ++++++------ ...ollectionSupport_Dictionaries.verified.txt | 17 ++++++++-------- ...Tests.CollectionSupport_Lists.verified.txt | 7 +++---- ...nceTests.CultureSpecification.verified.txt | 5 ++++- ...eTests.CyclicReferenceSupport.verified.txt | 20 +++++++++++++++---- ...ethod_ForDefaultSerialization.verified.txt | 5 ++++- ...thod_WithConfigurationSupport.verified.txt | 3 +++ ...ptanceTests.PropertyExclusion.verified.txt | 3 +++ ...rtySerializationSpecification.verified.txt | 5 ++++- ...eTests.StringPropertyTrimming.verified.txt | 10 ++++++++++ ...cceptanceTests.StringTrimming.verified.txt | 5 ++++- ...AcceptanceTests.TypeExclusion.verified.txt | 5 ++++- ...ypeSerializationSpecification.verified.txt | 7 +++++-- .../Tests/ObjectPrinterAcceptanceTests.cs | 19 +++++++++++++++--- ObjectPrinting/Tests/Person.cs | 2 ++ 15 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringPropertyTrimming.verified.txt diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Arrays.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Arrays.verified.txt index a6d4b4ec..07f3ce1b 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Arrays.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Arrays.verified.txt @@ -1,8 +1,7 @@ -Double[]: -[ -- 1 -- 2 -- 3.14 -- 4 -- 5 +[ + 1, + 2, + 3.14, + 4, + 5, ] diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Dictionaries.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Dictionaries.verified.txt index ecfae574..4f6f66c9 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Dictionaries.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Dictionaries.verified.txt @@ -1,9 +1,10 @@ -Dictionary`2: -[ -- KeyValuePair`2: - Key = a - Value = 1.1 -- KeyValuePair`2: - Key = b - Value = 2.2 +[ +{ + key: a + value: 1.1 +}, +{ + key: b + value: 2.2 +}, ] diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Lists.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Lists.verified.txt index c27220a5..7d778fc4 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Lists.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CollectionSupport_Lists.verified.txt @@ -1,5 +1,4 @@ -List`1: -[ -- Alex -- John +[ + Alex, + John, ] diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CultureSpecification.verified.txt index 13b2853d..0506c78e 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CultureSpecification.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CultureSpecification.verified.txt @@ -1,7 +1,10 @@ Person: - Id = Guid: +{ + Id = 00000000-0000-0000-0000-000000000000 Name = Alex Surname = Smith Height = 177.4 Age = 19 OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CyclicReferenceSupport.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CyclicReferenceSupport.verified.txt index 11148c21..13d7020f 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CyclicReferenceSupport.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.CyclicReferenceSupport.verified.txt @@ -1,25 +1,37 @@ Person: - Id = Guid: +{ + Id = 00000000-0000-0000-0000-000000000000 Name = Alex Surname = Smith Height = 177,4 Age = 19 OtherPerson = Person: - Id = Guid: + { + Id = 00000000-0000-0000-0000-000000000000 Name = Alex Surname = Smith Height = 177,4 Age = 19 OtherPerson = Person: - Id = Guid: + { + Id = 00000000-0000-0000-0000-000000000000 Name = Alex Surname = Smith Height = 177,4 Age = 19 OtherPerson = Person: - Id = null + { + Id = 00000000-0000-0000-0000-000000000000 Name = Alex Surname = Smith Height = 177,4 Age = 19 OtherPerson = null + Persons = null + } + Persons = [] + } + Persons = [] + } + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_ForDefaultSerialization.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_ForDefaultSerialization.verified.txt index 82b7833b..bb4f3210 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_ForDefaultSerialization.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_ForDefaultSerialization.verified.txt @@ -1,7 +1,10 @@ Person: - Id = Guid: +{ + Id = 00000000-0000-0000-0000-000000000000 Name = Alex Surname = Smith Height = 177,4 Age = 19 OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_WithConfigurationSupport.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_WithConfigurationSupport.verified.txt index ff2da16e..05b70cd8 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_WithConfigurationSupport.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.ExtensionMethod_WithConfigurationSupport.verified.txt @@ -1,6 +1,9 @@ Person: +{ Name = Alex Surname = Smith Height = 177,4 Age = 19 OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertyExclusion.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertyExclusion.verified.txt index ff2da16e..05b70cd8 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertyExclusion.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertyExclusion.verified.txt @@ -1,6 +1,9 @@ Person: +{ Name = Alex Surname = Smith Height = 177,4 Age = 19 OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertySerializationSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertySerializationSpecification.verified.txt index 3b06ffa1..39df8a01 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertySerializationSpecification.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.PropertySerializationSpecification.verified.txt @@ -1,7 +1,10 @@ Person: - Id = Guid: +{ + Id = 00000000-0000-0000-0000-000000000000 Name = Alex Surname = Smith Height = 177,4 Age = 19 OtherPerson = John + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringPropertyTrimming.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringPropertyTrimming.verified.txt new file mode 100644 index 00000000..5fbca773 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringPropertyTrimming.verified.txt @@ -0,0 +1,10 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Sm + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringTrimming.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringTrimming.verified.txt index 0ee6ea0f..4892b622 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringTrimming.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.StringTrimming.verified.txt @@ -1,7 +1,10 @@ Person: - Id = Guid: +{ + Id = 00000000-0000-0000-0000-000000000000 Name = Al Surname = Sm Height = 177,4 Age = 19 OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeExclusion.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeExclusion.verified.txt index d537721a..aec1d7db 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeExclusion.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeExclusion.verified.txt @@ -1,5 +1,8 @@ Person: - Id = Guid: +{ + Id = 00000000-0000-0000-0000-000000000000 Height = 177,4 Age = 19 OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeSerializationSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeSerializationSpecification.verified.txt index a934f6d3..8b6b1ba3 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeSerializationSpecification.verified.txt +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.TypeSerializationSpecification.verified.txt @@ -1,7 +1,10 @@ Person: +{ Id = 00000000-0000-0000-0000-000000000000 - Name = Alex - Surname = Smith + Name = ALEX + Surname = SMITH Height = 177,4 Age = 19 OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs index 15e625dd..1bd407b6 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Text; using System.Threading.Tasks; using NUnit.Framework; using VerifyNUnit; @@ -19,7 +20,7 @@ public void SetUp() _person = new Person { Id = new Guid(), Name = "Alex", Surname = "Smith", - Age = 19, Height = 177.4 + Age = 19, Height = 177.4, Persons = new List() }; _config = ObjectPrinter.For(); } @@ -40,8 +41,8 @@ public Task TypeExclusion() public Task TypeSerializationSpecification() { var str = _config - .ForType() - .Use(o => o.ToString()) + .ForType() + .Use(o => o.ToUpper()) .PrintToString(_person); return Verifier.Verify(str); @@ -84,6 +85,18 @@ public Task StringTrimming() return Verifier.Verify(str); } + + [Test] + [Description("Возможность обрезания конкретных строковых полей")] + public Task StringPropertyTrimming() + { + var str = _config + .ForProperty(x => x.Surname) + .UseMaxLength(2) + .PrintToString(_person); + + return Verifier.Verify(str); + } [Test] [Description("Возможность исключения из сериализации конкретного свойства/поля")] diff --git a/ObjectPrinting/Tests/Person.cs b/ObjectPrinting/Tests/Person.cs index 16f94ff6..0abeca92 100644 --- a/ObjectPrinting/Tests/Person.cs +++ b/ObjectPrinting/Tests/Person.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace ObjectPrinting.Tests { @@ -11,5 +12,6 @@ public class Person public int Age { get; set; } public Person? OtherPerson { get; set; } + public List Persons { get; set; } } } \ No newline at end of file From 3c8a091239f28deca24a9e1a0b27ea1d846b1aff Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Thu, 12 Dec 2024 22:29:39 +0500 Subject: [PATCH 20/21] improved collections serialization --- ObjectPrinting/PrintingConfig.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 8a3761d2..38412eb2 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -141,7 +141,7 @@ private string PrintToString(object obj, int nestingLevel, int recursionDepth) private string SerializeEnumerable(StringBuilder sb, IEnumerable enumerable, int nestingLevel, int recursionDepth) { var bracketIndentation = new string('\t', nestingLevel); - sb.AppendLine($"{bracketIndentation}["); + sb.AppendLine($"["); if (!enumerable.GetEnumerator().MoveNext()) { return "[]"; @@ -158,14 +158,14 @@ private string SerializeEnumerable(StringBuilder sb, IEnumerable enumerable, int valueString = PrintToString(element, nestingLevel + 1, recursionDepth + 1); sb.AppendLine($"{valueString},"); } - sb.AppendLine($"{bracketIndentation}]"); + sb.Append($"{bracketIndentation}]"); return sb.ToString(); } private string SerializeDictionary(StringBuilder sb, IDictionary dictionary, int nestingLevel, int recursionDepth) { var bracketIndentation = new string('\t', nestingLevel); - sb.AppendLine($"{bracketIndentation}["); + sb.AppendLine($"["); foreach (DictionaryEntry element in dictionary) { var key = element.Key; From aea11ea19d4d53d549f7ec1ecf595c1ff471b368 Mon Sep 17 00:00:00 2001 From: Nikita Shevyrin Date: Fri, 13 Dec 2024 00:29:05 +0500 Subject: [PATCH 21/21] addded more tests --- ObjectPrinting/ObjectPrinting.csproj | 1 + ...tionTests.ArrayOfNonValueType.verified.txt | 22 ++++ ...ationTests.DictionaryOfArrays.verified.txt | 42 +++++++ ...ests.DictionaryOfNonValueType.verified.txt | 41 +++++++ ...DictionaryWithNonValueTypeKey.verified.txt | 46 ++++++++ ...rializationTests.ListOfArrays.verified.txt | 46 ++++++++ ...ationTests.ListOfNonValueType.verified.txt | 22 ++++ .../CollectionsSerializationTests.cs | 104 ++++++++++++++++++ ...sts.BasicCultureSpecification.verified.txt | 10 ++ ...s.ComplexCultureSpecification.verified.txt | 1 + ...ests.DateCultureSpecification.verified.txt | 1 + ...s.DefaultCultureSpecification.verified.txt | 1 + ...faultDateCultureSpecification.verified.txt | 1 + ...ultDoubleCultureSpecification.verified.txt | 10 ++ ...aultFloatCultureSpecification.verified.txt | 1 + ...ts.DoubleCultureSpecification.verified.txt | 10 ++ ...sts.FloatCultureSpecification.verified.txt | 1 + .../CultureSpecificationTests.cs | 92 ++++++++++++++++ ...esTypesOfFieldsOfInnerObjects.verified.txt | 26 +++++ ...onTests.MultipleTypeExclusion.verified.txt | 7 ++ .../PropertyExclusionTests.cs | 60 ++++++++++ ...eldSerializationSpecification.verified.txt | 10 ++ ...rtySerializationSpecification.verified.txt | 10 ++ ...rtySerializationSpecification.verified.txt | 19 ++++ ...rtySerializationSpecification.verified.txt | 10 ++ .../PropertySerializationTests.cs | 69 ++++++++++++ ...PropertiesAreTrimmedCorrectly.verified.txt | 19 ++++ ...esBecomeEmptyWhenLengthIsZero.verified.txt | 19 ++++ ...iesDontChangeIfLengthIsBigger.verified.txt | 19 ++++ ...DontChangeWithDefaultSettings.verified.txt | 19 ++++ .../StringTrimmingTests.cs | 86 +++++++++++++++ ...ngIfSpecifiedTypeIsNotPresent.verified.txt | 10 ++ ...esTypesOfFieldsOfInnerObjects.verified.txt | 32 ++++++ ...onTests.MultipleTypeExclusion.verified.txt | 7 ++ .../TypeExclusionTests.cs | 71 ++++++++++++ ...erializationSpecificationTest.verified.txt | 10 ++ ...erializationSpecificationTest.verified.txt | 10 ++ ...pecificationForComplexObjects.verified.txt | 8 ++ .../TypeSerializationTests.cs | 73 ++++++++++++ ObjectPrinting/Tests/Person.cs | 5 + ObjectPrinting/Tests/PersonDatabase.cs | 9 ++ 41 files changed, 1060 insertions(+) create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ArrayOfNonValueType.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryOfArrays.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryOfNonValueType.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryWithNonValueTypeKey.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ListOfArrays.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ListOfNonValueType.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.cs create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.BasicCultureSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.ComplexCultureSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DateCultureSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultCultureSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultDateCultureSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultDoubleCultureSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultFloatCultureSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DoubleCultureSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.FloatCultureSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.cs create mode 100644 ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.ExcludesTypesOfFieldsOfInnerObjects.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.MultipleTypeExclusion.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.cs create mode 100644 ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.BasicFieldSerializationSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.BasicPropertySerializationSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.InnerPropertySerializationSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.MultiplePropertySerializationSpecification.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.cs create mode 100644 ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesAreTrimmedCorrectly.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesBecomeEmptyWhenLengthIsZero.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesDontChangeIfLengthIsBigger.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesDontChangeWithDefaultSettings.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.cs create mode 100644 ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.DoesNotExcludeAnythingIfSpecifiedTypeIsNotPresent.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.ExcludesTypesOfFieldsOfInnerObjects.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.MultipleTypeExclusion.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.cs create mode 100644 ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.BasicTypeSerializationSpecificationTest.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.MultipleTypeSerializationSpecificationTest.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.TypeSerializationSpecificationForComplexObjects.verified.txt create mode 100644 ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.cs create mode 100644 ObjectPrinting/Tests/PersonDatabase.cs diff --git a/ObjectPrinting/ObjectPrinting.csproj b/ObjectPrinting/ObjectPrinting.csproj index 6cd512b5..ca9fb986 100644 --- a/ObjectPrinting/ObjectPrinting.csproj +++ b/ObjectPrinting/ObjectPrinting.csproj @@ -5,6 +5,7 @@ + diff --git a/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ArrayOfNonValueType.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ArrayOfNonValueType.verified.txt new file mode 100644 index 00000000..ae62ca7b --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ArrayOfNonValueType.verified.txt @@ -0,0 +1,22 @@ +[ + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] + }, + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = John + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + }, +] \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryOfArrays.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryOfArrays.verified.txt new file mode 100644 index 00000000..e5dbf526 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryOfArrays.verified.txt @@ -0,0 +1,42 @@ +[ +{ + key: one + value: [ + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] + }, + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = John + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + }, + ] +}, +{ + key: two + value: [ + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Steve + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + }, + ] +}, +] diff --git a/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryOfNonValueType.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryOfNonValueType.verified.txt new file mode 100644 index 00000000..e94fd217 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryOfNonValueType.verified.txt @@ -0,0 +1,41 @@ +[ +{ + key: John + value: Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = John + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + } +}, +{ + key: Alex + value: Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] + } +}, +{ + key: Steve + value: Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Steve + Surname = Smith + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + } +}, +] diff --git a/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryWithNonValueTypeKey.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryWithNonValueTypeKey.verified.txt new file mode 100644 index 00000000..18dbc286 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.DictionaryWithNonValueTypeKey.verified.txt @@ -0,0 +1,46 @@ +[ +{ + key: Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] + } + value: Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = John + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + } +}, +{ + key: Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = John + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + } + value: Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Steve + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + } +}, +] diff --git a/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ListOfArrays.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ListOfArrays.verified.txt new file mode 100644 index 00000000..0751e43d --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ListOfArrays.verified.txt @@ -0,0 +1,46 @@ +[ + [ + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] + }, + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = John + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + }, + ], + [ + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Veronica + Surname = null + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + }, + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Steve + Surname = null + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + }, + ], +] \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ListOfNonValueType.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ListOfNonValueType.verified.txt new file mode 100644 index 00000000..ae62ca7b --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.ListOfNonValueType.verified.txt @@ -0,0 +1,22 @@ +[ + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] + }, + Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = John + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + }, +] \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.cs b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.cs new file mode 100644 index 00000000..24e24d02 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CollectionsSerializationTests/CollectionsSerializationTests.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using NUnit.Framework; +using VerifyNUnit; + +namespace ObjectPrinting.Tests.ObjectPrinter_CollectionsSerializationTests; + +[TestFixture] +public class CollectionsSerializationTests +{ + private Person _person; + private Person _otherPerson; + private Person[] _persons; + private List _personList; + + [SetUp] + public void SetUp() + { + _person = new Person + { + Id = new Guid(), Name = "Alex", Surname = "Smith", + Age = 19, Height = 177.4, Persons = new List() + }; + _otherPerson = new Person { Id = new Guid(), Name = "John", Surname = "Doe" }; + + _persons = new Person[] { _person, _otherPerson }; + + _personList = new List(); + _personList.Add(_person); + _personList.Add(_otherPerson); + } + + [Test] + public Task ArrayOfNonValueType() + { + var str = ObjectPrinter.For() + .PrintToString(_persons); + + return Verifier.Verify(str); + } + + [Test] + public Task ListOfNonValueType() + { + var str = ObjectPrinter.For>() + .PrintToString(_personList); + + return Verifier.Verify(str); + } + + [Test] + public Task ListOfArrays() + { + var list = new List(); + list.Add(new Person[] { _person, _otherPerson }); + list.Add(new Person[] { new Person{ Name = "Veronica" }, new Person { Name = "Steve" } }); + + var str = ObjectPrinter.For>() + .PrintToString(list); + + return Verifier.Verify(str); + } + + [Test] + public Task DictionaryOfNonValueType() + { + var dictionary = new Dictionary(); + dictionary.Add("John", _otherPerson); + dictionary.Add("Alex", _person); + dictionary.Add("Steve", new Person { Name = "Steve", Surname = "Smith" }); + + var str = ObjectPrinter.For>() + .PrintToString(dictionary); + + return Verifier.Verify(str); + } + + [Test] + public Task DictionaryOfArrays() + { + var dictionary = new Dictionary(); + dictionary.Add("one", _persons); + dictionary.Add("two", new Person[] { new Person { Name = "Steve", Surname = "Doe" } }); + + var str = ObjectPrinter.For>() + .PrintToString(dictionary); + + return Verifier.Verify(str); + } + + [Test] + public Task DictionaryWithNonValueTypeKey() + { + var dictionary = new Dictionary(); + dictionary.Add(_person, _otherPerson); + dictionary.Add(_otherPerson, new Person { Name = "Steve", Surname = "Doe" }); + + var str = ObjectPrinter.For>() + .PrintToString(dictionary); + + return Verifier.Verify(str); + } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.BasicCultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.BasicCultureSpecification.verified.txt new file mode 100644 index 00000000..0506c78e --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.BasicCultureSpecification.verified.txt @@ -0,0 +1,10 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177.4 + Age = 19 + OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.ComplexCultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.ComplexCultureSpecification.verified.txt new file mode 100644 index 00000000..58c28c57 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.ComplexCultureSpecification.verified.txt @@ -0,0 +1 @@ +09/01/2021 00:00:00 \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DateCultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DateCultureSpecification.verified.txt new file mode 100644 index 00000000..58c28c57 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DateCultureSpecification.verified.txt @@ -0,0 +1 @@ +09/01/2021 00:00:00 \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultCultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultCultureSpecification.verified.txt new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultCultureSpecification.verified.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultDateCultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultDateCultureSpecification.verified.txt new file mode 100644 index 00000000..195ace8b --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultDateCultureSpecification.verified.txt @@ -0,0 +1 @@ +01.09.2021 0:00:00 \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultDoubleCultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultDoubleCultureSpecification.verified.txt new file mode 100644 index 00000000..bb4f3210 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultDoubleCultureSpecification.verified.txt @@ -0,0 +1,10 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultFloatCultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultFloatCultureSpecification.verified.txt new file mode 100644 index 00000000..7b53f087 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DefaultFloatCultureSpecification.verified.txt @@ -0,0 +1 @@ +3,14 \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DoubleCultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DoubleCultureSpecification.verified.txt new file mode 100644 index 00000000..0506c78e --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.DoubleCultureSpecification.verified.txt @@ -0,0 +1,10 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177.4 + Age = 19 + OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.FloatCultureSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.FloatCultureSpecification.verified.txt new file mode 100644 index 00000000..cee8eed8 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.FloatCultureSpecification.verified.txt @@ -0,0 +1 @@ +3.14 \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.cs b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.cs new file mode 100644 index 00000000..7d3b24d0 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_CultureSpecificationTests/CultureSpecificationTests.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Threading.Tasks; +using NUnit.Framework; +using VerifyNUnit; + +namespace ObjectPrinting.Tests.ObjectPrinter_CultureSpecificationTests; + +[TestFixture] +public class CultureSpecificationTests +{ + private Person _person; + private PrintingConfig _personConfig; + private DateTime _dateTime; + private float _float; + + [SetUp] + public void SetUp() + { + _person = new Person + { + Id = new Guid(), Name = "Alex", Surname = "Smith", + Age = 19, Height = 177.4, Persons = new List() + }; + + _personConfig = ObjectPrinter.For(); + + _dateTime = DateTime.Parse("2021-09-01"); + _float = 3.14f; + } + + [Test] + public Task DoubleCultureSpecification() + { + var str = _personConfig + .ForType() + .UseCulture(CultureInfo.InvariantCulture) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task DefaultDoubleCultureSpecification() + { + var str = _personConfig + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task FloatCultureSpecification() + { + var str = ObjectPrinter.For() + .ForType() + .UseCulture(CultureInfo.InvariantCulture) + .PrintToString(_float); + + return Verifier.Verify(str); + } + + [Test] + public Task DefaultFloatCultureSpecification() + { + var str = ObjectPrinter.For() + .PrintToString(_float); + + return Verifier.Verify(str); + } + + [Test] + public Task DateCultureSpecification() + { + var str = ObjectPrinter.For() + .ForType() + .UseCulture(CultureInfo.InvariantCulture) + .PrintToString(_dateTime); + + return Verifier.Verify(str); + } + + [Test] + public Task DefaultDateCultureSpecification() + { + var str = ObjectPrinter.For() + .PrintToString(_dateTime); + + return Verifier.Verify(str); + } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.ExcludesTypesOfFieldsOfInnerObjects.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.ExcludesTypesOfFieldsOfInnerObjects.verified.txt new file mode 100644 index 00000000..c74cedf5 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.ExcludesTypesOfFieldsOfInnerObjects.verified.txt @@ -0,0 +1,26 @@ +PersonDatabase: +{ + People = [ + Person: + { + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + }, + Person: + { + Name = John + Surname = Doe + Height = 0 + Age = 0 + }, + ] + Owner = Person: + { + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.MultipleTypeExclusion.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.MultipleTypeExclusion.verified.txt new file mode 100644 index 00000000..735f1407 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.MultipleTypeExclusion.verified.txt @@ -0,0 +1,7 @@ +Person: +{ + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.cs b/ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.cs new file mode 100644 index 00000000..0cb7b055 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_PropertyExclusionTests/PropertyExclusionTests.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using NUnit.Framework; +using VerifyNUnit; + +namespace ObjectPrinting.Tests.ObjectPrinter_PropertyExclusionTests; + +[TestFixture] +public class PropertyExclusionTests +{ + private Person _person; + private Person _otherPerson; + private PersonDatabase _personDatabase; + private PrintingConfig _personConfig; + private PrintingConfig _personDatabaseConfig; + + [SetUp] + public void SetUp() + { + _person = new Person + { + Id = new Guid(), Name = "Alex", Surname = "Smith", + Age = 19, Height = 177.4, Persons = new List() + }; + _otherPerson = new Person { Id = new Guid(), Name = "John", Surname = "Doe" }; + + _personDatabase = new PersonDatabase(); + _personDatabase.People.Add(_person); + _personDatabase.People.Add(_otherPerson); + _personDatabase.Owner = _person; + + _personConfig = ObjectPrinter.For(); + _personDatabaseConfig = ObjectPrinter.For(); + } + + [Test] + public Task MultipleTypeExclusion() + { + var str = _personConfig + .ExceptProperty(x => x.Id) + .ExceptProperty(x => x.OtherPerson!) + .ExceptProperty(x => x.Persons) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task ExcludesTypesOfFieldsOfInnerObjects() + { + var str = _personDatabaseConfig + .ExceptProperty(x => x.Owner.Id) + .ExceptProperty(x => x.Owner.OtherPerson!) + .ExceptProperty(x => x.Owner.Persons) + .PrintToString(_personDatabase); + + return Verifier.Verify(str); + } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.BasicFieldSerializationSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.BasicFieldSerializationSpecification.verified.txt new file mode 100644 index 00000000..d3018130 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.BasicFieldSerializationSpecification.verified.txt @@ -0,0 +1,10 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = ALEX + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.BasicPropertySerializationSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.BasicPropertySerializationSpecification.verified.txt new file mode 100644 index 00000000..d3018130 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.BasicPropertySerializationSpecification.verified.txt @@ -0,0 +1,10 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = ALEX + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.InnerPropertySerializationSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.InnerPropertySerializationSpecification.verified.txt new file mode 100644 index 00000000..21a6328e --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.InnerPropertySerializationSpecification.verified.txt @@ -0,0 +1,19 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = ALEX + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = JOHN + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + } + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.MultiplePropertySerializationSpecification.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.MultiplePropertySerializationSpecification.verified.txt new file mode 100644 index 00000000..b1b18c63 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.MultiplePropertySerializationSpecification.verified.txt @@ -0,0 +1,10 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = ALEX + Surname = smith + Height = 177,4 + Age = 19 + OtherPerson = 00000000-0000-0000-0000-000000000000 + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.cs b/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.cs new file mode 100644 index 00000000..0a3b01bc --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_PropertySerializationTests/PropertySerializationTests.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using NUnit.Framework; +using VerifyNUnit; + +namespace ObjectPrinting.Tests.ObjectPrinter_PropertySerializationTests; + +[TestFixture] +public class PropertySerializationTests +{ + private Person _person; + private Person _otherPerson; + private PrintingConfig _personConfig; + + [SetUp] + public void SetUp() + { + _person = new Person + { + Id = new Guid(), Name = "Alex", Surname = "Smith", + Age = 19, Height = 177.4, Persons = new List() + }; + _otherPerson = new Person { Id = new Guid(), Name = "John", Surname = "Doe" }; + + _personConfig = ObjectPrinter.For(); + } + + [Test] + public Task BasicPropertySerializationSpecification() + { + var str = _personConfig + .ForProperty(x => x.Name) + .Use(o => o.ToUpper()) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task MultiplePropertySerializationSpecification() + { + _person.OtherPerson = _otherPerson; + + var str = _personConfig + .ForProperty(x => x.Name) + .Use(o => o.ToUpper()) + .ForProperty(x => x.Surname) + .Use(o => o.ToLower()) + .ForProperty(x => x.OtherPerson) + .Use(o => o.Id.ToString()) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task InnerPropertySerializationSpecification() + { + _person.OtherPerson = _otherPerson; + + var str = _personConfig + .ForProperty(x => x.Name) + .Use(o => o.ToUpper()) + .PrintToString(_person); + + return Verifier.Verify(str); + } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesAreTrimmedCorrectly.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesAreTrimmedCorrectly.verified.txt new file mode 100644 index 00000000..121a2cc7 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesAreTrimmedCorrectly.verified.txt @@ -0,0 +1,19 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = Al + Surname = Sm + Height = 177,4 + Age = 19 + OtherPerson = Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = Jo + Surname = Do + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + } + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesBecomeEmptyWhenLengthIsZero.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesBecomeEmptyWhenLengthIsZero.verified.txt new file mode 100644 index 00000000..ae530dd4 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesBecomeEmptyWhenLengthIsZero.verified.txt @@ -0,0 +1,19 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = + Surname = + Height = 177,4 + Age = 19 + OtherPerson = Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = + Surname = + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + } + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesDontChangeIfLengthIsBigger.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesDontChangeIfLengthIsBigger.verified.txt new file mode 100644 index 00000000..b900795c --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesDontChangeIfLengthIsBigger.verified.txt @@ -0,0 +1,19 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = John + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + } + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesDontChangeWithDefaultSettings.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesDontChangeWithDefaultSettings.verified.txt new file mode 100644 index 00000000..796024bb --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.StringPropertiesDontChangeWithDefaultSettings.verified.txt @@ -0,0 +1,19 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = Person: + { + Id = 00000000-0000-0000-0000-000000000000 + Name = John + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + } + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.cs b/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.cs new file mode 100644 index 00000000..b9bad982 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_StringTrimmingTests/StringTrimmingTests.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using FluentAssertions; +using NUnit.Framework; +using VerifyNUnit; + +namespace ObjectPrinting.Tests.ObjectPrinter_StringTrimmingTests; + +[TestFixture] +public class StringTrimmingTests +{ + private Person _person; + private Person _otherPerson; + private PrintingConfig _personConfig; + + [SetUp] + public void SetUp() + { + _person = new Person + { + Id = new Guid(), Name = "Alex", Surname = "Smith", + Age = 19, Height = 177.4, Persons = new List() + }; + _otherPerson = new Person { Id = new Guid(), Name = "John", Surname = "Doe" }; + _person.OtherPerson = _otherPerson; + + _personConfig = ObjectPrinter.For(); + } + + [Test] + public Task StringPropertiesAreTrimmedCorrectly() + { + var str = _personConfig + .ForType() + .UseMaxLength(2) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task StringPropertiesBecomeEmptyWhenLengthIsZero() + { + var str = _personConfig + .ForType() + .UseMaxLength(0) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task StringPropertiesDontChangeIfLengthIsBigger() + { + var str = _personConfig + .ForType() + .UseMaxLength(15) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task StringPropertiesDontChangeWithDefaultSettings() + { + _person.Name = new string('a', 100); + + var str = _personConfig + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public void UseMaxLengthThrowsWhenArgumentIsLessThanZero() + { + _person.Name = new string('a', 100); + + Action act = () => _personConfig + .ForType() + .UseMaxLength(-1); + + act.Should().Throw(); + } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.DoesNotExcludeAnythingIfSpecifiedTypeIsNotPresent.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.DoesNotExcludeAnythingIfSpecifiedTypeIsNotPresent.verified.txt new file mode 100644 index 00000000..bb4f3210 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.DoesNotExcludeAnythingIfSpecifiedTypeIsNotPresent.verified.txt @@ -0,0 +1,10 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.ExcludesTypesOfFieldsOfInnerObjects.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.ExcludesTypesOfFieldsOfInnerObjects.verified.txt new file mode 100644 index 00000000..7a15861a --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.ExcludesTypesOfFieldsOfInnerObjects.verified.txt @@ -0,0 +1,32 @@ +PersonDatabase: +{ + People = [ + Person: + { + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] + }, + Person: + { + Name = John + Surname = Doe + Height = 0 + Age = 0 + OtherPerson = null + Persons = null + }, + ] + Owner = Person: + { + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] + } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.MultipleTypeExclusion.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.MultipleTypeExclusion.verified.txt new file mode 100644 index 00000000..735f1407 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.MultipleTypeExclusion.verified.txt @@ -0,0 +1,7 @@ +Person: +{ + Name = Alex + Surname = Smith + Height = 177,4 + Age = 19 +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.cs b/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.cs new file mode 100644 index 00000000..17dc1421 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_TypeExclusionTests/TypeExclusionTests.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; +using VerifyNUnit; + +namespace ObjectPrinting.Tests.ObjectPrinter_TypeExclusionTests; + +[TestFixture] +public class TypeExclusionTests +{ + private Person _person; + private Person _otherPerson; + private PersonDatabase _personDatabase; + private PrintingConfig _personConfig; + private PrintingConfig _personDatabaseConfig; + + [SetUp] + public void SetUp() + { + _person = new Person + { + Id = new Guid(), Name = "Alex", Surname = "Smith", + Age = 19, Height = 177.4, Persons = new List() + }; + _otherPerson = new Person { Id = new Guid(), Name = "John", Surname = "Doe" }; + + _personDatabase = new PersonDatabase(); + _personDatabase.People.Add(_person); + _personDatabase.People.Add(_otherPerson); + _personDatabase.Owner = _person; + + _personConfig = ObjectPrinter.For(); + _personDatabaseConfig = ObjectPrinter.For(); + } + + [Test] + public Task MultipleTypeExclusion() + { + var str = _personConfig + .ExceptType() + .ExceptType() + .ExceptType>() + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task DoesNotExcludeAnythingIfSpecifiedTypeIsNotPresent() + { + var str = _personConfig + .ExceptType() + .ExceptType() + .ExceptType() + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task ExcludesTypesOfFieldsOfInnerObjects() + { + var str = _personDatabaseConfig + .ExceptType() + .PrintToString(_personDatabase); + + return Verifier.Verify(str); + } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.BasicTypeSerializationSpecificationTest.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.BasicTypeSerializationSpecificationTest.verified.txt new file mode 100644 index 00000000..8b6b1ba3 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.BasicTypeSerializationSpecificationTest.verified.txt @@ -0,0 +1,10 @@ +Person: +{ + Id = 00000000-0000-0000-0000-000000000000 + Name = ALEX + Surname = SMITH + Height = 177,4 + Age = 19 + OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.MultipleTypeSerializationSpecificationTest.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.MultipleTypeSerializationSpecificationTest.verified.txt new file mode 100644 index 00000000..cddb5c64 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.MultipleTypeSerializationSpecificationTest.verified.txt @@ -0,0 +1,10 @@ +Person: +{ + Id = 00000 + Name = ALEX + Surname = SMITH + Height = 177,4 + Age = -19 + OtherPerson = null + Persons = [] +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.TypeSerializationSpecificationForComplexObjects.verified.txt b/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.TypeSerializationSpecificationForComplexObjects.verified.txt new file mode 100644 index 00000000..be07a3c3 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.TypeSerializationSpecificationForComplexObjects.verified.txt @@ -0,0 +1,8 @@ +PersonDatabase: +{ + People = [ + Alex, + John, + ] + Owner = Alex +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.cs b/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.cs new file mode 100644 index 00000000..07a98a48 --- /dev/null +++ b/ObjectPrinting/Tests/ObjectPrinter_TypeSerializationTests/TypeSerializationTests.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using NUnit.Framework; +using VerifyNUnit; + +namespace ObjectPrinting.Tests.ObjectPrinter_TypeSerializationTests; + +[TestFixture] +public class TypeSerializationTests +{ + private Person _person; + private Person _otherPerson; + private PersonDatabase _personDatabase; + private PrintingConfig _personConfig; + private PrintingConfig _personDatabaseConfig; + + [SetUp] + public void SetUp() + { + _person = new Person + { + Id = new Guid(), Name = "Alex", Surname = "Smith", + Age = 19, Height = 177.4, Persons = new List() + }; + _otherPerson = new Person { Id = new Guid(), Name = "John", Surname = "Doe" }; + + _personDatabase = new PersonDatabase(); + _personDatabase.People.Add(_person); + _personDatabase.People.Add(_otherPerson); + _personDatabase.Owner = _person; + + _personConfig = ObjectPrinter.For(); + _personDatabaseConfig = ObjectPrinter.For(); + } + + [Test] + public Task BasicTypeSerializationSpecificationTest() + { + var str = _personConfig + .ForType() + .Use(o => o.ToUpper()) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task MultipleTypeSerializationSpecificationTest() + { + var str = _personConfig + .ForType() + .Use(o => o.ToUpper()) + .ForType() + .Use(o => o.ToString().Substring(0, 5)) + .ForType() + .Use(o => (-o).ToString()) + .PrintToString(_person); + + return Verifier.Verify(str); + } + + [Test] + public Task TypeSerializationSpecificationForComplexObjects() + { + var str = _personDatabaseConfig + .ForType() + .Use(o => o.Name) + .PrintToString(_personDatabase); + + return Verifier.Verify(str); + } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/Person.cs b/ObjectPrinting/Tests/Person.cs index 0abeca92..0f37de62 100644 --- a/ObjectPrinting/Tests/Person.cs +++ b/ObjectPrinting/Tests/Person.cs @@ -13,5 +13,10 @@ public class Person public Person? OtherPerson { get; set; } public List Persons { get; set; } + + public override int GetHashCode() + { + return HashCode.Combine(Id, Name, Surname, Height, Age); + } } } \ No newline at end of file diff --git a/ObjectPrinting/Tests/PersonDatabase.cs b/ObjectPrinting/Tests/PersonDatabase.cs new file mode 100644 index 00000000..06f586dd --- /dev/null +++ b/ObjectPrinting/Tests/PersonDatabase.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace ObjectPrinting.Tests; + +public class PersonDatabase +{ + public List People { get; set; } = new List(); + public Person Owner { get; set; } +} \ No newline at end of file