Skip to content

Д/з ObjectPrinter#246

Open
Azkraft wants to merge 5 commits intokontur-courses:masterfrom
Azkraft:master
Open

Д/з ObjectPrinter#246
Azkraft wants to merge 5 commits intokontur-courses:masterfrom
Azkraft:master

Conversation

@Azkraft
Copy link
Copy Markdown

@Azkraft Azkraft commented Nov 19, 2025

@LevShisterov
Copy link
Copy Markdown

При коммите старайся описывать смысл изменений.
"Add homework" в будущем мало расскажет что же на самом деле в коде поменялось.


namespace Homework;

public class ObjectEqualityComparer : IEqualityComparer<object>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Может стоит в названии класса отразить что он сравнивает по ссылке? Иначе потом забудется и придется в эту реализацию проваливаться чтобы узнать как она сравнивает.

@@ -0,0 +1,259 @@
using FluentAssertions;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Давай вынесем тесты в отдельный проект?
А то потом захочется использовать код как библиотеку и тесты поедут вместе с кодом куда-нибудь на боевую площадку.

.Printing<int>().Using(i => i.ToString("X"))
//3. Для числовых типов указать культуру
.Printing<double>().Using(CultureInfo.InvariantCulture)
//4. Настроить сериализацию конкретного свойства
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Тут, кажется, не хватает настройки сериализации для конкретного свойства.


public PrintingConfig<TOwner> Using(CultureInfo culture)
{
if (typeof(TPropType).GetMethod("ToString", [typeof(IFormatProvider)]) is null)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можем как-нибудь поэлегантнее проверить и, желательно, не в рантайме чтобы падало, а еще на этапе компиляции?

{
var lambda = memberSelector as LambdaExpression;
var visitor = new LastMemberVisitor();
visitor.Visit(lambda.Body);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Интересно, зачем поход в Visitor?

var expected =
$"""
Person
{"\t"}Age = {person.Age}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

У меня тестики разваливаются, пишут что "Expected \r\n, Actual \n"

namespace HomeworkTests;

public class Person
{
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

У меня StackOverflow в тестах, если я поле с базовым типом enum добавляю.

}

[Test]
public void Should_ExcludeParticularPropertyOrField()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Интересно, а мы сумеем исключить вложенное поле?
Person.Address.Street например.

toStringMethod?.DeclaringType == type;
}

HashSet<Type> IPrintingConfig.ExcludeTypes { get; } = [];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Можешь рассказать почему выбрал такое решение (с интерфейсом)?
Какие плюсы/минусы по сравнению с альтернативами?

{"\t"}Parent = {person.Parent?.ToString() ?? "null"}
{"\t"}Weight = {person.Weight}

""";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

А если для какого-нибудь теста нам надо будет еще поле в объект добавить, то это каждый тест придется поправить?

[Test]
public void Should_ExcludePropertyOrFieldWithParticularType()
{
var person = CreatePerson();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Если я пробую иерархию Person1->Person2->Person2, то мне Person2 совсем не выводит, даже раз.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants