ファイル

72 行
2.5 KiB
C#

using YukkuriMovieMaker.UndoRedo;
namespace UndoTree.Tests;
public sealed class UndoTreeCodecTests
{
[Fact]
public void PlainValuesRoundTripThroughTheirDeclaredType()
{
Assert.Equal("5", UndoTreeCodec.Encode(5, typeof(int)));
Assert.True(UndoTreeCodec.TryDecode("5", typeof(int), out var number));
Assert.Equal(5, number);
Assert.True(UndoTreeCodec.TryDecode(UndoTreeCodec.Encode(2.5, typeof(double))!, typeof(double), out var real));
Assert.Equal(2.5, real);
Assert.True(UndoTreeCodec.TryDecode(UndoTreeCodec.Encode(Shade.Light, typeof(Shade))!, typeof(Shade), out var shade));
Assert.Equal(Shade.Light, shade);
Assert.True(UndoTreeCodec.TryDecode(UndoTreeCodec.Encode("a\nb", typeof(string))!, typeof(string), out var text));
Assert.Equal("a\nb", text);
}
[Fact]
public void ANullStringAndANullObjectRoundTrip()
{
Assert.True(UndoTreeCodec.TryDecode(UndoTreeCodec.Encode(null, typeof(string))!, typeof(string), out var text));
Assert.Null(text);
Assert.True(UndoTreeCodec.TryDecode(UndoTreeCodec.Encode(null, typeof(Part))!, typeof(Part), out var part));
Assert.Null(part);
}
[Fact]
public void AnObjectKeepsItsRuntimeTypeEvenWhenDeclaredAsAnInterface()
{
var original = new Part("knob") { Value = 3 };
var text = UndoTreeCodec.Encode(original, typeof(IUndoRedoable))!;
Assert.True(UndoTreeCodec.TryDecode(text, typeof(IUndoRedoable), out var decoded));
var part = Assert.IsType<Part>(decoded);
Assert.NotSame(original, part);
Assert.Equal("knob", part.Label);
Assert.Equal(3, part.Value);
}
[Fact]
public void ATextThatIsNotJsonOrDoesNotMatchTheTypeIsRejected()
{
Assert.False(UndoTreeCodec.TryDecode("not json", typeof(int), out _));
Assert.False(UndoTreeCodec.TryDecode("\"word\"", typeof(int), out _));
Assert.False(UndoTreeCodec.TryDecode("[]", typeof(Part), out _));
}
[Fact]
public void AValueThatCannotBeReadBackIsNotKeptForReplay()
{
var poisoned = new Poisoned();
Assert.NotNull(UndoTreeCodec.Encode(poisoned, typeof(Poisoned)));
Assert.Null(UndoTreeCodec.EncodeReplayable(poisoned, typeof(Poisoned)));
Assert.Equal("5", UndoTreeCodec.EncodeReplayable(5, typeof(int)));
}
[Fact]
public void AValueTooLargeToKeepInARecordIsRefused()
{
Assert.Null(UndoTreeCodec.Encode(new string('a', 1_500_000), typeof(string)));
}
}