initial commit
This commit is contained in:
commit
d3fcd7f10c
3159 files changed
+349702
No files matched your search
+57
@@ -0,0 +1,57 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var util = require('../util')
|
||||
, assert = require('assert')
|
||||
, AssertionError = assert.AssertionError;
|
||||
|
||||
module.exports = function(should) {
|
||||
var i = should.format;
|
||||
|
||||
/**
|
||||
* Expose assert to should
|
||||
*
|
||||
* This allows you to do things like below
|
||||
* without require()ing the assert module.
|
||||
*
|
||||
* should.equal(foo.bar, undefined);
|
||||
*
|
||||
*/
|
||||
util.merge(should, assert);
|
||||
|
||||
|
||||
/**
|
||||
* Assert _obj_ exists, with optional message.
|
||||
*
|
||||
* @param {*} obj
|
||||
* @param {String} [msg]
|
||||
* @api public
|
||||
*/
|
||||
should.exist = should.exists = function(obj, msg) {
|
||||
if(null == obj) {
|
||||
throw new AssertionError({
|
||||
message: msg || ('expected ' + i(obj) + ' to exist'), stackStartFunction: should.exist
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Asserts _obj_ does not exist, with optional message.
|
||||
*
|
||||
* @param {*} obj
|
||||
* @param {String} [msg]
|
||||
* @api public
|
||||
*/
|
||||
|
||||
should.not = {};
|
||||
should.not.exist = should.not.exists = function(obj, msg) {
|
||||
if(null != obj) {
|
||||
throw new AssertionError({
|
||||
message: msg || ('expected ' + i(obj) + ' to not exist'), stackStartFunction: should.not.exist
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
Assertion.add('true', function() {
|
||||
this.is.exactly(true)
|
||||
}, true);
|
||||
|
||||
Assertion.add('false', function() {
|
||||
this.is.exactly(false)
|
||||
}, true);
|
||||
|
||||
Assertion.add('ok', function() {
|
||||
this.params = { operator: 'to be truthy' };
|
||||
|
||||
this.assert(this.obj);
|
||||
}, true);
|
||||
};
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
/*!
|
||||
* Portions copyright (c) 2010, 2011, 2012 Wojciech Zawistowski, Travis Jeffery
|
||||
* From the jasmine-jquery project under the MIT License.
|
||||
*/
|
||||
|
||||
var util = require('../../util');
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
var i = should.format;
|
||||
var $ = this.jQuery || this.$;
|
||||
|
||||
/* Otherwise, node's util.inspect loops hangs */
|
||||
if (typeof HTMLElement !== "undefined" && HTMLElement && !HTMLElement.prototype.inspect) {
|
||||
HTMLElement.prototype.inspect = function () {
|
||||
return this.outerHTML;
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof jQuery !== "undefined" && jQuery && !jQuery.prototype.inspect) {
|
||||
jQuery.fn.inspect = function () {
|
||||
var elementList = this.toArray().map(function (e) {
|
||||
return util.inspect(e);
|
||||
}).join(", ");
|
||||
if (this.selector) {
|
||||
return "SELECTOR(" + this.selector + ") matching " + this.length + " elements" + (elementList.length ? ": " + elementList : "");
|
||||
} else {
|
||||
return elementList;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function jQueryAttributeTestHelper(method, singular, plural, nameOrHash, value) {
|
||||
var keys = util.isObject(nameOrHash) ? Object.keys(nameOrHash) : [nameOrHash];
|
||||
var allRelevantAttributes = keys.reduce(function (memo, key) {
|
||||
var value = $(this.obj)[method](key);
|
||||
if (typeof value !== 'undefined') {
|
||||
memo[key] = value;
|
||||
}
|
||||
return memo;
|
||||
}.bind(this), {});
|
||||
|
||||
if (arguments.length === 4 && util.isObject(nameOrHash)) {
|
||||
this.params = { operator: 'to have ' + plural + ' ' + i(nameOrHash) };
|
||||
allRelevantAttributes.should.have.properties(nameOrHash);
|
||||
} else if (arguments.length === 4) {
|
||||
this.params = { operator: 'to have ' + singular + ' ' + i(nameOrHash) };
|
||||
allRelevantAttributes.should.have.property(nameOrHash);
|
||||
} else {
|
||||
this.params = { operator: 'to have ' + singular + ' ' + i(nameOrHash) + ' with value ' + i(value) };
|
||||
allRelevantAttributes.should.have.property(nameOrHash, value);
|
||||
}
|
||||
}
|
||||
|
||||
var browserTagCaseIndependentHtml = function (html) {
|
||||
return $('<div/>').append(html).html();
|
||||
};
|
||||
|
||||
var addJqPredicateAssertion = function (predicate, nameOverride, operatorOverride) {
|
||||
Assertion.add(nameOverride || predicate, function() {
|
||||
this.params = { operator: 'to be ' + (operatorOverride || predicate) };
|
||||
this.assert($(this.obj).is(':' + predicate));
|
||||
}, true);
|
||||
}
|
||||
|
||||
Assertion.add('className', function(className) {
|
||||
this.params = { operator: 'to have class ' + className };
|
||||
this.assert($(this.obj).hasClass(className));
|
||||
});
|
||||
|
||||
Assertion.add('css', function(css) {
|
||||
this.params = { operator: 'to have css ' + i(css) };
|
||||
for (var prop in css) {
|
||||
var value = css[prop];
|
||||
if (value === 'auto' && $(this.obj).get(0).style[prop] === 'auto') {
|
||||
continue;
|
||||
}
|
||||
$(this.obj).css(prop).should.eql(value);
|
||||
}
|
||||
});
|
||||
|
||||
addJqPredicateAssertion('visible');
|
||||
addJqPredicateAssertion('hidden');
|
||||
addJqPredicateAssertion('selected');
|
||||
addJqPredicateAssertion('checked');
|
||||
addJqPredicateAssertion('disabled');
|
||||
addJqPredicateAssertion('empty', 'emptyJq');
|
||||
addJqPredicateAssertion('focus', 'focused', 'focused');
|
||||
|
||||
Assertion.add('inDOM', function() {
|
||||
this.params = { operator: 'to be in the DOM' };
|
||||
this.assert($.contains(document.documentElement, $(this.obj)[0]));
|
||||
}, true);
|
||||
|
||||
Assertion.add('exist', function() {
|
||||
this.params = { operator: 'to exist' };
|
||||
$(this.obj).should.not.have.length(0);
|
||||
}, true);
|
||||
|
||||
Assertion.add('attr', function() {
|
||||
var args = [
|
||||
'attr',
|
||||
'attribute',
|
||||
'attributes'
|
||||
].concat(Array.prototype.slice.call(arguments, 0));
|
||||
jQueryAttributeTestHelper.apply(this, args);
|
||||
});
|
||||
|
||||
Assertion.add('prop', function() {
|
||||
var args = [
|
||||
'prop',
|
||||
'property',
|
||||
'properties'
|
||||
].concat(Array.prototype.slice.call(arguments, 0));
|
||||
jQueryAttributeTestHelper.apply(this, args);
|
||||
});
|
||||
|
||||
Assertion.add('elementId', function(id) {
|
||||
this.params = { operator: 'to have ID ' + i(id) };
|
||||
this.obj.should.have.attr('id', id);
|
||||
});
|
||||
|
||||
Assertion.add('html', function(html) {
|
||||
this.params = { operator: 'to have HTML ' + i(html) };
|
||||
$(this.obj).html().should.eql(browserTagCaseIndependentHtml(html));
|
||||
});
|
||||
|
||||
Assertion.add('containHtml', function(html) {
|
||||
this.params = { operator: 'to contain HTML ' + i(html) };
|
||||
$(this.obj).html().indexOf(browserTagCaseIndependentHtml(html)).should.be.above(-1);
|
||||
});
|
||||
|
||||
Assertion.add('text', function(text) {
|
||||
this.params = { operator: 'to have text ' + i(text) };
|
||||
var trimmedText = $.trim($(this.obj).text());
|
||||
|
||||
if (util.isRegExp(text)) {
|
||||
trimmedText.should.match(text);
|
||||
} else {
|
||||
trimmedText.should.eql(text);
|
||||
}
|
||||
});
|
||||
|
||||
Assertion.add('containText', function(text) {
|
||||
this.params = { operator: 'to contain text ' + i(text) };
|
||||
var trimmedText = $.trim($(this.obj).text());
|
||||
|
||||
if (util.isRegExp(text)) {
|
||||
trimmedText.should.match(text);
|
||||
} else {
|
||||
trimmedText.indexOf(text).should.be.above(-1);
|
||||
}
|
||||
});
|
||||
|
||||
Assertion.add('value', function(val) {
|
||||
this.params = { operator: 'to have value ' + i(val) };
|
||||
$(this.obj).val().should.eql(val);
|
||||
});
|
||||
|
||||
Assertion.add('data', function() {
|
||||
var args = [
|
||||
'data',
|
||||
'data',
|
||||
'data'
|
||||
].concat(Array.prototype.slice.call(arguments, 0));
|
||||
jQueryAttributeTestHelper.apply(this, args);
|
||||
});
|
||||
|
||||
Assertion.add('containElement', function(target) {
|
||||
this.params = { operator: 'to contain ' + $(target).inspect() };
|
||||
$(this.obj).find(target).should.not.have.length(0);
|
||||
});
|
||||
|
||||
Assertion.add('matchedBy', function(selector) {
|
||||
this.params = { operator: 'to be matched by selector ' + selector };
|
||||
$(this.obj).filter(selector).should.not.have.length(0);
|
||||
});
|
||||
|
||||
Assertion.add('handle', function(event) {
|
||||
this.params = { operator: 'to handle ' + event };
|
||||
|
||||
var events = $._data($(this.obj).get(0), "events");
|
||||
|
||||
if (!events || !event || typeof event !== "string") {
|
||||
return this.assert(false);
|
||||
}
|
||||
|
||||
var namespaces = event.split("."),
|
||||
eventType = namespaces.shift(),
|
||||
sortedNamespaces = namespaces.slice(0).sort(),
|
||||
namespaceRegExp = new RegExp("(^|\\.)" + sortedNamespaces.join("\\.(?:.*\\.)?") + "(\\.|$)");
|
||||
|
||||
if (events[eventType] && namespaces.length) {
|
||||
for (var i = 0; i < events[eventType].length; i++) {
|
||||
var namespace = events[eventType][i].namespace;
|
||||
|
||||
if (namespaceRegExp.test(namespace)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
events.should.have.property(eventType);
|
||||
events[eventType].should.not.have.length(0);
|
||||
return;
|
||||
}
|
||||
|
||||
this.assert(false);
|
||||
});
|
||||
|
||||
Assertion.add('handleWith', function(eventName, eventHandler) {
|
||||
this.params = { operator: 'to handle ' + eventName + ' with ' + eventHandler };
|
||||
|
||||
var normalizedEventName = eventName.split('.')[0],
|
||||
stack = $._data($(this.obj).get(0), "events")[normalizedEventName];
|
||||
|
||||
for (var i = 0; i < stack.length; i++) {
|
||||
if (stack[i].handler == eventHandler) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.assert(false);
|
||||
});
|
||||
};
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
|
||||
function addLink(name) {
|
||||
Object.defineProperty(Assertion.prototype, name, {
|
||||
get: function() {
|
||||
return this;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
['an', 'of', 'a', 'and', 'be', 'have', 'with', 'is', 'which'].forEach(addLink);
|
||||
};
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var util = require('../util'),
|
||||
eql = require('../eql');
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
var i = should.format;
|
||||
|
||||
Assertion.add('include', function(obj, description) {
|
||||
if(!Array.isArray(this.obj) && !util.isString(this.obj)) {
|
||||
this.params = { operator: 'to include an object equal to ' + i(obj), message: description };
|
||||
var cmp = {};
|
||||
for(var key in obj) cmp[key] = this.obj[key];
|
||||
this.assert(eql(cmp, obj));
|
||||
} else {
|
||||
this.params = { operator: 'to include ' + i(obj), message: description };
|
||||
|
||||
this.assert(~this.obj.indexOf(obj));
|
||||
}
|
||||
});
|
||||
|
||||
Assertion.add('includeEql', function(obj, description) {
|
||||
this.params = { operator: 'to include an object equal to ' + i(obj), message: description };
|
||||
|
||||
this.assert(this.obj.some(function(item) {
|
||||
return eql(obj, item);
|
||||
}));
|
||||
});
|
||||
};
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var eql = require('../eql');
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
Assertion.add('eql', function(val, description) {
|
||||
this.params = { operator: 'to equal', expected: val, showDiff: true, message: description };
|
||||
|
||||
this.assert(eql(val, this.obj));
|
||||
});
|
||||
|
||||
Assertion.add('equal', function(val, description) {
|
||||
this.params = { operator: 'to be', expected: val, showDiff: true, message: description };
|
||||
|
||||
this.assert(val === this.obj);
|
||||
});
|
||||
|
||||
Assertion.alias('equal', 'exactly');
|
||||
};
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
var i = should.format;
|
||||
|
||||
Assertion.add('throw', function(message) {
|
||||
var fn = this.obj
|
||||
, err = {}
|
||||
, errorInfo = ''
|
||||
, ok = true;
|
||||
|
||||
try {
|
||||
fn();
|
||||
ok = false;
|
||||
} catch(e) {
|
||||
err = e;
|
||||
}
|
||||
|
||||
if(ok) {
|
||||
if('string' == typeof message) {
|
||||
ok = message == err.message;
|
||||
} else if(message instanceof RegExp) {
|
||||
ok = message.test(err.message);
|
||||
} else if('function' == typeof message) {
|
||||
ok = err instanceof message;
|
||||
}
|
||||
|
||||
if(message && !ok) {
|
||||
if('string' == typeof message) {
|
||||
errorInfo = " with a message matching '" + message + "', but got '" + err.message + "'";
|
||||
} else if(message instanceof RegExp) {
|
||||
errorInfo = " with a message matching " + message + ", but got '" + err.message + "'";
|
||||
} else if('function' == typeof message) {
|
||||
errorInfo = " of type " + message.name + ", but got " + err.constructor.name;
|
||||
}
|
||||
} else {
|
||||
errorInfo = " (got " + i(err) + ")";
|
||||
}
|
||||
}
|
||||
|
||||
this.params = { operator: 'to throw exception' + errorInfo };
|
||||
|
||||
this.assert(ok);
|
||||
});
|
||||
|
||||
Assertion.alias('throw', 'throwError');
|
||||
};
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
//var statusCodes = require('http').STATUS_CODES;
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
|
||||
Assertion.add('header', function(field, val) {
|
||||
this
|
||||
.have.property('headers')
|
||||
.and.have.property(field.toLowerCase(), val);
|
||||
});
|
||||
|
||||
Assertion.add('status', function(code) {
|
||||
//this.params = { operator: 'to have response code ' + code + ' ' + i(statusCodes[code])
|
||||
// + ', but got ' + this.obj.statusCode + ' ' + i(statusCodes[this.obj.statusCode]) }
|
||||
|
||||
this.have.property('statusCode', code);
|
||||
});
|
||||
|
||||
Assertion.add('json', function() {
|
||||
this.have.property('headers')
|
||||
.and.have.property('content-type').include('application/json');
|
||||
}, true);
|
||||
|
||||
Assertion.add('html', function() {
|
||||
this.have.property('headers')
|
||||
.and.have.property('content-type').include('text/html');
|
||||
}, true);
|
||||
};
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var util = require('../util'),
|
||||
eql = require('../eql');
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
var i = should.format;
|
||||
|
||||
Assertion.add('match', function(other, description) {
|
||||
this.params = { operator: 'to match ' + i(other), message: description };
|
||||
|
||||
if(!eql(this.obj, other)) {
|
||||
if(util.isRegExp(other)) { // something - regex
|
||||
|
||||
if(util.isString(this.obj)) {
|
||||
|
||||
this.assert(other.exec(this.obj));
|
||||
} else if(Array.isArray(this.obj)) {
|
||||
|
||||
this.obj.forEach(function(item) {
|
||||
this.assert(other.exec(item));// should we try to convert to String and exec?
|
||||
}, this);
|
||||
} else if(util.isObject(this.obj)) {
|
||||
|
||||
var notMatchedProps = [], matchedProps = [];
|
||||
util.forOwn(this.obj, function(value, name) {
|
||||
if(other.exec(value)) matchedProps.push(i(name));
|
||||
else notMatchedProps.push(i(name));
|
||||
}, this);
|
||||
|
||||
if(notMatchedProps.length)
|
||||
this.params.operator += '\n\tnot matched properties: ' + notMatchedProps.join(', ');
|
||||
if(matchedProps.length)
|
||||
this.params.operator += '\n\tmatched properties: ' + matchedProps.join(', ');
|
||||
|
||||
this.assert(notMatchedProps.length == 0);
|
||||
} // should we try to convert to String and exec?
|
||||
} else if(util.isFunction(other)) {
|
||||
var res;
|
||||
try {
|
||||
res = other(this.obj);
|
||||
} catch(e) {
|
||||
if(e instanceof should.AssertionError) {
|
||||
this.params.operator += '\n\t' + e.message;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
|
||||
if(res instanceof Assertion) {
|
||||
this.params.operator += '\n\t' + res.getMessage();
|
||||
}
|
||||
|
||||
//if we throw exception ok - it is used .should inside
|
||||
if(util.isBoolean(res)) {
|
||||
this.assert(res); // if it is just boolean function assert on it
|
||||
}
|
||||
} else if(util.isObject(other)) { // try to match properties (for Object and Array)
|
||||
notMatchedProps = []; matchedProps = [];
|
||||
|
||||
util.forOwn(other, function(value, key) {
|
||||
try {
|
||||
this.obj[key].should.match(value);
|
||||
matchedProps.push(key);
|
||||
} catch(e) {
|
||||
if(e instanceof should.AssertionError) {
|
||||
notMatchedProps.push(key);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}, this);
|
||||
|
||||
if(notMatchedProps.length)
|
||||
this.params.operator += '\n\tnot matched properties: ' + notMatchedProps.join(', ');
|
||||
if(matchedProps.length)
|
||||
this.params.operator += '\n\tmatched properties: ' + matchedProps.join(', ');
|
||||
|
||||
this.assert(notMatchedProps.length == 0);
|
||||
} else {
|
||||
this.assert(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Assertion.add('matchEach', function(other, description) {
|
||||
this.params = { operator: 'to match each ' + i(other), message: description };
|
||||
|
||||
var f = other;
|
||||
|
||||
if(util.isRegExp(other))
|
||||
f = function(it) {
|
||||
return !!other.exec(it);
|
||||
};
|
||||
else if(!util.isFunction(other))
|
||||
f = function(it) {
|
||||
return eql(it, other);
|
||||
};
|
||||
|
||||
util.forOwn(this.obj, function(value, key) {
|
||||
var res = f(value, key);
|
||||
|
||||
//if we throw exception ok - it is used .should inside
|
||||
if(util.isBoolean(res)) {
|
||||
this.assert(res); // if it is just boolean function assert on it
|
||||
}
|
||||
}, this);
|
||||
});
|
||||
};
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
Assertion.add('NaN', function() {
|
||||
this.params = { operator: 'to be NaN' };
|
||||
|
||||
this.is.a.Number
|
||||
.and.assert(isNaN(this.obj));
|
||||
}, true);
|
||||
|
||||
Assertion.add('Infinity', function() {
|
||||
this.params = { operator: 'to be Infinity' };
|
||||
|
||||
this.is.a.Number
|
||||
.and.not.a.NaN
|
||||
.and.assert(!isFinite(this.obj));
|
||||
}, true);
|
||||
|
||||
Assertion.add('within', function(start, finish, description) {
|
||||
this.params = { operator: 'to be within ' + start + '..' + finish, message: description };
|
||||
|
||||
this.assert(this.obj >= start && this.obj <= finish);
|
||||
});
|
||||
|
||||
Assertion.add('approximately', function(value, delta, description) {
|
||||
this.params = { operator: 'to be approximately ' + value + " ±" + delta, message: description };
|
||||
|
||||
this.assert(Math.abs(this.obj - value) <= delta);
|
||||
});
|
||||
|
||||
Assertion.add('above', function(n, description) {
|
||||
this.params = { operator: 'to be above ' + n, message: description };
|
||||
|
||||
this.assert(this.obj > n);
|
||||
});
|
||||
|
||||
Assertion.add('below', function(n, description) {
|
||||
this.params = { operator: 'to be below ' + n, message: description };
|
||||
|
||||
this.assert(this.obj < n);
|
||||
});
|
||||
|
||||
Assertion.alias('above', 'greaterThan');
|
||||
Assertion.alias('below', 'lessThan');
|
||||
|
||||
};
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var util = require('../util'),
|
||||
eql = require('../eql');
|
||||
|
||||
var aSlice = Array.prototype.slice;
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
var i = should.format;
|
||||
|
||||
Assertion.add('property', function(name, val) {
|
||||
if(arguments.length > 1) {
|
||||
var p = {};
|
||||
p[name] = val;
|
||||
this.have.properties(p);
|
||||
} else {
|
||||
this.have.properties(name);
|
||||
}
|
||||
this.obj = this.obj[name];
|
||||
});
|
||||
|
||||
Assertion.add('properties', function(names) {
|
||||
var values = {};
|
||||
if(arguments.length > 1) {
|
||||
names = aSlice.call(arguments);
|
||||
} else if(!Array.isArray(names)) {
|
||||
if(util.isString(names)) {
|
||||
names = [names];
|
||||
} else {
|
||||
values = names;
|
||||
names = Object.keys(names);
|
||||
}
|
||||
}
|
||||
|
||||
var obj = Object(this.obj), missingProperties = [];
|
||||
|
||||
//just enumerate properties and check if they all present
|
||||
names.forEach(function(name) {
|
||||
if(!(name in obj)) missingProperties.push(i(name));
|
||||
});
|
||||
|
||||
var props = missingProperties;
|
||||
if(props.length === 0) {
|
||||
props = names.map(i);
|
||||
}
|
||||
|
||||
var operator = (props.length === 1 ?
|
||||
'to have property ' : 'to have properties ') + props.join(', ');
|
||||
|
||||
this.params = { operator: operator };
|
||||
|
||||
this.assert(missingProperties.length === 0);
|
||||
|
||||
// check if values in object matched expected
|
||||
var valueCheckNames = Object.keys(values);
|
||||
if(valueCheckNames.length) {
|
||||
var wrongValues = [];
|
||||
props = [];
|
||||
|
||||
// now check values, as there we have all properties
|
||||
valueCheckNames.forEach(function(name) {
|
||||
var value = values[name];
|
||||
if(!eql(obj[name], value)) {
|
||||
wrongValues.push(i(name) + ' of ' + i(value) + ' (got ' + i(obj[name]) + ')');
|
||||
} else {
|
||||
props.push(i(name) + ' of ' + i(value));
|
||||
}
|
||||
});
|
||||
|
||||
if(wrongValues.length > 0) {
|
||||
props = wrongValues;
|
||||
}
|
||||
|
||||
operator = (props.length === 1 ?
|
||||
'to have property ' : 'to have properties ') + props.join(', ');
|
||||
|
||||
this.params = { operator: operator };
|
||||
|
||||
this.assert(wrongValues.length === 0);
|
||||
}
|
||||
});
|
||||
|
||||
Assertion.add('length', function(n, description) {
|
||||
this.have.property('length', n, description);
|
||||
});
|
||||
|
||||
Assertion.alias('length', 'lengthOf');
|
||||
|
||||
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
||||
|
||||
Assertion.add('ownProperty', function(name, description) {
|
||||
this.params = { operator: 'to have own property ' + i(name), message: description };
|
||||
|
||||
this.assert(hasOwnProperty.call(this.obj, name));
|
||||
|
||||
this.obj = this.obj[name];
|
||||
});
|
||||
|
||||
Assertion.alias('hasOwnProperty', 'ownProperty');
|
||||
|
||||
Assertion.add('empty', function() {
|
||||
this.params = { operator: 'to be empty' };
|
||||
|
||||
if(util.isString(this.obj) || Array.isArray(this.obj) || util.isArguments(this.obj)) {
|
||||
this.have.property('length', 0);
|
||||
} else {
|
||||
var obj = Object(this.obj); // wrap to reference for booleans and numbers
|
||||
for(var prop in obj) {
|
||||
this.have.not.ownProperty(prop);
|
||||
}
|
||||
}
|
||||
}, true);
|
||||
|
||||
Assertion.add('keys', function(keys) {
|
||||
if(arguments.length > 1) keys = aSlice.call(arguments);
|
||||
else if(arguments.length === 1 && util.isString(keys)) keys = [ keys ];
|
||||
else if(arguments.length === 0) keys = [];
|
||||
|
||||
var obj = Object(this.obj);
|
||||
|
||||
// first check if some keys are missing
|
||||
var missingKeys = [];
|
||||
keys.forEach(function(key) {
|
||||
if(!hasOwnProperty.call(this.obj, key))
|
||||
missingKeys.push(i(key));
|
||||
}, this);
|
||||
|
||||
// second check for extra keys
|
||||
var extraKeys = [];
|
||||
Object.keys(obj).forEach(function(key) {
|
||||
if(keys.indexOf(key) < 0) {
|
||||
extraKeys.push(i(key));
|
||||
}
|
||||
});
|
||||
|
||||
var verb = keys.length === 0 ? 'to be empty' :
|
||||
'to have ' + (keys.length === 1 ? 'key ' : 'keys ');
|
||||
|
||||
this.params = { operator: verb + keys.map(i).join(', ')};
|
||||
|
||||
if(missingKeys.length > 0)
|
||||
this.params.operator += '\n\tmissing keys: ' + missingKeys.join(', ');
|
||||
|
||||
if(extraKeys.length > 0)
|
||||
this.params.operator += '\n\textra keys: ' + extraKeys.join(', ');
|
||||
|
||||
this.assert(missingKeys.length === 0 && extraKeys.length === 0);
|
||||
});
|
||||
|
||||
Assertion.alias("keys", "key");
|
||||
|
||||
Assertion.add('containEql', function(other) {
|
||||
this.params = { operator: 'to contain ' + i(other) };
|
||||
var obj = this.obj;
|
||||
if(Array.isArray(obj)) {
|
||||
this.assert(obj.some(function(item) {
|
||||
return eql(item, other);
|
||||
}));
|
||||
} else if(util.isString(obj)) {
|
||||
// expect obj to be string
|
||||
this.assert(obj.indexOf(String(other)) >= 0);
|
||||
} else if(util.isObject(obj)) {
|
||||
// object contains object case
|
||||
util.forOwn(other, function(value, key) {
|
||||
obj[key].should.eql(value);
|
||||
});
|
||||
} else {
|
||||
//other uncovered cases
|
||||
this.assert(false);
|
||||
}
|
||||
});
|
||||
|
||||
Assertion.add('containDeep', function(other) {
|
||||
this.params = { operator: 'to contain ' + i(other) };
|
||||
|
||||
var obj = this.obj;
|
||||
if(Array.isArray(obj)) {
|
||||
if(Array.isArray(other)) {
|
||||
var otherIdx = 0;
|
||||
obj.forEach(function(item) {
|
||||
try {
|
||||
should(item).not.be.null.and.containDeep(other[otherIdx]);
|
||||
otherIdx++;
|
||||
} catch(e) {
|
||||
if(e instanceof should.AssertionError) {
|
||||
return;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
this.assert(otherIdx == other.length);
|
||||
//search array contain other as sub sequence
|
||||
} else {
|
||||
this.assert(false);
|
||||
}
|
||||
} else if(util.isString(obj)) {// expect other to be string
|
||||
this.assert(obj.indexOf(String(other)) >= 0);
|
||||
} else if(util.isObject(obj)) {// object contains object case
|
||||
if(util.isObject(other)) {
|
||||
util.forOwn(other, function(value, key) {
|
||||
should(obj[key]).not.be.null.and.containDeep(value);
|
||||
});
|
||||
} else {//one of the properties contain value
|
||||
this.assert(false);
|
||||
}
|
||||
} else {
|
||||
this.eql(other);
|
||||
}
|
||||
});
|
||||
|
||||
};
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
Assertion.add('startWith', function(str, description) {
|
||||
this.params = { operator: 'to start with ' + should.format(str), message: description };
|
||||
|
||||
this.assert(0 === this.obj.indexOf(str));
|
||||
});
|
||||
|
||||
Assertion.add('endWith', function(str, description) {
|
||||
this.params = { operator: 'to end with ' + should.format(str), message: description };
|
||||
|
||||
this.assert(this.obj.indexOf(str, this.obj.length - str.length) >= 0);
|
||||
});
|
||||
};
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
/*!
|
||||
* Should
|
||||
* Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
var util = require('../util');
|
||||
|
||||
module.exports = function(should, Assertion) {
|
||||
Assertion.add('Number', function() {
|
||||
this.params = { operator: 'to be a number' };
|
||||
|
||||
this.assert(util.isNumber(this.obj));
|
||||
}, true);
|
||||
|
||||
Assertion.add('arguments', function() {
|
||||
this.params = { operator: 'to be arguments' };
|
||||
|
||||
this.assert(util.isArguments(this.obj));
|
||||
}, true);
|
||||
|
||||
Assertion.add('type', function(type, description) {
|
||||
this.params = { operator: 'to have type ' + type, message: description };
|
||||
|
||||
(typeof this.obj).should.be.exactly(type, description);
|
||||
});
|
||||
|
||||
Assertion.add('instanceof', function(constructor, description) {
|
||||
this.params = { operator: 'to be an instance of ' + constructor.name, message: description };
|
||||
|
||||
this.assert(Object(this.obj) instanceof constructor);
|
||||
});
|
||||
|
||||
Assertion.add('Function', function() {
|
||||
this.params = { operator: 'to be a function' };
|
||||
|
||||
this.assert(util.isFunction(this.obj));
|
||||
}, true);
|
||||
|
||||
Assertion.add('Object', function() {
|
||||
this.params = { operator: 'to be an object' };
|
||||
|
||||
this.assert(util.isObject(this.obj));
|
||||
}, true);
|
||||
|
||||
Assertion.add('String', function() {
|
||||
this.params = { operator: 'to be a string' };
|
||||
|
||||
this.assert(util.isString(this.obj));
|
||||
}, true);
|
||||
|
||||
Assertion.add('Array', function() {
|
||||
this.params = { operator: 'to be an array' };
|
||||
|
||||
this.assert(Array.isArray(this.obj));
|
||||
}, true);
|
||||
|
||||
Assertion.add('Boolean', function() {
|
||||
this.params = { operator: 'to be a boolean' };
|
||||
|
||||
this.assert(util.isBoolean(this.obj));
|
||||
}, true);
|
||||
|
||||
Assertion.add('Error', function() {
|
||||
this.params = { operator: 'to be an error' };
|
||||
|
||||
this.assert(util.isError(this.obj));
|
||||
}, true);
|
||||
|
||||
Assertion.add('null', function() {
|
||||
this.params = { operator: 'to be null' };
|
||||
|
||||
this.assert(this.obj === null);
|
||||
}, true);
|
||||
|
||||
Assertion.alias('instanceof', 'instanceOf');
|
||||
};
|
||||
Reference in new issue
Block a user