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
30 changes: 30 additions & 0 deletions UndoRedo.Test/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,36 @@
Assert.AreEqual(stack.HasUnsavedChanges, newStack.HasUnsavedChanges);
}

[TestMethod]
public async Task UndoRedoService_SaveLoadState_PreservesCommandMetadata()
{
// Arrange
UndoRedoService stack = CreateService();
stack.SetSerializer(new JsonUndoRedoSerializer());

Dictionary<string, object> customData = new() { ["author"] = "alice" };
stack.Execute(new DelegateCommand("Big edit", () => { }, () => { }, ChangeType.Insert, ["doc"], size: 42, customData: customData));
ChangeMetadata original = stack.Commands[0].Metadata;

// Act
byte[] data = await stack.SaveStateAsync().ConfigureAwait(false);

Check warning on line 156 in UndoRedo.Test/SerializationTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_UndoRedo&issues=AaDbmO84s4mYgcx97wdT&open=AaDbmO84s4mYgcx97wdT&pullRequest=80

UndoRedoService newStack = CreateService();
newStack.SetSerializer(new JsonUndoRedoSerializer());
bool success = await newStack.LoadStateAsync(data).ConfigureAwait(false);

Check warning on line 160 in UndoRedo.Test/SerializationTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_UndoRedo&issues=AaDbmO84s4mYgcx97wdU&open=AaDbmO84s4mYgcx97wdU&pullRequest=80

// Assert
Assert.IsTrue(success);
ChangeMetadata loaded = newStack.Commands[0].Metadata;
Assert.AreEqual(original.Timestamp, loaded.Timestamp, "The timestamp must be when the change was made, not when it was loaded");
Assert.AreEqual(42, loaded.Size);
Assert.AreEqual(ChangeType.Insert, loaded.ChangeType);
Assert.HasCount(1, loaded.AffectedItems);
Assert.AreEqual("doc", loaded.AffectedItems[0]);
Assert.IsNotNull(loaded.CustomData, "CustomData must survive a save and load");
Assert.AreEqual("alice", loaded.CustomData["author"].ToString());
}

[TestMethod]
public async Task UndoRedoService_NoSerializer_ThrowsInvalidOperationException()
{
Expand Down
3 changes: 3 additions & 0 deletions UndoRedo/Services/JsonUndoRedoSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
ex);
}

instance?.DeserializeData(serializableCommand.Data!);

Check warning on line 127 in UndoRedo/Services/JsonUndoRedoSerializer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this null-forgiving operator; the compiler already knows this expression is not null here.

Check warning on line 127 in UndoRedo/Services/JsonUndoRedoSerializer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this null-forgiving operator; the compiler already knows this expression is not null here.

Check warning on line 127 in UndoRedo/Services/JsonUndoRedoSerializer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this null-forgiving operator; the compiler already knows this expression is not null here.

Check warning on line 127 in UndoRedo/Services/JsonUndoRedoSerializer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this null-forgiving operator; the compiler already knows this expression is not null here.

Check warning on line 127 in UndoRedo/Services/JsonUndoRedoSerializer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this null-forgiving operator; the compiler already knows this expression is not null here.

Check warning on line 127 in UndoRedo/Services/JsonUndoRedoSerializer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this null-forgiving operator; the compiler already knows this expression is not null here.

Check warning on line 127 in UndoRedo/Services/JsonUndoRedoSerializer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this null-forgiving operator; the compiler already knows this expression is not null here.

Check warning on line 127 in UndoRedo/Services/JsonUndoRedoSerializer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this null-forgiving operator; the compiler already knows this expression is not null here.

Check warning on line 127 in UndoRedo/Services/JsonUndoRedoSerializer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this null-forgiving operator; the compiler already knows this expression is not null here.

Check warning on line 127 in UndoRedo/Services/JsonUndoRedoSerializer.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this null-forgiving operator; the compiler already knows this expression is not null here.
return (ICommand)instance!;
}

Expand Down Expand Up @@ -187,6 +187,9 @@
{
public override string Description { get; } = $"[Placeholder] {description}";

// Keep the deserialized metadata, rather than the fresh timestamp, size and custom data BaseCommand builds
public override ChangeMetadata Metadata { get; protected set; } = Ensure.NotNull(metadata);

public override void Execute() => throw new NotSupportedException("Placeholder commands cannot be executed");

public override void Undo() => throw new NotSupportedException("Placeholder commands cannot be undone");
Expand Down
Loading