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

Юдин Павел #194

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
13 changes: 13 additions & 0 deletions ObjectPrinterTests/Collections.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using ObjectPrinting.Tests;

namespace ObjectPrinting
{
public class Collections
{
public Dictionary<int, string> Dictionary { set; get; }
public int[][] Array { set; get; }
public List<object> List { set; get; }

public List<Person> Persons { set; get; }
}
}
1 change: 1 addition & 0 deletions ObjectPrinterTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
8 changes: 8 additions & 0 deletions ObjectPrinterTests/Kid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ObjectPrinterTests
{
public class Kid
{
public string Name { set; get; }
public Kid Parent { set; get; }
}
}
205 changes: 205 additions & 0 deletions ObjectPrinterTests/ObjectPrinterAcceptanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
using System.Globalization;
using FluentAssertions;
using ObjectPrinting;
using ObjectPrinting.Tests;

namespace ObjectPrinterTests;

[TestFixture]
public class ObjectPrinterAcceptanceTests
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly string newLine = Environment.NewLine;

[Test]
public void ObjectPrinter_OnSubsequentPrints_ShouldNotContainCycle()
{
var person = new Person { Name = "Alex", Age = 19 };

var printer = ObjectPrinter.For<Person>();
var result = printer.PrintToString(person);
var result2 = printer.PrintToString(person);
result2.Should().NotContain("Cycle");
}
[Test]
public void ObjectPrinter_WhenExcludingIntType_ShouldPrintObjectWithoutIntProperties()
{
var person = new Person { Name = "Alex", Age = 19 };

var printer = ObjectPrinter.For<Person>();
var result = printer.Excluding<double>().PrintToString(person);
result.Should().NotContain(nameof(person.Height));
}

[Test]
public void ObjectPrinter_WhenExcludingHeight_ShouldPrintObjectWithoutIntProperty()
{
var person = new Person { Name = "Alex", Age = 19 };
var printer = ObjectPrinter.For<Person>();
var result = printer.Excluding<int>().PrintToString(person);
result.Should().NotContain(nameof(person.Age));
}

[Test]
public void ObjectPrinter_WhenPrintingPropertyUsingSomeConditional_ShouldPrintObjectWithModifiedProperty()
{
var person = new Person { Name = "Alex", Age = 15 };
const string nameProperty = nameof(person.Age);
var printer = ObjectPrinter.For<Person>();
var result = printer.Printing(x => x.Age).Using(x => nameProperty).PrintToString(person);
result.Should().Contain($"{nameProperty} = {nameProperty}");
}

[Test]
public void ObjectPrinter_WhenChangingPropertiesByType_ShouldBeObjectWithModifiedProperty()
{
var person = new Person { Name = "Alex", Age = 15, Height = 1.2 };

var printer = ObjectPrinter.For<Person>();
var result = printer.Printing<double>().Using(x => (x * x).ToString()).PrintToString(person);
result.Should().Contain($"{nameof(person.Height)} = {person.Height * person.Height}");
}

[Test]
public void PrintToString_WhenPrintAllMembersInObject_ShouldPrintAllPublicMembers()
{
var person = new Person { Name = "Alex", Age = 15, Height = 1.2 };
var printer = ObjectPrinter.For<Person>();
var result = printer.PrintToString(person);
foreach (var property in person.GetType().GetProperties())
{
result.Should().Contain($"{property.Name}");
}

foreach (var field in person.GetType().GetFields())
{
result.Should().Contain($"{field.Name}");
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
}
}

[Test]
public void ObjectPrinter_WhenCultureIsSet_ShouldPrintPropertyWithCulture()
{
var person = new Person { Name = "Alex", Age = 15, Height = 2.4 };
var culture = new CultureInfo("en-GB");
var printer = ObjectPrinter.For<Person>();
var result = printer.Printing(x => x.Height).Using(culture).PrintToString(person);
result.Should().Contain($"{nameof(person.Height)} = {person.Height.ToString(culture)}");
}

[Test]
public void ObjectPrinter_WhenTrimmedStringProperties_ShouldPrintObjectWithTrimmedProperties()
{
const int maxLen = 1;
var person = new Person { Name = "Alex", Age = 15, Height = 2.4, Id = Guid.Empty };
var printer = ObjectPrinter.For<Person>();
var result = printer.Printing<string>().TrimmedToLength(maxLen).PrintToString(person);
result.Should().Contain($"{nameof(person.Name)} = {person.Name[..maxLen]}");
}

[Test]
public void ObjectPrinter_WhenTrimmedStringPropertiesButCroppingLengthLess0_ShouldTrowArgumentException()
{
var person = new Person { Name = "Alex", Age = 15, Height = 2.4, Id = Guid.Empty };
var printer = ObjectPrinter.For<Person>();
var action = () => { printer.Printing<string>().TrimmedToLength(-10).PrintToString(person); };
action.Should().Throw<ArgumentException>("Error: The length of the truncated string cannot be negative");
}

