browser_editor_goto_line.js (2891B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 function testJumpToLine(ed, inputLine, expectCursor) { 7 ed.jumpToLine(); 8 const editorDoc = ed.container.contentDocument; 9 const lineInput = editorDoc.querySelector("input"); 10 lineInput.value = inputLine; 11 EventUtils.synthesizeKey("VK_RETURN", {}, editorDoc.defaultView); 12 // CodeMirror lines and columns are 0-based. 13 ch( 14 ed.getCursor(), 15 expectCursor, 16 "jumpToLine " + inputLine + " expects cursor " + expectCursor.toSource() 17 ); 18 } 19 20 async function test() { 21 waitForExplicitFinish(); 22 const { ed, win } = await setup(); 23 const textLines = [ 24 "// line 1", 25 "// line 2", 26 "// line 3", 27 "// line 4", 28 "// line 5", 29 "", 30 ]; 31 ed.setText(textLines.join("\n")); 32 await promiseWaitForFocus(); 33 34 const testVectors = [ 35 // Various useless inputs go to line 0, column 0 or do nothing. 36 ["", { line: 0, ch: 0 }], 37 [":", { line: 0, ch: 0 }], 38 [" ", { line: 0, ch: 0 }], 39 [" : ", { line: 0, ch: 0 }], 40 ["a:b", { line: 0, ch: 0 }], 41 ["LINE: COLUMN ", { line: 0, ch: 0 }], 42 ["-1", { line: 0, ch: 0 }], 43 [":-1", { line: 0, ch: 0 }], 44 ["-1:-1", { line: 0, ch: 0 }], 45 ["0", { line: 0, ch: 0 }], 46 [":0", { line: 0, ch: 0 }], 47 ["0:0", { line: 0, ch: 0 }], 48 // Starting here expect data needs to get updated for length changes to 49 // "textLines" above. 50 // Just jump to line 51 ["1", { line: 0, ch: 0 }], 52 // Jump to second character in line 53 ["1:2", { line: 0, ch: 1 }], 54 // Jump to last character on line 55 ["1:9", { line: 0, ch: 8 }], 56 // Jump just after last character on line (end of line) 57 ["1:10", { line: 0, ch: 9 }], 58 // Jump one character past end of line (gets clamped to end of line) 59 ["1:11", { line: 0, ch: 9 }], 60 ["2", { line: 1, ch: 0 }], 61 ["2:2", { line: 1, ch: 1 }], 62 ["2:10", { line: 1, ch: 9 }], 63 ["2:11", { line: 1, ch: 10 }], 64 ["2:12", { line: 1, ch: 10 }], 65 ["3", { line: 2, ch: 0 }], 66 ["3:2", { line: 2, ch: 1 }], 67 ["3:11", { line: 2, ch: 10 }], 68 ["3:12", { line: 2, ch: 11 }], 69 ["3:13", { line: 2, ch: 11 }], 70 ["4", { line: 3, ch: 0 }], 71 ["4:2", { line: 3, ch: 1 }], 72 ["4:12", { line: 3, ch: 11 }], 73 ["4:13", { line: 3, ch: 12 }], 74 ["4:14", { line: 3, ch: 12 }], 75 ["5", { line: 4, ch: 0 }], 76 ["5:2", { line: 4, ch: 1 }], 77 ["5:13", { line: 4, ch: 12 }], 78 ["5:14", { line: 4, ch: 13 }], 79 ["5:15", { line: 4, ch: 13 }], 80 // One line beyond last newline in editor text: 81 ["6", { line: 5, ch: 0 }], 82 ["6:2", { line: 5, ch: 0 }], 83 // Two line beyond last newline in editor text (gets clamped): 84 ["7", { line: 5, ch: 0 }], 85 ["7:2", { line: 5, ch: 0 }], 86 ]; 87 testVectors.forEach(vector => { 88 testJumpToLine(ed, vector[0], vector[1]); 89 }); 90 teardown(ed, win); 91 }