2008-11-27 22:30:46 +00:00
|
|
|
// Crockford's helpers
|
|
|
|
|
|
|
|
// Object.create instead of new Object
|
|
|
|
if (typeof Object.create !== 'function') {
|
|
|
|
Object.create = function (o) {
|
2008-12-01 22:24:13 +00:00
|
|
|
var F = function () {
|
|
|
|
};
|
|
|
|
F.prototype = o;
|
|
|
|
return new F();
|
2008-11-27 22:30:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Klass.method instead of Klass.prototype.name = function
|
2008-12-01 20:26:12 +00:00
|
|
|
if (typeof Function.method !== 'function') {
|
|
|
|
Function.prototype.method = function (name, func) {
|
|
|
|
this.prototype[name] = func;
|
|
|
|
return this;
|
|
|
|
}
|
2008-11-27 22:30:46 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 19:35:10 +00:00
|
|
|
/*
|
|
|
|
* Jasmine internal classes & objects
|
|
|
|
*/
|
|
|
|
|
2008-12-04 00:23:17 +00:00
|
|
|
/*
|
|
|
|
* object for holding results; allows for the results array to hold another nestedResults()
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
var nestedResults = function() {
|
|
|
|
var that = {
|
|
|
|
totalCount: 0,
|
|
|
|
passedCount: 0,
|
|
|
|
failedCount: 0,
|
|
|
|
results: [],
|
|
|
|
|
|
|
|
rollupCounts: function (result) {
|
|
|
|
that.totalCount += result.totalCount;
|
|
|
|
that.passedCount += result.passedCount;
|
|
|
|
that.failedCount += result.failedCount;
|
|
|
|
},
|
|
|
|
|
|
|
|
push: function (result) {
|
|
|
|
if (result.results) {
|
|
|
|
that.rollupCounts(result);
|
|
|
|
} else {
|
|
|
|
that.totalCount++;
|
|
|
|
result.passed ? that.passedCount++ : that.failedCount++;
|
|
|
|
}
|
|
|
|
that.results.push(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return that;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* base for Runner & Suite: allows for a queue of functions to get executed, allowing for
|
|
|
|
* any one action to complete, including asynchronous calls, before going to the next
|
|
|
|
* action.
|
|
|
|
*
|
|
|
|
**/
|
2008-12-02 19:21:50 +00:00
|
|
|
var actionCollection = function () {
|
2008-12-04 20:54:54 +00:00
|
|
|
var that = {
|
2008-12-02 19:21:50 +00:00
|
|
|
actions: [],
|
|
|
|
index: 0,
|
|
|
|
finished: false,
|
2008-12-02 21:59:39 +00:00
|
|
|
results: nestedResults(),
|
|
|
|
|
2008-12-04 17:37:36 +00:00
|
|
|
finish: function () {
|
|
|
|
if (that.finishCallback) {
|
|
|
|
that.finishCallback();
|
|
|
|
}
|
|
|
|
that.finished = true;
|
|
|
|
},
|
|
|
|
|
2008-12-02 21:59:39 +00:00
|
|
|
report: function (result) {
|
|
|
|
that.results.push(result);
|
|
|
|
},
|
2008-12-02 19:21:50 +00:00
|
|
|
|
|
|
|
execute: function () {
|
|
|
|
if (that.actions.length > 0) {
|
|
|
|
that.next();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
getCurrentAction: function () {
|
|
|
|
return that.actions[that.index];
|
|
|
|
},
|
|
|
|
|
|
|
|
next: function() {
|
2008-12-02 23:27:07 +00:00
|
|
|
if (that.index >= that.actions.length) {
|
2008-12-04 17:37:36 +00:00
|
|
|
that.finish();
|
2008-12-02 23:27:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentAction = that.getCurrentAction();
|
|
|
|
|
|
|
|
if (that.beforeEach) {
|
|
|
|
that.beforeEach.apply(currentAction);
|
2008-12-02 19:21:50 +00:00
|
|
|
}
|
2008-12-02 23:27:07 +00:00
|
|
|
|
|
|
|
currentAction.execute();
|
|
|
|
that.waitForDone(currentAction);
|
2008-12-02 19:21:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
waitForDone: function(action) {
|
|
|
|
var id = setInterval(function () {
|
|
|
|
if (action.finished) {
|
|
|
|
clearInterval(id);
|
2008-12-02 21:59:39 +00:00
|
|
|
that.report(action.results);
|
2008-12-02 23:27:07 +00:00
|
|
|
|
|
|
|
if (that.afterEach) {
|
|
|
|
that.afterEach.apply(action);
|
|
|
|
}
|
|
|
|
|
2008-12-02 19:21:50 +00:00
|
|
|
that.index++;
|
|
|
|
that.next();
|
|
|
|
}
|
|
|
|
}, 150);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return that;
|
|
|
|
}
|
|
|
|
|
2008-12-08 19:35:10 +00:00
|
|
|
/*
|
|
|
|
* queuedFunction is how actionCollection's actions are implemented
|
|
|
|
*/
|
|
|
|
var queuedFunction = function(func, timeout, spec) {
|
|
|
|
var that = {
|
|
|
|
func: func,
|
|
|
|
next: function () {
|
|
|
|
spec.finish(); // default value is to be done after one function
|
|
|
|
},
|
|
|
|
execute: function () {
|
|
|
|
if (timeout > 0) {
|
|
|
|
setTimeout(function () {
|
|
|
|
that.func.apply(spec);
|
|
|
|
that.next();
|
|
|
|
}, timeout);
|
|
|
|
} else {
|
|
|
|
that.func.apply(spec);
|
|
|
|
that.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return that;
|
|
|
|
}
|
|
|
|
|
2008-11-27 22:30:46 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Jasmine
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/*
|
2008-11-30 02:12:55 +00:00
|
|
|
* Matchers methods; add your own with Matchers.method()
|
2008-11-27 22:30:46 +00:00
|
|
|
*/
|
2008-12-04 00:23:17 +00:00
|
|
|
|
2008-12-01 23:59:41 +00:00
|
|
|
Matchers = function (actual, results) {
|
2008-11-30 02:12:55 +00:00
|
|
|
this.actual = actual;
|
|
|
|
this.passing_message = 'Passed.'
|
2008-12-02 21:59:39 +00:00
|
|
|
this.results = results || nestedResults();
|
2008-11-30 02:12:55 +00:00
|
|
|
}
|
2008-11-27 22:30:46 +00:00
|
|
|
|
2008-11-30 02:12:55 +00:00
|
|
|
Matchers.method('report', function (result, failing_message) {
|
2008-11-27 22:30:46 +00:00
|
|
|
|
2008-12-01 23:59:41 +00:00
|
|
|
this.results.push({
|
2008-11-30 02:12:55 +00:00
|
|
|
passed: result,
|
|
|
|
message: result ? this.passing_message : failing_message
|
|
|
|
});
|
2008-11-27 22:30:46 +00:00
|
|
|
|
2008-11-30 02:12:55 +00:00
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
Matchers.method('should_equal', function (expected) {
|
|
|
|
return this.report((this.actual === expected),
|
2008-12-01 22:24:13 +00:00
|
|
|
'Expected ' + expected + ' but got ' + this.actual + '.');
|
2008-11-30 02:12:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Matchers.method('should_not_equal', function (expected) {
|
|
|
|
return this.report((this.actual !== expected),
|
2008-12-01 22:24:13 +00:00
|
|
|
'Expected ' + expected + ' to not equal ' + this.actual + ', but it does.');
|
2008-11-30 02:12:55 +00:00
|
|
|
});
|
2008-11-27 22:30:46 +00:00
|
|
|
|
2008-11-30 02:12:55 +00:00
|
|
|
/*
|
|
|
|
* Jasmine spec constructor
|
|
|
|
*/
|
2008-12-01 20:26:12 +00:00
|
|
|
|
2008-12-02 18:27:09 +00:00
|
|
|
var it = function (description, func) {
|
2008-12-01 20:26:12 +00:00
|
|
|
var that = {
|
|
|
|
description: description,
|
|
|
|
queue: [],
|
2008-12-01 22:24:13 +00:00
|
|
|
currentTimeout: 0,
|
2008-12-02 00:32:14 +00:00
|
|
|
finished: false,
|
2008-12-02 21:59:39 +00:00
|
|
|
|
|
|
|
results: nestedResults(),
|
2008-12-01 23:59:41 +00:00
|
|
|
|
|
|
|
expects_that: function (actual) {
|
|
|
|
return new Matchers(actual, that.results);
|
|
|
|
},
|
2008-12-01 23:15:34 +00:00
|
|
|
|
2008-12-01 20:26:12 +00:00
|
|
|
waits: function (timeout) {
|
2008-12-01 22:24:13 +00:00
|
|
|
that.currentTimeout = timeout;
|
2008-12-01 20:26:12 +00:00
|
|
|
return that;
|
|
|
|
},
|
2008-12-01 23:15:34 +00:00
|
|
|
|
2008-12-01 22:24:13 +00:00
|
|
|
resetTimeout: function() {
|
|
|
|
that.currentTimeout = 0;
|
|
|
|
},
|
2008-12-01 23:15:34 +00:00
|
|
|
|
2008-12-04 20:54:54 +00:00
|
|
|
finishCallback: function () {
|
|
|
|
if (Jasmine.reporter) {
|
2008-12-06 01:33:35 +00:00
|
|
|
Jasmine.reporter.reportSpecResults(that.results);
|
2008-12-04 20:54:54 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-12-01 22:24:13 +00:00
|
|
|
finish: function() {
|
2008-12-04 20:54:54 +00:00
|
|
|
that.finishCallback();
|
2008-12-02 00:32:14 +00:00
|
|
|
that.finished = true;
|
2008-12-01 22:24:13 +00:00
|
|
|
},
|
2008-12-01 23:15:34 +00:00
|
|
|
|
2008-12-01 20:26:12 +00:00
|
|
|
execute: function () {
|
2008-12-01 22:24:13 +00:00
|
|
|
if (that.queue[0]) {
|
|
|
|
that.queue[0].execute();
|
2008-12-01 20:26:12 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-01 22:24:13 +00:00
|
|
|
};
|
2008-12-01 20:26:12 +00:00
|
|
|
|
|
|
|
var addToQueue = function(func) {
|
2008-12-01 23:15:34 +00:00
|
|
|
var currentFunction = queuedFunction(func, that.currentTimeout, that);
|
2008-12-01 22:24:13 +00:00
|
|
|
that.queue.push(currentFunction);
|
2008-12-01 23:15:34 +00:00
|
|
|
|
|
|
|
if (that.queue.length > 1) {
|
|
|
|
var previousFunction = that.queue[that.queue.length - 2];
|
|
|
|
previousFunction.next = function () {
|
2008-12-01 22:24:13 +00:00
|
|
|
currentFunction.execute();
|
|
|
|
}
|
|
|
|
}
|
2008-12-01 23:15:34 +00:00
|
|
|
|
2008-12-01 22:24:13 +00:00
|
|
|
that.resetTimeout();
|
2008-12-01 20:26:12 +00:00
|
|
|
return that;
|
2008-11-27 22:30:46 +00:00
|
|
|
}
|
2008-12-01 20:26:12 +00:00
|
|
|
|
|
|
|
that.runs = addToQueue;
|
|
|
|
|
2008-12-02 18:27:09 +00:00
|
|
|
currentSuite.specs.push(that);
|
|
|
|
currentSpec = that;
|
2008-12-02 01:57:21 +00:00
|
|
|
|
2008-12-02 18:27:09 +00:00
|
|
|
if (func) {
|
|
|
|
func();
|
|
|
|
}
|
2008-12-02 01:57:21 +00:00
|
|
|
|
2008-12-02 22:26:47 +00:00
|
|
|
that.results.description = description;
|
2008-12-01 20:26:12 +00:00
|
|
|
return that;
|
2008-11-27 22:30:46 +00:00
|
|
|
}
|
|
|
|
|
2008-12-02 18:27:09 +00:00
|
|
|
var runs = function (func) {
|
|
|
|
currentSpec.runs(func);
|
|
|
|
}
|
|
|
|
|
|
|
|
var waits = function (timeout) {
|
|
|
|
currentSpec.waits(timeout);
|
|
|
|
}
|
|
|
|
|
2008-12-02 23:27:07 +00:00
|
|
|
var beforeEach = function (beforeEach) {
|
|
|
|
currentSuite.beforeEach = beforeEach;
|
|
|
|
}
|
|
|
|
|
|
|
|
var afterEach = function (afterEach) {
|
|
|
|
currentSuite.afterEach = afterEach;
|
|
|
|
}
|
2008-12-02 18:27:09 +00:00
|
|
|
|
|
|
|
var describe = function (description, spec_definitions) {
|
2008-12-02 19:21:50 +00:00
|
|
|
var that = actionCollection();
|
2008-12-02 18:27:09 +00:00
|
|
|
|
2008-12-02 19:21:50 +00:00
|
|
|
that.description = description;
|
|
|
|
that.specs = that.actions;
|
2008-12-02 18:27:09 +00:00
|
|
|
|
2008-12-02 01:57:21 +00:00
|
|
|
currentSuite = that;
|
2008-12-04 00:23:17 +00:00
|
|
|
Jasmine.suites.push(that);
|
2008-12-02 18:27:09 +00:00
|
|
|
|
2008-12-02 19:21:50 +00:00
|
|
|
spec_definitions();
|
2008-12-02 22:26:47 +00:00
|
|
|
|
|
|
|
that.results.description = description;
|
|
|
|
|
2008-12-06 01:33:35 +00:00
|
|
|
that.finishCallback = function () {
|
|
|
|
if (Jasmine.reporter) {
|
|
|
|
Jasmine.reporter.reportSuiteResults(that.results);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-02 01:57:21 +00:00
|
|
|
return that;
|
|
|
|
}
|
|
|
|
|
2008-12-04 00:23:17 +00:00
|
|
|
var Runner = function () {
|
2008-12-02 19:21:50 +00:00
|
|
|
var that = actionCollection();
|
2008-12-02 18:27:09 +00:00
|
|
|
|
2008-12-02 19:21:50 +00:00
|
|
|
that.suites = that.actions;
|
2008-12-02 22:26:47 +00:00
|
|
|
that.results.description = 'All Jasmine Suites';
|
2008-12-02 21:59:39 +00:00
|
|
|
|
2008-12-04 18:56:58 +00:00
|
|
|
that.finishCallback = function () {
|
|
|
|
if (that.reporter) {
|
2008-12-06 01:33:35 +00:00
|
|
|
that.reporter.reportRunnerResults(that.results);
|
2008-12-04 18:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-04 00:23:17 +00:00
|
|
|
Jasmine = that;
|
2008-12-02 19:08:12 +00:00
|
|
|
return that;
|
|
|
|
}
|
|
|
|
|
2008-12-04 00:23:17 +00:00
|
|
|
var Jasmine = Runner();
|
2008-12-04 01:11:05 +00:00
|
|
|
var currentSuite;
|
2008-12-02 19:08:12 +00:00
|
|
|
var currentSpec;
|
|
|
|
|
2008-12-08 19:35:10 +00:00
|
|
|
/* JasmineReporters.reporter
|
|
|
|
* Base object that will get called whenever a Spec, Suite, or Runner is done. It is up to
|
|
|
|
* descendants of this object to do something with the results (see json_reporter.js)
|
|
|
|
*/
|
|
|
|
var JasmineReporters = {};
|
2008-12-06 01:33:35 +00:00
|
|
|
|
2008-12-08 19:35:10 +00:00
|
|
|
JasmineReporters.reporter = function (callbacks) {
|
|
|
|
var that = {
|
|
|
|
callbacks: callbacks || {},
|
2008-12-05 18:22:03 +00:00
|
|
|
|
2008-12-08 19:35:10 +00:00
|
|
|
doCallback: function (callback, results) {
|
|
|
|
if (callback) { callback(results); }
|
|
|
|
},
|
2008-12-05 18:22:03 +00:00
|
|
|
|
2008-12-08 19:35:10 +00:00
|
|
|
reportRunnerResults: function (results) { that.doCallback(that.callbacks.runnerCallback, results); },
|
|
|
|
reportSuiteResults: function (results) { that.doCallback(that.callbacks.suiteCallback, results); },
|
|
|
|
reportSpecResults: function (results) { that.doCallback(that.callbacks.specCallback, results);}
|
2008-12-05 18:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return that;
|
2008-12-08 19:35:10 +00:00
|
|
|
}
|