/* ***** BEGIN LICENSE BLOCK ***** * Distributed under the BSD license: * * Copyright (c) 2010, Ajax.org B.V. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Ajax.org B.V. nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * ***** END LICENSE BLOCK ***** */ if (typeof process !== "undefined") { require("amd-loader"); require("./test/mockdom"); } define(function(require, exports, module) { "use strict"; var lang = require("./lib/lang"); var EditSession = require("./edit_session").EditSession; var Editor = require("./editor").Editor; var UndoManager = require("./undomanager").UndoManager; var MockRenderer = require("./test/mockrenderer").MockRenderer; var Range = require("./range").Range; var assert = require("./test/assertions"); var JavaScriptMode = require("./mode/javascript").Mode; function createFoldTestSession() { var lines = [ "function foo(items) {", " for (var i=0; i>", [1, 2], 1); // Test wrapping for punctuation in EditSession.prototype.$wrapAsCode = false; computeAndAssert("ab cde, Juhu kinners", [3, 8, 13, 19], 6); }, "test get longest line" : function() { var session = new EditSession(["12"]); session.setTabSize(4); assert.equal(session.getScreenWidth(), 2); session.doc.insertNewLine({row: 0, column: Infinity}); session.doc.insertLines(1, ["123"]); assert.equal(session.getScreenWidth(), 3); session.doc.insertNewLine({row: 0, column: Infinity}); session.doc.insertLines(1, ["\t\t"]); assert.equal(session.getScreenWidth(), 8); session.setTabSize(2); assert.equal(session.getScreenWidth(), 4); }, "test getDisplayString": function() { var session = new EditSession(["12"]); session.setTabSize(4); assert.equal(session.$getDisplayTokens("\t").length, 4); assert.equal(session.$getDisplayTokens("abc").length, 3); assert.equal(session.$getDisplayTokens("abc\t").length, 4); }, "test issue 83": function() { var session = new EditSession(""); var editor = new Editor(new MockRenderer(), session); var document = session.getDocument(); session.setUseWrapMode(true); document.insertLines(0, ["a", "b"]); document.insertLines(2, ["c", "d"]); document.removeLines(1, 2); }, "test wrapMode init has to create wrapData array": function() { var session = new EditSession("foo bar\nfoo bar"); var editor = new Editor(new MockRenderer(), session); var document = session.getDocument(); session.setUseWrapMode(true); session.setWrapLimitRange(3, 3); session.adjustWrapLimit(80); // Test if wrapData is there and was computed. assert.equal(session.$wrapData.length, 2); assert.equal(session.$wrapData[0].length, 1); assert.equal(session.$wrapData[1].length, 1); }, "test first line blank with wrap": function() { var session = new EditSession("\nfoo"); session.setUseWrapMode(true); assert.equal(session.doc.getValue(), ["", "foo"].join("\n")); }, "test first line blank with wrap 2" : function() { var session = new EditSession(""); session.setUseWrapMode(true); session.setValue("\nfoo"); assert.equal(session.doc.getValue(), ["", "foo"].join("\n")); }, "test fold getFoldDisplayLine": function() { var session = createFoldTestSession(); function assertDisplayLine(foldLine, str) { var line = session.getLine(foldLine.end.row); var displayLine = session.getFoldDisplayLine(foldLine, foldLine.end.row, line.length); assert.equal(displayLine, str); } assertDisplayLine(session.$foldData[0], "function foo(args...) {") assertDisplayLine(session.$foldData[1], " for (vfoo...ert(items[bar...\"juhu\");"); }, "test foldLine idxToPosition": function() { var session = createFoldTestSession(); function assertIdx2Pos(foldLineIdx, idx, row, column) { var foldLine = session.$foldData[foldLineIdx]; assert.position(foldLine.idxToPosition(idx), row, column); } // "function foo(items) {", // " for (var i=0; iThe cursor in this paragraph text will be offset by 1 row.

", "

Everything after this will be offset as well due to the folds in the row before too.

" ].join("\n")); session.addFold('...', new Range(0, 8, 0, 42)); session.addFold('...', new Range(1, 8, 1, 42)); session.addFold('...', new Range(3, 7, 3, 51)); session.setOption("wrap", 40); session.remove(new Range(0,0, 2, 5)); // needed because adjustWrapLimit is called async from renderer session.adjustWrapLimit(80); assert.equal(session.$wrapData + "", [[], [], [40, 76]] + ""); }, "test add fold": function() { var session = createFoldTestSession(); var fold; function tryAddFold(placeholder, range, shouldFail) { var fail = false; try { fold = session.addFold(placeholder, range); } catch (e) { fail = true; } if (fail != shouldFail) { throw new Error("Expected to get an exception"); } } tryAddFold("foo", new Range(0, 13, 0, 17), false); tryAddFold("foo", new Range(0, 14, 0, 18), true); tryAddFold("foo", new Range(0, 13, 0, 18), false); assert.equal(session.$foldData[0].folds.length, 1); tryAddFold("f", new Range(0, 13, 0, 18), false); tryAddFold("foo", new Range(0, 18, 0, 21), false); assert.equal(session.$foldData[0].folds.length, 2); session.removeFold(fold); tryAddFold("foo", new Range(0, 18, 0, 22), false); tryAddFold("foo", new Range(0, 18, 0, 19), true); tryAddFold("foo", new Range(0, 22, 1, 10), false); }, "test add subfolds": function() { var session = createFoldTestSession(); var fold, oldFold; var foldData = session.$foldData; oldFold = foldData[0].folds[0]; fold = session.addFold("fold0", new Range(0, 10, 0, 21)); assert.equal(foldData[0].folds.length, 1); assert.equal(fold.subFolds.length, 1); assert.equal(fold.subFolds[0], oldFold); session.expandFold(fold); assert.equal(foldData[0].folds.length, 1); assert.equal(foldData[0].folds[0], oldFold); assert.equal(fold.subFolds.length, 0); fold = session.addFold("fold0", new Range(0, 13, 2, 10)); assert.equal(foldData.length, 1); assert.equal(fold.subFolds.length, 2); assert.equal(fold.subFolds[0], oldFold); session.expandFold(fold); assert.equal(foldData.length, 2); assert.equal(foldData[0].folds.length, 1); assert.equal(foldData[0].folds[0], oldFold); assert.equal(fold.subFolds.length, 0); session.unfold(null, true); fold = session.addFold("fold0", new Range(0, 0, 0, 21)); session.addFold("fold0", new Range(0, 1, 0, 5)); session.addFold("fold0", new Range(0, 6, 0, 8)); assert.equal(fold.subFolds.length, 2); }, "test row cache": function() { var session = createFoldTestSession(); session.screenToDocumentPosition(2,3); assertArray(session.$docRowCache, [1,3]); assertArray(session.$screenRowCache, [1,2]); session.screenToDocumentPosition(5,3); assertArray(session.$docRowCache, [1,3,4]); assertArray(session.$screenRowCache, [1,2,3]); session.screenToDocumentPosition(0,3); assertArray(session.$docRowCache, [1,3,4]); assertArray(session.$screenRowCache, [1,2,3]); var pos = session.screenToDocumentPosition(0,0); assert.equal(pos.row, 0); assertArray(session.$docRowCache, [1,3,4]); assertArray(session.$screenRowCache, [1,2,3]); session.screenToDocumentPosition(1,0); assertArray(session.$docRowCache, [1,3,4]); assertArray(session.$screenRowCache, [1,2,3]); session.$resetRowCache(); assertArray(session.$docRowCache, []); assertArray(session.$screenRowCache, []); session.screenToDocumentPosition(1,3); assertArray(session.$docRowCache, [1]); assertArray(session.$screenRowCache, [1]); session.screenToDocumentPosition(5,3); assertArray(session.$docRowCache, [1,3,4]); assertArray(session.$screenRowCache, [1,2,3]); session = new EditSession(new Array(30).join("\n")); session.documentToScreenPosition(2,0); session.documentToScreenPosition(2,0); assertArray(session.$docRowCache, [1,2]); assertArray(session.$screenRowCache, [1,2]); }, "test annotations": function() { var session = new EditSession([]), annotation = {row: 0, type: 'info', text: "This is a test."}; session.clearAnnotations(); assertArray(session.getAnnotations(), []); session.setAnnotations([annotation]); assertArray(session.getAnnotations(), [annotation]); }, "test: mode loading" : function(next) { if (!require.undef) { console.log("Skipping test: This test only runs in the browser"); next(); return; } var session = new EditSession([]); session.setMode("ace/mode/javascript"); assert.equal(session.$modeid, "ace/mode/javascript"); session.on("changeMode", function() { assert.equal(session.$modeid, "ace/mode/javascript"); }); session.setMode("ace/mode/sh", function(mode) { assert.ok(!mode); }); setTimeout(function() { session.setMode("ace/mode/javascript", function(mode) { session.setMode("ace/mode/javascript"); assert.equal(session.$modeid, "ace/mode/javascript"); next(); }); }, 0); } }; }); if (typeof module !== "undefined" && module === require.main) { require("asyncjs").test.testcase(module.exports).exec(); }