Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ldqk/Masuit.Tools
Browse files Browse the repository at this point in the history
  • Loading branch information
ldqk committed Dec 9, 2024
2 parents 9408ad3 + b099caf commit 3ad6f84
Showing 1 changed file with 71 additions and 16 deletions.
87 changes: 71 additions & 16 deletions Test/Masuit.Tools.Abstractions.Test/Tree/TreeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TreeTest
public void Can_BuildTree()
{
// arrange
var list = new List<MyClass>()
List<MyClass> list = new()
{
new MyClass
{
Expand All @@ -25,38 +25,38 @@ public void Can_BuildTree()
Id = 20000
}
};
for (int i = 2; i < 1500; i++)
for(int i = 2 ; i < 1500 ; i++)
{
list.Add(new MyClass
{
Name = $"这是第{i}个子节点",
Id = i,
ParentId = (i - 1)
ParentId = i - 1
});
}

for (int i = 20001; i < 40000; i++)
for(int i = 20001 ; i < 40000 ; i++)
{
list.Add(new MyClass
{
Name = $"这是第{i}个子节点",
Id = i,
ParentId = (i - 1)
ParentId = i - 1
});
}

// act
var tree = list.ToTree();
List<MyClass> tree = list.ToTree();

// assert
Assert.Equal(tree[0].Children.FirstOrDefault().Children.FirstOrDefault().Children.FirstOrDefault().Children.FirstOrDefault().Children.FirstOrDefault().Children.FirstOrDefault().Children.FirstOrDefault().Id, 8);
Assert.Equal(tree.Count, 2);
Assert.Equal(tree[0].AllChildren().Count, 1498);
var a = tree.Filter(c => c.Id == 39999).ToList();
List<MyClass> a = tree.Filter(c => c.Id == 39999).ToList();
Assert.Equal(a[0].Id, 39999);
var raw = tree.Flatten().ToList();
List<MyClass> raw = tree.Flatten().ToList();
Assert.Equal(raw.Count, list.Count);
var allParent = a[0].AllParent();
List<MyClass> allParent = a[0].AllParent();
Assert.Equal(allParent[0].AllChildren().Count, 19999);
Assert.Equal(a[0].Root(), list[1]);
Assert.StartsWith("Root", a[0].Path());
Expand All @@ -67,7 +67,7 @@ public void Can_BuildTree()
public void Can_BuildTree2()
{
// arrange
var list = new List<MyClass2>()
List<MyClass2> list = new()
{
new MyClass2
{
Expand All @@ -80,7 +80,7 @@ public void Can_BuildTree2()
Id = "20000"
}
};
for (int i = 2; i < 1500; i++)
for(int i = 2 ; i < 1500 ; i++)
{
list.Add(new MyClass2
{
Expand All @@ -90,7 +90,7 @@ public void Can_BuildTree2()
});
}

for (int i = 20001; i < 40000; i++)
for(int i = 20001 ; i < 40000 ; i++)
{
list.Add(new MyClass2
{
Expand All @@ -101,22 +101,70 @@ public void Can_BuildTree2()
}

// act
var tree = list.ToTree();
List<MyClass2> tree = list.ToTree();

// assert
Assert.Equal(tree[0].Children.FirstOrDefault().Children.FirstOrDefault().Children.FirstOrDefault().Children.FirstOrDefault().Children.FirstOrDefault().Children.FirstOrDefault().Children.FirstOrDefault().Id, "8");
Assert.Equal(tree.Count, 2);
Assert.Equal(tree[0].AllChildren().Count, 1498);
var a = tree.Filter(c => c.Id == "39999").ToList();
List<MyClass2> a = tree.Filter(c => c.Id == "39999").ToList();
Assert.Equal(a[0].Id, "39999");
var raw = tree.Flatten().ToList();
List<MyClass2> raw = tree.Flatten().ToList();
Assert.Equal(raw.Count, list.Count);
var allParent = a[0].AllParent();
List<MyClass2> allParent = a[0].AllParent();
Assert.Equal(allParent[0].AllChildren().Count, 19999);
Assert.Equal(a[0].Root(), list[1]);
Assert.StartsWith("Root", a[0].Path());
Assert.Equal(a[0].Level(), 20000);
}

[Fact]
public void Can_BuildTree3()
{
// 0-1-3
// -4-5
// -2
MyClass3 tree0 = new()
{
Id = 0,
ParentId = -1,
};
MyClass3 tree1 = new()
{
Id = 1,
ParentId = 0,
};
MyClass3 tree2 = new()
{
Id = 2,
ParentId = 0,
};
MyClass3 tree3 = new()
{
Id = 3,
ParentId = 1,
};
MyClass3 tree4 = new()
{
Id = 4,
ParentId = 1,
};
MyClass3 tree5 = new()
{
Id = 5,
ParentId = 4,
};

// 准备数据
List<MyClass3> list = new() { tree0, tree1, tree2, tree3, tree4, tree5 };

// 执行
List<Tree<MyClass3>> nodes = list.ToTreeGeneral(c => c.Id, c => c.ParentId, -1);

//验证
Assert.NotNull(nodes);
Assert.Equal(5, nodes.Flatten().Count()); // 错误,返回的节点数为 2
}
}

internal class MyClass : ITree<MyClass>, ITreeEntity<MyClass, int>
Expand Down Expand Up @@ -173,4 +221,11 @@ internal class MyClass2 : ITree<MyClass2>, ITreeEntity<MyClass2>
/// 父级id
/// </summary>
public string ParentId { get; set; }
}

internal class MyClass3
{
public long Id { get; set; }

public long ParentId { get; set; }
}

0 comments on commit 3ad6f84

Please sign in to comment.