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 List<int>[] Array { set; get; }
public List<int> 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 ObjectPrinting.Tests
{
public class Kid
{
public string Name { set; get; }
public Kid Parent { set; get; }
}
}
149 changes: 149 additions & 0 deletions ObjectPrinterTests/ObjectPrinterAcceptanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System.Globalization;
using FluentAssertions;

namespace ObjectPrinting.Tests;

[TestFixture]
public class ObjectPrinterAcceptanceTests
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
{
[Test]
public void ObjectPrinter_PrintObjectWithoutIntProperties_WhenExcludingIntType()
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
{
var person = new Person { Name = "Alex", Age = 19 };

var printer = ObjectPrinter.For<Person>();
var s1 = printer.Excluding(x => x.Height).PrintToString(person);
s1.Should().NotContain("Height");
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
}

[Test]
public void ObjectPrinter_PrintObjectWithoutIntProperty_WhenExcludingHeight()
{
var person = new Person { Name = "Alex", Age = 19 };
var printer = ObjectPrinter.For<Person>();
var s1 = printer.Excluding<int>().PrintToString(person);
s1.Should().NotContain("Age");
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
}

[Test] public void ObjectPrinter_PrintObjectWithModifiedProperty_PrintingPropertyUsingSomeConditional()
{
var person = new Person { Name = "Alex", Age = 15 };
var printer = ObjectPrinter.For<Person>();
var s1 = printer.Printing(x => x.Age).Using(x=> x.ToString("X")).PrintToString(person);
s1.Should().Contain("Age = F");
}
[Test]
public void ObjectPrinter_ChangingPropertiesByType_ObjectWithModifiedProperty()
{
var person = new Person { Name = "Alex", Age = 15, Height = 1.2};
var printer = ObjectPrinter.For<Person>();
var s1 = printer.Printing<double>().Using(x=> (x * 2).ToString()).PrintToString(person);
s1.Should().Contain("Height = 2,4");
}

[Test]
public void ObjectPrinter_PrintPropertyWithCulture_WhenCultureIsSet()
{
var person = new Person { Name = "Alex", Age = 15, Height = 2.4 };
var printer = ObjectPrinter.For<Person>();
var s1 = printer.Printing(x => x.Height).Using(new CultureInfo("en-GB")).PrintToString(person);
s1.Should().Contain("Height = 2.4");
}
[Test]
public void ObjectPrinter_PrintObjectWithTrimmedProperties_WhenTrimmedStringProperties()
{
var person = new Person { Name = "Alex", Age = 15, Height = 2.4, Id = Guid.Empty};
var printer = ObjectPrinter.For<Person>();
var s1 = printer.Printing<string>().TrimmedToLength(1).PrintToString(person);
s1.Should().Contain("Name = A\r\n\t");
}
[Test]
public void ObjectPrinter_PrintObjectWithModifiedProperty_WhenTrimmedStringPropertiesButCroppingLengthLess0()
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
{
var person = new Person { Name = "Alex", Age = 15, Height = 2.4, Id = Guid.Empty};
var printer = ObjectPrinter.For<Person>();
Action 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_PrintObjectWithCycleProperty_WhenPropertyRefersItself()
{
var kid = new Kid { Name = "Pasha"};
var parent = new Kid{Name = "Lev"};
kid.Parent = parent;
parent.Parent = kid;



var printer = ObjectPrinter.For<Kid>();
var s1 = printer.PrintToString(kid);
s1.Should().Contain("Parent = (Cycle) ObjectPrinting.Tests.Kid");
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
}
[Test]
public void ObjectPrinter_PrintingDictionaryProperty_PrintObject()
{
var collections = new Collections();
collections.Dictionary = new Dictionary<int, string>()
{
{1, "hello"},
{2, "hell"},
{3, "hel"},
{4, "he"},
{5, "h"},
};
var printer = ObjectPrinter.For<Collections>();
const string expected = "Dictionary = \r\n\t\t" +
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
"1\r\n : hello\r\n\t\t" +
"2\r\n : hell\r\n\t\t" +
"3\r\n : hel\r\n\t\t" +
"4\r\n : he\r\n\t\t" +
"5\r\n : h\r\n\t";
var s1 = printer.PrintToString(collections);
s1.Should().Contain(expected);
}
[Test]
public void ObjectPrinter_WhenThereIsObjectWithListProperty_PrintObject()
{
var collections = new Collections();
collections.List = new List<int>() { 1, 2, 3 };
var printer = ObjectPrinter.For<Collections>();
const string expected = "List = \r\n\t\t1\r\n\t\t2\r\n\t\t3\r\n\t";
var s1 = printer.PrintToString(collections);
s1.Should().Contain(expected);
}
[Test]
public void ObjectPrinter_WhenThereIsArrayGenericObjects_PrintObject()
{
var collections = new Collections();
collections.List = new List<int>() { 1, 2, 3 };
collections.Array = new [] { collections.List };
var printer = ObjectPrinter.For<Collections>();
const string expected = "Array = " +
"\r\n\t\tList`1" +
"\r\n\t\t1" +
"\r\n\t\t2" +
"\r\n\t\t3" +
"\r\n\tList = " +
"\r\n\t\t1" +
"\r\n\t\t2" +
"\r\n\t\t" +
"3\r\n\t";
var s1 = printer.PrintToString(collections);
s1.Should().Contain(expected);
}
[Test]
public void ObjectPrinter_PrintingSomeClassesInList_PrintObject()
{
var collections = new Collections();
collections.List = null;
collections.Array = null;
collections.Persons = new List<Person> {new Person(){Name = "Lev"} };
var printer = ObjectPrinter.For<Collections>();
const string expected = "Persons = \r\n\t\tPerson";
var s1 = printer.PrintToString(collections);
s1.Should().Contain(expected);
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
}
}
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