Skip to content
Merged
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
80 changes: 80 additions & 0 deletions Promete.Test/NodeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using FluentAssertions;
using Promete.Nodes;

namespace Promete.Test;

public class NodeTests
{
[Fact]
public void AddSelfAsChild_ShouldThrowArgumentException()
{
// Arrange
var container = new Container();

// Act & Assert
var act = () => container.Add(container);
act.Should().Throw<ArgumentException>()
.WithMessage("*自分自身*");
}

[Fact]
public void InsertSelfAsChild_ShouldThrowArgumentException()
{
// Arrange
var container = new Container();

// Act & Assert
var act = () => container.Insert(0, container);
act.Should().Throw<ArgumentException>()
.WithMessage("*自分自身*");
}

[Fact]
public void AddDifferentNode_ShouldSucceed()
{
// Arrange
var container = new Container();
var child = new Container();

// Act
container.Add(child);

// Assert
container.Count.Should().Be(1);
child.Parent.Should().Be(container);
}

[Fact]
public void InsertDifferentNode_ShouldSucceed()
{
// Arrange
var container = new Container();
var child = new Container();

// Act
container.Insert(0, child);

// Assert
container.Count.Should().Be(1);
child.Parent.Should().Be(container);
}

[Fact]
public void InsertNodeWithExistingParent_ShouldMoveNode()
{
// Arrange
var oldParent = new Container();
var newParent = new Container();
var child = new Container();

oldParent.Add(child);

// Act
newParent.Insert(0, child);

// Assert
oldParent.Count.Should().Be(0);
newParent.Count.Should().Be(1);
child.Parent.Should().Be(newParent);
}
}
12 changes: 11 additions & 1 deletion Promete/Nodes/ContainableNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ protected void SortChildrenIfNeeded()

protected void Add(Node node)
{
if (node == this)
{
throw new ArgumentException("ノードの子要素に自分自身を追加することはできません。", nameof(node));
}

node.Parent?.Remove(node);

node.Parent = this;
Expand All @@ -92,7 +97,12 @@ protected void Clear()

protected void Insert(int index, Node node)
{
node.Parent?.Remove(this);
if (node == this)
{
throw new ArgumentException("ノードの子要素に自分自身を追加することはできません。", nameof(node));
}

node.Parent?.Remove(node);

children.Insert(index, node);
node.Parent = this;
Expand Down
Loading