michael@0: (function() { michael@0: "use strict"; michael@0: michael@0: var Pos = CodeMirror.Pos; michael@0: namespace = "sublime_"; michael@0: michael@0: function stTest(name) { michael@0: var actions = Array.prototype.slice.call(arguments, 1); michael@0: testCM(name, function(cm) { michael@0: for (var i = 0; i < actions.length; i++) { michael@0: var action = actions[i]; michael@0: if (typeof action == "string" && i == 0) michael@0: cm.setValue(action); michael@0: else if (typeof action == "string") michael@0: cm.execCommand(action); michael@0: else if (action instanceof Pos) michael@0: cm.setCursor(action); michael@0: else michael@0: action(cm); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: function at(line, ch, msg) { michael@0: return function(cm) { michael@0: eq(cm.listSelections().length, 1); michael@0: eqPos(cm.getCursor("head"), Pos(line, ch), msg); michael@0: eqPos(cm.getCursor("anchor"), Pos(line, ch), msg); michael@0: }; michael@0: } michael@0: michael@0: function val(content, msg) { michael@0: return function(cm) { eq(cm.getValue(), content, msg); }; michael@0: } michael@0: michael@0: function argsToRanges(args) { michael@0: if (args.length % 4) throw new Error("Wrong number of arguments for ranges."); michael@0: var ranges = []; michael@0: for (var i = 0; i < args.length; i += 4) michael@0: ranges.push({anchor: Pos(args[i], args[i + 1]), michael@0: head: Pos(args[i + 2], args[i + 3])}); michael@0: return ranges; michael@0: } michael@0: michael@0: function setSel() { michael@0: var ranges = argsToRanges(arguments); michael@0: return function(cm) { cm.setSelections(ranges, 0); }; michael@0: } michael@0: michael@0: function hasSel() { michael@0: var ranges = argsToRanges(arguments); michael@0: return function(cm) { michael@0: var sels = cm.listSelections(); michael@0: if (sels.length != ranges.length) michael@0: throw new Failure("Expected " + ranges.length + " selections, but found " + sels.length); michael@0: for (var i = 0; i < sels.length; i++) { michael@0: eqPos(sels[i].anchor, ranges[i].anchor, "anchor " + i); michael@0: eqPos(sels[i].head, ranges[i].head, "head " + i); michael@0: } michael@0: }; michael@0: } michael@0: michael@0: stTest("bySubword", "the foo_bar DooDahBah \n a", michael@0: "goSubwordLeft", at(0, 0), michael@0: "goSubwordRight", at(0, 3), michael@0: "goSubwordRight", at(0, 7), michael@0: "goSubwordRight", at(0, 11), michael@0: "goSubwordRight", at(0, 15), michael@0: "goSubwordRight", at(0, 18), michael@0: "goSubwordRight", at(0, 21), michael@0: "goSubwordRight", at(0, 22), michael@0: "goSubwordRight", at(1, 0), michael@0: "goSubwordRight", at(1, 2), michael@0: "goSubwordRight", at(1, 2), michael@0: "goSubwordLeft", at(1, 1), michael@0: "goSubwordLeft", at(1, 0), michael@0: "goSubwordLeft", at(0, 22), michael@0: "goSubwordLeft", at(0, 18), michael@0: "goSubwordLeft", at(0, 15), michael@0: "goSubwordLeft", at(0, 12), michael@0: "goSubwordLeft", at(0, 8), michael@0: "goSubwordLeft", at(0, 4), michael@0: "goSubwordLeft", at(0, 0)); michael@0: michael@0: stTest("splitSelectionByLine", "abc\ndef\nghi", michael@0: setSel(0, 1, 2, 2), michael@0: "splitSelectionByLine", michael@0: hasSel(0, 1, 0, 3, michael@0: 1, 0, 1, 3, michael@0: 2, 0, 2, 2)); michael@0: michael@0: stTest("splitSelectionByLineMulti", "abc\ndef\nghi\njkl", michael@0: setSel(0, 1, 1, 1, michael@0: 1, 2, 3, 2, michael@0: 3, 3, 3, 3), michael@0: "splitSelectionByLine", michael@0: hasSel(0, 1, 0, 3, michael@0: 1, 0, 1, 1, michael@0: 1, 2, 1, 3, michael@0: 2, 0, 2, 3, michael@0: 3, 0, 3, 2, michael@0: 3, 3, 3, 3)); michael@0: michael@0: stTest("selectLine", "abc\ndef\nghi", michael@0: setSel(0, 1, 0, 1, michael@0: 2, 0, 2, 1), michael@0: "selectLine", michael@0: hasSel(0, 0, 1, 0, michael@0: 2, 0, 2, 3), michael@0: setSel(0, 1, 1, 0), michael@0: "selectLine", michael@0: hasSel(0, 0, 2, 0)); michael@0: michael@0: stTest("insertLineAfter", "abcde\nfghijkl\nmn", michael@0: setSel(0, 1, 0, 1, michael@0: 0, 3, 0, 3, michael@0: 1, 2, 1, 2, michael@0: 1, 3, 1, 5), "insertLineAfter", michael@0: hasSel(1, 0, 1, 0, michael@0: 3, 0, 3, 0), val("abcde\n\nfghijkl\n\nmn")); michael@0: michael@0: stTest("insertLineBefore", "abcde\nfghijkl\nmn", michael@0: setSel(0, 1, 0, 1, michael@0: 0, 3, 0, 3, michael@0: 1, 2, 1, 2, michael@0: 1, 3, 1, 5), "insertLineBefore", michael@0: hasSel(0, 0, 0, 0, michael@0: 2, 0, 2, 0), val("\nabcde\n\nfghijkl\nmn")); michael@0: michael@0: stTest("selectNextOccurrence", "a foo bar\nfoobar foo", michael@0: setSel(0, 2, 0, 5), michael@0: "selectNextOccurrence", hasSel(0, 2, 0, 5, michael@0: 1, 0, 1, 3), michael@0: "selectNextOccurrence", hasSel(0, 2, 0, 5, michael@0: 1, 0, 1, 3, michael@0: 1, 7, 1, 10), michael@0: "selectNextOccurrence", hasSel(0, 2, 0, 5, michael@0: 1, 0, 1, 3, michael@0: 1, 7, 1, 10), michael@0: Pos(0, 3), "selectNextOccurrence", hasSel(0, 2, 0, 5), michael@0: "selectNextOccurrence", hasSel(0, 2, 0, 5, michael@0: 1, 7, 1, 10), michael@0: setSel(0, 6, 0, 9), michael@0: "selectNextOccurrence", hasSel(0, 6, 0, 9, michael@0: 1, 3, 1, 6)); michael@0: michael@0: stTest("selectScope", "foo(a) {\n bar[1, 2];\n}", michael@0: "selectScope", hasSel(0, 0, 2, 1), michael@0: Pos(0, 4), "selectScope", hasSel(0, 4, 0, 5), michael@0: Pos(0, 5), "selectScope", hasSel(0, 4, 0, 5), michael@0: Pos(0, 6), "selectScope", hasSel(0, 0, 2, 1), michael@0: Pos(0, 8), "selectScope", hasSel(0, 8, 2, 0), michael@0: Pos(1, 2), "selectScope", hasSel(0, 8, 2, 0), michael@0: Pos(1, 6), "selectScope", hasSel(1, 6, 1, 10), michael@0: Pos(1, 9), "selectScope", hasSel(1, 6, 1, 10)); michael@0: michael@0: stTest("goToBracket", "foo(a) {\n bar[1, 2];\n}", michael@0: Pos(0, 0), "goToBracket", at(0, 0), michael@0: Pos(0, 4), "goToBracket", at(0, 5), "goToBracket", at(0, 4), michael@0: Pos(0, 8), "goToBracket", at(2, 0), "goToBracket", at(0, 8), michael@0: Pos(1, 2), "goToBracket", at(2, 0), michael@0: Pos(1, 7), "goToBracket", at(1, 10), "goToBracket", at(1, 6)); michael@0: michael@0: stTest("swapLine", "1\n2\n3---\n4\n5", michael@0: "swapLineDown", val("2\n1\n3---\n4\n5"), michael@0: "swapLineUp", val("1\n2\n3---\n4\n5"), michael@0: "swapLineUp", val("1\n2\n3---\n4\n5"), michael@0: Pos(4, 1), "swapLineDown", val("1\n2\n3---\n4\n5"), michael@0: setSel(0, 1, 0, 1, michael@0: 1, 0, 2, 0, michael@0: 2, 2, 2, 2), michael@0: "swapLineDown", val("4\n1\n2\n3---\n5"), michael@0: hasSel(1, 1, 1, 1, michael@0: 2, 0, 3, 0, michael@0: 3, 2, 3, 2), michael@0: "swapLineUp", val("1\n2\n3---\n4\n5"), michael@0: hasSel(0, 1, 0, 1, michael@0: 1, 0, 2, 0, michael@0: 2, 2, 2, 2)); michael@0: michael@0: stTest("swapLineUpFromEnd", "a\nb\nc", michael@0: Pos(2, 1), "swapLineUp", michael@0: hasSel(1, 1, 1, 1), val("a\nc\nb")); michael@0: michael@0: stTest("joinLines", "abc\ndef\nghi\njkl", michael@0: "joinLines", val("abc def\nghi\njkl"), at(0, 4), michael@0: "undo", michael@0: setSel(0, 2, 1, 1), "joinLines", michael@0: val("abc def ghi\njkl"), hasSel(0, 2, 0, 8), michael@0: "undo", michael@0: setSel(0, 1, 0, 1, michael@0: 1, 1, 1, 1, michael@0: 3, 1, 3, 1), "joinLines", michael@0: val("abc def ghi\njkl"), hasSel(0, 4, 0, 4, michael@0: 0, 8, 0, 8, michael@0: 1, 3, 1, 3)); michael@0: michael@0: stTest("duplicateLine", "abc\ndef\nghi", michael@0: Pos(1, 0), "duplicateLine", val("abc\ndef\ndef\nghi"), at(2, 0), michael@0: "undo", michael@0: setSel(0, 1, 0, 1, michael@0: 1, 1, 1, 1, michael@0: 2, 1, 2, 1), "duplicateLine", michael@0: val("abc\nabc\ndef\ndef\nghi\nghi"), hasSel(1, 1, 1, 1, michael@0: 3, 1, 3, 1, michael@0: 5, 1, 5, 1)); michael@0: stTest("duplicateLineSelection", "abcdef", michael@0: setSel(0, 1, 0, 1, michael@0: 0, 2, 0, 4, michael@0: 0, 5, 0, 5), michael@0: "duplicateLine", michael@0: val("abcdef\nabcdcdef\nabcdcdef"), hasSel(2, 1, 2, 1, michael@0: 2, 4, 2, 6, michael@0: 2, 7, 2, 7)); michael@0: michael@0: stTest("selectLinesUpward", "123\n345\n789\n012", michael@0: setSel(0, 1, 0, 1, michael@0: 1, 1, 1, 3, michael@0: 2, 0, 2, 0, michael@0: 3, 0, 3, 0), michael@0: "selectLinesUpward", michael@0: hasSel(0, 1, 0, 1, michael@0: 0, 3, 0, 3, michael@0: 1, 0, 1, 0, michael@0: 1, 1, 1, 3, michael@0: 2, 0, 2, 0, michael@0: 3, 0, 3, 0)); michael@0: michael@0: stTest("selectLinesDownward", "123\n345\n789\n012", michael@0: setSel(0, 1, 0, 1, michael@0: 1, 1, 1, 3, michael@0: 2, 0, 2, 0, michael@0: 3, 0, 3, 0), michael@0: "selectLinesDownward", michael@0: hasSel(0, 1, 0, 1, michael@0: 1, 1, 1, 3, michael@0: 2, 0, 2, 0, michael@0: 2, 3, 2, 3, michael@0: 3, 0, 3, 0)); michael@0: michael@0: stTest("sortLines", "c\nb\na\nC\nB\nA", michael@0: "sortLines", val("A\nB\nC\na\nb\nc"), michael@0: "undo", michael@0: setSel(0, 0, 2, 0, michael@0: 3, 0, 5, 0), michael@0: "sortLines", val("a\nb\nc\nA\nB\nC"), michael@0: hasSel(0, 0, 2, 1, michael@0: 3, 0, 5, 1), michael@0: "undo", michael@0: setSel(1, 0, 4, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA")); michael@0: michael@0: stTest("bookmarks", "abc\ndef\nghi\njkl", michael@0: Pos(0, 1), "toggleBookmark", michael@0: setSel(1, 1, 1, 2), "toggleBookmark", michael@0: setSel(2, 1, 2, 2), "toggleBookmark", michael@0: "nextBookmark", hasSel(0, 1, 0, 1), michael@0: "nextBookmark", hasSel(1, 1, 1, 2), michael@0: "nextBookmark", hasSel(2, 1, 2, 2), michael@0: "prevBookmark", hasSel(1, 1, 1, 2), michael@0: "prevBookmark", hasSel(0, 1, 0, 1), michael@0: "prevBookmark", hasSel(2, 1, 2, 2), michael@0: "prevBookmark", hasSel(1, 1, 1, 2), michael@0: "toggleBookmark", michael@0: "prevBookmark", hasSel(2, 1, 2, 2), michael@0: "prevBookmark", hasSel(0, 1, 0, 1), michael@0: "selectBookmarks", hasSel(0, 1, 0, 1, michael@0: 2, 1, 2, 2), michael@0: "clearBookmarks", michael@0: Pos(0, 0), "selectBookmarks", at(0, 0)); michael@0: michael@0: stTest("upAndDowncaseAtCursor", "abc\ndef x\nghI", michael@0: setSel(0, 1, 0, 3, michael@0: 1, 1, 1, 1, michael@0: 1, 4, 1, 4), "upcaseAtCursor", michael@0: val("aBC\nDEF x\nghI"), hasSel(0, 1, 0, 3, michael@0: 1, 3, 1, 3, michael@0: 1, 4, 1, 4), michael@0: "downcaseAtCursor", michael@0: val("abc\ndef x\nghI"), hasSel(0, 1, 0, 3, michael@0: 1, 3, 1, 3, michael@0: 1, 4, 1, 4)); michael@0: michael@0: stTest("mark", "abc\ndef\nghi", michael@0: Pos(1, 1), "setSublimeMark", michael@0: Pos(2, 1), "selectToSublimeMark", hasSel(2, 1, 1, 1), michael@0: Pos(0, 1), "swapWithSublimeMark", at(1, 1), "swapWithSublimeMark", at(0, 1), michael@0: "deleteToSublimeMark", val("aef\nghi"), michael@0: "sublimeYank", val("abc\ndef\nghi"), at(1, 1)); michael@0: michael@0: stTest("findUnder", "foo foobar a", michael@0: "findUnder", hasSel(0, 4, 0, 7), michael@0: "findUnder", hasSel(0, 0, 0, 3), michael@0: "findUnderPrevious", hasSel(0, 4, 0, 7), michael@0: "findUnderPrevious", hasSel(0, 0, 0, 3), michael@0: Pos(0, 4), "findUnder", hasSel(0, 4, 0, 10), michael@0: Pos(0, 11), "findUnder", hasSel(0, 11, 0, 11)); michael@0: })();