Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save devinrhode2/9dde25f66a8adeae72f36eee5c712271 to your computer and use it in GitHub Desktop.
Save devinrhode2/9dde25f66a8adeae72f36eee5c712271 to your computer and use it in GitHub Desktop.
assertEqual
assertEqual: function(actual, expected) {
// Uses TraceKit to get stacktrace of caller,
// it looks for the line number of the first anonymous eval
// Stack traces are pretty nasty and not standardized yet
// so this is not as elegant as one might hope.
// Safari doesn't even give line numbers for anonymous evals,
// so they can go sit in the dunce corner today.
// This returns 0 if not found, which will mean that all
// the assertion failures are shown on the first line.
var getLineNum = function(stacktrace) {
var err = new Error();
TraceKit.remoteFetching = false;
TraceKit.collectWindowErrors = false;
var stacktrace = TraceKit.computeStackTrace.ofCaller();
var lines = stacktrace.stack;
for (var i = 0; i < lines.length; i++) {
if (lines[i].func === "Object.apply.get.message") {
// Chrome
return lines[i].line - 5;
} else if (lines[i].func === "anonymous/<") {
// Firefox
return lines[i].line - 4;
}
}
return -1;
};
if (_.isEqual(actual, expected)) {
return;
}
var msg = $._("Assertion failed: " +
"%(actual)s is not equal to %(expected)s.", {
actual: JSON.stringify(actual),
expected: JSON.stringify(expected)
});
var lineNum = getLineNum();
// Display on first line if we didn't find a line #
if (lineNum < 0) {
lineNum = 0;
}
this.output.assertions.push({
row: lineNum, column: 0, text: msg
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment