|
| 1 | +import { TestRunner } from "./tester"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Ace Editor API Compatibility Tests |
| 5 | + * |
| 6 | + * These tests validate that the CodeMirror-based editor (from editorManager) |
| 7 | + * properly implements the Ace Editor API compatibility layer. |
| 8 | + */ |
| 9 | +export async function runAceCompatibilityTests(writeOutput) { |
| 10 | + const runner = new TestRunner("Ace API Compatibility"); |
| 11 | + |
| 12 | + function getEditor() { |
| 13 | + return editorManager?.editor; |
| 14 | + } |
| 15 | + |
| 16 | + async function createTestFile(text = "") { |
| 17 | + const EditorFile = acode.require("editorFile"); |
| 18 | + const file = new EditorFile("__ace_test__.txt", { |
| 19 | + text, |
| 20 | + render: true, |
| 21 | + }); |
| 22 | + await new Promise((r) => setTimeout(r, 100)); |
| 23 | + return file; |
| 24 | + } |
| 25 | + |
| 26 | + runner.test("editorManager.editor exists", (test) => { |
| 27 | + test.assert( |
| 28 | + typeof editorManager !== "undefined", |
| 29 | + "editorManager should exist", |
| 30 | + ); |
| 31 | + test.assert( |
| 32 | + editorManager.editor != null, |
| 33 | + "editorManager.editor should exist", |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + runner.test("editor.getValue()", (test) => { |
| 38 | + const editor = getEditor(); |
| 39 | + test.assert( |
| 40 | + typeof editor.getValue === "function", |
| 41 | + "getValue should be a function", |
| 42 | + ); |
| 43 | + const value = editor.getValue(); |
| 44 | + test.assert(typeof value === "string", "getValue should return string"); |
| 45 | + }); |
| 46 | + |
| 47 | + runner.test("editor.insert()", (test) => { |
| 48 | + const editor = getEditor(); |
| 49 | + test.assert( |
| 50 | + typeof editor.insert === "function", |
| 51 | + "insert should be a function", |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + runner.test("editor.getCursorPosition()", (test) => { |
| 56 | + const editor = getEditor(); |
| 57 | + test.assert( |
| 58 | + typeof editor.getCursorPosition === "function", |
| 59 | + "getCursorPosition should exist", |
| 60 | + ); |
| 61 | + const pos = editor.getCursorPosition(); |
| 62 | + test.assert(typeof pos.row === "number", "row should be number"); |
| 63 | + test.assert(typeof pos.column === "number", "column should be number"); |
| 64 | + }); |
| 65 | + |
| 66 | + runner.test("editor.gotoLine()", (test) => { |
| 67 | + const editor = getEditor(); |
| 68 | + test.assert( |
| 69 | + typeof editor.gotoLine === "function", |
| 70 | + "gotoLine should be a function", |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + runner.test("editor.moveCursorToPosition()", (test) => { |
| 75 | + const editor = getEditor(); |
| 76 | + test.assert( |
| 77 | + typeof editor.moveCursorToPosition === "function", |
| 78 | + "moveCursorToPosition should exist", |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + runner.test("editor.selection object", (test) => { |
| 83 | + const editor = getEditor(); |
| 84 | + test.assert(editor.selection != null, "selection should exist"); |
| 85 | + }); |
| 86 | + |
| 87 | + runner.test("editor.selection.getRange()", (test) => { |
| 88 | + const editor = getEditor(); |
| 89 | + test.assert( |
| 90 | + typeof editor.selection.getRange === "function", |
| 91 | + "getRange should be a function", |
| 92 | + ); |
| 93 | + const range = editor.selection.getRange(); |
| 94 | + test.assert(range.start != null, "range should have start"); |
| 95 | + test.assert(range.end != null, "range should have end"); |
| 96 | + }); |
| 97 | + |
| 98 | + runner.test("editor.selection.getCursor()", (test) => { |
| 99 | + const editor = getEditor(); |
| 100 | + test.assert( |
| 101 | + typeof editor.selection.getCursor === "function", |
| 102 | + "getCursor should be a function", |
| 103 | + ); |
| 104 | + const pos = editor.selection.getCursor(); |
| 105 | + test.assert(typeof pos.row === "number", "row should be number"); |
| 106 | + test.assert(typeof pos.column === "number", "column should be number"); |
| 107 | + }); |
| 108 | + |
| 109 | + runner.test("editor.getCopyText()", (test) => { |
| 110 | + const editor = getEditor(); |
| 111 | + test.assert( |
| 112 | + typeof editor.getCopyText === "function", |
| 113 | + "getCopyText should exist", |
| 114 | + ); |
| 115 | + const text = editor.getCopyText(); |
| 116 | + test.assert(typeof text === "string", "should return string"); |
| 117 | + }); |
| 118 | + |
| 119 | + runner.test("editor.session exists", async (test) => { |
| 120 | + const testFile = await createTestFile("test"); |
| 121 | + const editor = getEditor(); |
| 122 | + test.assert(editor.session != null, "session should exist"); |
| 123 | + testFile.remove(false); |
| 124 | + }); |
| 125 | + |
| 126 | + runner.test("editor.setTheme()", (test) => { |
| 127 | + const editor = getEditor(); |
| 128 | + test.assert( |
| 129 | + typeof editor.setTheme === "function", |
| 130 | + "setTheme should be a function", |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + runner.test("editor.commands object", (test) => { |
| 135 | + const editor = getEditor(); |
| 136 | + test.assert(editor.commands != null, "commands should exist"); |
| 137 | + }); |
| 138 | + |
| 139 | + runner.test("editor.commands.addCommand()", (test) => { |
| 140 | + const editor = getEditor(); |
| 141 | + test.assert( |
| 142 | + typeof editor.commands.addCommand === "function", |
| 143 | + "addCommand should be a function", |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + runner.test("editor.commands.removeCommand()", (test) => { |
| 148 | + const editor = getEditor(); |
| 149 | + test.assert( |
| 150 | + typeof editor.commands.removeCommand === "function", |
| 151 | + "removeCommand should exist", |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + runner.test("editor.commands.commands getter", (test) => { |
| 156 | + const editor = getEditor(); |
| 157 | + const cmds = editor.commands.commands; |
| 158 | + test.assert( |
| 159 | + typeof cmds === "object" && cmds !== null, |
| 160 | + "commands should return object", |
| 161 | + ); |
| 162 | + }); |
| 163 | + |
| 164 | + runner.test("editor.execCommand()", (test) => { |
| 165 | + const editor = getEditor(); |
| 166 | + test.assert( |
| 167 | + typeof editor.execCommand === "function", |
| 168 | + "execCommand should be a function", |
| 169 | + ); |
| 170 | + }); |
| 171 | + |
| 172 | + runner.test("editor.focus()", (test) => { |
| 173 | + const editor = getEditor(); |
| 174 | + test.assert( |
| 175 | + typeof editor.focus === "function", |
| 176 | + "focus should be a function", |
| 177 | + ); |
| 178 | + }); |
| 179 | + |
| 180 | + runner.test("editor.state (CodeMirror)", (test) => { |
| 181 | + const editor = getEditor(); |
| 182 | + test.assert(editor.state != null, "state should exist"); |
| 183 | + }); |
| 184 | + |
| 185 | + runner.test("editor.dispatch (CodeMirror)", (test) => { |
| 186 | + const editor = getEditor(); |
| 187 | + test.assert( |
| 188 | + typeof editor.dispatch === "function", |
| 189 | + "dispatch should be a function", |
| 190 | + ); |
| 191 | + }); |
| 192 | + |
| 193 | + runner.test("editor.contentDOM (CodeMirror)", (test) => { |
| 194 | + const editor = getEditor(); |
| 195 | + test.assert(editor.contentDOM != null, "contentDOM should exist"); |
| 196 | + }); |
| 197 | + |
| 198 | + // Session API tests |
| 199 | + |
| 200 | + runner.test("session.getValue()", async (test) => { |
| 201 | + const testFile = await createTestFile("test content"); |
| 202 | + const editor = getEditor(); |
| 203 | + test.assert( |
| 204 | + typeof editor.session.getValue === "function", |
| 205 | + "getValue should exist", |
| 206 | + ); |
| 207 | + const value = editor.session.getValue(); |
| 208 | + test.assert(typeof value === "string", "should return string"); |
| 209 | + test.assertEqual(value, "test content"); |
| 210 | + testFile.remove(false); |
| 211 | + }); |
| 212 | + |
| 213 | + runner.test("session.setValue()", async (test) => { |
| 214 | + const testFile = await createTestFile("original"); |
| 215 | + const editor = getEditor(); |
| 216 | + test.assert( |
| 217 | + typeof editor.session.setValue === "function", |
| 218 | + "setValue should exist", |
| 219 | + ); |
| 220 | + editor.session.setValue("modified"); |
| 221 | + test.assertEqual(editor.session.getValue(), "modified"); |
| 222 | + testFile.remove(false); |
| 223 | + }); |
| 224 | + |
| 225 | + runner.test("session.getLength()", async (test) => { |
| 226 | + const testFile = await createTestFile("line1\nline2\nline3"); |
| 227 | + const editor = getEditor(); |
| 228 | + test.assert( |
| 229 | + typeof editor.session.getLength === "function", |
| 230 | + "getLength should exist", |
| 231 | + ); |
| 232 | + const len = editor.session.getLength(); |
| 233 | + test.assert(typeof len === "number", "should return number"); |
| 234 | + test.assertEqual(len, 3); |
| 235 | + testFile.remove(false); |
| 236 | + }); |
| 237 | + |
| 238 | + runner.test("session.getLine()", async (test) => { |
| 239 | + const testFile = await createTestFile("first\nsecond\nthird"); |
| 240 | + const editor = getEditor(); |
| 241 | + test.assert( |
| 242 | + typeof editor.session.getLine === "function", |
| 243 | + "getLine should exist", |
| 244 | + ); |
| 245 | + test.assertEqual(editor.session.getLine(0), "first"); |
| 246 | + test.assertEqual(editor.session.getLine(1), "second"); |
| 247 | + test.assertEqual(editor.session.getLine(2), "third"); |
| 248 | + testFile.remove(false); |
| 249 | + }); |
| 250 | + |
| 251 | + return await runner.run(writeOutput); |
| 252 | +} |
0 commit comments