From 386fc862d05c011f516d308190401d679ef70ba4 Mon Sep 17 00:00:00 2001 From: Trofimov_Nikita Date: Tue, 26 Dec 2023 20:33:18 +0500 Subject: [PATCH 1/3] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrinting/ISerializer.cs | 13 ++ ObjectPrinting/ObjectPrinting.csproj | 2 +- ObjectPrinting/PrintingConfig.cs | 125 ++++++++++++++-- ObjectPrinting/PropertySetting.cs | 35 +++++ ObjectPrinting/StringSetting.cs | 18 +++ .../Tests/ObjectPrinterAcceptanceTests.cs | 64 +++++---- ObjectPrintingTests/ObjectPrintingTests.cs | 134 ++++++++++++++++++ .../ObjectPrintingTests.csproj | 24 ++++ ObjectPrintingTests/Person.cs | 11 ++ ObjectPrintingTests/Usings.cs | 3 + fluent-api.sln | 6 + 11 files changed, 392 insertions(+), 43 deletions(-) create mode 100644 ObjectPrinting/ISerializer.cs create mode 100644 ObjectPrinting/PropertySetting.cs create mode 100644 ObjectPrinting/StringSetting.cs create mode 100644 ObjectPrintingTests/ObjectPrintingTests.cs create mode 100644 ObjectPrintingTests/ObjectPrintingTests.csproj create mode 100644 ObjectPrintingTests/Person.cs create mode 100644 ObjectPrintingTests/Usings.cs diff --git a/ObjectPrinting/ISerializer.cs b/ObjectPrinting/ISerializer.cs new file mode 100644 index 00000000..52b38426 --- /dev/null +++ b/ObjectPrinting/ISerializer.cs @@ -0,0 +1,13 @@ +using System; +using System.Linq.Expressions; + +namespace ObjectPrinting +{ + public interface ISerializer + { + string PrintToString(TOwner obj); + PropertySetting SelectProperty

(Expression> properties); + ISerializer ChangeProperty(Func method); + ISerializer Exclude(); + } +} \ No newline at end of file diff --git a/ObjectPrinting/ObjectPrinting.csproj b/ObjectPrinting/ObjectPrinting.csproj index 1c5eaf1c..fb67b65b 100644 --- a/ObjectPrinting/ObjectPrinting.csproj +++ b/ObjectPrinting/ObjectPrinting.csproj @@ -1,7 +1,7 @@  - 8 + latest netcoreapp3.1 false diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index a9e08211..9dfde033 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -1,40 +1,135 @@ using System; +using System.Collections; +using System.Collections.Generic; +using System.Globalization; using System.Linq; +using System.Linq.Expressions; using System.Text; + namespace ObjectPrinting { - public class PrintingConfig + public class PrintingConfig : ISerializer { + private readonly Dictionary> propertiesOptions = new(); + + private readonly Dictionary> stringOptions = new(); + + private readonly Dictionary> optionsTypes = new(); + + private readonly HashSet exceptTypes = new(); + + private readonly Type[] types = + { + typeof(int), + typeof(double), + typeof(float), + typeof(bool), + typeof(DateTime), + typeof(TimeSpan) + }; + + public string PrintToString(TOwner obj) { return PrintToString(obj, 0); } - private string PrintToString(object obj, int nestingLevel) + public PropertySetting SelectProperty

(Expression> properties) + { + var memberExpression = (MemberExpression)properties.Body; + var propertySetting = new PropertySetting(this); + propertiesOptions[memberExpression.Member.Name] = propertySetting; + return propertySetting; + } + + public StringSetting SelectProperty(Expression> properties) + { + var memberExpression = (MemberExpression)properties.Body; + var stringSetting = new StringSetting(this); + stringOptions[memberExpression.Member.Name] = stringSetting; + propertiesOptions[memberExpression.Member.Name] = stringSetting; + return stringSetting; + } + + public ISerializer ChangeProperty(Func method) + { + optionsTypes.Add(typeof(T2), method); + return this; + } + + public ISerializer Exclude() + { + exceptTypes.Add(typeof(T2)); + return this; + } + + public PrintingConfig Exclude(Expression> properties) + { + var memberExpression = (MemberExpression)properties.Body; + var propertySetting = new PropertySetting(this, true); + propertiesOptions[memberExpression.Member.Name] = propertySetting; + return this; + } + + private string PrintToString(object obj, int id, string name = "") { - //TODO apply configurations if (obj == null) - return "null" + Environment.NewLine; + return "null"; + + if (types.Contains(obj.GetType()) || id > 1) + { + var cultureInfo = CultureInfo.CurrentCulture; + if (propertiesOptions.TryGetValue(name, out var option)) + cultureInfo = option.Culture; + return string.Format(cultureInfo, "{0}", obj); + } - var finalTypes = new[] + if (obj is string s) { - typeof(int), typeof(double), typeof(float), typeof(string), - typeof(DateTime), typeof(TimeSpan) - }; - if (finalTypes.Contains(obj.GetType())) - return obj + Environment.NewLine; + var maxLength = -1; + if (stringOptions.TryGetValue(name, out var option)) + maxLength = option.MaxLength; + if (maxLength < 0) + return obj as string; + return s[..maxLength]; + } - var identation = new string('\t', nestingLevel + 1); var sb = new StringBuilder(); var type = obj.GetType(); - sb.AppendLine(type.Name); + sb.Append(type.Name + ":"); foreach (var propertyInfo in type.GetProperties()) { - sb.Append(identation + propertyInfo.Name + " = " + - PrintToString(propertyInfo.GetValue(obj), - nestingLevel + 1)); + var variable = propertyInfo.GetValue(obj); + if (exceptTypes.Contains(propertyInfo.PropertyType) || + propertiesOptions.ContainsKey(propertyInfo.Name) && + propertiesOptions[propertyInfo.Name].IsExcept) + continue; + + if (propertiesOptions.ContainsKey(propertyInfo.Name) && + propertiesOptions[propertyInfo.Name].OutputMethod != null) + sb.Append(propertyInfo.Name + " = " + + propertiesOptions[propertyInfo.Name].OutputMethod.Invoke(variable)); + + else if (optionsTypes.TryGetValue(propertyInfo.PropertyType, out var optionsType)) + sb.Append(propertyInfo.Name + " = " + + optionsType.Invoke(variable)); + + else if (variable is IList list) + { + for (var i = 0; i < list.Count; i++) + sb.Append(i + " = " + + PrintToString(list[i], id + 1, propertyInfo.Name)); + } + + else + { + sb.Append(propertyInfo.Name + " = " + + PrintToString(propertyInfo.GetValue(obj), id + 1, + propertyInfo.Name)); + } } + return sb.ToString(); } } diff --git a/ObjectPrinting/PropertySetting.cs b/ObjectPrinting/PropertySetting.cs new file mode 100644 index 00000000..7f9daaaf --- /dev/null +++ b/ObjectPrinting/PropertySetting.cs @@ -0,0 +1,35 @@ +using System; +using System.Globalization; +using Microsoft.VisualBasic.FileIO; +using NUnit.Framework; + +namespace ObjectPrinting +{ + public class PropertySetting + { + protected internal bool IsExcept { get; } + protected internal Func OutputMethod { private set; get; } + protected PrintingConfig Config { get; private set; } + protected internal CultureInfo Culture { get; private set; } + + public PropertySetting(PrintingConfig config, bool isExcept = false) + { + IsExcept = isExcept; + OutputMethod = null; + Config = config; + Culture = CultureInfo.CurrentCulture; + } + + public PrintingConfig ChangeField(Func func) + { + OutputMethod = func; + return Config; + } + + public PrintingConfig ChangeCulture(CultureInfo culture) + { + Culture = culture; + return Config; + } + } +} \ No newline at end of file diff --git a/ObjectPrinting/StringSetting.cs b/ObjectPrinting/StringSetting.cs new file mode 100644 index 00000000..9e11d9f1 --- /dev/null +++ b/ObjectPrinting/StringSetting.cs @@ -0,0 +1,18 @@ +namespace ObjectPrinting +{ + public class StringSetting : PropertySetting + { + protected internal int MaxLength { protected set; get; } + + public StringSetting(PrintingConfig config, bool isExcept = false) : base(config, isExcept) + { + MaxLength = -1; + } + + public PrintingConfig SetMaxLength(int length) + { + MaxLength = length; + return Config; + } + } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs index 4c8b2445..13dd27f5 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs @@ -1,27 +1,37 @@ -using NUnit.Framework; - -namespace ObjectPrinting.Tests -{ - [TestFixture] - public class ObjectPrinterAcceptanceTests - { - [Test] - public void Demo() - { - var person = new Person { Name = "Alex", Age = 19 }; - - var printer = ObjectPrinter.For(); - //1. Исключить из сериализации свойства определенного типа - //2. Указать альтернативный способ сериализации для определенного типа - //3. Для числовых типов указать культуру - //4. Настроить сериализацию конкретного свойства - //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) - //6. Исключить из сериализации конкретного свойства - - string s1 = printer.PrintToString(person); - - //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию - //8. ...с конфигурированием - } - } -} \ No newline at end of file +// using System; +// using System.Diagnostics.CodeAnalysis; +// using System.Globalization; +// using NUnit.Framework; +// using ObjectPrinting.Solved; +// +// namespace ObjectPrinting.Tests +// { +// [TestFixture] +// public class ObjectPrinterAcceptanceTests +// { +// [Test] +// public void Demo() +// { +// var person = new Person { Name = "Alex", Age = 19 }; +// +// var printer = ObjectPrinter.For(); +// printer +// //1. Исключить из сериализации свойства определенного типа +// .Exluding() +// //2. Указать альтернативный способ сериализации для определенного типа +// //3. Для числовых типов указать культуру +// .Printing().By(CultureInfo.CurrentCulture) +// //4. Настроить сериализацию конкретного свойства +// .Printing().By(s => s.ToString()) +// //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) +// +// //6. Исключить из сериализации конкретного свойства +// .Exluding(p = p.Age); +// +// string s1 = printer.PrintToString(person); +// +// //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию +// //8. ...с конфигурированием +// } +// } +// } \ No newline at end of file diff --git a/ObjectPrintingTests/ObjectPrintingTests.cs b/ObjectPrintingTests/ObjectPrintingTests.cs new file mode 100644 index 00000000..fa682b69 --- /dev/null +++ b/ObjectPrintingTests/ObjectPrintingTests.cs @@ -0,0 +1,134 @@ +using System.Globalization; +using ObjectPrinting; + + +namespace ObjectPrintingTests; + +[TestFixture] +public class Tests +{ + private Person person; + + [SetUp] + public void SetUp() + { + person = new Person + { + Id = new Guid(), + Name = "Nikita", + Height = 178, + Age = 20, + Birthday = new DateTime(2003, 5, 6), + Sex = true, + }; + } + + [Test] + public void PrintIsUnchanged() + { + var printer = ObjectPrinter.For(); + var s = printer.PrintToString(person); + s.Should().Be("Person:"+ + "Id = Guid:"+ + "Name = Nikita"+ + "Height = 178"+ + "Age = 20"+ + "Birthday = 06.05.2003 0:00:00"+ + "Sex = True"); + } + + [Test] + public void ExclusionFieldPropertySerialization() + { + var printer = ObjectPrinter.For(); + var s = printer + .Exclude(x => x.Name) + .Exclude(x => x.Height) + .PrintToString(person); + s.Should().Be("Person:"+ + "Id = Guid:"+ + "Age = 20"+ + "Birthday = 06.05.2003 0:00:00"+ + "Sex = True"); + } + + [Test] + public void ExclusionFromPropertySerialization() + { + var printer = ObjectPrinter.For(); + var s = printer + .Exclude() + .Exclude() + .PrintToString(person); + s.Should().Be("Person:"+ + "Id = Guid:" + + "Height = 178"+ + "Age = 20"+ + "Sex = True"); + } + + [Test] + public void ChangingSerializationField()//WithChangerOutputNameProperty() + { + var printer = ObjectPrinter.For(); + var s = printer + .SelectProperty(x => x.Name) + .ChangeField(_ => "Anton") + .PrintToString(person); + s.Should().Be("Person:" + + "Id = Guid:"+ + "Name = Anton"+ + "Height = 178"+ + "Age = 20"+ + "Birthday = 06.05.2003 0:00:00"+ + "Sex = True"); + } + + [Test] + public void ChangingSerializationProperty() + { + var printer = ObjectPrinter.For(); + var s = printer + .ChangeProperty(_ => "Egor") + .PrintToString(person); + s.Should().Be("Person:"+ + "Id = Guid:"+ + "Name = Egor"+ + "Height = 178"+ + "Age = 20"+ + "Birthday = 06.05.2003 0:00:00"+ + "Sex = True"); + } + + [Test] + public void TrimmedString() + { + var printer = ObjectPrinter.For(); + var s = printer.SelectProperty(x => x.Name) + .SetMaxLength(5) + .PrintToString(person); + s.Should().Be("Person:"+ + "Id = Guid:"+ + "Name = Nikit"+ + "Height = 178"+ + "Age = 20"+ + "Birthday = 06.05.2003 0:00:00"+ + "Sex = True"); + } + [Test] + public void ChangingCulture() + { + var printer = ObjectPrinter.For(); + var s = printer + .SelectProperty(x => x.Birthday) + .ChangeCulture(new CultureInfo("en-US")) + .PrintToString(person); + s.Should().Be("Person:"+ + "Id = Guid:"+ + "Name = Nikita"+ + "Height = 178"+ + "Age = 20"+ + "Birthday = 5/6/2003 12:00:00 AM"+ + "Sex = True"); + } +} \ No newline at end of file diff --git a/ObjectPrintingTests/ObjectPrintingTests.csproj b/ObjectPrintingTests/ObjectPrintingTests.csproj new file mode 100644 index 00000000..1103f246 --- /dev/null +++ b/ObjectPrintingTests/ObjectPrintingTests.csproj @@ -0,0 +1,24 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + + diff --git a/ObjectPrintingTests/Person.cs b/ObjectPrintingTests/Person.cs new file mode 100644 index 00000000..af852a0f --- /dev/null +++ b/ObjectPrintingTests/Person.cs @@ -0,0 +1,11 @@ +namespace ObjectPrintingTests; + +public class Person +{ + public Guid Id { get; set; } + public string Name { get; set; } + public double Height { get; set; } + public int Age { get; set; } + public DateTime Birthday { get; set; } + public bool Sex { get; set; } +} \ No newline at end of file diff --git a/ObjectPrintingTests/Usings.cs b/ObjectPrintingTests/Usings.cs new file mode 100644 index 00000000..fcd0042a --- /dev/null +++ b/ObjectPrintingTests/Usings.cs @@ -0,0 +1,3 @@ +global using NUnit.Framework; +global using ObjectPrintingTests; +global using FluentAssertions; \ No newline at end of file diff --git a/fluent-api.sln b/fluent-api.sln index 69c8db9e..5b29bd7d 100644 --- a/fluent-api.sln +++ b/fluent-api.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FluentMapping.Tests", "Samp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spectacle", "Samples\Spectacle\Spectacle.csproj", "{EFA9335C-411B-4597-B0B6-5438D1AE04C3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObjectPrintingTests", "ObjectPrintingTests\ObjectPrintingTests.csproj", "{96B40C5C-1956-428A-9CD4-932B139D1B9B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -35,6 +37,10 @@ Global {EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.ActiveCfg = Release|Any CPU {EFA9335C-411B-4597-B0B6-5438D1AE04C3}.Release|Any CPU.Build.0 = Release|Any CPU + {96B40C5C-1956-428A-9CD4-932B139D1B9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {96B40C5C-1956-428A-9CD4-932B139D1B9B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {96B40C5C-1956-428A-9CD4-932B139D1B9B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {96B40C5C-1956-428A-9CD4-932B139D1B9B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From d49ca922313d8a737fae82f49b013df216d42935 Mon Sep 17 00:00:00 2001 From: Nikita_Trofimov Date: Wed, 27 Dec 2023 03:37:08 +0500 Subject: [PATCH 2/3] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B5?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=BC=D0=B5=D1=87=D0=B0=D0=BD=D0=B8=D1=8F=20(?= =?UTF-8?q?=D0=BF=D0=BE=D0=BC=D0=B5=D1=82=D0=B8=D0=BB=20=D0=BB=D0=B0=D0=B9?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC).=20=D0=94=D0=BE=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BB=20=D1=81=D0=B5=D1=80=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8E=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=BB=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D1=80=D1=8F=20=D0=B8=20=D0=BC=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B8=D0=B2=D0=B0,=20=D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BB=20=D1=82=D0=B5=D1=81=D1=82=D1=8B.=20=D0=A1=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B0=D0=BB=20=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=BB=D0=BD?= =?UTF-8?q?=D0=BE=D0=B5=20=D1=80=D0=B5=D1=88=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrinting/ISerializer.cs | 4 +-- ObjectPrinting/PrintingConfig.cs | 32 +++++++++++++++------- ObjectPrinting/PropertySetting.cs | 5 +--- ObjectPrinting/StringSetting.cs | 2 +- ObjectPrintingTests/ObjectPrintingTests.cs | 1 + ObjectPrintingTests/Usings.cs | 3 +- 6 files changed, 28 insertions(+), 19 deletions(-) diff --git a/ObjectPrinting/ISerializer.cs b/ObjectPrinting/ISerializer.cs index 52b38426..767fd109 100644 --- a/ObjectPrinting/ISerializer.cs +++ b/ObjectPrinting/ISerializer.cs @@ -7,7 +7,7 @@ public interface ISerializer { string PrintToString(TOwner obj); PropertySetting SelectProperty

(Expression> properties); - ISerializer ChangeProperty(Func method); - ISerializer Exclude(); + ISerializer ChangeProperty(Func method); + ISerializer Exclude(); } } \ No newline at end of file diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index 9dfde033..4b9e23b2 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -40,6 +40,7 @@ public PropertySetting SelectProperty

(Expression> pro var memberExpression = (MemberExpression)properties.Body; var propertySetting = new PropertySetting(this); propertiesOptions[memberExpression.Member.Name] = propertySetting; + return propertySetting; } @@ -49,6 +50,7 @@ public StringSetting SelectProperty(Expression> pro var stringSetting = new StringSetting(this); stringOptions[memberExpression.Member.Name] = stringSetting; propertiesOptions[memberExpression.Member.Name] = stringSetting; + return stringSetting; } @@ -69,6 +71,7 @@ public PrintingConfig Exclude(Expression> propertie var memberExpression = (MemberExpression)properties.Body; var propertySetting = new PropertySetting(this, true); propertiesOptions[memberExpression.Member.Name] = propertySetting; + return this; } @@ -76,12 +79,13 @@ private string PrintToString(object obj, int id, string name = "") { if (obj == null) return "null"; - + if (types.Contains(obj.GetType()) || id > 1) { var cultureInfo = CultureInfo.CurrentCulture; if (propertiesOptions.TryGetValue(name, out var option)) cultureInfo = option.Culture; + return string.Format(cultureInfo, "{0}", obj); } @@ -92,6 +96,7 @@ private string PrintToString(object obj, int id, string name = "") maxLength = option.MaxLength; if (maxLength < 0) return obj as string; + return s[..maxLength]; } @@ -105,28 +110,35 @@ private string PrintToString(object obj, int id, string name = "") propertiesOptions.ContainsKey(propertyInfo.Name) && propertiesOptions[propertyInfo.Name].IsExcept) continue; - if (propertiesOptions.ContainsKey(propertyInfo.Name) && propertiesOptions[propertyInfo.Name].OutputMethod != null) + { sb.Append(propertyInfo.Name + " = " + - propertiesOptions[propertyInfo.Name].OutputMethod.Invoke(variable)); - + propertiesOptions[propertyInfo.Name].OutputMethod.Invoke(variable)); + } else if (optionsTypes.TryGetValue(propertyInfo.PropertyType, out var optionsType)) + { sb.Append(propertyInfo.Name + " = " + - optionsType.Invoke(variable)); - + optionsType.Invoke(variable)); + } else if (variable is IList list) { for (var i = 0; i < list.Count; i++) - sb.Append(i + " = " + + sb.Append("\n" + i + " = " + PrintToString(list[i], id + 1, propertyInfo.Name)); } - + else if (variable is IDictionary dict) + { + foreach (var key in dict.Keys) + { + var value = dict[key]; + sb.Append($"\n{key} = {PrintToString(value, id + 1, name)}"); + } + } else { sb.Append(propertyInfo.Name + " = " + - PrintToString(propertyInfo.GetValue(obj), id + 1, - propertyInfo.Name)); + PrintToString(propertyInfo.GetValue(obj), id + 1, propertyInfo.Name)); } } diff --git a/ObjectPrinting/PropertySetting.cs b/ObjectPrinting/PropertySetting.cs index 7f9daaaf..e3d7c4aa 100644 --- a/ObjectPrinting/PropertySetting.cs +++ b/ObjectPrinting/PropertySetting.cs @@ -1,21 +1,18 @@ using System; using System.Globalization; -using Microsoft.VisualBasic.FileIO; -using NUnit.Framework; namespace ObjectPrinting { public class PropertySetting { protected internal bool IsExcept { get; } - protected internal Func OutputMethod { private set; get; } + protected internal Func OutputMethod { get; private set; } protected PrintingConfig Config { get; private set; } protected internal CultureInfo Culture { get; private set; } public PropertySetting(PrintingConfig config, bool isExcept = false) { IsExcept = isExcept; - OutputMethod = null; Config = config; Culture = CultureInfo.CurrentCulture; } diff --git a/ObjectPrinting/StringSetting.cs b/ObjectPrinting/StringSetting.cs index 9e11d9f1..632e0484 100644 --- a/ObjectPrinting/StringSetting.cs +++ b/ObjectPrinting/StringSetting.cs @@ -2,7 +2,7 @@ namespace ObjectPrinting { public class StringSetting : PropertySetting { - protected internal int MaxLength { protected set; get; } + protected internal int MaxLength { get; private set; } public StringSetting(PrintingConfig config, bool isExcept = false) : base(config, isExcept) { diff --git a/ObjectPrintingTests/ObjectPrintingTests.cs b/ObjectPrintingTests/ObjectPrintingTests.cs index fa682b69..7002accd 100644 --- a/ObjectPrintingTests/ObjectPrintingTests.cs +++ b/ObjectPrintingTests/ObjectPrintingTests.cs @@ -131,4 +131,5 @@ public void ChangingCulture() "Birthday = 5/6/2003 12:00:00 AM"+ "Sex = True"); } + } \ No newline at end of file diff --git a/ObjectPrintingTests/Usings.cs b/ObjectPrintingTests/Usings.cs index fcd0042a..e0315ac9 100644 --- a/ObjectPrintingTests/Usings.cs +++ b/ObjectPrintingTests/Usings.cs @@ -1,3 +1,2 @@ global using NUnit.Framework; -global using ObjectPrintingTests; -global using FluentAssertions; \ No newline at end of file +global using FluentAssertions; From 5690a6ab097b5e29488d34df440d71ec4f4de6fd Mon Sep 17 00:00:00 2001 From: Nikita_Trofimov Date: Wed, 27 Dec 2023 03:37:29 +0500 Subject: [PATCH 3/3] =?UTF-8?q?=D0=97=D0=B0=D0=B1=D1=8B=D0=BB=20=D0=B2=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=88=D0=BB=D0=BE=D0=BC=20=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D0=B5=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectPrintingTests/CollectionsTests.cs | 76 +++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 ObjectPrintingTests/CollectionsTests.cs diff --git a/ObjectPrintingTests/CollectionsTests.cs b/ObjectPrintingTests/CollectionsTests.cs new file mode 100644 index 00000000..98f86b21 --- /dev/null +++ b/ObjectPrintingTests/CollectionsTests.cs @@ -0,0 +1,76 @@ +using ObjectPrinting; + +namespace ObjectPrintingTests; + +public class CollectionsTests +{ + private class TestDictionary + { + public Dictionary DictionaryProperty { get; set; } + } + + [Test] + public void PrintToString_WithDictionary() + { + var obj = new TestDictionary + { + DictionaryProperty = new Dictionary + { + { "One", 1 }, + { "Two", 2 }, + { "Three", 3 } + } + }; + + var printer = ObjectPrinter.For(); + var result = printer.PrintToString(obj); + + result.Should().Be("TestDictionary:" + + "\nOne = 1" + + "\nTwo = 2" + + "\nThree = 3"); + } + + private class TestArray + { + public int[] ArrayProperty { get; set; } + } + + [Test] + public void PrintToString_WithArray() + { + var obj = new TestArray + { + ArrayProperty = new[] { 10, 20, 30 } + }; + var printingConfig = new PrintingConfig(); + var result = printingConfig.PrintToString(obj); + + result.Should().Be("TestArray:" + + "\n0 = 10" + + "\n1 = 20" + + "\n2 = 30"); + } + + private class TestList + { + public List ListProperty { get; set; } + } + + [Test] + public void PrintToString_WithList() + { + var obj = new TestList + { + ListProperty = new List { "One", "Two", "Three" } + }; + + var printer = ObjectPrinter.For(); + var result = printer.PrintToString(obj); + + result.Should().Be("TestList:" + + "\n0 = One" + + "\n1 = Two" + + "\n2 = Three"); + } +} \ No newline at end of file