Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Холстинин Егор #188

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
22 changes: 8 additions & 14 deletions ObjectPrinting/PrintingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ namespace ObjectPrinting
public class PrintingConfig<TOwner>
{
private readonly HashSet<Type> excludedTypes = new HashSet<Type>();
private readonly HashSet<PropertyInfo> excludedProperites = new HashSet<PropertyInfo>();

private readonly Type[] finalTypes =
{
typeof(int), typeof(double), typeof(float), typeof(string),
typeof(DateTime), typeof(TimeSpan)
};
private readonly HashSet<PropertyInfo> excludedProperties = new HashSet<PropertyInfo>();

internal readonly Dictionary<Type, Func<object, string>> typeSerializers =
new Dictionary<Type, Func<object, string>>();
Expand All @@ -40,7 +34,7 @@ public PropertyPrintingConfig<TOwner, TPropType> Printing<TPropType>(Expression<
public PrintingConfig<TOwner> Excluding<TPropType>(Expression<Func<TOwner, TPropType>> memberSelector)
{
var propInfo = ((MemberExpression)memberSelector.Body).Member as PropertyInfo;
excludedProperites.Add(propInfo);
excludedProperties.Add(propInfo);
return this;
}

Expand All @@ -58,8 +52,8 @@ public string PrintToString(TOwner obj)
private string SerializeIEnumerable(IEnumerable enumerable, ImmutableHashSet<object> nestedObjects)
{
var sb = new StringBuilder();
if (enumerable is IDictionary)
return SerializeIDictionary((IDictionary)enumerable, nestedObjects);
if (enumerable is IDictionary dictionary)
return SerializeIDictionary(dictionary, nestedObjects);
sb.Append("[ ");
foreach (var element in enumerable)
{
Expand Down Expand Up @@ -96,7 +90,7 @@ private string PrintToString(object obj, ImmutableHashSet<object> nestedObjects)
return "null" + Environment.NewLine;

var objType = obj.GetType();
if (finalTypes.Contains(objType))
if (obj.GetType().IsValueType || obj is string)
{
if (typeSerializers.TryGetValue(objType, out var serializer))
return serializer(obj) + Environment.NewLine;
Yrwlcm marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -113,7 +107,7 @@ private string PrintToString(object obj, ImmutableHashSet<object> nestedObjects)
foreach (var propertyInfo in objType.GetProperties())
{
var propertyValue = propertyInfo.GetValue(obj);
if (Excluded(propertyInfo) || nestedObjects.Contains(propertyValue))
if (ExcludedFromSerialization(propertyInfo) || nestedObjects.Contains(propertyValue))
continue;

if (propertySerializers.TryGetValue(propertyInfo, out var serializer))
Expand All @@ -128,9 +122,9 @@ private string PrintToString(object obj, ImmutableHashSet<object> nestedObjects)
return sb.ToString();
}

private bool Excluded(PropertyInfo propertyInfo)
private bool ExcludedFromSerialization(PropertyInfo propertyInfo)
{
return excludedTypes.Contains(propertyInfo.PropertyType) || excludedProperites.Contains(propertyInfo);
return excludedTypes.Contains(propertyInfo.PropertyType) || excludedProperties.Contains(propertyInfo);
}
}
}
8 changes: 8 additions & 0 deletions ObjectPrinting/TypePrintingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ public PrintingConfig<TOwner> Using(Func<TPropType, string> print)
printingConfig.typeSerializers.Add(typeof(TPropType), castedFunc);
return printingConfig;
}

public PrintingConfig<TOwner> Using(CultureInfo culture)
{
var culturedFunc = new Func<TPropType, string>(d => ((IConvertible)d).ToString(culture));
var castedFunc = new Func<object, string>(obj => culturedFunc((TPropType)obj));
printingConfig.typeSerializers.Add(typeof(TPropType), castedFunc);
return printingConfig;
}
Yrwlcm marked this conversation as resolved.
Show resolved Hide resolved
}
}
44 changes: 0 additions & 44 deletions ObjectPrinting/TypePrintingConfigExtensions.cs

This file was deleted.

38 changes: 20 additions & 18 deletions ObjectPrinting_Should/ObjectPrinterAcceptanceTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Globalization;
using System.Globalization;
using FluentAssertions;
using NUnit.Framework;
using ObjectPrinting;

