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