1
0

test: finish tas op test

This commit is contained in:
2026-04-01 13:16:05 +08:00
parent c74e22bff0
commit 5f10338d33
2 changed files with 39 additions and 26 deletions

View File

@@ -385,25 +385,19 @@ namespace BallanceTasEditor.Backend {
} }
} }
public enum InsertFrameOperationPosition { public enum InsertFrameOperationKind {
Before, After Before, After
} }
public enum InsertFrameOperationMode {
Insert, Overwrite
}
public class InsertFrameOperation : ITasRevocableOperation { public class InsertFrameOperation : ITasRevocableOperation {
public InsertFrameOperation(InsertFrameOperationPosition pos, InsertFrameOperationMode mode, int index, IExactSizeEnumerable<TasFrame> frames) { public InsertFrameOperation(InsertFrameOperationKind kind, int index, IExactSizeEnumerable<TasFrame> frames) {
m_Position = pos; m_Kind = kind;
m_Mode = mode;
m_Index = index; m_Index = index;
m_InsertedFrames = frames.Select((frame) => frame.ToRaw()).ToArray(); m_InsertedFrames = frames.Select((frame) => frame.ToRaw()).ToArray();
m_IsExecuted = false; m_IsExecuted = false;
} }
private InsertFrameOperationPosition m_Position; private InsertFrameOperationKind m_Kind;
private InsertFrameOperationMode m_Mode;
private int m_Index; private int m_Index;
private RawTasFrame[] m_InsertedFrames; private RawTasFrame[] m_InsertedFrames;
private bool m_IsExecuted; private bool m_IsExecuted;
@@ -420,11 +414,11 @@ namespace BallanceTasEditor.Backend {
// Check arguments // Check arguments
// If we insert before some frame, the valid index can be [0, count], // If we insert before some frame, the valid index can be [0, count],
// however, if we insert after some frame, the valid index is [0, count), // however, if we insert after some frame, the valid index is [0, count),
switch (m_Position) { switch (m_Kind) {
case InsertFrameOperationPosition.Before: case InsertFrameOperationKind.Before:
ArgumentOutOfRangeException.ThrowIfGreaterThan(m_Index, seq.GetCount()); ArgumentOutOfRangeException.ThrowIfGreaterThan(m_Index, seq.GetCount());
break; break;
case InsertFrameOperationPosition.After: case InsertFrameOperationKind.After:
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(m_Index, seq.GetCount()); ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(m_Index, seq.GetCount());
break; break;
default: default:
@@ -438,9 +432,9 @@ namespace BallanceTasEditor.Backend {
var iter = m_InsertedFrames.Select((frame) => TasFrame.FromRaw(frame)); var iter = m_InsertedFrames.Select((frame) => TasFrame.FromRaw(frame));
var exactSizedIter = new ExactSizeEnumerableAdapter<TasFrame>(iter, count); var exactSizedIter = new ExactSizeEnumerableAdapter<TasFrame>(iter, count);
// Compute the insert index // Compute the insert index
var index = m_Position switch { var index = m_Kind switch {
InsertFrameOperationPosition.Before => m_Index, InsertFrameOperationKind.Before => m_Index,
InsertFrameOperationPosition.After => m_Index + 1, InsertFrameOperationKind.After => m_Index + 1,
_ => throw new UnreachableException("Unknown InsertFrameOperationKind"), _ => throw new UnreachableException("Unknown InsertFrameOperationKind"),
}; };
// Execute inserting. // Execute inserting.
@@ -460,9 +454,9 @@ namespace BallanceTasEditor.Backend {
var count = m_InsertedFrames.Length; var count = m_InsertedFrames.Length;
if (count != 0) { if (count != 0) {
// Compute the index for removing // Compute the index for removing
var index = m_Position switch { var index = m_Kind switch {
InsertFrameOperationPosition.Before => m_Index, InsertFrameOperationKind.Before => m_Index,
InsertFrameOperationPosition.After => m_Index + 1, InsertFrameOperationKind.After => m_Index + 1,
_ => throw new UnreachableException("Unknown InsertFrameOperationKind"), _ => throw new UnreachableException("Unknown InsertFrameOperationKind"),
}; };
// Execute removing. // Execute removing.

View File

@@ -321,18 +321,37 @@ namespace BallanceTasEditorTests.Backend {
public required string Inserted { get; init; } public required string Inserted { get; init; }
public required string Expected { get; init; } public required string Expected { get; init; }
public required InsertFrameOperationPosition Position { get; init; } public required InsertFrameOperationKind Kind { get; init; }
public required InsertFrameOperationMode Mode { get; init; }
public required int Index { get; init; } public required int Index { get; init; }
} }
private static IEnumerable<InsertFrameOperationTestPayload> GetInsertFrameOperationTestPayload() { private static IEnumerable<InsertFrameOperationTestPayload> GetInsertFrameOperationTestPayload() {
yield return new InsertFrameOperationTestPayload { yield return new InsertFrameOperationTestPayload {
Source = "1,1;1,2;1,3;1,4;1,5", Source = "1,1;1,2;1,3;1,4;1,5",
Inserted = "1,6;1,7;1,8;1,9;1,10", Inserted = "1,6;1,7;1,8",
Expected = "1,1;1,2;2,1;2,2;1,3;1,4;1,5", Expected = "1,1;1,2;1,6;1,7;1,8;1,3;1,4;1,5",
Position = InsertFrameOperationPosition.After, Kind = InsertFrameOperationKind.Before,
Mode = InsertFrameOperationMode.Overwrite, Index = 2
};
yield return new InsertFrameOperationTestPayload {
Source = "1,1;1,2;1,3;1,4;1,5",
Inserted = "1,6;1,7;1,8",
Expected = "1,1;1,2;1,3;1,6;1,7;1,8;1,4;1,5",
Kind = InsertFrameOperationKind.After,
Index = 2
};
yield return new InsertFrameOperationTestPayload {
Source = "1,1;1,2;1,3;1,4;1,5",
Inserted = "1,6;1,7",
Expected = "1,1;1,2;1,3;1,4;1,5;1,6;1,7",
Kind = InsertFrameOperationKind.After,
Index = 4
};
yield return new InsertFrameOperationTestPayload {
Source = "1,1;1,2;1,3;1,4;1,5",
Inserted = "1,6;1,7",
Expected = "1,6;1,7;1,1;1,2;1,3;1,4;1,5",
Kind = InsertFrameOperationKind.Before,
Index = 0 Index = 0
}; };
} }
@@ -358,7 +377,7 @@ namespace BallanceTasEditorTests.Backend {
sequence.Clear(); sequence.Clear();
// Now we can test it // Now we can test it
var op = new InsertFrameOperation(payload.Position, payload.Mode, payload.Index, insertedIter); var op = new InsertFrameOperation(payload.Kind, payload.Index, insertedIter);
AssertRevocableOperation(sequence, op, payload.Source, payload.Expected); AssertRevocableOperation(sequence, op, payload.Source, payload.Expected);
} }