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

Зиновьева Милана #216

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ObjectPrintingHomework/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public class Person
public DateTime DateBirth {get; set; }
public Person[] Parents { get; set; }
public Person[] Friends { get; set; }
public Dictionary<string, int> LimbToNumbersFingers {get; set;}
public List<Person> Childs {get; set;}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

докопался: дети по-английски - children, а словарь конечностей к числу пальцев - LimbToFingersCount

}
35 changes: 35 additions & 0 deletions ObjectPrintingHomework/PrintingConfig.cs
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;
Expand Down Expand Up @@ -56,6 +58,9 @@ private string PrintToString(object obj, int nestingLevel)
return string.Empty;

This comment was marked as resolved.

This comment was marked as resolved.

processedObjects.Add(obj);

if (obj is ICollection)
return ProcessCollections(obj, nestingLevel);

if (typeSerializers.TryGetValue(type, out var serializer))
return serializer(obj);

Expand Down Expand Up @@ -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)
Copy link

Choose a reason for hiding this comment

The 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();
}
}
53 changes: 50 additions & 3 deletions TestsObjectPrinting/TestsObjectPrintingHomework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

напрягают эти тримы. Теперь понял зачем ты везде тыкала newLine. Ну ладно, пусть будет перенос у пустого объекта

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -160,7 +164,7 @@ public void TestExceptionForTrimming()


[Test]
public void Work_WhenReferenceCycles()
public void TestWhenReferenceCycles()
{
const string excepted = "Circular Reference";
person.Parents = [person];
Expand All @@ -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);
}
}