<commit_before>(function() {
  module("IndexableCodeMirror");
  
  function icmTest(cb) {
    return function() {
      var place = $("<div></div>").appendTo(document.body);
      var cm = IndexableCodeMirror(place[0], {mode: "text/plain"});
      var content = "hello\nthere";
      cm.setValue(content);
      try {
        cb(cm, content);
      } finally {
        place.remove();
      }
    };
  }
  
  test("indexFromCoords() works", icmTest(function(cm, content) {
    equal(cm.indexFromCoords({line: 0, ch: 0}), 0,
          "index of line 0, char 0 is 0");
    equal(cm.indexFromCoords({line: 1, ch: 0}), content.indexOf("there"),
          "index of line 1, char 0 works");
  }));
  
  test("getCursorIndex() works", icmTest(function(cm, content) {
    cm.setCursor({line: 1, ch: 0});
    equal(cm.getCursorIndex(), content.indexOf("there"));
  }));
})();

module("Editor");

test("ParsingCodeMirror works", function() {
  var place = $("<div></div>").appendTo(document.body);
  var events = [];
  var fakeTime = {
    id: 0,
    setTimeout: function(cb, ms) {
      this.cb = cb;
      events.push("time.setTimeout(fn, " + ms + ") -> " + this.id);
      return this.id++;
    },
    clearTimeout: function(id) {
      events.push("time.clearTimeout(" + id + ")");
    }
  };
  var cm = ParsingCodeMirror(place[0], {
    mode: "text/plain",
    parseDelay: 1,
    parse: function(code) {
      return {
        error: "here is an error",
        document: "here is a document"
      };
    },
    time: fakeTime
  });
  cm.on("all", function(eventName, arg) {
    events.push("cm.trigger('" + eventName + "')");
  });

  cm.setValue("hello");
  deepEqual(events, [
    "cm.trigger('change')",
    "time.setTimeout(fn, 1) -> 0",
  ], "change event is triggered and timeout function is set");
  events = [];

  cm.on("reparse", function(arg) {
    equal(arg.document, "here is a document", "document passed on reparse");
    equal(arg.error, "here is an error", "error passed on reparse");
    equal(arg.sourceCode, "hello", "source code passed on reparse");
  });
  cm.reparse();
  deepEqual(events, [
    "cm.trigger('reparse')",
    "cm.trigger('cursor-activity')",
  ], "reparse and cursor activity events are triggered");
  cm.off("reparse");
  events = [];

  cm.setValue("hello goober");
  deepEqual(events, [
    "cm.trigger('change')",
    "time.clearTimeout(0)",
    "time.setTimeout(fn, 1) -> 1"
  ], "old timeout function is cleared");
  events = [];

  cm.on("reparse", function(event) {
    equal(event.sourceCode, "hello goober",
          "correct source code is passed on reparse event");
  });
  fakeTime.cb();
  deepEqual(events, [
    "cm.trigger('reparse')",
    "cm.trigger('cursor-activity')"
  ], "reparse/cursor-activity events are triggered by timeout function");
  events = [];
  
  cm.setCursor({line: 0, ch: 2});
  deepEqual(events, ["cm.trigger('cursor-activity')"],
            "cursor-activity event is triggered by setCursor()");

  place.remove();
});

