app/assets/javascripts/netzke/testing/expect/expect.js in netzke-testing-1.0.0.0.pre vs app/assets/javascripts/netzke/testing/expect/expect.js in netzke-testing-1.0.0.0
- old
+ new
@@ -1,12 +1,8 @@
-
(function (global, module) {
- if ('undefined' == typeof module) {
- var module = { exports: {} }
- , exports = module.exports
- }
+ var exports = module.exports;
/**
* Exports.
*/
@@ -15,11 +11,11 @@
/**
* Exports version.
*/
- expect.version = '0.1.2';
+ expect.version = '0.3.1';
/**
* Possible assertion flags.
*/
@@ -54,11 +50,11 @@
}
}
}
var $flags = flag ? flags[flag] : keys(flags)
- , self = this
+ , self = this;
if ($flags) {
for (var i = 0, l = $flags.length; i < l; i++) {
// avoid recursion
if (this.flags[$flags[i]]) continue;
@@ -69,11 +65,11 @@
if ('function' == typeof Assertion.prototype[name]) {
// clone the function, make sure we dont touch the prot reference
var old = this[name];
this[name] = function () {
return old.apply(self, arguments);
- }
+ };
for (var fn in Assertion.prototype) {
if (Assertion.prototype.hasOwnProperty(fn) && fn != name) {
this[name][fn] = bind(assertion[fn], assertion);
}
@@ -81,24 +77,31 @@
} else {
this[name] = assertion;
}
}
}
- };
+ }
/**
* Performs an assertion
*
* @api private
*/
- Assertion.prototype.assert = function (truth, msg, error) {
+ Assertion.prototype.assert = function (truth, msg, error, expected) {
var msg = this.flags.not ? error : msg
- , ok = this.flags.not ? !truth : truth;
+ , ok = this.flags.not ? !truth : truth
+ , err;
if (!ok) {
- throw new Error(msg.call(this));
+ err = new Error(msg.call(this));
+ if (arguments.length > 3) {
+ err.actual = this.obj;
+ err.expected = expected;
+ err.showDiff = true;
+ }
+ throw err;
}
this.and = new Assertion(this.obj);
};
@@ -114,40 +117,54 @@
, function(){ return 'expected ' + i(this.obj) + ' to be truthy' }
, function(){ return 'expected ' + i(this.obj) + ' to be falsy' });
};
/**
+ * Creates an anonymous function which calls fn with arguments.
+ *
+ * @api public
+ */
+
+ Assertion.prototype.withArgs = function() {
+ expect(this.obj).to.be.a('function');
+ var fn = this.obj;
+ var args = Array.prototype.slice.call(arguments);
+ return expect(function() { fn.apply(null, args); });
+ };
+
+ /**
* Assert that the function throws.
*
* @param {Function|RegExp} callback, or regexp to match error string against
* @api public
*/
+ Assertion.prototype['throw'] =
Assertion.prototype.throwError =
Assertion.prototype.throwException = function (fn) {
expect(this.obj).to.be.a('function');
var thrown = false
- , not = this.flags.not
+ , not = this.flags.not;
try {
this.obj();
} catch (e) {
- if ('function' == typeof fn) {
- fn(e);
- } else if ('object' == typeof fn) {
+ if (isRegExp(fn)) {
var subject = 'string' == typeof e ? e : e.message;
if (not) {
expect(subject).to.not.match(fn);
} else {
expect(subject).to.match(fn);
}
+ } else if ('function' == typeof fn) {
+ fn(e);
}
thrown = true;
}
- if ('object' == typeof fn && not) {
+ if (isRegExp(fn) && not) {
// in the presence of a matcher, ensure the `not` only applies to
// the matching.
this.flags.not = false;
}
@@ -210,13 +227,14 @@
* @api public
*/
Assertion.prototype.eql = function (obj) {
this.assert(
- expect.eql(obj, this.obj)
+ expect.eql(this.obj, obj)
, function(){ return 'expected ' + i(this.obj) + ' to sort of equal ' + i(obj) }
- , function(){ return 'expected ' + i(this.obj) + ' to sort of not equal ' + i(obj) });
+ , function(){ return 'expected ' + i(this.obj) + ' to sort of not equal ' + i(obj) }
+ , obj);
return this;
};
/**
* Assert within start to finish (inclusive).
@@ -248,13 +266,14 @@
var n = /^[aeiou]/.test(type) ? 'n' : '';
// typeof with support for 'array'
this.assert(
'array' == type ? isArray(this.obj) :
- 'object' == type
- ? 'object' == typeof this.obj && null !== this.obj
- : type == typeof this.obj
+ 'regexp' == type ? isRegExp(this.obj) :
+ 'object' == type
+ ? 'object' == typeof this.obj && null !== this.obj
+ : type == typeof this.obj
, function(){ return 'expected ' + i(this.obj) + ' to be a' + n + ' ' + type }
, function(){ return 'expected ' + i(this.obj) + ' not to be a' + n + ' ' + type });
} else {
// instanceof
var name = type.name || 'supplied constructor';
@@ -457,19 +476,20 @@
, function(){ return 'expected ' + i(this.obj) + ' to ' + str }
, function(){ return 'expected ' + i(this.obj) + ' to not ' + str });
return this;
};
+
/**
* Assert a failure.
*
* @param {String ...} custom message
* @api public
*/
Assertion.prototype.fail = function (msg) {
- msg = msg || "explicit failure";
- this.assert(false, msg, msg);
+ var error = function() { return msg || "explicit failure"; }
+ this.assert(false, error, error);
return this;
};
/**
* Function bind implementation.
@@ -494,11 +514,11 @@
if (!fn.call(scope, arr[i], i, arr)) {
return false;
}
}
return true;
- };
+ }
/**
* Array indexOf compatibility.
*
* @see bit.ly/a5Dxa2
@@ -516,18 +536,17 @@
for (var j = arr.length, i = i < 0 ? i + j < 0 ? 0 : i + j : i || 0
; i < j && arr[i] !== o; i++);
return j <= i ? -1 : i;
- };
+ }
// https://gist.github.com/1044128/
var getOuterHTML = function(element) {
if ('outerHTML' in element) return element.outerHTML;
var ns = "http://www.w3.org/1999/xhtml";
var container = document.createElementNS(ns, '_');
- var elemProto = (window.HTMLElement || window.Element).prototype;
var xmlSerializer = new XMLSerializer();
var html;
if (document.xmlVersion) {
return xmlSerializer.serializeToString(element);
} else {
@@ -560,11 +579,11 @@
function i (obj, showHidden, depth) {
var seen = [];
function stylize (str) {
return str;
- };
+ }
function format (value, recurseTimes) {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (value && typeof value.inspect === 'function' &&
@@ -618,10 +637,15 @@
// Dates without properties can be shortcutted
if (isDate(value) && $keys.length === 0) {
return stylize(value.toUTCString(), 'date');
}
+ // Error objects can be shortcutted
+ if (value instanceof Error) {
+ return stylize("["+value.toString()+"]", 'Error');
+ }
+
var base, type, braces;
// Determine the object type
if (isArray(value)) {
type = 'Array';
braces = ['[', ']'];
@@ -738,15 +762,17 @@
}
return output;
}
return format(obj, (typeof depth === 'undefined' ? 2 : depth));
- };
+ }
+ expect.stringify = i;
+
function isArray (ar) {
- return Object.prototype.toString.call(ar) == '[object Array]';
- };
+ return Object.prototype.toString.call(ar) === '[object Array]';
+ }
function isRegExp(re) {
var s;
try {
s = '' + re;
@@ -760,16 +786,15 @@
re.constructor.name === 'RegExp' &&
re.compile &&
re.test &&
re.exec &&
s.match(/^\/.*\/[gim]{0,3}$/);
- };
+ }
function isDate(d) {
- if (d instanceof Date) return true;
- return false;
- };
+ return d instanceof Date;
+ }
function keys (obj) {
if (Object.keys) {
return Object.keys(obj);
}
@@ -795,11 +820,11 @@
for (var i= 0, n = arr.length; i<n; i++)
if (i in arr)
other[i] = mapper.call(that, arr[i], i, arr);
return other;
- };
+ }
function reduce (arr, fun) {
if (Array.prototype.reduce) {
return Array.prototype.reduce.apply(
arr
@@ -836,62 +861,70 @@
if (i in this)
rv = fun.call(null, rv, this[i], i, this);
}
return rv;
- };
+ }
/**
* Asserts deep equality
*
* @see taken from node.js `assert` module (copyright Joyent, MIT license)
* @api private
*/
- expect.eql = function eql (actual, expected) {
+ expect.eql = function eql(actual, expected) {
// 7.1. All identical values are equivalent, as determined by ===.
if (actual === expected) {
return true;
} else if ('undefined' != typeof Buffer
- && Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
+ && Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
if (actual.length != expected.length) return false;
for (var i = 0; i < actual.length; i++) {
if (actual[i] !== expected[i]) return false;
}
return true;
- // 7.2. If the expected value is a Date object, the actual value is
- // equivalent if it is also a Date object that refers to the same time.
+ // 7.2. If the expected value is a Date object, the actual value is
+ // equivalent if it is also a Date object that refers to the same time.
} else if (actual instanceof Date && expected instanceof Date) {
return actual.getTime() === expected.getTime();
- // 7.3. Other pairs that do not both pass typeof value == "object",
- // equivalence is determined by ==.
+ // 7.3. Other pairs that do not both pass typeof value == "object",
+ // equivalence is determined by ==.
} else if (typeof actual != 'object' && typeof expected != 'object') {
return actual == expected;
-
+ // If both are regular expression use the special `regExpEquiv` method
+ // to determine equivalence.
+ } else if (isRegExp(actual) && isRegExp(expected)) {
+ return regExpEquiv(actual, expected);
// 7.4. For all other Object pairs, including Array objects, equivalence is
// determined by having the same number of owned properties (as verified
// with Object.prototype.hasOwnProperty.call), the same set of keys
// (although not necessarily the same order), equivalent values for every
// corresponding key, and an identical "prototype" property. Note: this
// accounts for both named and indexed properties on Arrays.
} else {
return objEquiv(actual, expected);
}
- }
+ };
function isUndefinedOrNull (value) {
return value === null || value === undefined;
}
function isArguments (object) {
return Object.prototype.toString.call(object) == '[object Arguments]';
}
+ function regExpEquiv (a, b) {
+ return a.source === b.source && a.global === b.global &&
+ a.ignoreCase === b.ignoreCase && a.multiline === b.multiline;
+ }
+
function objEquiv (a, b) {
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
return false;
// an identical "prototype" property.
if (a.prototype !== b.prototype) return false;
@@ -956,11 +989,11 @@
f(d.getUTCMonth() + 1) + '-' +
f(d.getUTCDate()) + 'T' +
f(d.getUTCHours()) + ':' +
f(d.getUTCMinutes()) + ':' +
f(d.getUTCSeconds()) + 'Z' : null;
- };
+ }
var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
gap,
indent,
@@ -1246,8 +1279,7 @@
window.expect = module.exports;
}
})(
this
- , 'undefined' != typeof module ? module : {}
- , 'undefined' != typeof exports ? exports : {}
+ , 'undefined' != typeof module ? module : {exports: {}}
);