[Test]
public void ObjectPrinter_WhenPropertyRefersItself_ShouldPrintObjectWithCycleProperty()
{
var kid = new Kid { Name = "Pasha" };
var parent = new Kid { Name = "Lev" };
kid.Parent = parent;
parent.Parent = kid;


var printer = ObjectPrinter.For<Kid>();
var result = printer.PrintToString(kid);
result.Should().Contain(kid.GetType().Name);
result.Should().Contain($"\t{nameof(kid.Parent)} = {kid.GetType().Name}");
result.Should().Contain($"\t\t{nameof(kid.Parent)} = (Cycle){kid.Parent.GetType().FullName}");
}

[Test]
public void ObjectPrinter_WhenPrintingDictionaryProperty_ShouldPrintObject()
{
var dictionary = new Dictionary<int, string>
{
{ 1, "hello" },
{ 2, "hell" },
{ 3, "hel" },
{ 4, "he" },
{ 5, "h" }
};
var collections = new Collections();
collections.Dictionary = new Dictionary<int, string>(dictionary);
var printer = ObjectPrinter.For<Collections>();
var result = printer.PrintToString(collections);
result.Should().Contain($"{nameof(collections.Dictionary)}");
foreach (var value in dictionary)
{
result.Should().Contain($"{value.Key}{newLine} : {value.Value}{newLine}");
}
}

[Test]
public void ObjectPrinter_WhenThereIsObjectWithListProperty_ShouldPrintObject()
{
var collections = new Collections();
collections.List = new List<object> { 1, 2, 3 };
var printer = ObjectPrinter.For<Collections>();
var result = printer.PrintToString(collections);
foreach (var value in collections.List)
{
result.Should().Contain($"{value}{newLine}");
}
}

[Test]
public void ObjectPrinter_WhenThereIsArrayGenericObjects_ShouldPrintObject()
{
var collections = new Collections();
collections.Array = new[] { new int[] { 1, 2, 3 } };
var printer = ObjectPrinter.For<Collections>();
var result = printer.PrintToString(collections);
result.Should().Contain($"{nameof(collections.Array)} = {newLine}");
foreach (var list in collections.Array)
{
result.Should().Contain($"\t\t{list.GetType().Name}{newLine}");
foreach (var value in list) result.Should().Contain($"\t\t\t{value}{newLine}");
}
}

[Test]
public void ObjectPrinter_WhenThereIsEnumerableTypeRefersItself_ShouldPrintObject()
{
var collections = new Collections();
collections.List = new List<object>();
collections.List.Add(collections.List);
var printer = ObjectPrinter.For<Collections>();
var result = printer.PrintToString(collections);
result.Should().Contain($"{nameof(collections.List)} = {newLine}");
foreach (var list in collections.List)
{
result.Should().Contain($"\t\t\t(Cycle){list.GetType().FullName}");
}
}

[Test]
public void ObjectPrinter_WhenPrintingSomeClassesInList_ShouldPrintObject()
{
var collections = new Collections();
var child = new Person { Name = "Child" };
collections.Persons = new List<Person> { new() { Name = "Lev" }, child, child };
var printer = ObjectPrinter.For<Collections>();
var result = printer.PrintToString(collections);
result.Should().Contain($"{nameof(collections.Persons)} = ");
foreach (var person in collections.Persons)
{
result.Should().Contain($"\t\t{person.GetType().Name}");
result.Should().Contain($"\t\t\t{nameof(person.Name)} = {person.Name}{newLine}");
result.Should().Contain($"\t\t\t{nameof(person.Age)} = {person.Age}{newLine}");
}
}
}
25 changes: 25 additions & 0 deletions ObjectPrinterTests/ObjectPrinterTests.csproj
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.6.0"/>
<PackageReference Include="NUnit" Version="3.13.3"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
<PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ObjectPrinting\ObjectPrinting.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ namespace ObjectPrinting.Tests
{
public class Person
{
public Guid Id { get; set; }
public Guid Id { get; set; } = Guid.Empty;
public string Name { get; set; }
public double Height { get; set; }
public int Age { get; set; }

}
}
7 changes: 7 additions & 0 deletions ObjectPrinting/IPropertyPrintingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ObjectPrinting
{
public interface IPropertyPrintingConfig<TOwner, TPropType>
{
PrintingConfig<TOwner> ParentConfig { get; }
}
}
3 changes: 2 additions & 1 deletion ObjectPrinting/ObjectPrinting.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<LangVersion>8</LangVersion>
<LangVersion>latest</LangVersion>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
</ItemGroup>
Expand Down
Loading