Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,5 @@ _Pvt_Extensions
.fake/

.idea/

fluent-api.sln
148 changes: 148 additions & 0 deletions ObjectPrinterTests/CollectionPrintingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using ObjectPrinterTests.Entities;
using ObjectPrinting;
using ObjectPrinting.Extensions;

namespace ObjectPrinterTests;

[TestFixture]
public class CollectionPrintingSnapshotTests

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Стоит добавить тесты для граничных случаев (null-элементов, null-коллекций и т.д.)

{
private static readonly VerifySettings SnapshotSettings;

static CollectionPrintingSnapshotTests()
{
SnapshotSettings = new VerifySettings();
SnapshotSettings.UseDirectory("ExpectedResults");
}

[Test]
public Task IntArray_Snapshot()
{
var config = new PrintingConfig<int[]>();
var value = new[] { 1, 2, 3 };

var actual = config.PrintToString(value);

return Verify(actual, SnapshotSettings);
}

[Test]
public Task PrintToString_StringList_ShouldHandleCorrect()
{
var config = new PrintingConfig<List<string>>();
var value = new List<string> { "one", "two" };

var actual = config.PrintToString(value);

return Verify(actual, SnapshotSettings);
}

[Test]
public Task PrintToString_Dictionary_ShouldHandleCorrect()
{
var config = new PrintingConfig<Dictionary<string, int>>();
var value = new Dictionary<string, int>
{
["a"] = 1,
["b"] = 2
};

var actual = config.PrintToString(value);

return Verify(actual, SnapshotSettings);
}

[Test]
public Task PrintToString_ShouldHandleClassWithCollections_Correctly()
{
var config = new PrintingConfig<Person>();
var person = new Person
{
Name = "John",
Scores = [10, 20],
Tags = ["dev", "qa"],
Addresses = new Dictionary<string, Address>
{
["home"] = new() { City = "Chelyabinsk" },
["work"] = new() { City = "Sverdlovsk" }
}
};

var actual = config.PrintToString(person);

return Verify(actual, SnapshotSettings);
}

[Test]
public Task PrintToString_ShouldHandleListOfLists_Correctly()
{
var config = new PrintingConfig<List<List<int>>>();
var value = new List<List<int>>
{
new() { 1, 2 },
new() { 3 }
};

var actual = config.PrintToString(value);

return Verify(actual, SnapshotSettings);
}

[Test]
public Task PrintToString_ShouldHandleDictionaryWithListValues_Correctly()
{
var config = new PrintingConfig<Dictionary<string, List<int>>>();
var value = new Dictionary<string, List<int>>
{
["first"] = [1, 2],
["second"] = [3]
};

var actual = config.PrintToString(value);

return Verify(actual, SnapshotSettings);
}

[Test]
public Task PrintToString_ShouldHandleDictionaryWithNull_Correctly()
{
var config = new PrintingConfig<Dictionary<string, List<int>>>();
var value = new Dictionary<string, List<int>?>
{
["first"] = null,
["second"] = null
};

var actual = config.PrintToString(value);

return Verify(actual, SnapshotSettings);
}

[Test]
public Task PrintToString_ShouldHandleListWithNull_Correctly()
{
var config = new PrintingConfig<Dictionary<string, List<int>>>();
var value = new List<string?>()
{
null,
null,
null
};

var actual = config.PrintToString(value);

return Verify(actual, SnapshotSettings);
}

[Test]
public Task PrintToString_ShouldHandleListCycle_Correctly()
{
var config = new PrintingConfig<List<object>>();
var value = new List<object>();
value.Add(value);

var actual = config.PrintToString(value);

return Verify(actual, SnapshotSettings);
}
}
9 changes: 9 additions & 0 deletions ObjectPrinterTests/Entities/A.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ObjectPrinterTests.Entities;

public class A
{
public string Name { get; set; }
public int Number { get; set; }
public double Price { get; set; }
public Guid Id { get; set; }
}
7 changes: 7 additions & 0 deletions ObjectPrinterTests/Entities/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ObjectPrinterTests.Entities;

public class Address
{
public string City { get; set; } = "";
public string Street { get; set; } = "";
}
7 changes: 7 additions & 0 deletions ObjectPrinterTests/Entities/B.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ObjectPrinterTests.Entities;

public class B
{
public A Data { get; set; }
public B Parent { get; set; }
}
9 changes: 9 additions & 0 deletions ObjectPrinterTests/Entities/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ObjectPrinterTests.Entities;

public class Person
{
public string Name { get; set; } = "";
public int[] Scores { get; set; } = [];
public List<string> Tags { get; set; } = [];
public Dictionary<string, Address> Addresses { get; set; } = new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Int32[] [
1
2
3
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Dictionary<String, Int32> {
[a] = 1
[b] = 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Person:
Name = John
Scores = Int32[] [
10
20
]
Tags = List<String> [
dev
qa
]
Addresses = Dictionary<String, Address> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Хорошо бы переделать на языконезависимый JSON-подобный формат. Типы данных не должны быть явно прописаны, т.к. названия типов это детали C#. List<String> [ dev qa ] -> ["dev", "qa"]

[home] = Address:
City = Chelyabinsk
Street =

[work] = Address:
City = Sverdlovsk
Street = <cyclic reference>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Dictionary<String, List<Int32>> {
[first] = List<Int32> [
1
2
]
[second] = List<Int32> [
3
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Dictionary<String, List<Int32>> {
[first] = null
[second] = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
List<Object> [
<cyclic reference>
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
List<List<Int32>> [
List<Int32> [
1
2
]
List<Int32> [
3
]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
List<String> [
null
null
null
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
List<String> [
one
two
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
A:
Name = null
Number = 0
Price = 1234,56
Id = 00000000-0000-0000-0000-000000000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
A:
Name = NAME=Alex
Number = 0
Price = 0
Id = 00000000-0000-0000-0000-000000000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
A:
Name = Alex
Price = 0
Id = 00000000-0000-0000-0000-000000000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
A:
Name = Alex
Number = 0
Price = 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
B:
Data = A:
Name = Child
Number = 0
Price = 0
Id = 00000000-0000-0000-0000-000000000000

Parent = B:
Data = null
Parent = <cyclic reference>

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
A:
Name = Alex
Number = 0
Price = 0
Id = 00000000-0000-0000-0000-000000000000
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
A:
Name = null
Number = INT(42)
Price = 0
Id = 00000000-0000-0000-0000-000000000000
Loading