namespace ObjectPrinting_Should
Expand Down Expand Up @@ -43,7 +41,7 @@ public void PrintToString_UsesCustomSerializator_WhenGivenToType()
{
var printer = ObjectPrinter.For<Person>().Printing<int>().Using(i => i.ToString("X"));

var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid",
var expectedString = string.Join(Environment.NewLine, "Person", "\tId = 00000000-0000-0000-0000-000000000000",
"\tSibling = null", "\tName = Alex", "\tHeight = 179,5", "\tAge = 13", "\tFavouriteNumbers = null", "");
var outputString = printer.PrintToString(person);
outputString.Should().Be(expectedString);
Expand All @@ -54,7 +52,7 @@ public void PrintToString_UsesCustomSerialization_WhenGivenToProperty()
{
var printer = ObjectPrinter.For<Person>().Printing(p => p.Age).Using(i => i.ToString("X"));

var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid",
var expectedString = string.Join(Environment.NewLine, "Person", "\tId = 00000000-0000-0000-0000-000000000000",
"\tSibling = null", "\tName = Alex", "\tHeight = 179,5", "\tAge = 13", "\tFavouriteNumbers = null", "");
var outputString = printer.PrintToString(person);
outputString.Should().Be(expectedString);
Expand All @@ -65,7 +63,7 @@ public void PrintToString_TrimsStringProperties_WhenTrimmingIsSet()
{
var printer = ObjectPrinter.For<Person>().Printing(p => p.Name).TrimmedToLength(1);

var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid",
var expectedString = string.Join(Environment.NewLine, "Person", "\tId = 00000000-0000-0000-0000-000000000000",
"\tSibling = null", "\tName = A", "\tHeight = 179,5", "\tAge = 19", "\tFavouriteNumbers = null", "");
var outputString = printer.PrintToString(person);
outputString.Should().Be(expectedString);
Expand All @@ -76,7 +74,7 @@ public void PrintToString_UsesCustomCulture_WhenGivenToNumericType()
{
var printer = ObjectPrinter.For<Person>().Printing<double>().Using(CultureInfo.InvariantCulture);

var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid",
var expectedString = string.Join(Environment.NewLine, "Person", "\tId = 00000000-0000-0000-0000-000000000000",
"\tSibling = null", "\tName = Alex", "\tHeight = 179.5", "\tAge = 19", "\tFavouriteNumbers = null", "");
var outputString = printer.PrintToString(person);
outputString.Should().Be(expectedString);
Expand All @@ -86,7 +84,7 @@ public void PrintToString_UsesCustomCulture_WhenGivenToNumericType()
public void PrintToString_SerializesClass_WhenCalledFromItInstance()
{
var outputString = person.PrintToString();
var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid",
var expectedString = string.Join(Environment.NewLine, "Person", "\tId = 00000000-0000-0000-0000-000000000000",
"\tSibling = null", "\tName = Alex", "\tHeight = 179,5", "\tAge = 19", "\tFavouriteNumbers = null", "");
outputString.Should().Be(expectedString);
}
Expand All @@ -95,7 +93,7 @@ public void PrintToString_SerializesClass_WhenCalledFromItInstance()
public void PrintToString_SerializesClass_WhenCalledFromItInstanceWithConfig()
{
var outputString = person.PrintToString(s => s.Excluding(p => p.Age));
var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid",
var expectedString = string.Join(Environment.NewLine, "Person", "\tId = 00000000-0000-0000-0000-000000000000",
"\tSibling = null", "\tName = Alex", "\tHeight = 179,5", "\tFavouriteNumbers = null", "");
outputString.Should().Be(expectedString);
}
Expand All @@ -109,10 +107,11 @@ public void PrintToString_SerializesCyclicReferences()
//var printer = ObjectPrinter.For<Person>().Excluding<double>().Excluding<Guid>();

var outputString = firstPerson.PrintToString();
var expectedString = string.Join(Environment.NewLine, "Person", "\tId = Guid",
"\tSibling = Person", "\t\tId = Guid", "\t\tName = John", "\t\tHeight = 0", "\t\tAge = 20",
"\t\tFavouriteNumbers = null", "\tName = Ben", "\tHeight = 0", "\tAge = 20",
"\tFavouriteNumbers = null", "");
var expectedString = string.Join(Environment.NewLine, "Person",
"\tId = 00000000-0000-0000-0000-000000000000",
"\tSibling = Person", "\t\tId = 00000000-0000-0000-0000-000000000000",
"\t\tName = John", "\t\tHeight = 0", "\t\tAge = 20", "\t\tFavouriteNumbers = null",
"\tName = Ben", "\tHeight = 0", "\tAge = 20", "\tFavouriteNumbers = null", "");
outputString.Should().Be(expectedString);
}

Expand All @@ -122,7 +121,8 @@ public void PrintToString_SerializesArray_WhenInClass()
person.FavouriteNumbers = new[] { 1.1, 2.2, 3.3, 4.4, 5.5 };
var outputString = person.PrintToString();
var expectedString = string.Join(Environment.NewLine, "Person",
"\tId = Guid", "\tSibling = null", "\tName = Alex", "\tHeight = 179,5", "\tAge = 19",
"\tId = 00000000-0000-0000-0000-000000000000",
"\tSibling = null", "\tName = Alex", "\tHeight = 179,5", "\tAge = 19",
"\tFavouriteNumbers = [ 1,1 2,2 3,3 4,4 5,5 ]", "");
outputString.Should().Be(expectedString);
}
Expand All @@ -149,10 +149,12 @@ public void PrintToString_SerializesList()
public void PrintToString_SerializesDictionary()
{
var numbers = new Dictionary<Person, double> { { new Person(), 1 }, { new Person(), 2 } };
var printer = ObjectPrinter.For<Dictionary<Person, double>>().Excluding<Guid>();
var a = new[] { numbers };
var b = new HashSet<Dictionary<Person,double>[]> {a,};
var printer = ObjectPrinter.For<HashSet<Dictionary<Person,double>[]>>().Excluding<Guid>();

var outputString = printer.PrintToString(numbers);
var expectedString = @"{
var outputString = printer.PrintToString(b);
var expectedString = @"[ [ {
Yrwlcm marked this conversation as resolved.
Show resolved Hide resolved
[
Person
Sibling = null
Expand All @@ -173,7 +175,7 @@ public void PrintToString_SerializesDictionary()
:
2
],
}
} ] ]
";
outputString.Should().Be(expectedString);
}
Expand Down