Skip to content

Commit

Permalink
✨ ChangeFormat fluent style
Browse files Browse the repository at this point in the history
  • Loading branch information
pleonex committed Nov 24, 2023
1 parent 38e4d7b commit d662676
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Yarhl.UnitTests/FileSystem/NodeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ public void ChangeFormat()
var dummyFormat1 = new StringFormat("3");
var dummyFormat2 = new IntFormat(4);
using var node = new Node("mytest", dummyFormat1);
node.ChangeFormat(dummyFormat2);
Node output = node.ChangeFormat(dummyFormat2);
Assert.AreNotSame(node.Format, dummyFormat1);
Assert.AreSame(node.Format, dummyFormat2);
Assert.AreSame(node, output);
}

[Test]
Expand Down
7 changes: 5 additions & 2 deletions src/Yarhl/FileSystem/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public IFormat? Format {
/// <summary>
/// Change the format of the current node.
/// </summary>
/// <returns>This node.</returns>
/// <remarks>
/// <para>If the previous format was a container, this method will
/// remove the children of the node.
Expand All @@ -137,14 +138,14 @@ public IFormat? Format {
/// If <see langword="true" /> the method will dispose the previous
/// format.
/// </param>
public void ChangeFormat(IFormat? newFormat, bool disposePreviousFormat = true)
public Node ChangeFormat(IFormat? newFormat, bool disposePreviousFormat = true)
{
if (Disposed) {
throw new ObjectDisposedException(nameof(Node));
}

if (newFormat == Format) {
return;
return this;
}

// If it was a container, clean children
Expand All @@ -162,6 +163,8 @@ public void ChangeFormat(IFormat? newFormat, bool disposePreviousFormat = true)
if (IsContainer) {
AddContainerChildren();
}

return this;
}

/// <summary>
Expand Down

0 comments on commit d662676

Please sign in to comment.