Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Two more assertions: assertError and assertNoError #2

Open
wants to merge 4 commits into from

1 participant

@dandean

Hi Tobie,

This pull request bundles in one more commit which adds two new assertions:

assertError and assertNoError

They're both documented to describe their expectations.

Dan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 507 additions and 193 deletions.
  1. +327 −192 dist/evidence.js
  2. +180 −1 src/assertions.js
View
519 dist/evidence.js
@@ -228,6 +228,32 @@ var Assertions = (function() {
);
}
+ function assertError(func, message) {
+ var throwsError = false;
+ try {
+ func();
+ } catch (e) {
+ throwsError = true;
+ }
+ this._assertExpression(
+ throwsError === true, message || 'Failed assertion.',
+ 'Expected function "%s" to throw an error.', func
+ );
+ }
+
+ function assertNoError(func, message) {
+ var throwsError = false;
+ try {
+ func();
+ } catch (e) {
+ throwsError = true;
+ }
+ this._assertExpression(
+ throwsError === false, message || 'Failed assertion.',
+ 'Did not expect function "%s" to throw an error.', func
+ );
+ }
+
return {
_assertExpression: _assertExpression,
skip: skip,
@@ -245,7 +271,9 @@ var Assertions = (function() {
assertIn: assertIn,
refuteIn: refuteIn,
fail: fail,
- flunk: fail
+ flunk: fail,
+ assertError: assertError,
+ assertNoError: assertNoError
};
})();
Evidence.Assertions = Assertions;
@@ -288,18 +316,20 @@ function TestCase(methodName) {
})();
(function(p) {
- function run(result) {
+
+ function run(result, callback) {
this._result = result;
- defer(this._run, this);
+ this._callback = callback || function() {};
+ defer(function() { this.next(result); }, this);
}
- function _run() {
+
+ function next(result) {
try {
if (this._nextAssertions) {
- this._result.restartTest(this);
+ result.resumeTest(this);
this._nextAssertions(this);
} else {
- /*this._globalProperties = objectKeys(global);*/
- this._result.startTest(this);
+ result.startTest(this);
this.setUp(this);
this[this._methodName](this);
}
@@ -307,7 +337,7 @@ function TestCase(methodName) {
this._filterException(e);
} finally {
if (this._paused) {
- this._result.pauseTest(this);
+ result.pauseTest(this);
} else {
try {
this.tearDown(this);
@@ -315,31 +345,32 @@ function TestCase(methodName) {
this._filterException(e);
} finally {
this._nextAssertions = null;
- this._result.stopTest(this);
- this.parent.next();
+ result.stopTest(this);
+ this._callback(result);
}
}
}
}
function _filterException(e) {
- var name = e.name;
+ var name = e.name,
+ result = this._result;
switch(name) {
case 'AssertionFailedError':
- this._result.addFailure(this, e);
+ result.addFailure(this, e);
break;
case 'AssertionSkippedError':
- this._result.addSkip(this, e);
+ result.addSkip(this, e);
break;
default:
- this._result.addError(this, e);
+ result.addError(this, e);
}
}
function pause(assertions) {
- this._paused = true;
var self = this;
- if (assertions) { this._nextAssertions = assertions; }
+ this._paused = true;
+ if (assertions) { self._nextAssertions = assertions; }
self._timeoutId = global.setTimeout(function() {
self.resume(function() {
self.fail('Test timed out. Testing was not resumed after being paused.');
@@ -352,7 +383,7 @@ function TestCase(methodName) {
this._paused = false;
global.clearTimeout(this._timeoutId);
if (assertions) { this._nextAssertions = assertions; }
- this._run();
+ this.next(this._result);
}
}
@@ -369,7 +400,7 @@ function TestCase(methodName) {
}
p.run = run;
- p._run = _run;
+ p.next = next;
p.addAssertion = addAssertion;
p._filterException = _filterException;
p.pause = pause;
@@ -391,45 +422,41 @@ function TestSuite(name, tests) {
TestSuite.displayName = 'TestSuite';
(function(p) {
- function run(result) {
+ function run(result, callback) {
this._index = 0;
- this._result = result;
+ this._callback = callback || function() {};
result.startSuite(this);
- this.next();
+ this.next(result);
return result;
}
- function next() {
- var next = this._tests[this._index];
+ function next(result) {
+ var self = this,
+ next = self._tests[self._index];
if (next) {
- this._index++;
- next.run(this._result);
+ self._index++;
+ next.run(result, function(result) {
+ self.next(result);
+ });
} else {
- this._result.stopSuite(this);
- if (this.parent) {
- this.parent.next();
- } else {
- this._result.stop(new Date());
- }
+ result.stopSuite(self);
+ self._callback(result);
}
}
function push() {
for (var i = 0, length = arguments.length; i < length; i++) {
- var test = arguments[i];
- test.parent = this;
- this._tests.push(test);
+ this._tests.push(arguments[i]);
}
}
function addTest(test) {
- test.parent = this;
this._tests.push(test);
}
function addTests(tests) {
for (var i = 0, length = tests.length; i < length; i++) {
- this.addTest(tests[i]);
+ this._tests.push(tests[i]);
}
}
@@ -451,6 +478,7 @@ TestSuite.displayName = 'TestSuite';
function toString() {
return this.name;
}
+
p.run = run;
p.next = next;
p.push = push;
@@ -466,10 +494,14 @@ TestRunner.displayName = 'TestRunner';
(function(p) {
function run(suite) {
- suite.parent = null;
- var result = this._makeResult();
- result.start(new Date());
- suite.run(result);
+ var self = this,
+ result = self._makeResult();
+ Evidence.currentResult = result;
+ this._suite = suite;
+ self.start(result);
+ suite.run(result, function(result) {
+ self.stop(result);
+ });
return result;
}
@@ -477,6 +509,16 @@ TestRunner.displayName = 'TestRunner';
return new TestResult();
}
+ function start(result) {
+ result.start();
+ }
+
+ function stop(result) {
+ result.stop();
+ }
+
+ p.start = start;
+ p.stop = stop;
p.run = run;
p._makeResult = _makeResult;
})(TestRunner.prototype);
@@ -698,12 +740,18 @@ TestResult.displayName = 'TestResult';
function pauseTest(testcase) {}
- function restartTest(testcase) {}
+ function resumeTest(testcase) {}
function startSuite(suite) {}
function stopSuite(suite) {}
+ function loadPage(win) {}
+
+ function startPage(win, suite) {}
+
+ function stopPage(win) {}
+
function start(t0) {
this.t0 = t0;
}
@@ -727,27 +775,31 @@ TestResult.displayName = 'TestResult';
p.startTest = startTest;
p.stopTest = stopTest;
p.pauseTest = pauseTest;
- p.restartTest = restartTest;
+ p.resumeTest = resumeTest;
p.startSuite = startSuite;
p.stopSuite = stopSuite;
+ p.loadPage = loadPage;
+ p.startPage = startPage;
+ p.stopPage = stopPage;
p.start = start;
p.stop = stop;
p.toString = toString;
})(TestResult.prototype);
Evidence.TestResult = TestResult;
function TestResultTree(name) {
- this.testCount = 0;
+ this.testCount = 0;
this.assertionCount = 0;
- this.skipCount = 0;
- this.skips = [];
- this.failureCount = 0;
- this.failures = [];
- this.errors = [];
- this.errorCount = 0;
- this.testCount = 0;
+ this.skipCount = 0;
+ this.skips = [];
+ this.failureCount = 0;
+ this.failures = [];
+ this.errors = [];
+ this.errorCount = 0;
+ this.testCount = 0;
this.name = name;
}
+chain(TestResultTree, TestResult);
TestResultTree.displayName = 'TestResultTree';
(function(p) {
@@ -793,10 +845,6 @@ TestResultTree.displayName = 'TestResultTree';
this.currentNode = this.currentNode.parent || this;
}
- function pauseTest(testcase) {}
-
- function restartTest(testcase) {}
-
function startSuite(suite) {
this.createChildNode(suite.name);
}
@@ -805,14 +853,14 @@ TestResultTree.displayName = 'TestResultTree';
this.currentNode = this.currentNode.parent || this;
}
- function start(t0) {
- this.t0 = t0;
+ function start() {
+ this.t0 = new Date();
this.currentNode = this;
}
- function stop(t1) {
- this.t1 = t1;
+ function stop() {
this.currentNode = null;
+ this.t1 = new Date();
}
function toString() {
@@ -841,72 +889,19 @@ TestResultTree.displayName = 'TestResultTree';
child.parent = this;
}
- function toASCIITree(prefix) {
- var str = '',
- results = this.toString(),
- name = this.name || 'Test results',
- childLength = this.children && this.children.length,
- rest,
- max;
-
- prefix = prefix || '';
- max = 100 - results.length - prefix.length;
- max = Math.max(max, 0);
-
- if (name.length > max) {
- name = '...' + name.substr(name.length - max + 3);
- }
-
- rest = (max - name.length);
- str += name;
- str += ' ';
- for (var i = 0; i < rest; i++) { str += '_'; }
- str += ' ';
- str += results;
-
- if (this.errorCount > 0) {
- str += ' E';
- } else if (this.failureCount > 0) {
- str += ' F';
- } else if (this.skipCount > 0) {
- str += ' S';
- }
-
- str += '\n';
-
- if (childLength) {
- for (var i = 0; i < childLength; i++) {
- str += prefix;
- if (i == childLength - 1) { // last
- str += '\'-- ';
- str += this.children[i].toASCIITree(prefix + ' ');
- str += prefix;
- str += '\n';
- } else {
- str += '|-- ';
- str += this.children[i].toASCIITree(prefix + '| ');
- }
- }
- }
- return str;
- }
-
- p.toASCIITree = toASCIITree;
p.createChildNode = createChildNode;
- p.appendChild = appendChild;
- p.addAssertion = addAssertion;
- p.addSkip = addSkip;
- p.addFailure = addFailure;
- p.addError = addError;
- p.startTest = startTest;
- p.stopTest = stopTest;
- p.pauseTest = pauseTest;
- p.restartTest = restartTest;
- p.startSuite = startSuite;
- p.stopSuite = stopSuite;
- p.start = start;
- p.stop = stop;
- p.toString = toString;
+ p.appendChild = appendChild;
+ p.addAssertion = addAssertion;
+ p.addSkip = addSkip;
+ p.addFailure = addFailure;
+ p.addError = addError;
+ p.startTest = startTest;
+ p.stopTest = stopTest;
+ p.startSuite = startSuite;
+ p.stopSuite = stopSuite;
+ p.start = start;
+ p.stop = stop;
+ p.toString = toString;
})(TestResultTree.prototype);
Evidence.TestResultTree = TestResultTree;
var Console = {};
@@ -947,6 +942,14 @@ Logger.NOTSET = 0;
this.log(Logger.DEBUG, template, params);
}
+ function group(title) {
+ this.prefix += ' ';
+ }
+
+ function groupEnd() {
+ this.prefix = this.prefix.substr(0, this.prefix.length - 4);
+ }
+
function log(level, template, params) {
level = level || Logger.NOTSET;
var c = global.console;
@@ -954,7 +957,7 @@ Logger.NOTSET = 0;
var method = Logger.LEVELS[level].toLowerCase();
if (method === 'critical') { method = 'error'; }
method = (method in c) ? method : 'log';
-
+ template = this.prefix + template;
if (level >= this.level) {
if (params) {
params = params.slice(0);
@@ -966,6 +969,9 @@ Logger.NOTSET = 0;
}
}
+ p.prefix = '';
+ p.group = group;
+ p.groupEnd = groupEnd;
p.log = log;
p.critical = critical;
p.error = error;
@@ -1060,9 +1066,9 @@ CommandLineLogger.displayName = 'CommandLineLogger';
function log(level, msg, params) {
level = level || Logger.NOTSET;
if (level >= this.level) {
- var prefix = '';
+ var prefix = this.prefix;
if (level > Logger.INFO) {
- prefix = Logger.LEVELS[level]+ ': ';
+ prefix += Logger.LEVELS[level]+ ': ';
}
if (params) {
msg = UI.printf(msg, params);
@@ -1133,25 +1139,29 @@ ConsoleTestResult.displayName = 'ConsoleTestResult';
this.logger.info('Paused testcase ' + testcase + '.');
}
- function restartTest(testcase) {
+ function resumeTest(testcase) {
this.logger.info('Restarted testcase ' + testcase + '.');
}
function startSuite(suite) {
this.logger.info('Started suite ' + suite + '.');
+ this.logger.group('Suite ' + suite);
}
function stopSuite(suite) {
+ this.logger.groupEnd();
this.logger.info('Completed suite ' + suite + '.');
}
function start(t0) {
_super.start.call(this, t0);
this.logger.info('Started tests.');
+ this.logger.group('Tests');
}
function stop(t1) {
_super.stop.call(this, t1);
+ this.logger.groupEnd();
this.logger.info('Completed tests in ' + ((t1 - this.t0)/1000) + 's.');
this.logger.info(this.toString() + '.');
}
@@ -1163,7 +1173,7 @@ ConsoleTestResult.displayName = 'ConsoleTestResult';
p.startTest = startTest;
p.stopTest = stopTest;
p.pauseTest = pauseTest;
- p.restartTest = restartTest;
+ p.resumeTest = resumeTest;
p.startSuite = startSuite;
p.stopSuite = stopSuite;
p.start = start;
@@ -1193,7 +1203,8 @@ AbstractWidget.displayName = 'Widget';
this.element.appendChild(element);
return child;
}
-
+ p.draw = function() { return this; };
+ p.redraw = function() { return this.draw() };
p.appendChild = appendChild;
p.escapeHTML = escapeHTML;
p.toElement = toElement;
@@ -1209,99 +1220,117 @@ chain(LabelledText, AbstractWidget);
LabelledText.displayName = 'LabelledText';
(function(p) {
- function update(content) {
- content = this.escapeHTML(content);
- content = TEMPLATE.replace('{{ label }}', this.label).replace('{{ content }}', content);
- defer(function() { this.element.innerHTML = content; }, this);
+ function setContent(content) {
+ this._content = this.escapeHTML(content);
+ this._content = TEMPLATE.replace('{{ label }}', this.label).replace('{{ content }}', content);
+ return this;
+ }
+
+ function draw() {
+ this.element.innerHTML = this._content;
return this;
}
var TEMPLATE = '<strong>{{ label }}:</strong> {{ content }}';
- p.update = update;
+ p.setContent = setContent;
+ p.draw = draw;
})(LabelledText.prototype);
Web.LabelledText = LabelledText;
function ProgressBar(width, doc) {
- this.width = width;
- this.level = 0;
+ this._width = width;
+ this._level = 0;
AbstractWidget.call(this, doc);
- this.build();
+ this._build();
}
chain(ProgressBar, AbstractWidget);
ProgressBar.displayName = 'ProgressBar';
(function(p) {
- function build() {
- this.element = this.createDiv(this.width);
+ function _build() {
+ this.element = this._createDiv(this.width);
this.element.id = 'evidence_progress_bar_container';
- this.progressBar = this.createDiv(0);
+ this.progressBar = this._createDiv(0);
this.progressBar.id = 'evidence_progress_bar';
this.element.appendChild(this.progressBar);
return this;
}
- function createDiv(width) {
+ function _createDiv(width) {
var element = this.doc.createElement('div');
element.style.width = width + 'px';
return element;
}
- function update(ratio) {
- var value = Math.floor(ratio * this.width);
- defer(function() {
- this.progressBar.style.width = value + 'px';
- }, this);
+ function draw() {
+ this.progressBar.style.width = this._value + 'px';
+ var className = (Logger.LEVELS[this._level] || '').toLowerCase();
+ this.progressBar.className = className;
+ return this;
+ }
+
+ function setValue(ratio) {
+ this._value = Math.floor(ratio * this._width);
return this;
}
function setLevel(level) {
- if (level > this.level) {
- this.level = level;
- this.progressBar.className = (Logger.LEVELS[level] || '').toLowerCase();
+ if (level > this._level) {
+ this._level = level;
}
return this;
}
- p.build = build;
- p.createDiv = createDiv;
- p.update = update;
+ p._build = _build;
+ p._createDiv = _createDiv;
+ p.draw = draw;
+ p.setValue = setValue;
p.setLevel = setLevel;
})(ProgressBar.prototype);
Web.ProgressBar = ProgressBar;
function WebGUI(doc) {
AbstractWidget.call(this, doc);
+ this._build();
}
chain(WebGUI, AbstractWidget);
WebGUI.displayName = 'WebGUI';
(function(p) {
- function build() {
+ function _build() {
this.element = this.doc.createElement('div');
this.element.id = 'evidence';
- this.appendChild(new LabelledText('User agent string').update(global.navigator.userAgent))
- this.status = this.appendChild(new LabelledText('Status').update('Idle.'));
+ this.appendChild(new LabelledText('User agent string').setContent(global.navigator.userAgent).draw())
+ this.status = this.appendChild(new LabelledText('Status'));
this.progressBar = this.appendChild(new ProgressBar(300));
this.results = this.appendChild(new LabelledText('Results'));
return this;
}
+ function draw() {
+ defer(function() {
+ this.status.draw();
+ this.progressBar.draw();
+ this.results.draw();
+ }, this);
+ }
- function updateResults(txt) {
- txt = this.appendFullStop(txt);
- this.results.update(txt);
+ function setResults(txt) {
+ txt = this._appendFullStop(txt);
+ this.results.setContent(txt);
return this;
}
- function updateStatus(txt) {
- txt = this.appendFullStop(txt);
- this.status.update(txt);
+ function setStatus(txt) {
+ txt = this._appendFullStop(txt);
+ this.status.setContent(txt);
+ this.draw();
return this;
}
- function updateProgressBar(ratio) {
- this.progressBar.update(ratio);
+ function setProgress(ratio) {
+ this.progressBar.setValue(ratio);
return this;
}
@@ -1310,16 +1339,17 @@ WebGUI.displayName = 'WebGUI';
return this;
}
- function appendFullStop(txt) {
+ function _appendFullStop(txt) {
return (txt + '').replace(/\.?\s*$/, '.');
}
- p.build = build;
- p.updateResults = updateResults;
- p.updateStatus = updateStatus;
- p.updateProgressBar = updateProgressBar;
+ p._build = _build;
+ p.setResults = setResults;
+ p.setStatus = setStatus;
+ p.setProgress = setProgress;
p.setLevel = setLevel;
- p.appendFullStop = appendFullStop;
+ p._appendFullStop = _appendFullStop;
+ p.draw = draw;
})(WebGUI.prototype);
Web.GUI = WebGUI;
function WebTestRunner(logger) {
@@ -1340,6 +1370,7 @@ WebTestRunner.displayName = 'WebTestRunner';
Web.TestRunner = WebTestRunner;
function WebTestResult(name) {
TestResultTree.call(this, name);
+ this.pageCount = 0;
}
chain(WebTestResult, TestResultTree);
@@ -1350,77 +1381,103 @@ WebTestResult.displayName = 'WebTestResult';
function addAssertion() {
_super.addAssertion.call(this);
- this.gui.updateResults(this);
+ this.gui.setResults(this);
}
function addSkip(testcase, msg) {
_super.addSkip.call(this, testcase, msg);
var gui = this.gui;
- gui.updateResults(this);
+ gui.setResults(this);
gui.setLevel(Logger.WARN);
- gui.updateStatus('Skipping testcase ' + testcase + ': ' + msg.message);
+ gui.setStatus('Skipping testcase ' + testcase + ': ' + msg.message);
}
function addFailure(testcase, msg) {
_super.addFailure.call(this, testcase, msg);
var gui = this.gui;
- gui.updateResults(this);
+ gui.setResults(this);
gui.setLevel(Logger.ERROR);
- gui.updateStatus(testcase + ': ' + msg.message + ' ' + msg.template, msg.args);
+ gui.setStatus(testcase + ': ' + msg.message + ' ' + msg.template, msg.args);
}
function addError(testcase, error) {
_super.addError.call(this, testcase, error);
var gui = this.gui;
- gui.updateResults(this);
+ gui.setResults(this);
gui.setLevel(Logger.ERROR);
- gui.updateStatus(testcase + ' threw an error. ' + error);
+ gui.setStatus(testcase + ' threw an error. ' + error);
}
function startTest(testcase) {
_super.startTest.call(this, testcase);
- this.gui.updateStatus('Started testcase ' + testcase);
+ this.gui.setStatus('Started testcase ' + testcase);
}
function stopTest(testcase) {
_super.stopTest.call(this, testcase);
var gui = this.gui;
- gui.updateProgressBar(this.testCount / this.size);
- gui.updateStatus('Completed testcase ' + testcase);
+ gui.setProgress(this.getRatio());
+ gui.setStatus('Completed testcase ' + testcase);
}
function pauseTest(testcase) {
- this.gui.updateStatus('Paused testcase ' + testcase + '...');
+ this.gui.setStatus('Paused testcase ' + testcase + '...');
}
- function restartTest(testcase) {
- this.gui.updateStatus('Restarted testcase ' + testcase);
+ function resumeTest(testcase) {
+ this.gui.setStatus('Resumed testcase ' + testcase);
}
function startSuite(suite) {
_super.startSuite.call(this, suite);
if (!this.size) { this.size = suite.size(); }
- this.gui.updateStatus('Started suite ' + suite);
+ this.gui.setStatus('Started suite ' + suite);
}
function stopSuite(suite) {
_super.stopSuite.call(this, suite);
- this.gui.updateStatus('Completed suite ' + suite);
+ this.gui.setStatus('Completed suite ' + suite);
}
- function start(t0) {
- _super.start.call(this, t0);
+ function loadPage(page) {
+ this.gui.setStatus('Loading page ' + page.location.pathname + '...');
+ }
+
+ function startPage(page, suite) {
+ this.pageSize = suite.size();
+ this.previousTestCount = this.testCount;
+ this.gui.setStatus('Loaded page ' + page.location.pathname);
+ }
+
+ function stopPage(page) {
+ this.pageCount++;
+ this.gui.setStatus('Finished page ' + page.location.pathname);
+ }
+
+ function getRatio() {
+ if (!this.pageSize) {
+ return this.testCount / this.size;
+ }
+ var pageRatio = (this.testCount - this.previousTestCount) / this.pageSize;
+ return (pageRatio + this.pageCount) / this.size;
+ }
+
+ function start() {
+ _super.start.call(this);
var gui = new WebGUI(document);
this.gui = gui;
- document.body.appendChild(gui.build().toElement());
- gui.updateResults(this);
+ document.body.appendChild(gui.toElement());
+ gui.setResults(this);
}
- function stop(t1) {
- _super.stop.call(this, t1);
- this.gui.updateStatus('Completed tests in ' + ((t1 - this.t0)/1000) + 's');
+ function stop() {
+ _super.stop.call(this);
+ this.gui.setStatus('Completed tests in ' + ((this.t1 - this.t0)/1000) + 's');
+
+ console.log(new AsciiViewBuilder(this).draw())
}
+ p.getRatio = getRatio;
p.addAssertion = addAssertion;
p.addSkip = addSkip;
p.addFailure = addFailure;
@@ -1428,15 +1485,93 @@ WebTestResult.displayName = 'WebTestResult';
p.startTest = startTest;
p.stopTest = stopTest;
p.pauseTest = pauseTest;
- p.restartTest = restartTest;
+ p.resumeTest = resumeTest;
p.startSuite = startSuite;
p.stopSuite = stopSuite;
+ p.loadPage = loadPage;
+ p.startPage = startPage;
+ p.stopPage = stopPage;
p.start = start;
p.stop = stop;
})(WebTestResult.prototype);
Web.TestResult = WebTestResult;
+function AsciiViewBuilder(result) {
+ this.prefix = '';
+ this._result = result;
+}
+
+AsciiViewBuilder.name = AsciiViewBuilder.displayName = 'AsciiViewBuilder';
+
+(function(p) {
+
+ function draw() {
+ return this._build(this._result);
+ }
+
+ function _build(r) {
+ var rString = r.toString(),
+ max = 100 - rString.length - this.prefix.length,
+ str = r.name || 'Anonymous TestSuite';
+
+ str = this._truncate(str, max);
+ str += ' ' + this._times('.', max - str.length) + ' ';
+ str += rString;
+ str += this._displayStatus(r)
+ str += '\n';
+
+ var length = r.children ? r.children.length : 0,
+ i;
+ for (i = 0; i < length; i++) {
+ if (i === length - 1) { // last
+ str += this._buildChild(' ', '\'-- ', r.children[i], this.prefix + '\n');
+ } else {
+ str += this._buildChild('| ', '|-- ', r.children[i]);
+ }
+ }
+ return str;
+ }
+
+ function _buildChild(modifier, prefix, child, suffix) {
+ var str, original = this.prefix;
+ suffix = suffix || '';
+ this.prefix += modifier;
+ str = original + prefix + this._build(child) + suffix;
+ this.prefix = original;
+ return str;
+ }
+
+ function _truncate(str, size) {
+ size = Math.max(size, 0);
+ if (str.length > size) {
+ return '...' + str.substr(str.length - size + 3);
+ }
+ return str;
+ }
+
+ function _times(c, times) {
+ var str = '';
+ for (var i = 0; i < times; i++) { str += c; }
+ return str;
+ }
+
+ function _displayStatus(r) {
+ if (r.children) { return ''; }
+ if (r.errorCount > 0) { return ' E'; }
+ if (r.failureCount > 0) { return ' F'; }
+ if (r.skipCount > 0) { return ' S'; }
+ return '';
+ }
+
+ p.draw = draw;
+ p._build = _build;
+ p._buildChild = _buildChild;
+ p._displayStatus = _displayStatus;
+ p._times = _times;
+ p._truncate = _truncate;
+})(AsciiViewBuilder.prototype);
+
var UI = (function() {
function printf(template, args, inspector) {
var parts = [],
View
181 src/assertions.js
@@ -15,14 +15,34 @@ var Assertions = (function() {
}
}
+ /**
+ * Assertions.skip(message) -> undefined
+ * - message(String): The message to log when the test case is skipped.
+ *
+ * Skips the rest of the current test case and logs `message`.
+ **/
function skip(message) {
throw new AssertionSkippedError(message || 'Skipped!');
}
+ /**
+ * Assertions.fail(message) -> undefined
+ * - message(String): The message to log when the test case is failed.
+ *
+ * Fails the current test case and logs `message`. The rest of the test is
+ * not executed.
+ **/
function fail(message) {
this._assertExpression(false, message || 'Flunked!');
}
+ /**
+ * Assertions.assert(test, message) -> undefined
+ * - test(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `test` is not falsy, otherwise the test case fails.
+ **/
function assert(test, message) {
this._assertExpression(
!!test,
@@ -31,6 +51,13 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.refute(test, message) -> undefined
+ * - test(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `test` is falsy, otherwise the test case fails.
+ **/
function refute(test, message) {
this._assertExpression(
!test,
@@ -39,6 +66,13 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.assertTrue(test, message) -> undefined
+ * - test(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `test === true`, otherwise the test case fails.
+ **/
function assertTrue(test, message) {
this._assertExpression(
(test === true),
@@ -47,6 +81,13 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.refuteTrue(test, message) -> undefined
+ * - test(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `test !== true`, otherwise the test case fails.
+ **/
function refuteTrue(test, message) {
this._assertExpression(
(test !== true),
@@ -55,6 +96,13 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.assertNull(test, message) -> undefined
+ * - test(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `test === null`, otherwise the test case fails.
+ **/
function assertNull(test, message) {
this._assertExpression(
(test === null),
@@ -63,6 +111,13 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.refuteNull(test, message) -> undefined
+ * - test(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `test !== null`, otherwise the test case fails.
+ **/
function refuteNull(test, message) {
this._assertExpression(
(test !== null),
@@ -71,6 +126,13 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.assertUndefined(test, message) -> undefined
+ * - test(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `typeof test === 'undefined'`, otherwise the test case fails.
+ **/
function assertUndefined(test, message) {
this._assertExpression(
(typeof test === 'undefined'),
@@ -79,6 +141,13 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.refuteUndefined(test, message) -> undefined
+ * - test(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `typeof test !== 'undefined'`, otherwise the test case fails.
+ **/
function refuteUndefined(test, message) {
this._assertExpression(
(typeof test !== 'undefined'),
@@ -87,6 +156,13 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.assertFalse(test, message) -> undefined
+ * - test(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `test === false`, otherwise the test case fails.
+ **/
function assertFalse(test, message) {
this._assertExpression(
(test === false),
@@ -95,6 +171,13 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.refuteFalse(test, message) -> undefined
+ * - test(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `test !== false`, otherwise the test case fails.
+ **/
function refuteFalse(test, message) {
this._assertExpression(
(test !== false),
@@ -103,6 +186,14 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.assertEqual(expected, actual, message) -> undefined
+ * - expected(?): The expected value of `actual`.
+ * - actual(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `expected == actual`, otherwise the test case fails.
+ **/
function assertEqual(expected, actual, message) {
this._assertExpression(
(expected == actual),
@@ -111,6 +202,14 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.refuteEqual(expected, actual, message) -> undefined
+ * - expected(?): The expected value of `actual`.
+ * - actual(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `expected != actual`, otherwise the test case fails.
+ **/
function refuteEqual(expected, actual, message) {
this._assertExpression(
(expected != actual),
@@ -119,6 +218,14 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.assertIdentical(expected, actual, message) -> undefined
+ * - expected(?): The expected exact value of `actual`.
+ * - actual(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `expected === actual`, otherwise the test case fails.
+ **/
function assertIdentical(expected, actual, message) {
this._assertExpression(
(expected === actual),
@@ -127,6 +234,14 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.refuteIdentical(expected, actual, message) -> undefined
+ * - expected(?): The expected exact value of `actual`.
+ * - actual(?): The object to test.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `expected !== actual`, otherwise the test case fails.
+ **/
function refuteIdentical(expected, actual, message) {
this._assertExpression(
(expected !== actual),
@@ -135,6 +250,14 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.assertIn(property, object, message) -> undefined
+ * - property(String): A property name in an object.
+ * - object(Object): The object which should have property `property`.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `property in object`, otherwise the test case fails.
+ **/
function assertIn(property, object, message) {
this._assertExpression(
(property in object),
@@ -143,6 +266,14 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.refuteIn(property, object, message) -> undefined
+ * - property(String): A property name in an object.
+ * - object(Object): The object which should NOT have property `property`.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `!(property in object)`, otherwise the test case fails.
+ **/
function refuteIn(property, object, message) {
this._assertExpression(
!(property in object),
@@ -151,11 +282,54 @@ var Assertions = (function() {
);
}
+ /**
+ * Assertions.assertError(func, message) -> undefined
+ * - func(Function): A function that should throw an error when executed.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `func` will throw an error when executed, otherwise the test case fails.
+ **/
+ function assertError(func, message) {
+ var throwsError = false;
+ try {
+ func();
+ } catch (e) {
+ throwsError = true;
+ }
+ this._assertExpression(
+ throwsError === true, message || 'Failed assertion.',
+ 'Expected function "%s" to throw an error.', func
+ );
+ }
+
+ /**
+ * Assertions.assertNoError(func, message) -> undefined
+ * - func(Function): A function that should NOT throw an error when executed.
+ * - message(String): The message to log when the test case fails.
+ *
+ * Asserts `func` will NOT throw an error when executed, otherwise the test case fails.
+ **/
+ function assertNoError(func, message) {
+ var throwsError = false;
+ try {
+ func();
+ } catch (e) {
+ throwsError = true;
+ }
+ this._assertExpression(
+ throwsError === false, message || 'Failed assertion.',
+ 'Did not expect function "%s" to throw an error.', func
+ );
+ }
+
return {
_assertExpression: _assertExpression,
skip: skip,
assert: assert,
refute: refute,
+ /** alias of: Assertions.refute
+ * Assertions.assertNot(test, message) -> undefined
+ **/
assertNot: refute,
assertTrue: assertTrue,
assertNull: assertNull,
@@ -168,6 +342,11 @@ var Assertions = (function() {
assertIn: assertIn,
refuteIn: refuteIn,
fail: fail,
- flunk: fail
+ /** alias of: Assertions.fail
+ * Assertions.flunk(message) -> undefined
+ **/
+ flunk: fail,
+ assertError: assertError,
+ assertNoError: assertNoError
};
})();
Something went wrong with that request. Please try again.