|
| 1 | +import assert from "node:assert"; |
| 2 | +import { UndoStack } from "./UndoStack"; |
| 3 | + |
| 4 | +suite("UndoStack", () => { |
| 5 | + test("should undo and redo", () => { |
| 6 | + const undoStack = new UndoStack<string>(3); |
| 7 | + undoStack.push("a"); |
| 8 | + undoStack.push("b"); |
| 9 | + undoStack.push("c"); |
| 10 | + assert.strictEqual(undoStack.undo(), "b"); |
| 11 | + assert.strictEqual(undoStack.undo(), "a"); |
| 12 | + assert.strictEqual(undoStack.undo(), undefined); |
| 13 | + assert.strictEqual(undoStack.redo(), "b"); |
| 14 | + assert.strictEqual(undoStack.redo(), "c"); |
| 15 | + assert.strictEqual(undoStack.redo(), undefined); |
| 16 | + }); |
| 17 | + |
| 18 | + test("should clobber stack if push after undo", () => { |
| 19 | + const undoStack = new UndoStack<string>(3); |
| 20 | + undoStack.push("a"); |
| 21 | + undoStack.push("b"); |
| 22 | + undoStack.push("c"); |
| 23 | + assert.strictEqual(undoStack.undo(), "b"); |
| 24 | + undoStack.push("d"); |
| 25 | + assert.strictEqual(undoStack.undo(), "b"); |
| 26 | + assert.strictEqual(undoStack.redo(), "d"); |
| 27 | + assert.strictEqual(undoStack.redo(), undefined); |
| 28 | + }); |
| 29 | + |
| 30 | + test("should truncate history if max lenght exceeded", () => { |
| 31 | + const undoStack = new UndoStack<string>(3); |
| 32 | + undoStack.push("a"); |
| 33 | + undoStack.push("b"); |
| 34 | + undoStack.push("c"); |
| 35 | + undoStack.push("d"); |
| 36 | + assert.strictEqual(undoStack.undo(), "c"); |
| 37 | + assert.strictEqual(undoStack.undo(), "b"); |
| 38 | + assert.strictEqual(undoStack.undo(), undefined); |
| 39 | + }); |
| 40 | + |
| 41 | + test("should handle empty undo and redo", () => { |
| 42 | + const undoStack = new UndoStack<string>(3); |
| 43 | + assert.strictEqual(undoStack.undo(), undefined); |
| 44 | + assert.strictEqual(undoStack.redo(), undefined); |
| 45 | + }); |
| 46 | + |
| 47 | + test("should handle redo at end of stack", () => { |
| 48 | + const undoStack = new UndoStack<string>(3); |
| 49 | + undoStack.push("a"); |
| 50 | + assert.strictEqual(undoStack.redo(), undefined); |
| 51 | + }); |
| 52 | +}); |
0 commit comments