diff --git a/ObjectPrinting.Tests/ExpectedResponses/PrintToString_Array_SerializedArray.txt b/ObjectPrinting.Tests/ExpectedResponses/PrintToString_Array_SerializedArray.txt new file mode 100644 index 00000000..855b9585 --- /dev/null +++ b/ObjectPrinting.Tests/ExpectedResponses/PrintToString_Array_SerializedArray.txt @@ -0,0 +1,12 @@ +Int32[] + Length = 3 + LongLength = 3 + Rank = 1 + SyncRoot = ранее сериализованный объект (стр. 1) + IsReadOnly = False + IsFixedSize = True + IsSynchronized = False + collection items: + 1 + 2 + 3 diff --git a/ObjectPrinting.Tests/ExpectedResponses/PrintToString_CyclicLinks.txt b/ObjectPrinting.Tests/ExpectedResponses/PrintToString_CyclicLinks.txt new file mode 100644 index 00000000..bbeefa75 --- /dev/null +++ b/ObjectPrinting.Tests/ExpectedResponses/PrintToString_CyclicLinks.txt @@ -0,0 +1,15 @@ +Person + Id = 00000000-0000-0000-0000-000000000000 + Name = null + Height = 0 + Age = 0 + Parent = null + Child = Person + Id = 00000000-0000-0000-0000-000000000000 + Name = null + Height = 0 + Age = 0 + Parent = ранее сериализованный объект (стр. 1) + Child = null + Email = null + Email = null diff --git a/ObjectPrinting.Tests/ExpectedResponses/PrintToString_Dictionary_SerializedDictionary.txt b/ObjectPrinting.Tests/ExpectedResponses/PrintToString_Dictionary_SerializedDictionary.txt new file mode 100644 index 00000000..438f7b84 --- /dev/null +++ b/ObjectPrinting.Tests/ExpectedResponses/PrintToString_Dictionary_SerializedDictionary.txt @@ -0,0 +1,37 @@ +Dictionary, Int32> + Comparer = ObjectEqualityComparer> + Count = 3 + Keys = KeyCollection, Int32> + Count = 3 + collection items: + List + Capacity = 1 + Count = 1 + collection items: + 1 + List + Capacity = 1 + Count = 1 + collection items: + 2 + List + Capacity = 1 + Count = 1 + collection items: + 3 + Values = ValueCollection, Int32> + Count = 3 + collection items: + 1 + 2 + 3 + collection items: + KeyValuePair, Int32> + Key = ранее сериализованный объект (стр. 7) + Value = 1 + KeyValuePair, Int32> + Key = ранее сериализованный объект (стр. 12) + Value = 2 + KeyValuePair, Int32> + Key = ранее сериализованный объект (стр. 17) + Value = 3 diff --git a/ObjectPrinting.Tests/ObjectPrinting.Tests.csproj b/ObjectPrinting.Tests/ObjectPrinting.Tests.csproj new file mode 100644 index 00000000..66c96228 --- /dev/null +++ b/ObjectPrinting.Tests/ObjectPrinting.Tests.csproj @@ -0,0 +1,43 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + Always + + + Always + + + Always + + + + diff --git a/ObjectPrinting/Solved/Tests/Person.cs b/ObjectPrinting.Tests/Person.cs similarity index 57% rename from ObjectPrinting/Solved/Tests/Person.cs rename to ObjectPrinting.Tests/Person.cs index 858ebbf8..2e8148ea 100644 --- a/ObjectPrinting/Solved/Tests/Person.cs +++ b/ObjectPrinting.Tests/Person.cs @@ -1,6 +1,4 @@ -using System; - -namespace ObjectPrinting.Solved.Tests +namespace ObjectPrinting.Tests { public class Person { @@ -8,5 +6,8 @@ public class Person public string Name { get; set; } public double Height { get; set; } public int Age { get; set; } + public string Email; + public Person Parent { get; set; } + public Person Child { get; set; } } } \ No newline at end of file diff --git a/ObjectPrinting.Tests/PrintingConfigTests.cs b/ObjectPrinting.Tests/PrintingConfigTests.cs new file mode 100644 index 00000000..622e3fe9 --- /dev/null +++ b/ObjectPrinting.Tests/PrintingConfigTests.cs @@ -0,0 +1,270 @@ +using System.Globalization; +using FluentAssertions; + +namespace ObjectPrinting.Tests; + +public class PrintingConfigTests +{ + private readonly Person defaultPerson = new() + { + Name = "Alex", + Age = 19, + Email = "alex@gmail.com", + Height = 185.5 + }; + + [Test] + public void Excluding_MustExcludeFieldsAndTypePropertiesFromSerialization() + { + var expected = string.Join( + Environment.NewLine, + [ + "Person", + "\tId = 00000000-0000-0000-0000-000000000000", + "\tHeight = 185,5", + "\tAge = 19", + "\tParent = null", + "\tChild = null", + string.Empty + ]); + var printer = ObjectPrinter.For() + .Excluding(); + + var actual = printer.PrintToString(defaultPerson); + + actual.Should().Be(expected); + } + + [Test] + public void PrintToString_FullObjectSerialization() + { + var expected = string.Join( + Environment.NewLine, + [ + "Person", + "\tId = 00000000-0000-0000-0000-000000000000", + "\tName = Alex", + "\tHeight = 185,5", + "\tAge = 19", + "\tParent = null", + "\tChild = null", + "\tEmail = alex@gmail.com", + string.Empty + ]); + var printer = ObjectPrinter.For(); + + var actual = printer.PrintToString(defaultPerson); + + actual.Should().Be(expected); + } + + [Test] + public void Using_AlternativeWayToSerializeNumbers() + { + var expected = string.Join( + Environment.NewLine, + [ + "Person", + "\tId = 00000000-0000-0000-0000-000000000000", + "\tName = Alex", + "\tHeight = 185,5", + "\tAge = 10011", + "\tParent = null", + "\tChild = null", + "\tEmail = alex@gmail.com", + string.Empty + ]); + var printer = ObjectPrinter.For() + .Printing() + .Using(number => Convert.ToString(number, 2)); + + var actual = printer.PrintToString(defaultPerson); + + actual.Should().Be(expected); + } + + [Test] + public void Excluding_NamePropertyIsExcluded() + { + var expected = string.Join( + Environment.NewLine, + [ + "Person", + "\tId = 00000000-0000-0000-0000-000000000000", + "\tHeight = 185,5", + "\tAge = 19", + "\tParent = null", + "\tChild = null", + "\tEmail = alex@gmail.com", + string.Empty + ]); + var printer = ObjectPrinter.For() + .Excluding(p => p.Name); + + var actual = printer.PrintToString(defaultPerson); + + actual.Should().Be(expected); + } + + [Test] + public void Using_OtherLocalizationOfEmailField() + { + var expected = string.Join( + Environment.NewLine, + [ + "Person", + "\tId = 00000000-0000-0000-0000-000000000000", + "\tName = Alex", + "\tHeight = 185,5", + "\tAge = 19", + "\tParent = null", + "\tChild = null", + "\tEmail = ALEX@GMAIL.COM", + string.Empty + ]); + var printer = ObjectPrinter.For() + .Printing(p => p.Email) + .Using(email => email.ToUpper() + Environment.NewLine); + + var actual = printer.PrintToString(defaultPerson); + + actual.Should().Be(expected); + } + + [Test] + public void Using_NamePropertyIsTruncated() + { + var person = new Person + { + Name = "Хьюберт Блейн Вольфешлегельштейнхаузенбергердорф-старший", + Height = defaultPerson.Height, + Age = defaultPerson.Age, + Email = defaultPerson.Email + }; + var expected = string.Join( + Environment.NewLine, + [ + "Person", + "\tId = 00000000-0000-0000-0000-000000000000", + "\tName = Хьюберт Блейн Вольфе...", + "\tHeight = 185,5", + "\tAge = 19", + "\tParent = null", + "\tChild = null", + "\tEmail = alex@gmail.com", + string.Empty + ]); + var printer = ObjectPrinter.For() + .Printing(p => p.Name) + .TrimmedToLength(20); + + var actual = printer.PrintToString(person); + + actual.Should().Be(expected); + } + + [Test] + public void Using_ChangedTypeSerializationCulture() + { + var expected = string.Join( + Environment.NewLine, + [ + "Person", + "\tId = 00000000-0000-0000-0000-000000000000", + "\tName = Alex", + "\tHeight = 185.5", + "\tAge = 19", + "\tParent = null", + "\tChild = null", + "\tEmail = alex@gmail.com", + string.Empty + ]); + var printer = ObjectPrinter.For() + .Printing() + .Using(new CultureInfo("en-US")); + + var actual = printer.PrintToString(defaultPerson); + + actual.Should().Be(expected); + } + + [Test] + public void PrintToString_CyclicLinks_NoStackOverflowException() + { + var parent = new Person(); + var child = new Person(); + parent.Child = child; + child.Parent = parent; + var printer = ObjectPrinter.For(); + Action act = () => printer.PrintToString(parent); + + act.Should().NotThrow(); + } + + [Test] + public void PrintToString_CyclicLinks() + { + var expected = File.ReadAllText("ExpectedResponses/PrintToString_CyclicLinks.txt"); + var parent = new Person(); + var child = new Person(); + parent.Child = child; + child.Parent = parent; + var printer = ObjectPrinter.For(); + + var actual = printer.PrintToString(parent); + + actual.Should().Be(expected); + } + + [Test] + public void PrintToString_List_SerializedList() + { + var expected = string.Join( + Environment.NewLine, + [ + "List", + "\tCapacity = 4", + "\tCount = 3", + "\tcollection items:", + "\t\t1", + "\t\t2", + "\t\t3", + string.Empty + ]); + var list = new List { 1, 2, 3 }; + + CheckSerializationOfCollection(expected, list); + } + + [Test] + public void PrintToString_Array_SerializedArray() + { + var expected = File.ReadAllText("ExpectedResponses/PrintToString_Array_SerializedArray.txt"); + var array = new[] { 1, 2, 3 }; + + CheckSerializationOfCollection(expected, array); + } + + [Test] + public void PrintToString_Dictionary_SerializedDictionary() + { + var expected = File.ReadAllText("ExpectedResponses/PrintToString_Dictionary_SerializedDictionary.txt"); + var dict = new Dictionary, int> + { + { ["1"], 1 }, + { ["2"], 2 }, + { ["3"], 3 } + }; + + CheckSerializationOfCollection(expected, dict); + } + + private void CheckSerializationOfCollection(string expected, TCollection collection) + { + var printer = ObjectPrinter.For(); + + var actual = printer.PrintToString(collection); + + actual.Should().Be(expected); + } +} \ No newline at end of file diff --git a/ObjectPrinting/IPropertyPrintingConfig.cs b/ObjectPrinting/IPropertyPrintingConfig.cs new file mode 100644 index 00000000..cae8f1dd --- /dev/null +++ b/ObjectPrinting/IPropertyPrintingConfig.cs @@ -0,0 +1,6 @@ +namespace ObjectPrinting; + +public interface IPropertyPrintingConfig +{ + PrintingConfig ParentConfig { get; } +} \ No newline at end of file diff --git a/ObjectPrinting/Solved/ObjectExtensions.cs b/ObjectPrinting/ObjectExtensions.cs similarity index 85% rename from ObjectPrinting/Solved/ObjectExtensions.cs rename to ObjectPrinting/ObjectExtensions.cs index b0c94553..87976203 100644 --- a/ObjectPrinting/Solved/ObjectExtensions.cs +++ b/ObjectPrinting/ObjectExtensions.cs @@ -1,4 +1,4 @@ -namespace ObjectPrinting.Solved +namespace ObjectPrinting { public static class ObjectExtensions { diff --git a/ObjectPrinting/ObjectPrinting.csproj b/ObjectPrinting/ObjectPrinting.csproj index c5db392f..6a9fd13d 100644 --- a/ObjectPrinting/ObjectPrinting.csproj +++ b/ObjectPrinting/ObjectPrinting.csproj @@ -3,10 +3,4 @@ net8.0 enable - - - - - - diff --git a/ObjectPrinting/PrintingConfig.cs b/ObjectPrinting/PrintingConfig.cs index a9e08211..719f3fbd 100644 --- a/ObjectPrinting/PrintingConfig.cs +++ b/ObjectPrinting/PrintingConfig.cs @@ -1,41 +1,195 @@ -using System; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; +using System.Linq.Expressions; +using System.Reflection; using System.Text; +using System.Text.RegularExpressions; namespace ObjectPrinting { - public class PrintingConfig + public record PrintingConfig(SerializationSettings Settings) { + private ulong CurrentLineNumberInSerialization { get; set; } = 1; + private readonly Dictionary serialized = new(); + + private readonly ImmutableArray finalTypes = + [ + typeof(int), + typeof(double), + typeof(float), + typeof(long), + typeof(ulong), + typeof(short), + typeof(DateTime), + typeof(TimeSpan), + typeof(string), + typeof(bool), + typeof(char), + typeof(Guid) + ]; + + private bool IsFinalType(Type type) => finalTypes.Contains(type); + + public PrintingConfig() : this(new SerializationSettings()) + { + } + + public PropertyPrintingConfig Printing() + { + return new PropertyPrintingConfig(this, typeof(TPropType)); + } + + public PropertyPrintingConfig Printing(Expression> memberSelector) + { + var memberInfo = GetMemberInfo(memberSelector); + + return new PropertyPrintingConfig(this, memberInfo); + } + + private MemberInfo GetMemberInfo(Expression> memberSelector) + { + if (memberSelector.Body.NodeType != ExpressionType.MemberAccess) + throw new ArgumentException("Должен возвращать поле или свойство объекта", nameof(memberSelector)); + var memberExpr = (MemberExpression)memberSelector.Body; + if (memberExpr.Member.MemberType != MemberTypes.Field && memberExpr.Member.MemberType != MemberTypes.Property) + throw new ArgumentException("Должен возвращать поле или свойство объекта", nameof(memberSelector)); + + return memberExpr.Member; + } + + public PrintingConfig Excluding(Expression> memberSelector) + { + return new PrintingConfig( + Settings with + { + ExcludedPropertiesAndFields = Settings.ExcludedPropertiesAndFields.Add(GetMemberInfo(memberSelector)) + }); + } + + public PrintingConfig Excluding() + { + return new PrintingConfig(Settings with + { + ExcludedTypes = Settings.ExcludedTypes.Add(typeof(TPropType)) + }); + } + public string PrintToString(TOwner obj) { return PrintToString(obj, 0); } - private string PrintToString(object obj, int nestingLevel) + private string PrintToString(object? obj, int nestingLevel) { - //TODO apply configurations - if (obj == null) + if (obj is null) + { + CurrentLineNumberInSerialization++; return "null" + Environment.NewLine; - - var finalTypes = new[] + } + var type = obj.GetType(); + if (Settings.AlternativeTypeSerialization.TryGetValue(type, out var serializer) && + !Settings.ExcludedTypes.Contains(type)) { - typeof(int), typeof(double), typeof(float), typeof(string), - typeof(DateTime), typeof(TimeSpan) - }; - if (finalTypes.Contains(obj.GetType())) + CurrentLineNumberInSerialization++; + return serializer(obj) + Environment.NewLine; + } + if (IsFinalType(type)) + { + CurrentLineNumberInSerialization++; return obj + Environment.NewLine; + } + if (serialized.TryGetValue(obj, out var lineNumber)) + { + CurrentLineNumberInSerialization++; + return $"ранее сериализованный объект (стр. {lineNumber}){Environment.NewLine}"; + } + serialized[obj] = CurrentLineNumberInSerialization; + + return SerializeComplexObject(type, obj, nestingLevel); + } - var identation = new string('\t', nestingLevel + 1); + private string SerializeComplexObject(Type type, object obj, int nestingLevel) + { var sb = new StringBuilder(); - var type = obj.GetType(); - sb.AppendLine(type.Name); - foreach (var propertyInfo in type.GetProperties()) + sb.AppendLine(SerializeNameOfType(type)); + CurrentLineNumberInSerialization++; + sb.Append(SerializeInstance(obj, nestingLevel)); + if (obj is IEnumerable) sb.Append(SerializeСollection(obj, nestingLevel)); + return sb.ToString(); + } + + private string SerializeNameOfType(Type type) + { + if (!type.IsGenericType) return type.Name; + + var separator = ", "; + var name = new Regex("(`[0-9]+)$").Replace(type.Name, ""); + + return $"{name}<{string.Join(separator, type.GenericTypeArguments.Select(SerializeNameOfType))}>"; + } + + private StringBuilder SerializeInstance(object obj, int nestingLevel) + { + var result = new StringBuilder(); + var tabulation = new string('\t', nestingLevel + 1); + + foreach (var member in GetPropertiesAndFields(obj.GetType())) { - sb.Append(identation + propertyInfo.Name + " = " + - PrintToString(propertyInfo.GetValue(obj), - nestingLevel + 1)); + // если member индексатор, то пропускаем + if (member.Name == "Item") continue; + var memberValue = GetValue(obj, member); + if (!(memberValue is not null && Settings.ExcludedTypes.Contains(memberValue.GetType()) || + Settings.ExcludedPropertiesAndFields.Contains(member))) + { + if (Settings.AlternativeSerializationOfFieldsAndProperties.TryGetValue(member, out var serializer)) + result.Append(tabulation + member.Name + " = " + serializer(memberValue)); + else + { + result.Append(tabulation + member.Name + " = " + + PrintToString(memberValue, nestingLevel + 1)); + } + } } - return sb.ToString(); + + return result; + } + + private StringBuilder SerializeСollection(object obj, int nestingLevel) + { + var result = new StringBuilder(); + result.Append($"{new string('\t', nestingLevel + 1)}collection items:{Environment.NewLine}"); + CurrentLineNumberInSerialization++; + var tabulation = new string('\t', nestingLevel + 2); + + foreach (var elem in (IEnumerable)obj) + { + if (elem is not null && Settings.ExcludedTypes.Contains(elem.GetType())) + continue; + result.Append(tabulation + PrintToString(elem, nestingLevel + 3)); + } + + return result; + } + + private IEnumerable GetPropertiesAndFields(Type type) + { + foreach (var property in type.GetProperties()) + yield return property; + foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Instance)) + yield return field; + } + + private object? GetValue(object obj, MemberInfo member) + { + if (member is PropertyInfo propertyInfo) + return propertyInfo.GetValue(obj); + if (member is FieldInfo fieldInfo) + return fieldInfo.GetValue(obj); + + throw new ArgumentException("member должен быть полем или свойством"); } } } \ No newline at end of file diff --git a/ObjectPrinting/PropertyPrintingConfig.cs b/ObjectPrinting/PropertyPrintingConfig.cs new file mode 100644 index 00000000..f93c3d75 --- /dev/null +++ b/ObjectPrinting/PropertyPrintingConfig.cs @@ -0,0 +1,61 @@ +using System; +using System.Globalization; +using System.Reflection; + +namespace ObjectPrinting +{ + public class PropertyPrintingConfig : IPropertyPrintingConfig + { + private readonly Type? type; + private readonly MemberInfo? member; + private readonly PrintingConfig printingConfig; + + public PropertyPrintingConfig(PrintingConfig printingConfig, MemberInfo member) + { + this.printingConfig = printingConfig; + this.member = member; + } + + public PropertyPrintingConfig(PrintingConfig printingConfig, Type type) + { + this.type = type; + this.printingConfig = printingConfig; + } + + public PrintingConfig Using(Func print) + { + return AddAlternativeSerializationMethod(obj => print((TPropType)obj)); + } + + public PrintingConfig Using(CultureInfo culture) + { + return AddAlternativeSerializationMethod(obj => Convert.ToString(obj, culture)); + } + + private PrintingConfig AddAlternativeSerializationMethod(Func serializer) + { + if (member is not null) + { + return new PrintingConfig(printingConfig.Settings with + { + AlternativeSerializationOfFieldsAndProperties = printingConfig.Settings.AlternativeSerializationOfFieldsAndProperties.Add( + member, + serializer) + }); + } + if (type is not null) + { + return new PrintingConfig(printingConfig.Settings with + { + AlternativeTypeSerialization = printingConfig.Settings.AlternativeTypeSerialization.Add( + typeof(TPropType), + serializer) + }); + } + + throw new InvalidOperationException(); + } + + PrintingConfig IPropertyPrintingConfig.ParentConfig => printingConfig; + } +} \ No newline at end of file diff --git a/ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs b/ObjectPrinting/PropertyPrintingConfigExtensions.cs similarity index 75% rename from ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs rename to ObjectPrinting/PropertyPrintingConfigExtensions.cs index dd392239..7b276f8e 100644 --- a/ObjectPrinting/Solved/PropertyPrintingConfigExtensions.cs +++ b/ObjectPrinting/PropertyPrintingConfigExtensions.cs @@ -1,6 +1,6 @@ using System; -namespace ObjectPrinting.Solved +namespace ObjectPrinting { public static class PropertyPrintingConfigExtensions { @@ -11,8 +11,7 @@ public static string PrintToString(this T obj, Func, Printi public static PrintingConfig TrimmedToLength(this PropertyPrintingConfig propConfig, int maxLen) { - return ((IPropertyPrintingConfig)propConfig).ParentConfig; + return propConfig.Using(str => (maxLen > str.Length ? str : str[..maxLen]) + "..." + Environment.NewLine); } - } } \ No newline at end of file diff --git a/ObjectPrinting/SerializationSettings.cs b/ObjectPrinting/SerializationSettings.cs new file mode 100644 index 00000000..d8c1497c --- /dev/null +++ b/ObjectPrinting/SerializationSettings.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Immutable; +using System.Reflection; + +namespace ObjectPrinting; + +public record SerializationSettings(ImmutableHashSet ExcludedTypes, + ImmutableHashSet ExcludedPropertiesAndFields, + ImmutableDictionary> AlternativeTypeSerialization, + ImmutableDictionary> AlternativeSerializationOfFieldsAndProperties) +{ + public SerializationSettings() : this(ImmutableHashSet.Empty, + ImmutableHashSet.Empty, + ImmutableDictionary>.Empty, + ImmutableDictionary>.Empty) + { + + } +} \ No newline at end of file diff --git a/ObjectPrinting/Solved/ObjectPrinter.cs b/ObjectPrinting/Solved/ObjectPrinter.cs deleted file mode 100644 index 540ee769..00000000 --- a/ObjectPrinting/Solved/ObjectPrinter.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace ObjectPrinting.Solved -{ - public class ObjectPrinter - { - public static PrintingConfig For() - { - return new PrintingConfig(); - } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Solved/PrintingConfig.cs b/ObjectPrinting/Solved/PrintingConfig.cs deleted file mode 100644 index 0ec5aeb2..00000000 --- a/ObjectPrinting/Solved/PrintingConfig.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Linq; -using System.Linq.Expressions; -using System.Text; - -namespace ObjectPrinting.Solved -{ - public class PrintingConfig - { - public PropertyPrintingConfig Printing() - { - return new PropertyPrintingConfig(this); - } - - public PropertyPrintingConfig Printing(Expression> memberSelector) - { - return new PropertyPrintingConfig(this); - } - - public PrintingConfig Excluding(Expression> memberSelector) - { - return this; - } - - internal PrintingConfig Excluding() - { - return this; - } - - public string PrintToString(TOwner obj) - { - return PrintToString(obj, 0); - } - - private string PrintToString(object obj, int nestingLevel) - { - //TODO apply configurations - if (obj == null) - return "null" + Environment.NewLine; - - var finalTypes = new[] - { - typeof(int), typeof(double), typeof(float), typeof(string), - typeof(DateTime), typeof(TimeSpan) - }; - if (finalTypes.Contains(obj.GetType())) - return obj + Environment.NewLine; - - var identation = 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)); - } - return sb.ToString(); - } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Solved/PropertyPrintingConfig.cs b/ObjectPrinting/Solved/PropertyPrintingConfig.cs deleted file mode 100644 index a509697d..00000000 --- a/ObjectPrinting/Solved/PropertyPrintingConfig.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Globalization; - -namespace ObjectPrinting.Solved -{ - public class PropertyPrintingConfig : IPropertyPrintingConfig - { - private readonly PrintingConfig printingConfig; - - public PropertyPrintingConfig(PrintingConfig printingConfig) - { - this.printingConfig = printingConfig; - } - - public PrintingConfig Using(Func print) - { - return printingConfig; - } - - public PrintingConfig Using(CultureInfo culture) - { - return printingConfig; - } - - PrintingConfig IPropertyPrintingConfig.ParentConfig => printingConfig; - } - - public interface IPropertyPrintingConfig - { - PrintingConfig ParentConfig { get; } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs deleted file mode 100644 index ac52d5ee..00000000 --- a/ObjectPrinting/Solved/Tests/ObjectPrinterAcceptanceTests.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Globalization; -using NUnit.Framework; - -namespace ObjectPrinting.Solved.Tests -{ - [TestFixture] - public class ObjectPrinterAcceptanceTests - { - [Test] - public void Demo() - { - var person = new Person { Name = "Alex", Age = 19 }; - - var printer = ObjectPrinter.For() - //1. Исключить из сериализации свойства определенного типа - .Excluding() - //2. Указать альтернативный способ сериализации для определенного типа - .Printing().Using(i => i.ToString("X")) - //3. Для числовых типов указать культуру - .Printing().Using(CultureInfo.InvariantCulture) - //4. Настроить сериализацию конкретного свойства - //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) - .Printing(p => p.Name).TrimmedToLength(10) - //6. Исключить из сериализации конкретного свойства - .Excluding(p => p.Age); - - string s1 = printer.PrintToString(person); - - //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию - string s2 = person.PrintToString(); - - //8. ...с конфигурированием - string s3 = person.PrintToString(s => s.Excluding(p => p.Age)); - Console.WriteLine(s1); - Console.WriteLine(s2); - Console.WriteLine(s3); - } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs b/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs deleted file mode 100644 index 4c8b2445..00000000 --- a/ObjectPrinting/Tests/ObjectPrinterAcceptanceTests.cs +++ /dev/null @@ -1,27 +0,0 @@ -using NUnit.Framework; - -namespace ObjectPrinting.Tests -{ - [TestFixture] - public class ObjectPrinterAcceptanceTests - { - [Test] - public void Demo() - { - var person = new Person { Name = "Alex", Age = 19 }; - - var printer = ObjectPrinter.For(); - //1. Исключить из сериализации свойства определенного типа - //2. Указать альтернативный способ сериализации для определенного типа - //3. Для числовых типов указать культуру - //4. Настроить сериализацию конкретного свойства - //5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) - //6. Исключить из сериализации конкретного свойства - - string s1 = printer.PrintToString(person); - - //7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию - //8. ...с конфигурированием - } - } -} \ No newline at end of file diff --git a/ObjectPrinting/Tests/Person.cs b/ObjectPrinting/Tests/Person.cs deleted file mode 100644 index f9555955..00000000 --- a/ObjectPrinting/Tests/Person.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace ObjectPrinting.Tests -{ - public class Person - { - public Guid Id { get; set; } - public string Name { get; set; } - public double Height { get; set; } - public int Age { get; set; } - } -} \ No newline at end of file diff --git a/fluent-api.sln b/fluent-api.sln index 69c8db9e..67e51342 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}") = "ObjectPrinting.Tests", "ObjectPrinting.Tests\ObjectPrinting.Tests.csproj", "{12F09D11-3144-4A92-A9CA-9DA9A7FE8DCA}" +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 + {12F09D11-3144-4A92-A9CA-9DA9A7FE8DCA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {12F09D11-3144-4A92-A9CA-9DA9A7FE8DCA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {12F09D11-3144-4A92-A9CA-9DA9A7FE8DCA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {12F09D11-3144-4A92-A9CA-9DA9A7FE8DCA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/fluent-api.sln.DotSettings b/fluent-api.sln.DotSettings index 135b83ec..229f449d 100644 --- a/fluent-api.sln.DotSettings +++ b/fluent-api.sln.DotSettings @@ -1,6 +1,9 @@  <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /> + <Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></Policy> + <Policy><Descriptor Staticness="Any" AccessRightKinds="Any" Description="Types and namespaces"><ElementKinds><Kind Name="NAMESPACE" /><Kind Name="CLASS" /><Kind Name="STRUCT" /><Kind Name="ENUM" /><Kind Name="DELEGATE" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="AaBb_AaBb" /></Policy> + True True True Imported 10.10.2016