uspec.js: first public release

This commit is contained in:
Loic Nageleisen 2013-08-19 18:44:10 +02:00
commit ec516c106c
8 changed files with 769 additions and 0 deletions

398
uspec.js Normal file
View file

@ -0,0 +1,398 @@
// Generated by CoffeeScript 1.6.2
/*
# uspec.js v0.5
# (c) 2013 Loic Nageleisen
# Licensed under 3-clause BSD
*/
(function() {
var Ansi, AnsiDotWriter, AnsiWriter, AssertionError, DotWriter, FAIL, Log, NaiveWriter, NullWriter, PASS, PENDING, PendingError, all_examples, assert, assert_throws, describe, indent, pending, run, summary;
Ansi = (function() {
function Ansi() {}
Ansi.CSI = "\x1B[";
Ansi.END = "m";
Ansi.BLACK = 0;
Ansi.RED = 1;
Ansi.GREEN = 2;
Ansi.YELLOW = 3;
Ansi.BLUE = 4;
Ansi.MAGENTA = 5;
Ansi.CYAN = 6;
Ansi.WHITE = 7;
Ansi.FORE = 30;
Ansi.BACK = 40;
Ansi.SGR_RESET = Ansi.CSI + Ansi.END;
Ansi.SGR_BLACK = Ansi.CSI + (Ansi.FORE + Ansi.BLACK) + Ansi.END;
Ansi.SGR_RED = Ansi.CSI + (Ansi.FORE + Ansi.RED) + Ansi.END;
Ansi.SGR_GREEN = Ansi.CSI + (Ansi.FORE + Ansi.GREEN) + Ansi.END;
Ansi.SGR_YELLOW = Ansi.CSI + (Ansi.FORE + Ansi.YELLOW) + Ansi.END;
Ansi.SGR_BLUE = Ansi.CSI + (Ansi.FORE + Ansi.BLUE) + Ansi.END;
Ansi.SGR_MAGENTA = Ansi.CSI + (Ansi.FORE + Ansi.MAGENTA) + Ansi.END;
Ansi.SGR_CYAN = Ansi.CSI + (Ansi.FORE + Ansi.CYAN) + Ansi.END;
Ansi.SGR_WHITE = Ansi.CSI + (Ansi.FORE + Ansi.WHITE) + Ansi.END;
return Ansi;
})();
indent = function(str, level) {
if (level == null) {
level = 0;
}
if (level > 0) {
str = indent(str, level - 1);
}
return str.replace(/^/gm, ' ');
};
all_examples = {};
describe = function(target) {
var examples, setup, teardown, _ref;
_ref = (function() {
switch (arguments.length) {
case 2:
return [
(function() {
return {};
}), (function() {}), arguments[1]
];
case 3:
return [arguments[1], (function() {}), arguments[2]];
default:
return [arguments[1], arguments[2], arguments[3]];
}
}).apply(this, arguments), setup = _ref[0], teardown = _ref[1], examples = _ref[2];
return all_examples[target] = {
setup: setup,
teardown: teardown,
examples: examples
};
};
AssertionError = (function() {
function AssertionError(message) {
this.message = message;
}
AssertionError.prototype.toString = function() {
return "AssertionError: " + this.message;
};
return AssertionError;
})();
PendingError = (function() {
function PendingError(message) {
this.message = message;
}
PendingError.prototype.toString = function() {
return "PendingError: " + this.message;
};
return PendingError;
})();
assert = function(callback) {
if (callback() !== true) {
throw new AssertionError("assertion failed:\n" + (indent(callback.toString())));
}
};
assert_throws = function(exception, callback) {
var e;
try {
callback();
} catch (_error) {
e = _error;
if (e instanceof exception) {
return;
}
throw e;
}
throw new AssertionError("assertion failed:\n" + (indent(callback.toString())));
};
pending = function(message) {
if (message == null) {
message = '';
}
throw new PendingError(message);
};
run = function(log) {
var e, example, info, target, test, _ref;
if (log == null) {
log = new Log(new AnsiWriter);
}
for (target in all_examples) {
info = all_examples[target];
log.current(target);
_ref = info.examples;
for (example in _ref) {
test = _ref[example];
try {
if (test.toString() === (function() {}).toString()) {
throw new PendingError;
}
test.call(info.setup());
info.teardown();
log.pass(example);
} catch (_error) {
e = _error;
if (e instanceof AssertionError) {
log.fail(example, e);
} else if (e instanceof PendingError) {
log.pending(example, e);
} else {
log.fail(example, e);
}
}
}
}
return log.results;
};
PASS = 'pass';
FAIL = 'fail';
PENDING = 'pending';
Log = (function() {
function Log(writer) {
this.writer = writer != null ? writer : new NaiveWriter;
this.results = {};
}
Log.prototype.log = function(example, status, message) {
if (message == null) {
message = '';
}
return this.results[this.target][example] = {
status: status,
message: message
};
};
Log.prototype.current = function(target) {
this.target = target;
this.results[this.target] = {};
return this.writer.current(target);
};
Log.prototype.pass = function(example) {
this.log(example, PASS);
return this.writer.pass(example);
};
Log.prototype.fail = function(example, e) {
this.log(example, FAIL, e.message);
return this.writer.fail(example);
};
Log.prototype.pending = function(example, p) {
this.log(example, PENDING);
return this.writer.pending(example, p.message);
};
return Log;
})();
summary = function(results) {
var counter, example, examples, result, target, total;
total = {
pass: 0,
fail: 0,
pending: 0
};
for (target in results) {
examples = results[target];
for (example in examples) {
result = examples[example];
switch (result.status) {
case PASS:
total.pass += 1;
break;
case FAIL:
total.fail += 1;
break;
case PENDING:
total.pending += 1;
}
}
}
total.all = total.pass + total.fail + total.pending;
console.log('');
if (total.fail > 0) {
console.log('Failures:\n');
}
counter = 0;
for (target in results) {
examples = results[target];
for (example in examples) {
result = examples[example];
if (result.status === FAIL) {
counter += 1;
console.log(indent("" + counter + ") " + Ansi.SGR_RED + target + " " + example + Ansi.SGR_RESET));
if (result.message !== void 0) {
console.log(indent(result.message, 2) + "\n");
}
}
}
}
console.log("" + total.all + " examples, " + total.fail + " failure, " + total.pending + " pending");
return total.fail === 0;
};
AnsiWriter = (function() {
function AnsiWriter() {}
AnsiWriter.prototype.current = function(target) {
return console.log("\n" + target);
};
AnsiWriter.prototype.pass = function(example) {
return console.log(" " + Ansi.SGR_GREEN + example + Ansi.SGR_RESET);
};
AnsiWriter.prototype.fail = function(example) {
return console.log(" " + Ansi.SGR_RED + example + Ansi.SGR_RESET);
};
AnsiWriter.prototype.pending = function(example) {
return console.log(" " + Ansi.SGR_YELLOW + example + Ansi.SGR_RESET);
};
return AnsiWriter;
})();
AnsiDotWriter = (function() {
function AnsiDotWriter() {}
AnsiDotWriter.prototype.current = function(target) {};
AnsiDotWriter.prototype.pass = function(example) {
return console.log("" + Ansi.SGR_GREEN + "." + Ansi.SGR_RESET);
};
AnsiDotWriter.prototype.fail = function(example) {
return console.log("" + Ansi.SGR_RED + "F" + Ansi.SGR_RESET);
};
AnsiDotWriter.prototype.pending = function(example) {
return console.log("" + Ansi.SGR_YELLOW + "#" + Ansi.SGR_RESET);
};
return AnsiDotWriter;
})();
DotWriter = (function() {
function DotWriter() {}
DotWriter.prototype.current = function(target) {};
DotWriter.prototype.pass = function(example) {
return console.log(".");
};
DotWriter.prototype.fail = function(example) {
return console.log("F");
};
DotWriter.prototype.pending = function(example) {
return console.log("#");
};
return DotWriter;
})();
NaiveWriter = (function() {
function NaiveWriter() {}
NaiveWriter.prototype.current = function(target) {
return console.log("" + target);
};
NaiveWriter.prototype.pass = function(example) {
return console.log(" " + example + ": pass");
};
NaiveWriter.prototype.fail = function(example) {
return console.log(" " + example + ": fail");
};
NaiveWriter.prototype.pending = function(example) {
return console.log(" " + example + ": pending");
};
return NaiveWriter;
})();
NullWriter = (function() {
function NullWriter() {}
NullWriter.prototype.current = function() {};
NullWriter.prototype.pass = function() {};
NullWriter.prototype.fail = function() {};
NullWriter.prototype.pending = function() {};
return NullWriter;
})();
exports.run = run;
exports.describe = describe;
exports.assert = assert;
exports.assert_throws = assert_throws;
exports.pending = pending;
exports.summary = summary;
exports.AssertionError = AssertionError;
exports.PendingError = PendingError;
}).call(this);