Allow tests to run in random order
This commit is contained in:
46
src/core/Order.js
Normal file
46
src/core/Order.js
Normal file
@@ -0,0 +1,46 @@
|
||||
/*jshint bitwise: false*/
|
||||
|
||||
getJasmineRequireObj().Order = function() {
|
||||
function Order(options) {
|
||||
this.random = 'random' in options ? options.random : true;
|
||||
var seed = this.seed = options.seed || generateSeed();
|
||||
this.sort = this.random ? randomOrder : naturalOrder;
|
||||
|
||||
function naturalOrder(items) {
|
||||
return items;
|
||||
}
|
||||
|
||||
function randomOrder(items) {
|
||||
var copy = items.slice();
|
||||
copy.sort(function(a, b) {
|
||||
return jenkinsHash(seed + a.id) - jenkinsHash(seed + b.id);
|
||||
});
|
||||
return copy;
|
||||
}
|
||||
|
||||
function generateSeed() {
|
||||
return String(Math.random()).slice(-5);
|
||||
}
|
||||
|
||||
// Bob Jenkins One-at-a-Time Hash algorithm is a non-cryptographic hash function
|
||||
// used to get a different output when the key changes slighly.
|
||||
// We use your return to sort the children randomly in a consistent way when
|
||||
// used in conjunction with a seed
|
||||
|
||||
function jenkinsHash(key) {
|
||||
var hash, i;
|
||||
for(hash = i = 0; i < key.length; ++i) {
|
||||
hash += key.charCodeAt(i);
|
||||
hash += (hash << 10);
|
||||
hash ^= (hash >> 6);
|
||||
}
|
||||
hash += (hash << 3);
|
||||
hash ^= (hash >> 11);
|
||||
hash += (hash << 15);
|
||||
return hash;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return Order;
|
||||
};
|
||||
Reference in New Issue
Block a user