Code coverage report for lib/chai-subset.js

Statements: 100% (29 / 29)      Branches: 100% (19 / 19)      Functions: 100% (7 / 7)      Lines: 100% (29 / 29)      Ignored: none     

All files » lib/ » chai-subset.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 561 1 1   1 13 13   13                   1 1       1 45 7   38 2   36 1     35 11 2   9 9 8 16         24 31 31 31 16   15      
module.exports = function(chai, utils) {
	var Assertion = chai.Assertion;
	var assertionPrototype = Assertion.prototype;
 
	Assertion.addMethod('containSubset', function (expected) {
		var actual = utils.flag(this, 'object');
		var showDiff = chai.config.showDiff;
 
		assertionPrototype.assert.call(this,
			compare(expected, actual),
			'expected #{act} to contain subset #{exp}',
			'expected #{act} to not contain subset #{exp}',
			expected,
			actual,
			showDiff
		);
	});
 
	chai.assert.containSubset = function(val, exp, msg) {
		new chai.Assertion(val, msg).to.be.containSubset(exp);
	};
};
 
function compare(expected, actual) {
	if (typeof(actual) !== typeof(expected)) {
		return false;
	}
	if (typeof(expected) !== 'object' || expected === null) {
		return expected === actual;
	}
	if (!!expected && !actual) {
		return false;
	}
 
	if (Array.isArray(expected)) {
		if (typeof(actual.length) !== 'number') {
			return false;
		}
		var aa = Array.prototype.slice.call(actual);
		return expected.every(function (exp) {
			return aa.some(function (act) {
				return compare(exp, act);
			});
		});
	}
 
	return Object.keys(expected).every(function (key) {
		var eo = expected[key];
		var ao = actual[key];
		if (typeof(eo) === 'object' && eo !== null && ao !== null) {
			return compare(eo, ao);
		}
		return ao === eo;
	});
}