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
16 changes: 16 additions & 0 deletions ObjectPrinterTests/Collections.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections;
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; }

public IEnumerable<int> Enumerable;
}
}
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;
21 changes: 21 additions & 0 deletions ObjectPrinterTests/Kid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace ObjectPrinterTests
{
public class Kid
{
public string Name { set; get; }
public Kid Parent;

public override int GetHashCode()
{
return Name.GetHashCode();
}

public override bool Equals(object? obj)
{
if (obj?.GetType() != GetType()) return false;
var kid = (Kid)obj;
return Name == kid.Name;
}
}

}
217 changes: 217 additions & 0 deletions ObjectPrinterTests/ObjectPrinterAcceptanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
using System.Collections;
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_ShouldPrintObjectWithoutIntPropertiesAndFields()
{
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));
result.Should().NotContain(nameof(person.Defects));
}

[Test]
public void ObjectPrinter_WhenExcludingMember_ShouldPrintObjectWithoutMember()
{
var person = new Person { Name = "Alex", Age = 19 };
var printer = ObjectPrinter.For<Person>();
var result = printer.Excluding(x => x.Age).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.Defects).Using(culture).PrintToString(person);
result.Should().Contain($"{nameof(person.Defects)} = {person.Defects.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>();
printer.Printing<string>().TrimmedToLength(maxLen);
var result = person.PrintToString();
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_WhenPrintingIEnumerableProperty_ShouldPrintObject()
{
var collections = new Collections();
collections.Enumerable = new Stack<int>(new[] { 1, 2, 3, 4 });
var printer = ObjectPrinter.For<Collections>();
var result = printer.PrintToString(collections);
foreach (var value in collections.Enumerable) result.Should().Contain($"{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[] { 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}");
}
}
[Test]
public void ObjectPrinter_WhenPrintingIdenticalPersons_ShouldPrintObjectWithoutCycle()
{
var person1 = new Kid() { Name = "Abobus" };
person1.Parent = new Kid() { Name = "Abobus" };
var result = person1.PrintToString();
result.Should().NotContain("(Cycle)");
}
}
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>
11 changes: 11 additions & 0 deletions ObjectPrinterTests/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace ObjectPrinting.Tests;

public class Person
{
public Guid Id { get; set; } = Guid.Empty;
public string Name { get; set; }
public double Height { get; set; }
public int Age { get; set; }
public int Defects = 45;
public string Text = "some text";
}
9 changes: 9 additions & 0 deletions ObjectPrinting/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ObjectPrinting;

public static class ObjectExtensions
{
public static string PrintToString<T>(this T obj)
{
return ObjectPrinter.For<T>().PrintToString(obj);
}
}
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