test("MarkTracker works", function() {
  var place = $("<div></div>").appendTo(document.body);
  var cm = CodeMirror(place[0], {mode: "text/plain"});
  var mt = MarkTracker(cm);
  
  cm.setValue("hello");
  mt.mark(2, 4, "blah");
  equal(place.find(".blah").text(), "ll", "source code is marked w/ class");
  mt.clear();
  equal(place.find(".blah").length, 0, "source code class is cleared");

  var thing = $("<div></div>");
  mt.mark(1, 4, "foo", thing[0]);<commit_msg>split out ParsingCodeMirror tests into multiple different tests<commit_after>(function() {
  module("IndexableCodeMirror");
  
  function icmTest(cb) {
    return function() {
      var place = $("<div></div>").appendTo(document.body);
      var cm = IndexableCodeMirror(place[0], {mode: "text/plain"});
      var content = "hello\nthere";
      cm.setValue(content);
      try {
        cb(cm, content);
      } finally {
        place.remove();
      }
    };
  }
  
  test("indexFromCoords() works", icmTest(function(cm, content) {
    equal(cm.indexFromCoords({line: 0, ch: 0}), 0,
          "index of line 0, char 0 is 0");
    equal(cm.indexFromCoords({line: 1, ch: 0}), content.indexOf("there"),
          "index of line 1, char 0 works");
  }));
  
  test("getCursorIndex() works", icmTest(function(cm, content) {
    cm.setCursor({line: 1, ch: 0});
    equal(cm.getCursorIndex(), content.indexOf("there"));
  }));
})();

(function() {
  module("ParsingCodeMirror");
  
  function pcmTest(cb) {
    return function() {
      var place = $("<div></div>").appendTo(document.body);
      var events = [];
      var fakeTime = {
        id: 0,
        setTimeout: function(cb, ms) {
          this.cb = cb;
          events.push("time.setTimeout(fn, " + ms + ") -> " + this.id);
          return this.id++;
        },
        clearTimeout: function(id) {
          events.push("time.clearTimeout(" + id + ")");
        }
      };
      var cm = ParsingCodeMirror(place[0], {
        mode: "text/plain",
        parseDelay: 1,
        parse: function(code) {
          return {
            error: "here is an error",
            document: "here is a document"
          };
        },
        time: fakeTime
      });
      cm.on("all", function(eventName, arg) {
        events.push("cm.trigger('" + eventName + "')");
      });
      try {
        cb(cm, events, fakeTime);
      } finally {
        place.remove();
      }
    };
  }
  
  test("change triggered and timeout set on codeMirror.setValue()",
       pcmTest(function(cm, events, fakeTime) {
         cm.setValue("hello");
         deepEqual(events, [
           "cm.trigger('change')",
           "time.setTimeout(fn, 1) -> 0",
         ]);
       }));

  test("reparse() triggers events and passes expected arguments",
       pcmTest(function(cm, events, fakeTime) {
         cm.setValue("hello"); events.splice(0);
         cm.on("reparse", function(arg) {
           equal(arg.document, "here is a document", "document passed");
           equal(arg.error, "here is an error", "error passed");
           equal(arg.sourceCode, "hello", "source code passed");
         });
         cm.reparse();
         deepEqual(events, [
           "cm.trigger('reparse')",
           "cm.trigger('cursor-activity')",
          ]);
       }));
  
  test("old timeout cancelled on multiple content changes",
       pcmTest(function(cm, events, fakeTime) {
         cm.setValue("hello"); events.splice(0);
         cm.setValue("hello goober");
         deepEqual(events, [
           "cm.trigger('change')",
           "time.clearTimeout(0)",
           "time.setTimeout(fn, 1) -> 1"
         ]);
       }));

  test("timeout function triggers events w/ expected args",
       pcmTest(function(cm, events, fakeTime) {
         cm.setValue("hello goober"); events.splice(0);
         cm.on("reparse", function(event) {
           equal(event.sourceCode, "hello goober",
                 "correct source code is passed on reparse event");
         });
         fakeTime.cb();
         deepEqual(events, [
           "cm.trigger('reparse')",
           "cm.trigger('cursor-activity')"
         ], "events are triggered");
       }));

  test("cursor-activity event is triggered by codeMirror.setCursor()",
       pcmTest(function(cm, events, fakeTime) {
         cm.setValue("hello"); events.splice(0);
         cm.setCursor({line: 0, ch: 2});
         deepEqual(events, ["cm.trigger('cursor-activity')"]);
       }));
})();

module("Editor");

test("MarkTracker works", function() {
  var place = $("<div></div>").appendTo(document.body);
  var cm = CodeMirror(place[0], {mode: "text/plain"});
  var mt = MarkTracker(cm);
  
  cm.setValue("hello");
  mt.mark(2, 4, "blah");
  equal(place.find(".blah").text(), "ll", "source code is marked w/ class");
  mt.clear();
  equal(place.find(".blah").length, 0, "source code class is cleared");

  var thing = $("<div></div>");
  mt.mark(1, 4, "foo", thing[0]);
{'fraction_non_alphanumeric': 0.1836657409745014, 'fraction_numerical': 0.008078767987881848, 'mean_word_length': 2.683403068340307, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '616056', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2513, 'n_words': 560}