openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

sublime_test.min.js (11040B)


      1 (function() {
      2   "use strict";
      3   
      4   var Pos = CodeMirror.Pos;
      5   namespace = "sublime_";
      6 
      7   function stTest(name) {
      8     var actions = Array.prototype.slice.call(arguments, 1);
      9     testCM(name, function(cm) {
     10       for (var i = 0; i < actions.length; i++) {
     11         var action = actions[i];
     12         if (typeof action == "string" && i == 0)
     13           cm.setValue(action);
     14         else if (typeof action == "string")
     15           cm.execCommand(action);
     16         else if (action instanceof Pos)
     17           cm.setCursor(action);
     18         else
     19           action(cm);
     20       }
     21     });
     22   }
     23 
     24   function at(line, ch, msg) {
     25     return function(cm) {
     26       eq(cm.listSelections().length, 1);
     27       eqCursorPos(cm.getCursor("head"), Pos(line, ch), msg);
     28       eqCursorPos(cm.getCursor("anchor"), Pos(line, ch), msg);
     29     };
     30   }
     31 
     32   function val(content, msg) {
     33     return function(cm) { eq(cm.getValue(), content, msg); };
     34   }
     35 
     36   function argsToRanges(args) {
     37     if (args.length % 4) throw new Error("Wrong number of arguments for ranges.");
     38     var ranges = [];
     39     for (var i = 0; i < args.length; i += 4)
     40       ranges.push({anchor: Pos(args[i], args[i + 1]),
     41                    head: Pos(args[i + 2], args[i + 3])});
     42     return ranges;
     43   }
     44 
     45   function setSel() {
     46     var ranges = argsToRanges(arguments);
     47     return function(cm) { cm.setSelections(ranges, 0); };
     48   }
     49 
     50   function hasSel() {
     51     var ranges = argsToRanges(arguments);
     52     return function(cm) {
     53       var sels = cm.listSelections();
     54       if (sels.length != ranges.length)
     55         throw new Failure("Expected " + ranges.length + " selections, but found " + sels.length);
     56       for (var i = 0; i < sels.length; i++) {
     57         eqCharPos(sels[i].anchor, ranges[i].anchor, "anchor " + i);
     58         eqCharPos(sels[i].head, ranges[i].head, "head " + i);
     59       }
     60     };
     61   }
     62 
     63   stTest("bySubword", "the foo_bar DooDahBah \n a",
     64          "goSubwordLeft", at(0, 0),
     65          "goSubwordRight", at(0, 3),
     66          "goSubwordRight", at(0, 7),
     67          "goSubwordRight", at(0, 11),
     68          "goSubwordRight", at(0, 15),
     69          "goSubwordRight", at(0, 18),
     70          "goSubwordRight", at(0, 21),
     71          "goSubwordRight", at(0, 22),
     72          "goSubwordRight", at(1, 0),
     73          "goSubwordRight", at(1, 2),
     74          "goSubwordRight", at(1, 2),
     75          "goSubwordLeft", at(1, 1),
     76          "goSubwordLeft", at(1, 0),
     77          "goSubwordLeft", at(0, 22),
     78          "goSubwordLeft", at(0, 18),
     79          "goSubwordLeft", at(0, 15),
     80          "goSubwordLeft", at(0, 12),
     81          "goSubwordLeft", at(0, 8),
     82          "goSubwordLeft", at(0, 4),
     83          "goSubwordLeft", at(0, 0));
     84 
     85   stTest("splitSelectionByLine", "abc\ndef\nghi",
     86          setSel(0, 1, 2, 2),
     87          "splitSelectionByLine",
     88          hasSel(0, 1, 0, 3,
     89                 1, 0, 1, 3,
     90                 2, 0, 2, 2));
     91 
     92   stTest("splitSelectionByLineMulti", "abc\ndef\nghi\njkl",
     93          setSel(0, 1, 1, 1,
     94                 1, 2, 3, 2,
     95                 3, 3, 3, 3),
     96          "splitSelectionByLine",
     97          hasSel(0, 1, 0, 3,
     98                 1, 0, 1, 1,
     99                 1, 2, 1, 3,
    100                 2, 0, 2, 3,
    101                 3, 0, 3, 2,
    102                 3, 3, 3, 3));
    103 
    104   stTest("selectLine", "abc\ndef\nghi",
    105          setSel(0, 1, 0, 1,
    106                 2, 0, 2, 1),
    107          "selectLine",
    108          hasSel(0, 0, 1, 0,
    109                 2, 0, 2, 3),
    110          setSel(0, 1, 1, 0),
    111          "selectLine",
    112          hasSel(0, 0, 2, 0));
    113 
    114   stTest("insertLineAfter", "abcde\nfghijkl\nmn",
    115          setSel(0, 1, 0, 1,
    116                 0, 3, 0, 3,
    117                 1, 2, 1, 2,
    118                 1, 3, 1, 5), "insertLineAfter",
    119          hasSel(1, 0, 1, 0,
    120                 3, 0, 3, 0), val("abcde\n\nfghijkl\n\nmn"));
    121 
    122   stTest("insertLineBefore", "abcde\nfghijkl\nmn",
    123          setSel(0, 1, 0, 1,
    124                 0, 3, 0, 3,
    125                 1, 2, 1, 2,
    126                 1, 3, 1, 5), "insertLineBefore",
    127          hasSel(0, 0, 0, 0,
    128                 2, 0, 2, 0), val("\nabcde\n\nfghijkl\nmn"));
    129 
    130   stTest("selectNextOccurrence", "a foo bar\nfoobar foo",
    131          setSel(0, 2, 0, 5),
    132          "selectNextOccurrence", hasSel(0, 2, 0, 5,
    133                                         1, 0, 1, 3),
    134          "selectNextOccurrence", hasSel(0, 2, 0, 5,
    135                                         1, 0, 1, 3,
    136                                         1, 7, 1, 10),
    137          "selectNextOccurrence", hasSel(0, 2, 0, 5,
    138                                         1, 0, 1, 3,
    139                                         1, 7, 1, 10),
    140          Pos(0, 3), "selectNextOccurrence", hasSel(0, 2, 0, 5),
    141         "selectNextOccurrence", hasSel(0, 2, 0, 5,
    142                                        1, 7, 1, 10),
    143          setSel(0, 6, 0, 9),
    144          "selectNextOccurrence", hasSel(0, 6, 0, 9,
    145                                         1, 3, 1, 6));
    146 
    147   stTest("selectScope", "foo(a) {\n  bar[1, 2];\n}",
    148          "selectScope", hasSel(0, 0, 2, 1),
    149          Pos(0, 4), "selectScope", hasSel(0, 4, 0, 5),
    150          Pos(0, 5), "selectScope", hasSel(0, 4, 0, 5),
    151          Pos(0, 6), "selectScope", hasSel(0, 0, 2, 1),
    152          Pos(0, 8), "selectScope", hasSel(0, 8, 2, 0),
    153          Pos(1, 2), "selectScope", hasSel(0, 8, 2, 0),
    154          Pos(1, 6), "selectScope", hasSel(1, 6, 1, 10),
    155          Pos(1, 9), "selectScope", hasSel(1, 6, 1, 10));
    156 
    157   stTest("goToBracket", "foo(a) {\n  bar[1, 2];\n}",
    158          Pos(0, 0), "goToBracket", at(0, 0),
    159          Pos(0, 4), "goToBracket", at(0, 5), "goToBracket", at(0, 4),
    160          Pos(0, 8), "goToBracket", at(2, 0), "goToBracket", at(0, 8),
    161          Pos(1, 2), "goToBracket", at(2, 0),
    162          Pos(1, 7), "goToBracket", at(1, 10), "goToBracket", at(1, 6));
    163 
    164   stTest("swapLine", "1\n2\n3---\n4\n5",
    165          "swapLineDown", val("2\n1\n3---\n4\n5"),
    166          "swapLineUp", val("1\n2\n3---\n4\n5"),
    167          "swapLineUp", val("1\n2\n3---\n4\n5"),
    168          Pos(4, 1), "swapLineDown", val("1\n2\n3---\n4\n5"),
    169          setSel(0, 1, 0, 1,
    170                 1, 0, 2, 0,
    171                 2, 2, 2, 2),
    172          "swapLineDown", val("4\n1\n2\n3---\n5"),
    173          hasSel(1, 1, 1, 1,
    174                 2, 0, 3, 0,
    175                 3, 2, 3, 2),
    176          "swapLineUp", val("1\n2\n3---\n4\n5"),
    177          hasSel(0, 1, 0, 1,
    178                 1, 0, 2, 0,
    179                 2, 2, 2, 2));
    180 
    181   stTest("swapLineEmptyBottomSel", "1\n2\n3",
    182          setSel(0, 1, 1, 0),
    183          "swapLineDown", val("2\n1\n3"), hasSel(1, 1, 2, 0),
    184          "swapLineUp", val("1\n2\n3"), hasSel(0, 1, 1, 0),
    185          "swapLineUp", val("1\n2\n3"), hasSel(0, 0, 0, 0));
    186 
    187   stTest("swapLineUpFromEnd", "a\nb\nc",
    188          Pos(2, 1), "swapLineUp",
    189          hasSel(1, 1, 1, 1), val("a\nc\nb"));
    190 
    191   stTest("joinLines", "abc\ndef\nghi\njkl",
    192          "joinLines", val("abc def\nghi\njkl"), at(0, 4),
    193          "undo",
    194          setSel(0, 2, 1, 1), "joinLines",
    195          val("abc def ghi\njkl"), hasSel(0, 2, 0, 8),
    196          "undo",
    197          setSel(0, 1, 0, 1,
    198                 1, 1, 1, 1,
    199                 3, 1, 3, 1), "joinLines",
    200          val("abc def ghi\njkl"), hasSel(0, 4, 0, 4,
    201                                          0, 8, 0, 8,
    202                                          1, 3, 1, 3));
    203 
    204   stTest("duplicateLine", "abc\ndef\nghi",
    205          Pos(1, 0), "duplicateLine", val("abc\ndef\ndef\nghi"), at(2, 0),
    206          "undo",
    207          setSel(0, 1, 0, 1,
    208                 1, 1, 1, 1,
    209                 2, 1, 2, 1), "duplicateLine",
    210          val("abc\nabc\ndef\ndef\nghi\nghi"), hasSel(1, 1, 1, 1,
    211                                                      3, 1, 3, 1,
    212                                                      5, 1, 5, 1));
    213   stTest("duplicateLineSelection", "abcdef",
    214          setSel(0, 1, 0, 1,
    215                 0, 2, 0, 4,
    216                 0, 5, 0, 5),
    217          "duplicateLine",
    218          val("abcdef\nabcdcdef\nabcdcdef"), hasSel(2, 1, 2, 1,
    219                                                    2, 4, 2, 6,
    220                                                    2, 7, 2, 7));
    221 
    222   stTest("selectLinesUpward", "123\n345\n789\n012",
    223          setSel(0, 1, 0, 1,
    224                 1, 1, 1, 3,
    225                 2, 0, 2, 0,
    226                 3, 0, 3, 0),
    227          "selectLinesUpward",
    228          hasSel(0, 1, 0, 1,
    229                 0, 3, 0, 3,
    230                 1, 0, 1, 0,
    231                 1, 1, 1, 3,
    232                 2, 0, 2, 0,
    233                 3, 0, 3, 0));
    234 
    235   stTest("selectLinesDownward", "123\n345\n789\n012",
    236          setSel(0, 1, 0, 1,
    237                 1, 1, 1, 3,
    238                 2, 0, 2, 0,
    239                 3, 0, 3, 0),
    240          "selectLinesDownward",
    241          hasSel(0, 1, 0, 1,
    242                 1, 1, 1, 3,
    243                 2, 0, 2, 0,
    244                 2, 3, 2, 3,
    245                 3, 0, 3, 0));
    246 
    247   stTest("sortLines", "c\nb\na\nC\nB\nA",
    248          "sortLines", val("A\nB\nC\na\nb\nc"),
    249          "undo",
    250          setSel(0, 0, 2, 0,
    251                 3, 0, 5, 0),
    252          "sortLines", val("b\nc\na\nB\nC\nA"),
    253          hasSel(0, 0, 2, 0,
    254                 3, 0, 5, 0),
    255          "undo",
    256          setSel(1, 0, 5, 0), "sortLinesInsensitive", val("c\na\nB\nb\nC\nA"));
    257 
    258   stTest("bookmarks", "abc\ndef\nghi\njkl",
    259          Pos(0, 1), "toggleBookmark",
    260          setSel(1, 1, 1, 2), "toggleBookmark",
    261          setSel(2, 1, 2, 2), "toggleBookmark",
    262          "nextBookmark", hasSel(0, 1, 0, 1),
    263          "nextBookmark", hasSel(1, 1, 1, 2),
    264          "nextBookmark", hasSel(2, 1, 2, 2),
    265          "prevBookmark", hasSel(1, 1, 1, 2),
    266          "prevBookmark", hasSel(0, 1, 0, 1),
    267          "prevBookmark", hasSel(2, 1, 2, 2),
    268          "prevBookmark", hasSel(1, 1, 1, 2),
    269          "toggleBookmark",
    270          "prevBookmark", hasSel(2, 1, 2, 2),
    271          "prevBookmark", hasSel(0, 1, 0, 1),
    272          "selectBookmarks", hasSel(0, 1, 0, 1,
    273                                    2, 1, 2, 2),
    274          "clearBookmarks",
    275          Pos(0, 0), "selectBookmarks", at(0, 0));
    276 
    277   stTest("smartBackspace", "  foo\n    bar",
    278          setSel(0, 2, 0, 2, 1, 4, 1, 4, 1, 6, 1, 6), "smartBackspace",
    279          val("foo\n  br"))
    280 
    281   stTest("upAndDowncaseAtCursor", "abc\ndef  x\nghI",
    282          setSel(0, 1, 0, 3,
    283                 1, 1, 1, 1,
    284                 1, 4, 1, 4), "upcaseAtCursor",
    285          val("aBC\nDEF  x\nghI"), hasSel(0, 1, 0, 3,
    286                                          1, 3, 1, 3,
    287                                          1, 4, 1, 4),
    288          "downcaseAtCursor",
    289          val("abc\ndef  x\nghI"), hasSel(0, 1, 0, 3,
    290                                          1, 3, 1, 3,
    291                                          1, 4, 1, 4));
    292 
    293   stTest("mark", "abc\ndef\nghi",
    294          Pos(1, 1), "setSublimeMark",
    295          Pos(2, 1), "selectToSublimeMark", hasSel(2, 1, 1, 1),
    296          Pos(0, 1), "swapWithSublimeMark", at(1, 1), "swapWithSublimeMark", at(0, 1),
    297          "deleteToSublimeMark", val("aef\nghi"),
    298          "sublimeYank", val("abc\ndef\nghi"), at(1, 1));
    299 
    300   stTest("findUnder", "foo foobar  a",
    301          "findUnder", hasSel(0, 4, 0, 7),
    302          "findUnder", hasSel(0, 0, 0, 3),
    303          "findUnderPrevious", hasSel(0, 4, 0, 7),
    304          "findUnderPrevious", hasSel(0, 0, 0, 3),
    305          Pos(0, 4), "findUnder", hasSel(0, 4, 0, 10),
    306          Pos(0, 11), "findUnder", hasSel(0, 11, 0, 11));
    307 })();