-
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
Alexander Andronov #203
Open
batyadmx
wants to merge
2
commits into
kontur-courses:master
Choose a base branch
from
batyadmx:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Alexander Andronov #203
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using NUnit.Framework; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" /> | ||
<PackageReference Include="NUnit" Version="3.13.3" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" /> | ||
<PackageReference Include="coverlet.collector" Version="3.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ObjectPrinting\ObjectPrinting.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ObjectPrinting.Tests.Tests | ||
{ | ||
public class Node | ||
{ | ||
public int Value { get; set; } | ||
public Node Next { get; set; } | ||
} | ||
} |
210 changes: 210 additions & 0 deletions
210
ObjectPrinting.Tests/Tests/ObjectPrinterAcceptanceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
using FluentAssertions; | ||
using FluentAssertions.Primitives; | ||
using NUnit.Framework; | ||
using System.Globalization; | ||
using System.Reflection; | ||
|
||
namespace ObjectPrinting.Tests.Tests | ||
{ | ||
[TestFixture] | ||
public class ObjectPrinterAcceptanceTests | ||
{ | ||
private Person person; | ||
private PrintingConfig<Person> personPrinter; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
person = new Person() | ||
{ | ||
Id = Guid.NewGuid(), | ||
Name = "John Doe", | ||
Height = 175.5 | ||
}; | ||
personPrinter = ObjectPrinter.For<Person>(); | ||
} | ||
|
||
[Test] | ||
public void PrintListOfInts_ReturnCorrectString() | ||
{ | ||
var list = new List<int>() { 1, 2, 3 }; | ||
var printer = ObjectPrinter.For<List<int>>(); | ||
|
||
var actual = printer.PrintToString(list); | ||
|
||
Assert.AreEqual("[1,2,3]", actual); | ||
} | ||
|
||
[Test] | ||
public void PrintArrayOfInts_ReturnCorrectString() | ||
{ | ||
var list = new int[] { 1, 2, 3 }; | ||
var printer = ObjectPrinter.For<int[]>(); | ||
|
||
var actual = printer.PrintToString(list); | ||
|
||
Assert.AreEqual("[1,2,3]", actual); | ||
} | ||
|
||
[Test] | ||
public void PrintListDictOfStrings_ReturnCorrectString() | ||
{ | ||
var dict = new Dictionary<int, string> | ||
{ | ||
{ 1, "a" }, | ||
{ 2, "b" } | ||
}; | ||
var printer = ObjectPrinter.For<Dictionary<int, string>>(); | ||
|
||
var actual = printer.PrintToString(dict); | ||
|
||
Assert.AreEqual("{1 : a,2 : b}", actual); | ||
} | ||
|
||
[Test] | ||
public void ExcludeProperty_Success() | ||
{ | ||
personPrinter.Excluding(o => o.Height); | ||
|
||
var actual = personPrinter.PrintToString(person); | ||
|
||
StringAssert.DoesNotContain("Height", actual); | ||
} | ||
|
||
[Test] | ||
public void ExcludeType_Success() | ||
{ | ||
personPrinter.Excluding<Guid>(); | ||
|
||
var actual = personPrinter.PrintToString(person); | ||
|
||
StringAssert.DoesNotContain("Id", actual); | ||
} | ||
|
||
[Test] | ||
public void PrintForTypes_Success() | ||
{ | ||
personPrinter.Print<string>().Using(o => "ABCDEF"); | ||
|
||
var actual = personPrinter.PrintToString(person); | ||
|
||
StringAssert.Contains("ABCDEF", actual); | ||
} | ||
|
||
[Test] | ||
public void PrintForProperties_Success() | ||
{ | ||
personPrinter.Print(o => o.Name).Using(o => "ABCDEF"); | ||
|
||
var actual = personPrinter.PrintToString(person); | ||
|
||
StringAssert.Contains("ABCDEF", actual); | ||
} | ||
|
||
[Test] | ||
public void TruncateString_Success() | ||
{ | ||
personPrinter.Print<string>().TruncateLength(6); | ||
|
||
var actual = personPrinter.PrintToString(person); | ||
|
||
StringAssert.DoesNotContain("John Do", actual); | ||
StringAssert.Contains("John D", actual); | ||
} | ||
|
||
[Test] | ||
public void SetMaxStringLengthViaConfigure_Success() | ||
{ | ||
personPrinter.Configure(opt => opt.MaxStringLength = 6); | ||
|
||
var actual = personPrinter.PrintToString(person); | ||
|
||
StringAssert.DoesNotContain("John Do", actual); | ||
StringAssert.Contains("John D", actual); | ||
} | ||
|
||
[TestCase("ru-RU")] | ||
[TestCase("en-US")] | ||
[TestCase("fr-FR")] | ||
public void SetCulture_ShouldApplyCulture(string code) | ||
{ | ||
var culture = new CultureInfo(code); | ||
personPrinter.Print<double>() | ||
.SetCulture(culture); | ||
var should = person.Height.ToString(culture); | ||
|
||
|
||
var actual = personPrinter.PrintToString(person); | ||
|
||
|
||
StringAssert.Contains(should, actual); | ||
} | ||
|
||
[TestCase("ru-RU")] | ||
[TestCase("en-US")] | ||
[TestCase("fr-FR")] | ||
public void SetCultureViaConfigure_ShouldApplyCulture(string code) | ||
{ | ||
var culture = new CultureInfo(code); | ||
personPrinter.Configure(opt => opt.CultureInfo = culture); | ||
var should = person.Height.ToString(culture); | ||
|
||
|
||
var actual = personPrinter.PrintToString(person); | ||
|
||
|
||
StringAssert.Contains(should, actual); | ||
} | ||
|
||
[Test] | ||
public void CyclicReference_DoesnThrowStackOverflow() | ||
{ | ||
var node1 = new Node() { Value = 1 }; | ||
var node2 = new Node() { Value = 2 }; | ||
node1.Next = node2; | ||
node2.Next = node1; | ||
var printer = ObjectPrinter.For<Node>(); | ||
|
||
Console.WriteLine(printer.PrintToString(node1)); | ||
Assert.DoesNotThrow(() => printer.PrintToString(node1)); | ||
} | ||
|
||
[Test] | ||
public void Demo() | ||
{ | ||
var person = new Person { Name = "Alex", Age = 19, Height = 172.1, | ||
Father = new Parent() { Name = "Bob Robinson", Age = 54, Height = 182.2}, | ||
Mother = new Parent() { Name = "Sara Robinson", Age = 50, Height = 185.4} | ||
}; | ||
|
||
var printer = ObjectPrinter.For<Person>(); | ||
//1. Исключить из сериализации свойства определенного типа | ||
//2. Указать альтернативный способ сериализации для определенного типа | ||
//3. Для числовых типов указать культуру | ||
//4. Настроить сериализацию конкретного свойства | ||
//5. Настроить обрезание строковых свойств (метод должен быть виден только для строковых свойств) | ||
//6. Исключить из сериализации конкретного свойства | ||
|
||
printer.Excluding(o => o.Id) | ||
.Print(o => o.Height) | ||
.Using(h => $"{h} сантиметров") | ||
.Print(o => o.Name) | ||
.TruncateLength(10) | ||
.Configure(opts => | ||
{ | ||
opts.MaxStringLength = 100; | ||
opts.CultureInfo = CultureInfo.InvariantCulture; | ||
}); | ||
|
||
|
||
|
||
string s1 = printer.PrintToString(person); | ||
|
||
Console.WriteLine(s1); | ||
|
||
s1.Should().Be(s1); | ||
//7. Синтаксический сахар в виде метода расширения, сериализующего по-умолчанию | ||
//8. ...с конфигурированием | ||
} | ||
} | ||
} |
9 changes: 8 additions & 1 deletion
9
ObjectPrinting/Solved/Tests/Person.cs → ObjectPrinting.Tests/Tests/Person.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
using System; | ||
|
||
namespace ObjectPrinting.Solved.Tests | ||
namespace ObjectPrinting.Tests.Tests | ||
{ | ||
public class Person | ||
{ | ||
public Guid Id { get; set; } | ||
public string Name { get; set; } | ||
public double Height { get; set; } | ||
public int Age { get; set; } | ||
public Parent Father { get; set; } | ||
public Parent Mother { get; set; } | ||
} | ||
|
||
public class Parent : Person | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class Configuration | ||
{ | ||
public List<Predicate> ToExclude; | ||
public List<(Predicate predicate, Func<object, string> serializer)> Serializers; | ||
public Options Options; | ||
|
||
public Configuration | ||
(List<Predicate> toExclude = null, | ||
List<(Predicate predicate, Func<object, string> serializer)> serializers =null) | ||
{ | ||
ToExclude = toExclude ?? new List<Predicate>(); | ||
Serializers = serializers ?? new List<(Predicate predicate, Func<object, string> serializer)>(); | ||
Options = new Options(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text; | ||
|
||
namespace ObjectPrinting | ||
{ | ||
public class Options | ||
{ | ||
public int MaxStringLength = -1; | ||
public CultureInfo CultureInfo = CultureInfo.InvariantCulture; | ||
public int MaxRecursionDepth = 10; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
sealed
(можно даже в шаблоне к классу это добавить). В некоторых ситуациях это микроскопически улучшает производительность, но что более важно - напоминает более тщательно продумывать иерархию наследованияinit-only
. Это даст хоть какую-то гарантию где-то в неожиданном месте не произойдет мутация и не внесет суету в поведение. Можно заморочиться и ввести еще и интерфейс, с аналогичными полями но толькоgetter
ами, тогда у тебя будет абсолютная иммутабельность и полиморфность