browser/devtools/sourceeditor/test/cm_multi_test.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/sourceeditor/test/cm_multi_test.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,285 @@
     1.4 +(function() {
     1.5 +  namespace = "multi_";
     1.6 +
     1.7 +  function hasSelections(cm) {
     1.8 +    var sels = cm.listSelections();
     1.9 +    var given = (arguments.length - 1) / 4;
    1.10 +    if (sels.length != given)
    1.11 +      throw new Failure("expected " + given + " selections, found " + sels.length);
    1.12 +    for (var i = 0, p = 1; i < given; i++, p += 4) {
    1.13 +      var anchor = Pos(arguments[p], arguments[p + 1]);
    1.14 +      var head = Pos(arguments[p + 2], arguments[p + 3]);
    1.15 +      eqPos(sels[i].anchor, anchor, "anchor of selection " + i);
    1.16 +      eqPos(sels[i].head, head, "head of selection " + i);
    1.17 +    }
    1.18 +  }
    1.19 +  function hasCursors(cm) {
    1.20 +    var sels = cm.listSelections();
    1.21 +    var given = (arguments.length - 1) / 2;
    1.22 +    if (sels.length != given)
    1.23 +      throw new Failure("expected " + given + " selections, found " + sels.length);
    1.24 +    for (var i = 0, p = 1; i < given; i++, p += 2) {
    1.25 +      eqPos(sels[i].anchor, sels[i].head, "something selected for " + i);
    1.26 +      var head = Pos(arguments[p], arguments[p + 1]);
    1.27 +      eqPos(sels[i].head, head, "selection " + i);
    1.28 +    }
    1.29 +  }
    1.30 +
    1.31 +  testCM("getSelection", function(cm) {
    1.32 +    select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)}, {anchor: Pos(2, 2), head: Pos(2, 0)});
    1.33 +    eq(cm.getSelection(), "1234\n56\n90");
    1.34 +    eq(cm.getSelection(false).join("|"), "1234|56|90");
    1.35 +    eq(cm.getSelections().join("|"), "1234\n56|90");
    1.36 +  }, {value: "1234\n5678\n90"});
    1.37 +
    1.38 +  testCM("setSelection", function(cm) {
    1.39 +    select(cm, Pos(3, 0), Pos(0, 0), {anchor: Pos(2, 5), head: Pos(1, 0)});
    1.40 +    hasSelections(cm, 0, 0, 0, 0,
    1.41 +                  2, 5, 1, 0,
    1.42 +                  3, 0, 3, 0);
    1.43 +    cm.setSelection(Pos(1, 2), Pos(1, 1));
    1.44 +    hasSelections(cm, 1, 2, 1, 1);
    1.45 +    select(cm, {anchor: Pos(1, 1), head: Pos(2, 4)},
    1.46 +           {anchor: Pos(0, 0), head: Pos(1, 3)},
    1.47 +           Pos(3, 0), Pos(2, 2));
    1.48 +    hasSelections(cm, 0, 0, 2, 4,
    1.49 +                  3, 0, 3, 0);
    1.50 +    cm.setSelections([{anchor: Pos(0, 1), head: Pos(0, 2)},
    1.51 +                      {anchor: Pos(1, 1), head: Pos(1, 2)},
    1.52 +                      {anchor: Pos(2, 1), head: Pos(2, 2)}], 1);
    1.53 +    eqPos(cm.getCursor("head"), Pos(1, 2));
    1.54 +    eqPos(cm.getCursor("anchor"), Pos(1, 1));
    1.55 +    eqPos(cm.getCursor("from"), Pos(1, 1));
    1.56 +    eqPos(cm.getCursor("to"), Pos(1, 2));
    1.57 +    cm.setCursor(Pos(1, 1));
    1.58 +    hasCursors(cm, 1, 1);
    1.59 +  }, {value: "abcde\nabcde\nabcde\n"});
    1.60 +
    1.61 +  testCM("somethingSelected", function(cm) {
    1.62 +    select(cm, Pos(0, 1), {anchor: Pos(0, 3), head: Pos(0, 5)});
    1.63 +    eq(cm.somethingSelected(), true);
    1.64 +    select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5));
    1.65 +    eq(cm.somethingSelected(), false);
    1.66 +  }, {value: "123456789"});
    1.67 +
    1.68 +  testCM("extendSelection", function(cm) {
    1.69 +    select(cm, Pos(0, 1), Pos(1, 1), Pos(2, 1));
    1.70 +    cm.setExtending(true);
    1.71 +    cm.extendSelections([Pos(0, 2), Pos(1, 0), Pos(2, 3)]);
    1.72 +    hasSelections(cm, 0, 1, 0, 2,
    1.73 +                  1, 1, 1, 0,
    1.74 +                  2, 1, 2, 3);
    1.75 +    cm.extendSelection(Pos(2, 4), Pos(2, 0));
    1.76 +    hasSelections(cm, 2, 4, 2, 0);
    1.77 +  }, {value: "1234\n1234\n1234"});
    1.78 +
    1.79 +  testCM("addSelection", function(cm) {
    1.80 +    select(cm, Pos(0, 1), Pos(1, 1));
    1.81 +    cm.addSelection(Pos(0, 0), Pos(0, 4));
    1.82 +    hasSelections(cm, 0, 0, 0, 4,
    1.83 +                  1, 1, 1, 1);
    1.84 +    cm.addSelection(Pos(2, 2));
    1.85 +    hasSelections(cm, 0, 0, 0, 4,
    1.86 +                  1, 1, 1, 1,
    1.87 +                  2, 2, 2, 2);
    1.88 +  }, {value: "1234\n1234\n1234"});
    1.89 +
    1.90 +  testCM("replaceSelection", function(cm) {
    1.91 +    var selections = [{anchor: Pos(0, 0), head: Pos(0, 1)},
    1.92 +                      {anchor: Pos(0, 2), head: Pos(0, 3)},
    1.93 +                      {anchor: Pos(0, 4), head: Pos(0, 5)},
    1.94 +                      {anchor: Pos(2, 1), head: Pos(2, 4)},
    1.95 +                      {anchor: Pos(2, 5), head: Pos(2, 6)}];
    1.96 +    var val = "123456\n123456\n123456";
    1.97 +    cm.setValue(val);
    1.98 +    cm.setSelections(selections);
    1.99 +    cm.replaceSelection("ab", "around");
   1.100 +    eq(cm.getValue(), "ab2ab4ab6\n123456\n1ab5ab");
   1.101 +    hasSelections(cm, 0, 0, 0, 2,
   1.102 +                  0, 3, 0, 5,
   1.103 +                  0, 6, 0, 8,
   1.104 +                  2, 1, 2, 3,
   1.105 +                  2, 4, 2, 6);
   1.106 +    cm.setValue(val);
   1.107 +    cm.setSelections(selections);
   1.108 +    cm.replaceSelection("", "around");
   1.109 +    eq(cm.getValue(), "246\n123456\n15");
   1.110 +    hasSelections(cm, 0, 0, 0, 0,
   1.111 +                  0, 1, 0, 1,
   1.112 +                  0, 2, 0, 2,
   1.113 +                  2, 1, 2, 1,
   1.114 +                  2, 2, 2, 2);
   1.115 +    cm.setValue(val);
   1.116 +    cm.setSelections(selections);
   1.117 +    cm.replaceSelection("X\nY\nZ", "around");
   1.118 +    hasSelections(cm, 0, 0, 2, 1,
   1.119 +                  2, 2, 4, 1,
   1.120 +                  4, 2, 6, 1,
   1.121 +                  8, 1, 10, 1,
   1.122 +                  10, 2, 12, 1);
   1.123 +    cm.replaceSelection("a", "around");
   1.124 +    hasSelections(cm, 0, 0, 0, 1,
   1.125 +                  0, 2, 0, 3,
   1.126 +                  0, 4, 0, 5,
   1.127 +                  2, 1, 2, 2,
   1.128 +                  2, 3, 2, 4);
   1.129 +    cm.replaceSelection("xy", "start");
   1.130 +    hasSelections(cm, 0, 0, 0, 0,
   1.131 +                  0, 3, 0, 3,
   1.132 +                  0, 6, 0, 6,
   1.133 +                  2, 1, 2, 1,
   1.134 +                  2, 4, 2, 4);
   1.135 +    cm.replaceSelection("z\nf");
   1.136 +    hasSelections(cm, 1, 1, 1, 1,
   1.137 +                  2, 1, 2, 1,
   1.138 +                  3, 1, 3, 1,
   1.139 +                  6, 1, 6, 1,
   1.140 +                  7, 1, 7, 1);
   1.141 +    eq(cm.getValue(), "z\nfxy2z\nfxy4z\nfxy6\n123456\n1z\nfxy5z\nfxy");
   1.142 +  });
   1.143 +
   1.144 +  function select(cm) {
   1.145 +    var sels = [];
   1.146 +    for (var i = 1; i < arguments.length; i++) {
   1.147 +      var arg = arguments[i];
   1.148 +      if (arg.head) sels.push(arg);
   1.149 +      else sels.push({head: arg, anchor: arg});
   1.150 +    }
   1.151 +    cm.setSelections(sels, sels.length - 1);
   1.152 +  }
   1.153 +
   1.154 +  testCM("indentSelection", function(cm) {
   1.155 +    select(cm, Pos(0, 1), Pos(1, 1));
   1.156 +    cm.indentSelection(4);
   1.157 +    eq(cm.getValue(), "    foo\n    bar\nbaz");
   1.158 +
   1.159 +    select(cm, Pos(0, 2), Pos(0, 3), Pos(0, 4));
   1.160 +    cm.indentSelection(-2);
   1.161 +    eq(cm.getValue(), "  foo\n    bar\nbaz");
   1.162 +
   1.163 +    select(cm, {anchor: Pos(0, 0), head: Pos(1, 2)},
   1.164 +           {anchor: Pos(1, 3), head: Pos(2, 0)});
   1.165 +    cm.indentSelection(-2);
   1.166 +    eq(cm.getValue(), "foo\n  bar\nbaz");
   1.167 +  }, {value: "foo\nbar\nbaz"});
   1.168 +
   1.169 +  testCM("killLine", function(cm) {
   1.170 +    select(cm, Pos(0, 1), Pos(0, 2), Pos(1, 1));
   1.171 +    cm.execCommand("killLine");
   1.172 +    eq(cm.getValue(), "f\nb\nbaz");
   1.173 +    cm.execCommand("killLine");
   1.174 +    eq(cm.getValue(), "fbbaz");
   1.175 +    cm.setValue("foo\nbar\nbaz");
   1.176 +    select(cm, Pos(0, 1), {anchor: Pos(0, 2), head: Pos(2, 1)});
   1.177 +    cm.execCommand("killLine");
   1.178 +    eq(cm.getValue(), "faz");
   1.179 +  }, {value: "foo\nbar\nbaz"});
   1.180 +
   1.181 +  testCM("deleteLine", function(cm) {
   1.182 +    select(cm, Pos(0, 0),
   1.183 +           {head: Pos(0, 1), anchor: Pos(2, 0)},
   1.184 +           Pos(4, 0));
   1.185 +    cm.execCommand("deleteLine");
   1.186 +    eq(cm.getValue(), "4\n6\n7");
   1.187 +    select(cm, Pos(2, 1));
   1.188 +    cm.execCommand("deleteLine");
   1.189 +    eq(cm.getValue(), "4\n6\n");
   1.190 +  }, {value: "1\n2\n3\n4\n5\n6\n7"});
   1.191 +
   1.192 +  testCM("deleteH", function(cm) {
   1.193 +    select(cm, Pos(0, 4), {anchor: Pos(1, 4), head: Pos(1, 5)});
   1.194 +    cm.execCommand("delWordAfter");
   1.195 +    eq(cm.getValue(), "foo bar baz\nabc ef ghi\n");
   1.196 +    cm.execCommand("delWordAfter");
   1.197 +    eq(cm.getValue(), "foo  baz\nabc  ghi\n");
   1.198 +    cm.execCommand("delCharBefore");
   1.199 +    cm.execCommand("delCharBefore");
   1.200 +    eq(cm.getValue(), "fo baz\nab ghi\n");
   1.201 +    select(cm, Pos(0, 3), Pos(0, 4), Pos(0, 5));
   1.202 +    cm.execCommand("delWordAfter");
   1.203 +    eq(cm.getValue(), "fo \nab ghi\n");
   1.204 +  }, {value: "foo bar baz\nabc def ghi\n"});
   1.205 +
   1.206 +  testCM("goLineStart", function(cm) {
   1.207 +    select(cm, Pos(0, 2), Pos(0, 3), Pos(1, 1));
   1.208 +    cm.execCommand("goLineStart");
   1.209 +    hasCursors(cm, 0, 0, 1, 0);
   1.210 +    select(cm, Pos(1, 1), Pos(0, 1));
   1.211 +    cm.setExtending(true);
   1.212 +    cm.execCommand("goLineStart");
   1.213 +    hasSelections(cm, 0, 1, 0, 0,
   1.214 +                  1, 1, 1, 0);
   1.215 +  }, {value: "foo\nbar\nbaz"});
   1.216 +
   1.217 +  testCM("moveV", function(cm) {
   1.218 +    select(cm, Pos(0, 2), Pos(1, 2));
   1.219 +    cm.execCommand("goLineDown");
   1.220 +    hasCursors(cm, 1, 2, 2, 2);
   1.221 +    cm.execCommand("goLineUp");
   1.222 +    hasCursors(cm, 0, 2, 1, 2);
   1.223 +    cm.execCommand("goLineUp");
   1.224 +    hasCursors(cm, 0, 0, 0, 2);
   1.225 +    cm.execCommand("goLineUp");
   1.226 +    hasCursors(cm, 0, 0);
   1.227 +    select(cm, Pos(0, 2), Pos(1, 2));
   1.228 +    cm.setExtending(true);
   1.229 +    cm.execCommand("goLineDown");
   1.230 +    hasSelections(cm, 0, 2, 2, 2);
   1.231 +  }, {value: "12345\n12345\n12345"});
   1.232 +
   1.233 +  testCM("moveH", function(cm) {
   1.234 +    select(cm, Pos(0, 1), Pos(0, 3), Pos(0, 5), Pos(2, 3));
   1.235 +    cm.execCommand("goCharRight");
   1.236 +    hasCursors(cm, 0, 2, 0, 4, 1, 0, 2, 4);
   1.237 +    cm.execCommand("goCharLeft");
   1.238 +    hasCursors(cm, 0, 1, 0, 3, 0, 5, 2, 3);
   1.239 +    for (var i = 0; i < 15; i++)
   1.240 +      cm.execCommand("goCharRight");
   1.241 +    hasCursors(cm, 2, 4, 2, 5);
   1.242 +  }, {value: "12345\n12345\n12345"});
   1.243 +
   1.244 +  testCM("newlineAndIndent", function(cm) {
   1.245 +    select(cm, Pos(0, 5), Pos(1, 5));
   1.246 +    cm.execCommand("newlineAndIndent");
   1.247 +    hasCursors(cm, 1, 2, 3, 2);
   1.248 +    eq(cm.getValue(), "x = [\n  1];\ny = [\n  2];");
   1.249 +    cm.undo();
   1.250 +    eq(cm.getValue(), "x = [1];\ny = [2];");
   1.251 +    hasCursors(cm, 0, 5, 1, 5);
   1.252 +    select(cm, Pos(0, 5), Pos(0, 6));
   1.253 +    cm.execCommand("newlineAndIndent");
   1.254 +    hasCursors(cm, 1, 2, 2, 0);
   1.255 +    eq(cm.getValue(), "x = [\n  1\n];\ny = [2];");
   1.256 +  }, {value: "x = [1];\ny = [2];", mode: "javascript"});
   1.257 +
   1.258 +  testCM("goDocStartEnd", function(cm) {
   1.259 +    select(cm, Pos(0, 1), Pos(1, 1));
   1.260 +    cm.execCommand("goDocStart");
   1.261 +    hasCursors(cm, 0, 0);
   1.262 +    select(cm, Pos(0, 1), Pos(1, 1));
   1.263 +    cm.execCommand("goDocEnd");
   1.264 +    hasCursors(cm, 1, 3);
   1.265 +    select(cm, Pos(0, 1), Pos(1, 1));
   1.266 +    cm.setExtending(true);
   1.267 +    cm.execCommand("goDocEnd");
   1.268 +    hasSelections(cm, 1, 1, 1, 3);
   1.269 +  }, {value: "abc\ndef"});
   1.270 +
   1.271 +  testCM("selectionHistory", function(cm) {
   1.272 +    for (var i = 0; i < 3; ++i)
   1.273 +      cm.addSelection(Pos(0, i * 2), Pos(0, i * 2 + 1));
   1.274 +    cm.execCommand("undoSelection");
   1.275 +    eq(cm.getSelection(), "1\n2");
   1.276 +    cm.execCommand("undoSelection");
   1.277 +    eq(cm.getSelection(), "1");
   1.278 +    cm.execCommand("undoSelection");
   1.279 +    eq(cm.getSelection(), "");
   1.280 +    eqPos(cm.getCursor(), Pos(0, 0));
   1.281 +    cm.execCommand("redoSelection");
   1.282 +    eq(cm.getSelection(), "1");
   1.283 +    cm.execCommand("redoSelection");
   1.284 +    eq(cm.getSelection(), "1\n2");
   1.285 +    cm.execCommand("redoSelection");
   1.286 +    eq(cm.getSelection(), "1\n2\n3");
   1.287 +  }, {value: "1 2 3"});
   1.288 +})();

mercurial