diff --git a/ObjectPrinting/ObjectExtensions.cs b/ObjectPrinting/ObjectExtensions.cs new file mode 100644 index 000000000..ed99d9bcb --- /dev/null +++ b/ObjectPrinting/ObjectExtensions.cs @@ -0,0 +1,16 @@ +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> config) + { + return config(ObjectPrinter.For()).PrintToString(obj); + } +} \ No newline at end of file diff --git a/ObjectPrinting/ObjectPrinterUsingProperties.cs b/ObjectPrinting/ObjectPrinterUsingProperties.cs new file mode 100644 index 000000000..2afaeb8ee --- /dev/null +++ b/ObjectPrinting/ObjectPrinterUsingProperties.cs @@ -0,0 +1,13 @@ +namespace ObjectPrinting; + +public class ObjectPrinterUsingProperties +{ + private PrintingConfig printingConfig; + + public PrintingConfig PrintingConfig => printingConfig; + + public ObjectPrinterUsingProperties(PrintingConfig printingConfig) + { + this.printingConfig = printingConfig; + } +} \ No newline at end of file diff --git a/ObjectPrinting/ObjectPrinterUsingPropertiesExtensions.cs b/ObjectPrinting/ObjectPrinterUsingPropertiesExtensions.cs new file mode 100644 index 000000000..cddb7622d --- /dev/null +++ b/ObjectPrinting/ObjectPrinterUsingPropertiesExtensions.cs @@ -0,0 +1,31 @@ +using System; +using System.Globalization; + +namespace ObjectPrinting; + +public static class ObjectPrinterUsingPropertiesExtensions +{ + public static PrintingConfig SetCulture(this ObjectPrinterUsingProperties properties, CultureInfo cultureInfo) + { + properties.PrintingConfig.SerializationsByType[typeof(int)] = obj => ((int)obj).ToString(cultureInfo); + return properties.PrintingConfig; + } + + public static PrintingConfig SetCulture(this ObjectPrinterUsingProperties properties, CultureInfo cultureInfo) + { + properties.PrintingConfig.SerializationsByType[typeof(double)] = obj => ((double)obj).ToString(cultureInfo); + return properties.PrintingConfig; + } + + public static PrintingConfig SetCulture(this ObjectPrinterUsingProperties properties, CultureInfo cultureInfo) + { + properties.PrintingConfig.SerializationsByType[typeof(decimal)] = obj => ((decimal)obj).ToString(cultureInfo); + return properties.PrintingConfig; + } + + public static PrintingConfig Trim(this ObjectPrinterUsingProperties properties, int length) + { + properties.PrintingConfig.SerializationsByType[typeof(string)] = obj => ((string)obj).Substring(0, Math.Min(length, ((string)obj).Length)); + return properties.PrintingConfig; + } +} \ No newline at end of file diff --git a/ObjectPrinting/ObjectPrinterUsingProperty.cs b/ObjectPrinting/ObjectPrinterUsingProperty.cs new file mode 100644 index 000000000..7ef686cfe --- /dev/null +++ b/ObjectPrinting/ObjectPrinterUsingProperty.cs @@ -0,0 +1,25 @@ +using System; + +namespace ObjectPrinting; + +public class ObjectPrinterUsingProperty +{ + private PrintingConfig printingConfig; + private string propertyFullName; + + public PrintingConfig PrintingConfig => printingConfig; + + public string PropertyFullName => propertyFullName; + + public ObjectPrinterUsingProperty(PrintingConfig printingConfig, string propertyFullName) + { + this.printingConfig = printingConfig; + this.propertyFullName = propertyFullName; + } + + public PrintingConfig Serialize(Func func) + { + printingConfig.SerializationsByPropertyFullName[propertyFullName] = func; + return printingConfig; + } +} \ No newline at end of file diff --git a/ObjectPrinting/ObjectPrinterUsingPropertyExtensions.cs b/ObjectPrinting/ObjectPrinterUsingPropertyExtensions.cs new file mode 100644 index 000000000..ef2f4d365 --- /dev/null +++ b/ObjectPrinting/ObjectPrinterUsingPropertyExtensions.cs @@ -0,0 +1,12 @@ +using System; + +namespace ObjectPrinting.Tests; + +public static class ObjectPrinterUsingPropertyExtensions +{ + public static PrintingConfig Trim(this ObjectPrinterUsingProperty property, int length) + { + property.PrintingConfig.SerializationsByPropertyFullName[property.PropertyFullName] = obj => ((string)obj).Substring(0, Math.Min(length, ((string)obj).Length)); + return property.PrintingConfig; + } +} \ No newline at end of file diff --git a/ObjectPrinting/ObjectPrinting.csproj b/ObjectPrinting/ObjectPrinting.csproj index c5db392ff..e02f71346 100644 --- a/ObjectPrinting/ObjectPrinting.csproj +++ b/ObjectPrinting/ObjectPrinting.csproj @@ -5,8 +5,14 @@ + + + + + + diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index a9e082117..98f552f04 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -1,19 +1,28 @@ using System; +using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Text; namespace ObjectPrinting { public class PrintingConfig { + public readonly Dictionary> SerializationsByType = []; + public readonly Dictionary> SerializationsByPropertyFullName = []; + private readonly HashSet excludedProperties = []; + private readonly HashSet excludedTypes = []; + + private readonly Stack currentPropertyFullName = new(); + public string PrintToString(TOwner obj) { + currentPropertyFullName.Clear(); return PrintToString(obj, 0); } private string PrintToString(object obj, int nestingLevel) { - //TODO apply configurations if (obj == null) return "null" + Environment.NewLine; @@ -22,20 +31,122 @@ private string PrintToString(object obj, int nestingLevel) typeof(int), typeof(double), typeof(float), typeof(string), typeof(DateTime), typeof(TimeSpan) }; + + var propertyFullName = string.Join(".", currentPropertyFullName.Reverse()); + if (SerializationsByPropertyFullName.TryGetValue(propertyFullName, out var value)) + return value(obj) + Environment.NewLine; + + if (SerializationsByType.ContainsKey(obj.GetType())) + return SerializationsByType[obj.GetType()](obj) + Environment.NewLine; + if (finalTypes.Contains(obj.GetType())) return obj + Environment.NewLine; + + if (obj is System.Collections.IEnumerable and not string) + { + return ProcessIEnumerable(obj, nestingLevel); + } - 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 + " = " + + currentPropertyFullName.Push(propertyInfo.Name); + var newPropertyFullName = string.Join(".", currentPropertyFullName.Reverse()); + + if (excludedProperties.Contains(newPropertyFullName)) + { + currentPropertyFullName.Pop(); + continue; + } + + if (excludedTypes.Contains(propertyInfo.PropertyType)) + continue; + + sb.Append(indentation + propertyInfo.Name + " = " + PrintToString(propertyInfo.GetValue(obj), nestingLevel + 1)); + + currentPropertyFullName.Pop(); + } + return sb.ToString(); + } + + private string ProcessIEnumerable(object obj, int nestingLevel) + { + var indentation = new string('\t', nestingLevel + 1); + var sb = new StringBuilder(); + sb.AppendLine(obj.GetType().Name); + + var enumerable = (System.Collections.IEnumerable)obj; + var index = 0; + + foreach (var item in enumerable) + { + currentPropertyFullName.Push($"[{index}]"); + sb.Append(indentation + $"[{index}] = " + + PrintToString(item, nestingLevel + 1)); + currentPropertyFullName.Pop(); + index++; } + return sb.ToString(); } + + + public PrintingConfig Exclude() + { + excludedTypes.Add(typeof(T)); + return this; + } + + public PrintingConfig Exclude(Expression> func) + { + var fullPropertyName = GetPropertyFullName(func.Body); + excludedProperties.Add(fullPropertyName); + return this; + } + + public PrintingConfig Serialize(Func func) + { + SerializationsByType[typeof(T)] = obj => func((T)obj); + return this; + } + + public ObjectPrinterUsingProperties Using() + { + return new ObjectPrinterUsingProperties(this); + } + + public ObjectPrinterUsingProperty Using(Expression> func) + { + return new ObjectPrinterUsingProperty(this, GetPropertyFullName(func.Body)); + } + + private string GetPropertyFullName(Expression expression) + { + if (expression is MemberExpression memberExpression) + { + var parent = GetPropertyFullName(memberExpression.Expression!); + return string.IsNullOrEmpty(parent) ? memberExpression.Member.Name : parent + "." + memberExpression.Member.Name; + } + + if (expression is UnaryExpression unaryExpression) + { + if (unaryExpression.NodeType == ExpressionType.Convert) + { + return GetPropertyFullName(unaryExpression.Operand); + } + throw new InvalidOperationException("Unsupported expression type"); + } + + if (expression is ParameterExpression) + { + return string.Empty; + } + throw new InvalidOperationException("Unsupported expression type"); + } } } \ No newline at end of file diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenExcludingProperty.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenExcludingProperty.approved.txt new file mode 100644 index 000000000..1d44b8291 --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenExcludingProperty.approved.txt @@ -0,0 +1,48 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Parents = List`1 + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Children = Dictionary`2 + [0] = KeyValuePair`2 + Key = 1 + Value = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = KeyValuePair`2 + Key = 2 + Value = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Father = Person + Id = Guid + Name = Alex + Height = 0 + Mother = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Age = 55335,57 diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenExcludingType.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenExcludingType.approved.txt new file mode 100644 index 000000000..0ec7ca9d1 --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenExcludingType.approved.txt @@ -0,0 +1,39 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Name = Alex + Height = 0 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Parents = List`1 + [0] = Person + Id = Guid + Name = Alex + Height = 0 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Children = Dictionary`2 + [0] = KeyValuePair`2 + Value = Person + Id = Guid + Name = Alex + Height = 0 + [1] = KeyValuePair`2 + Value = Person + Id = Guid + Name = Kate + Height = 0 + Father = Person + Id = Guid + Name = Alex + Height = 0 + Mother = Person + Id = Guid + Name = Kate + Height = 0 + Age = 55335,57 diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenLotOfRules.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenLotOfRules.approved.txt new file mode 100644 index 000000000..e0b4d6167 --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenLotOfRules.approved.txt @@ -0,0 +1,48 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Name = Al + Height = 0 + Age = 19INT: + [1] = Person + Id = Guid + Name = Ka + Height = 0 + Age = 21INT: + Parents = List`1 + [0] = Person + Id = Guid + Name = Al + Height = 0 + Age = 19INT: + [1] = Person + Id = Guid + Name = Ka + Height = 0 + Age = 21INT: + Children = Dictionary`2 + [0] = KeyValuePair`2 + Key = 1INT: + Value = Person + Id = Guid + Name = Al + Height = 0 + Age = 19INT: + [1] = KeyValuePair`2 + Key = 2INT: + Value = Person + Id = Guid + Name = Ka + Height = 0 + Age = 21INT: + Father = Person + Id = Guid + Name = Al + Height = 0 + Mother = Person + Id = Guid + Name = Ka + Height = 0 + Age = 21INT: + Age = A55335,57: diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenNoAdditionalRules.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenNoAdditionalRules.approved.txt new file mode 100644 index 000000000..ce7128252 --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenNoAdditionalRules.approved.txt @@ -0,0 +1,49 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Parents = List`1 + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Children = Dictionary`2 + [0] = KeyValuePair`2 + Key = 1 + Value = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = KeyValuePair`2 + Key = 2 + Value = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Father = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + Mother = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Age = 55335,57 diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenSerializationsProperties.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenSerializationsProperties.approved.txt new file mode 100644 index 000000000..3b5e63fbe --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenSerializationsProperties.approved.txt @@ -0,0 +1,49 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = NUMBER 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = NUMBER 21 + Parents = List`1 + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = NUMBER 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = NUMBER 21 + Children = Dictionary`2 + [0] = KeyValuePair`2 + Key = NUMBER 1 + Value = Person + Id = Guid + Name = Alex + Height = 0 + Age = NUMBER 19 + [1] = KeyValuePair`2 + Key = NUMBER 2 + Value = Person + Id = Guid + Name = Kate + Height = 0 + Age = NUMBER 21 + Father = Person + Id = Guid + Name = Alex + Height = 0 + Age = NUMBER 19 + Mother = Person + Id = Guid + Name = Kate + Height = 0 + Age = NUMBER 21 + Age = 55335,57 diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenSerializationsProperty.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenSerializationsProperty.approved.txt new file mode 100644 index 000000000..a8fc430fe --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenSerializationsProperty.approved.txt @@ -0,0 +1,49 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Parents = List`1 + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Children = Dictionary`2 + [0] = KeyValuePair`2 + Key = 1 + Value = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = KeyValuePair`2 + Key = 2 + Value = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Father = Person + Id = Guid + Name = PROPERTY Alex + Height = 0 + Age = 19 + Mother = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Age = 55335,57 diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenTrimmingProperties.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenTrimmingProperties.approved.txt new file mode 100644 index 000000000..5ef09c0e5 --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenTrimmingProperties.approved.txt @@ -0,0 +1,49 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Name = Ale + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kat + Height = 0 + Age = 21 + Parents = List`1 + [0] = Person + Id = Guid + Name = Ale + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kat + Height = 0 + Age = 21 + Children = Dictionary`2 + [0] = KeyValuePair`2 + Key = 1 + Value = Person + Id = Guid + Name = Ale + Height = 0 + Age = 19 + [1] = KeyValuePair`2 + Key = 2 + Value = Person + Id = Guid + Name = Kat + Height = 0 + Age = 21 + Father = Person + Id = Guid + Name = Ale + Height = 0 + Age = 19 + Mother = Person + Id = Guid + Name = Kat + Height = 0 + Age = 21 + Age = 55335,57 diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenTrimmingProperty.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenTrimmingProperty.approved.txt new file mode 100644 index 000000000..c5ffcb72c --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenTrimmingProperty.approved.txt @@ -0,0 +1,49 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Parents = List`1 + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Children = Dictionary`2 + [0] = KeyValuePair`2 + Key = 1 + Value = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = KeyValuePair`2 + Key = 2 + Value = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Father = Person + Id = Guid + Name = Ale + Height = 0 + Age = 19 + Mother = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Age = 55335,57 diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenUsingExtension.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenUsingExtension.approved.txt new file mode 100644 index 000000000..ce7128252 --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenUsingExtension.approved.txt @@ -0,0 +1,49 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Parents = List`1 + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Children = Dictionary`2 + [0] = KeyValuePair`2 + Key = 1 + Value = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = KeyValuePair`2 + Key = 2 + Value = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Father = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + Mother = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Age = 55335,57 diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenUsingExtensionWithConfig.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenUsingExtensionWithConfig.approved.txt new file mode 100644 index 000000000..c23bcfc92 --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenUsingExtensionWithConfig.approved.txt @@ -0,0 +1,31 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Height = 0 + [1] = Person + Id = Guid + Height = 0 + Parents = List`1 + [0] = Person + Id = Guid + Height = 0 + [1] = Person + Id = Guid + Height = 0 + Children = Dictionary`2 + [0] = KeyValuePair`2 + Value = Person + Id = Guid + Height = 0 + [1] = KeyValuePair`2 + Value = Person + Id = Guid + Height = 0 + Father = Person + Id = Guid + Height = 0 + Mother = Person + Id = Guid + Height = 0 + Age = 55335,57 diff --git a/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenWhenSettingCulture.approved.txt b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenWhenSettingCulture.approved.txt new file mode 100644 index 000000000..ce7128252 --- /dev/null +++ b/ObjectPrinting/Tests/ApprovalTests/ObjectPrinterAcceptanceTests.PrintToString_ReturnsString_WhenWhenSettingCulture.approved.txt @@ -0,0 +1,49 @@ +Family + GrandParents = Person[] + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Parents = List`1 + [0] = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Children = Dictionary`2 + [0] = KeyValuePair`2 + Key = 1 + Value = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + [1] = KeyValuePair`2 + Key = 2 + Value = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Father = Person + Id = Guid + Name = Alex + Height = 0 + Age = 19 + Mother = Person + Id = Guid + Name = Kate + Height = 0 + Age = 21 + Age = 55335,57 diff --git a/ObjectPrinting/Tests/Family.cs b/ObjectPrinting/Tests/Family.cs new file mode 100644 index 000000000..6ffc097b4 --- /dev/null +++ b/ObjectPrinting/Tests/Family.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace ObjectPrinting.Tests; + +public class Family +{ + public Person[] GrandParents { get; set; } + public List Parents { get; set; } + public Dictionary Children { get; set; } + public Person Father { get; set; } + public Person Mother { get; set; } + public double Age { get; set; } +} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs index 4c8b2445c..205249a39 100644 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs +++ b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs @@ -1,27 +1,136 @@ -using NUnit.Framework; +using System.Collections.Generic; +using System.Globalization; +using ApprovalTests; +using ApprovalTests.Namers; +using ApprovalTests.Reporters; +using NUnit.Framework; namespace ObjectPrinting.Tests { [TestFixture] + [UseReporter(typeof(DiffReporter))] + [UseApprovalSubdirectory("ApprovalTests")] public class ObjectPrinterAcceptanceTests { + private Family family; + + [SetUp] + public void SetUp() + { + family = new Family + { + Father = new Person { Name = "Alex", Age = 19 }, + Mother = new Person { Name = "Kate", Age = 21 }, + Age = 55335.57d, + GrandParents = [new Person { Name = "Alex", Age = 19 }, new Person { Name = "Kate", Age = 21 }], + Parents = [new Person { Name = "Alex", Age = 19 }, new Person { Name = "Kate", Age = 21 }], + Children = new Dictionary + { + [1] = new() { Name = "Alex", Age = 19 }, + [2] = new() { Name = "Kate", Age = 21 } + } + }; + } + [Test] - public void Demo() + public void PrintToString_ReturnsString_WhenLotOfRules() { - 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. ...с конфигурированием + var printer = ObjectPrinter.For() + .Serialize(x => $"{x}INT:") + .Using(x => x.Age).Serialize(x => $"A{x}:") + .Using().Trim(2) + .Exclude(x => x.Father.Age); + var result = printer.PrintToString(family); + + Approvals.Verify(result); + } + + [Test] + public void PrintToString_ReturnsString_WhenNoAdditionalRules() + { + var printer = ObjectPrinter.For(); + var result = printer.PrintToString(family); + + Approvals.Verify(result); + } + + [Test] + public void PrintToString_ReturnsString_WhenExcludingType() + { + var printer = ObjectPrinter.For().Exclude(); + var result = printer.PrintToString(family); + + Approvals.Verify(result); + } + + [Test] + public void PrintToString_ReturnsString_WhenSerializationsProperties() + { + var printer = ObjectPrinter.For().Serialize(x => $"NUMBER {x}"); + var result = printer.PrintToString(family); + + Approvals.Verify(result); + } + + [Test] + public void PrintToString_ReturnsString_WhenExcludingProperty() + { + var printer = ObjectPrinter.For().Exclude(x => x.Father.Age); + var result = printer.PrintToString(family); + + Approvals.Verify(result); + } + + [Test] + public void PrintToString_ReturnsString_WhenWhenSettingCulture() + { + var printer = ObjectPrinter.For().Using().SetCulture(CultureInfo.CurrentCulture); + var result = printer.PrintToString(family); + + Approvals.Verify(result); + } + + [Test] + public void PrintToString_ReturnsString_WhenTrimmingProperties() + { + var printer = ObjectPrinter.For().Using().Trim(3); + var result = printer.PrintToString(family); + + Approvals.Verify(result); + } + + [Test] + public void PrintToString_ReturnsString_WhenSerializationsProperty() + { + var printer = ObjectPrinter.For().Using(x => x.Father.Name).Serialize(x => $"PROPERTY {x}"); + var result = printer.PrintToString(family); + + Approvals.Verify(result); + } + + [Test] + public void PrintToString_ReturnsString_WhenUsingExtension() + { + var result = family.PrintToString(); + + Approvals.Verify(result); + } + + [Test] + public void PrintToString_ReturnsString_WhenUsingExtensionWithConfig() + { + var result = family.PrintToString(x => x.Exclude().Exclude()); + + Approvals.Verify(result); + } + + [Test] + public void PrintToString_ReturnsString_WhenTrimmingProperty() + { + var printer = ObjectPrinter.For().Using(x => x.Father.Name).Trim(3); + var result = printer.PrintToString(family); + + Approvals.Verify(result); } } } \ No newline at end of file diff --git a/ObjectPrinting/Tests/Person.cs b/ObjectPrinting/Tests/Person.cs index f95559554..d246884a4 100644 --- a/ObjectPrinting/Tests/Person.cs +++ b/ObjectPrinting/Tests/Person.cs @@ -1,12 +1,11 @@ using System; -namespace ObjectPrinting.Tests +namespace ObjectPrinting.Tests; + +public class Person { - public class Person - { - public Guid Id { get; set; } - public string Name { get; set; } - public double Height { get; set; } - public int Age { get; set; } - } -} \ No newline at end of file + public Guid Id { get; set; } + public string Name { get; set; } + public double Height { get; set; } + public int Age { get; set; } +}