|
| 1 | +import { TestRunner } from "./tester"; |
| 2 | + |
| 3 | +export async function runAceEditorTests(writeOutput) { |
| 4 | + const runner = new TestRunner("Ace Editor API Tests"); |
| 5 | + |
| 6 | + function createEditor() { |
| 7 | + const container = document.createElement("div"); |
| 8 | + container.style.width = "500px"; |
| 9 | + container.style.height = "300px"; |
| 10 | + container.style.backgroundColor = "#a02f2f"; |
| 11 | + document.body.appendChild(container); |
| 12 | + |
| 13 | + const editor = ace.edit(container); |
| 14 | + return { editor, container }; |
| 15 | + } |
| 16 | + |
| 17 | + async function withEditor(test, fn) { |
| 18 | + let editor, container; |
| 19 | + |
| 20 | + try { |
| 21 | + ({ editor, container } = createEditor()); |
| 22 | + test.assert(editor != null, "Editor instance should be created"); |
| 23 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 24 | + await fn(editor); |
| 25 | + await new Promise((resolve) => setTimeout(resolve, 200)); |
| 26 | + } finally { |
| 27 | + if (editor) editor.destroy(); |
| 28 | + if (container) container.remove(); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Test 1: Ace is available |
| 33 | + runner.test("Ace is loaded", async (test) => { |
| 34 | + test.assert(typeof ace !== "undefined", "Ace should be available globally"); |
| 35 | + test.assert( |
| 36 | + typeof ace.edit === "function", |
| 37 | + "ace.edit should be a function", |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + // Test 2: Editor creation |
| 42 | + runner.test("Editor creation", async (test) => { |
| 43 | + const { editor, container } = createEditor(); |
| 44 | + test.assert(editor != null, "Editor instance should be created"); |
| 45 | + test.assert( |
| 46 | + typeof editor.getSession === "function", |
| 47 | + "Editor should expose getSession", |
| 48 | + ); |
| 49 | + editor.destroy(); |
| 50 | + container.remove(); |
| 51 | + }); |
| 52 | + |
| 53 | + // Test 3: Session access |
| 54 | + runner.test("Session access", async (test) => { |
| 55 | + await withEditor(test, async (editor) => { |
| 56 | + const session = editor.getSession(); |
| 57 | + test.assert(session != null, "Editor session should exist"); |
| 58 | + test.assert( |
| 59 | + typeof session.getValue === "function", |
| 60 | + "Session should expose getValue", |
| 61 | + ); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + // Test 4: Set and get value |
| 66 | + runner.test("Set and get value", async (test) => { |
| 67 | + await withEditor(test, async (editor) => { |
| 68 | + const text = "Hello Ace Editor"; |
| 69 | + editor.setValue(text, -1); |
| 70 | + test.assertEqual(editor.getValue(), text); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + // Test 5: Cursor movement |
| 75 | + runner.test("Cursor movement", async (test) => { |
| 76 | + await withEditor(test, async (editor) => { |
| 77 | + editor.setValue("line1\nline2\nline3", -1); |
| 78 | + editor.moveCursorTo(1, 2); |
| 79 | + |
| 80 | + const pos = editor.getCursorPosition(); |
| 81 | + test.assertEqual(pos.row, 1); |
| 82 | + test.assertEqual(pos.column, 2); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + // Test 6: Selection API |
| 87 | + runner.test("Selection handling", async (test) => { |
| 88 | + await withEditor(test, async (editor) => { |
| 89 | + editor.setValue("abc\ndef", -1); |
| 90 | + editor.selectAll(); |
| 91 | + test.assert(editor.getSelectedText().length > 0); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + // Test 7: Undo manager |
| 96 | + runner.test("Undo manager works", async (test) => { |
| 97 | + await withEditor(test, async (editor) => { |
| 98 | + const session = editor.getSession(); |
| 99 | + const undoManager = session.getUndoManager(); |
| 100 | + |
| 101 | + session.setValue("one"); |
| 102 | + undoManager.reset(); |
| 103 | + |
| 104 | + editor.insert("\ntwo"); |
| 105 | + editor.undo(); |
| 106 | + |
| 107 | + test.assertEqual(editor.getValue(), "one"); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + // Test 8: Mode setting |
| 112 | + runner.test("Mode setting", async (test) => { |
| 113 | + await withEditor(test, async (editor) => { |
| 114 | + const session = editor.getSession(); |
| 115 | + session.setMode("ace/mode/javascript"); |
| 116 | + |
| 117 | + const mode = session.getMode(); |
| 118 | + test.assert(mode && mode.$id === "ace/mode/javascript"); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + // Test 9: Theme setting |
| 123 | + runner.test("Theme setting", async (test) => { |
| 124 | + await withEditor(test, async (editor) => { |
| 125 | + editor.setTheme("ace/theme/monokai"); |
| 126 | + test.assert(editor.getTheme().includes("monokai")); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + // Test 11: Line count |
| 131 | + runner.test("Line count", async (test) => { |
| 132 | + await withEditor(test, async (editor) => { |
| 133 | + editor.setValue("a\nb\nc\nd", -1); |
| 134 | + test.assertEqual(editor.session.getLength(), 4); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + // Test 12: Replace text |
| 139 | + runner.test("Replace text", async (test) => { |
| 140 | + await withEditor(test, async (editor) => { |
| 141 | + editor.setValue("hello world", -1); |
| 142 | + editor.find("world"); |
| 143 | + editor.replace("ace"); |
| 144 | + |
| 145 | + test.assertEqual(editor.getValue(), "hello ace"); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + // Test 13: Search API |
| 150 | + runner.test("Search API", async (test) => { |
| 151 | + await withEditor(test, async (editor) => { |
| 152 | + editor.setValue("foo bar foo", -1); |
| 153 | + editor.find("foo"); |
| 154 | + |
| 155 | + const range = editor.getSelectionRange(); |
| 156 | + test.assert(range.start.column === 0); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + // Test 14: Renderer availability |
| 161 | + runner.test("Renderer exists", async (test) => { |
| 162 | + await withEditor(test, async (editor) => { |
| 163 | + const renderer = editor.renderer; |
| 164 | + test.assert(renderer != null); |
| 165 | + test.assert(typeof renderer.updateFull === "function"); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + // Test 15: Editor options |
| 170 | + runner.test("Editor options", async (test) => { |
| 171 | + await withEditor(test, async (editor) => { |
| 172 | + editor.setOption("showPrintMargin", false); |
| 173 | + test.assertEqual(editor.getOption("showPrintMargin"), false); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + // Test 16: Scroll API |
| 178 | + runner.test("Scroll API", async (test) => { |
| 179 | + await withEditor(test, async (editor) => { |
| 180 | + editor.setValue(Array(100).fill("line").join("\n"), -1); |
| 181 | + editor.scrollToLine(50, true, true, () => {}); |
| 182 | + |
| 183 | + const firstVisibleRow = editor.renderer.getFirstVisibleRow(); |
| 184 | + test.assert(firstVisibleRow >= 0); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + // Test 17: Redo manager |
| 189 | + runner.test("Redo manager works", async (test) => { |
| 190 | + await withEditor(test, async (editor) => { |
| 191 | + const session = editor.getSession(); |
| 192 | + const undoManager = session.getUndoManager(); |
| 193 | + |
| 194 | + session.setValue("one"); |
| 195 | + undoManager.reset(); |
| 196 | + |
| 197 | + session.insert({ row: 0, column: 3 }, "\ntwo"); |
| 198 | + editor.undo(); |
| 199 | + editor.redo(); |
| 200 | + |
| 201 | + test.assertEqual(editor.getValue(), "one\ntwo"); |
| 202 | + }); |
| 203 | + }); |
| 204 | + |
| 205 | + // Test 18: Focus and blur |
| 206 | + runner.test("Focus and blur", async (test) => { |
| 207 | + await withEditor(test, async (editor) => { |
| 208 | + editor.focus(); |
| 209 | + test.assert(editor.isFocused()); |
| 210 | + |
| 211 | + editor.blur(); |
| 212 | + test.assert(!editor.isFocused()); |
| 213 | + }); |
| 214 | + }); |
| 215 | + |
| 216 | + return await runner.run(writeOutput); |
| 217 | +} |
0 commit comments