Skip to content

Commit

Permalink
added GroupUnDo.MergeDescriptionAction property
Browse files Browse the repository at this point in the history
  • Loading branch information
Doraku committed Oct 3, 2021
1 parent 14bcbc1 commit cfd1d1c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions documentation/NEXT_RELEASENOTES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
added ValueUnDo.MergeInterval property
added ValueUnDo.MergeDescriptionAction property
added GroupUnDo.MergeDescriptionAction property

---

Expand Down
36 changes: 36 additions & 0 deletions source/DefaultUnDo.Test/GroupUnDoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,42 @@ public void TryMerge_Should_return_true_When_merged()
Check.That(newUndo).IsEqualTo(undo);
}

[Fact]
public void TryMerge_Should_use_group_description()
{
IMergeableUnDo mergeable = Substitute.For<IMergeableUnDo>();
IUnDo undo = Substitute.For<IUnDo>();
IMergeableUnDo group = new GroupUnDo("test", mergeable);
undo.Description.Returns("test1");
mergeable.TryMerge(undo, out Arg.Any<IUnDo>()).Returns(x =>
{
x[1] = undo;
return true;
});
Check.That(group.TryMerge(undo, out IUnDo merged)).IsTrue();
Check.That(merged.Description).IsEqualTo(group.Description);
}

[Fact]
public void TryMerge_Should_use_MergeDescriptionAction_When_set()
{
IMergeableUnDo mergeable = Substitute.For<IMergeableUnDo>();
IUnDo undo = Substitute.For<IUnDo>();
IMergeableUnDo group = new GroupUnDo("test", mergeable);
undo.Description.Returns("test");
mergeable.TryMerge(undo, out Arg.Any<IUnDo>()).Returns(x =>
{
x[1] = undo;
return true;
});
GroupUnDo.MergeDescriptionAction = (_, _, _) => "kikoo";

Check.That(group.TryMerge(undo, out IUnDo merged)).IsTrue();
Check.That(merged.Description).IsEqualTo("kikoo");

GroupUnDo.MergeDescriptionAction = null;
}

#endregion
}
}
2 changes: 1 addition & 1 deletion source/DefaultUnDo.Test/ValueUnDoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void TryMerge_Should_use_non_generic_MergeDescriptionAction_When_Set()
}

[Fact]
public void TryMerge_Should_use_new_description_When_Set()
public void TryMerge_Should_use_new_description()
{
int item = 0;
void setter(int v) => item = v;
Expand Down
22 changes: 21 additions & 1 deletion source/DefaultUnDo/GroupUnDo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,31 @@ namespace DefaultUnDo
/// </summary>
public sealed class GroupUnDo : IMergeableUnDo
{
/// <summary>
/// Represents a method that will be called when merging a <see cref="GroupUnDo"/> and a <see cref="IMergeableUnDo"/> instances to get the resulting description.
/// </summary>
/// <param name="oldDescription">The description of the previous <see cref="GroupUnDo"/> merged.</param>
/// <param name="newDescription">The description of the new <see cref="IMergeableUnDo"/> merged.</param>
/// <param name="mergedDescription">The description of the new resulting <see cref="IMergeableUnDo"/> merged.</param>
/// <returns></returns>
public delegate object MergeDescriptionHandler(object oldDescription, object newDescription, object mergedDescription);

#region Fields

private readonly object _description;
private readonly IUnDo[] _commands;

#endregion

#region Properties

/// <summary>
/// The <see cref="MergeDescriptionHandler"/> used to merge description between a <see cref="GroupUnDo"/> and a <see cref="IMergeableUnDo"/> instances.
/// </summary>
public static MergeDescriptionHandler MergeDescriptionAction { get; set; }

#endregion

#region Initialisation

/// <summary>
Expand Down Expand Up @@ -79,7 +97,9 @@ bool IMergeableUnDo.TryMerge(IUnDo other, out IUnDo mergedCommand)
{
mergedCommand =
TryGetSingle(out IMergeableUnDo mergeable) && mergeable.TryMerge(other, out mergedCommand)
? new GroupUnDo(_description, mergedCommand)
? new GroupUnDo(
MergeDescriptionAction?.Invoke(_description, mergeable.Description, mergedCommand.Description) ?? _description,
mergedCommand)
: null;

return mergedCommand != null;
Expand Down
4 changes: 2 additions & 2 deletions source/DefaultUnDo/ValueUnDo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace DefaultUnDo
public static class ValueUnDo
{
/// <summary>
/// Represents a method that will be called when merging two <see cref="ValueUnDo{T}"/> instance to get the resulting description.
/// Represents a method that will be called when merging two <see cref="ValueUnDo{T}"/> instances to get the resulting description.
/// </summary>
/// <param name="oldDescription">The description of the previous <see cref="ValueUnDo{T}"/> merged.</param>
/// <param name="newDescription">The description of the new <see cref="ValueUnDo{T}"/> merged.</param>
Expand All @@ -35,7 +35,7 @@ public static class ValueUnDo
public sealed class ValueUnDo<T> : IMergeableUnDo
{
/// <summary>
/// Represents a method that will be called when merging two <see cref="ValueUnDo{T}"/> instance to get the resulting description.
/// Represents a method that will be called when merging two <see cref="ValueUnDo{T}"/> instances to get the resulting description.
/// </summary>
/// <param name="oldDescription">The description of the previous <see cref="ValueUnDo{T}"/> merged.</param>
/// <param name="oldValue">The old value used when undoing the resulting merged <see cref="ValueUnDo{T}"/>.</param>
Expand Down

0 comments on commit cfd1d1c

Please sign in to comment.