-
Notifications
You must be signed in to change notification settings - Fork 245
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
Зиновьева Милана #216
base: master
Are you sure you want to change the base?
Зиновьева Милана #216
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
using System.Collections; | ||
using System.Diagnostics.Metrics; | ||
using System.Globalization; | ||
using System.Linq.Expressions; | ||
using System.Text; | ||
|
@@ -56,6 +58,9 @@ private string PrintToString(object obj, int nestingLevel) | |
return string.Empty; | ||
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
Sorry, something went wrong. |
||
processedObjects.Add(obj); | ||
|
||
if (obj is ICollection) | ||
return ProcessCollections(obj, nestingLevel); | ||
|
||
if (typeSerializers.TryGetValue(type, out var serializer)) | ||
return serializer(obj); | ||
|
||
|
@@ -113,4 +118,34 @@ public void SetStringPropertyLength(string propertyName, int startIndex, int max | |
{ | ||
stringPropertyLengths[propertyName] = new Tuple<int, int>(startIndex, maxLength); | ||
} | ||
|
||
private string ProcessCollections(object obj, int nestingLevel){ | ||
if (obj is IDictionary dictionary) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ещё докопался: тут будет круче воткнуть IReadonlyDictionary, тк у него больше реализаций, но это будет всё тот же словарь, и если клиенту сковородка на голову упадёт, и он решит реализовать свой словарь, то он стопроц будет реализовывать IReadonlyDictionary (если сковородкой не так сильно огрело) |
||
return ExecuteDictionary(dictionary, nestingLevel); | ||
else{ | ||
var collection = (IEnumerable)obj; | ||
return ExecuteIEnumerable(collection, nestingLevel); | ||
} | ||
} | ||
|
||
private string ExecuteDictionary(IDictionary dictionary, int nestingLevel) | ||
{ | ||
var sb = new StringBuilder(); | ||
foreach(var key in dictionary.Keys){ | ||
sb.Append("{" + PrintToString(key, nestingLevel + 1) + " = "); | ||
sb.Append(PrintToString(dictionary[key], nestingLevel + 1) + "}; "); | ||
} | ||
return sb.ToString(); | ||
} | ||
|
||
private string ExecuteIEnumerable(IEnumerable collection, int nestingLevel) | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.Append('\n'); | ||
foreach(var value in collection){ | ||
sb.Append('\t', nestingLevel + 1); | ||
sb.Append(PrintToString(value, nestingLevel + 1)); | ||
} | ||
return sb.ToString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,9 @@ public void TestAllExcludingTypes() | |
{ | ||
const string excepted = "Person"; | ||
var result = ObjectPrinter.For<Person>() | ||
.Excluding<string>().Excluding<int>().Excluding<double>().Excluding<Guid>().Excluding<DateTime>().Excluding<Person[]>() | ||
.Excluding<string>().Excluding<int>().Excluding<double>() | ||
.Excluding<Guid>().Excluding<DateTime>().Excluding<Person[]>() | ||
.Excluding<Dictionary<string, int>>().Excluding<List<Person>>() | ||
.PrintToString(person); | ||
|
||
result.Trim().Should().Be(excepted); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. напрягают эти тримы. Теперь понял зачем ты везде тыкала newLine. Ну ладно, пусть будет перенос у пустого объекта |
||
|
@@ -61,7 +63,9 @@ public void TestExcludingAllProperties() | |
const string excepted = "Person"; | ||
var result = ObjectPrinter.For<Person>() | ||
.Excluding(p => p.Age).Excluding(p => p.CountEyes).Excluding(p => p.Height) | ||
.Excluding(p => p.Id).Excluding(p => p.Name).Excluding(p => p.Surname).Excluding(p => p.DateBirth).Excluding<Person[]>() | ||
.Excluding(p => p.Id).Excluding(p => p.Name).Excluding(p => p.Surname) | ||
.Excluding(p => p.DateBirth).Excluding(p => p.Friends).Excluding(p => p.LimbToNumbersFingers) | ||
.Excluding(p => p.Parents).Excluding(p => p.Childs) | ||
.PrintToString(person); | ||
|
||
result.Trim().Should().Be(excepted); | ||
|
@@ -160,7 +164,7 @@ public void TestExceptionForTrimming() | |
|
||
|
||
[Test] | ||
public void Work_WhenReferenceCycles() | ||
public void TestWhenReferenceCycles() | ||
{ | ||
const string excepted = "Circular Reference"; | ||
person.Parents = [person]; | ||
|
@@ -170,4 +174,47 @@ public void Work_WhenReferenceCycles() | |
Console.WriteLine(result); | ||
result.Should().Contain(excepted); | ||
} | ||
|
||
[Test] | ||
public void TestSerializingDictionary() | ||
{ | ||
const string excepted = "{Left_arm = 5}; {Left_leg = 6}; {Right_leg = 10};"; | ||
Dictionary<string, int> dict = new Dictionary<string, int>{ | ||
{"Left_arm", 5}, {"Left_leg", 6}, {"Right_leg", 10} | ||
}; | ||
person.LimbToNumbersFingers = dict; | ||
var result = ObjectPrinter.For<Person>().PrintToString(person); | ||
|
||
result.Should().Contain(excepted); | ||
} | ||
|
||
[Test] | ||
public void TestSerializingList() | ||
{ | ||
const string exceptedOleg = "Name = Oleg\n\t\t\tAge = 1"; | ||
const string exceptedMaria = "Name = Maria\n\t\t\tAge = 2"; | ||
List<Person> list = new List<Person>{new Person { Name = "Oleg", Age = 1} , new Person { Name = "Maria", Age = 2}}; | ||
person.Childs = list; | ||
var result = ObjectPrinter.For<Person>().Excluding(p => p.CountEyes).Excluding(p => p.Height) | ||
.Excluding(p => p.Id).Excluding(p => p.Surname) | ||
.Excluding(p => p.DateBirth).Excluding(p => p.Friends).Excluding(p => p.LimbToNumbersFingers) | ||
.Excluding(p => p.Parents).PrintToString(person); | ||
|
||
result.Should().Contain(exceptedOleg).And.Contain(exceptedMaria); | ||
} | ||
|
||
[Test] | ||
public void TestSerializingArray() | ||
{ | ||
const string exceptedAlbert = "Name = Albert\n\t\t\tAge = 54"; | ||
const string exceptedLiana = "Name = Liana\n\t\t\tAge = 55"; | ||
Person[] list = [new Person { Name = "Albert", Age = 54} , new Person { Name = "Liana", Age = 55}]; | ||
person.Parents = list; | ||
var result = ObjectPrinter.For<Person>().Excluding(p => p.CountEyes).Excluding(p => p.Height) | ||
.Excluding(p => p.Id).Excluding(p => p.Surname) | ||
.Excluding(p => p.DateBirth).Excluding(p => p.Friends).Excluding(p => p.LimbToNumbersFingers) | ||
.Excluding(p => p.Childs).PrintToString(person); | ||
|
||
result.Should().Contain(exceptedAlbert).And.Contain(exceptedLiana); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
докопался: дети по-английски - children, а словарь конечностей к числу пальцев - LimbToFingersCount