Skip to content

Commit

Permalink
Fix a number of ECMA-5-isms in the test runner
Browse files Browse the repository at this point in the history
We do still support IE7/8.

Also removes some non-CodeMirror-code-style things like left-aligned
commas and semicolons.
  • Loading branch information
marijnh committed Sep 7, 2012
1 parent fafa9d2 commit cab9417
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
24 changes: 14 additions & 10 deletions test/driver.js
Expand Up @@ -2,11 +2,18 @@ var tests = [], debug = null, debugUsed = new Array(), allNames = [];

function Failure(why) {this.message = why;}

function indexOf(collection, elt) {
if (collection.indexOf) return collection.indexOf(elt);
for (var i = 0, e = collection.length; i < e; ++i)
if (collection[i] == elt) return i;
return -1;
}

function test(name, run, expectedFail) {
// Force unique names
var originalName = name;
var i = 2; // Second function would be NAME_2
while(allNames.indexOf(name) !== -1){
while (indexOf(allNames, name) !== -1){
i++;
name = originalName + "_" + i;
}
Expand All @@ -32,7 +39,7 @@ function testCM(name, run, opts, expectedFail) {

function runTests(callback) {
if (debug) {
if (debug.indexOf("verbose") === 0) {
if (indexOf(debug, "verbose") === 0) {
verbose = true;
debug.splice(0, 1);
}
Expand All @@ -50,24 +57,21 @@ function runTests(callback) {
running = false;
return callback("done");
}
var test = tests[i]
, expFail = test.expectedFail
, startTime = Date.now()
;
var test = tests[i], expFail = test.expectedFail, startTime = +new Date;
if (debug !== null) {
var debugIndex = debug.indexOf(test.name);
var debugIndex = indexOf(debug, test.name);
if (debugIndex !== -1) {
// Remove from array for reporting incorrect tests later
debug.splice(debugIndex, 1);
} else {
var wildcardName = test.name.split("_").shift() + "_*";
debugIndex = debug.indexOf(wildcardName);
debugIndex = indexOf(debug, wildcardName);
if (debugIndex !== -1) {
// Remove from array for reporting incorrect tests later
debug.splice(debugIndex, 1);
debugUsed.push(wildcardName);
} else {
debugIndex = debugUsed.indexOf(wildcardName);
debugIndex = indexOf(debugUsed, wildcardName);
if (debugIndex !== -1) {
totalTests++;
} else {
Expand All @@ -87,7 +91,7 @@ function runTests(callback) {
}
if (!quit) { // Run next test
var delay = 0;
totalTime += (Date.now() - startTime);
totalTime += (+new Date) - startTime;
if (totalTime > 500){
totalTime = 0;
delay = 50;
Expand Down
27 changes: 13 additions & 14 deletions test/index.html
Expand Up @@ -41,25 +41,24 @@ <h1>CodeMirror: Test Suite</h1>
window.onload = function() {
runHarness();
};
window.addEventListener('hashchange', function(){
CodeMirror.connect(window, 'hashchange', function(){
runHarness();
});

function esc(str) {
return str.replace(/[<&]/, function(ch) { return ch == "<" ? "&lt;" : "&amp;"; });
}

var output = document.getElementById("output")
, progress = document.getElementById("progress")
, progressRan = document.getElementById("progress_ran").childNodes[0]
, progressTotal = document.getElementById("progress_total").childNodes[0];
var count = 0
, failed = 0
, bad = ""
, running = false // Flag that states tests are running
, quit = false // Flag to quit tests ASAP
, verbose = false // Adds message for *every* test to output
;
var output = document.getElementById("output"),
progress = document.getElementById("progress"),
progressRan = document.getElementById("progress_ran").childNodes[0],
progressTotal = document.getElementById("progress_total").childNodes[0];
var count = 0,
failed = 0,
bad = "",
running = false, // Flag that states tests are running
quit = false, // Flag to quit tests ASAP
verbose = false; // Adds message for *every* test to output

function runHarness(){
if (running) {
Expand Down Expand Up @@ -96,12 +95,12 @@ <h1>CodeMirror: Test Suite</h1>
if (!message) throw("must provide message");
var status = document.getElementById("status").childNodes[0];
status.nodeValue = message;
status.parentNode.setAttribute("class", className);
status.parentNode.className = className;
}
function addOutput(name, className, code){
var newOutput = document.createElement("dl");
var newTitle = document.createElement("dt");
newTitle.setAttribute("class", className);
newTitle.className = className;
newTitle.appendChild(document.createTextNode(name));
newOutput.appendChild(newTitle);
var newMessage = document.createElement("dd");
Expand Down

3 comments on commit cab9417

@0b10011
Copy link
Contributor

@0b10011 0b10011 commented on cab9417 Sep 7, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marijnh Indentation is off a bit on line 60/61, if you wanted to fix.

Also, I knew the indexOf() calls may cause an issue in some browsers, but you seem to use it quite a bit in lib/codemirror.js, so I thought it was fine (e.g. in onKeyPress(), skipTo(), match(), etc). But, if it's not, you may want to consider using this code instead (pulled from Array indexOf method | MDN):

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
        "use strict";
        if (this == null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 1) {
            n = Number(arguments[1]);
            if (n != n) { // shortcut for verifying if it's NaN
                n = 0;
            } else if (n != 0 && n != Infinity && n != -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
}

@marijnh
Copy link
Member Author

@marijnh marijnh commented on cab9417 Sep 7, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but you seem to use it quite a bit in lib/codemirror.js'

That's on strings. ECMA 3 defines indexOf on strings but not on arrays.

I don't think it'd be a very good idea for a library to pollute standard prototypes. In the test driver, it would be less of an issue, but still, I'd rather not.

@0b10011
Copy link
Contributor

@0b10011 0b10011 commented on cab9417 Sep 7, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's on strings. ECMA 3 defines indexOf on strings but not on arrays.

Learn something new every day. Thanks!

Please sign in to comment.