Skip to content
This repository has been archived by the owner on Nov 4, 2020. It is now read-only.

Commit

Permalink
Use modules
Browse files Browse the repository at this point in the history
  • Loading branch information
btd committed May 29, 2016
1 parent 32ddfb9 commit cd23439
Show file tree
Hide file tree
Showing 18 changed files with 276 additions and 232 deletions.
17 changes: 7 additions & 10 deletions lib/assertion-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
* Copyright(c) 2013-2016 Denis Bardadym <[email protected]>
* MIT Licensed
*/

var util = require('./util');
import { merge, format, functionName } from './util';

/**
* should AssertionError
Expand All @@ -14,8 +13,8 @@ var util = require('./util');
* @memberOf should
* @static
*/
var AssertionError = function AssertionError(options) {
util.merge(this, options);
export default function AssertionError(options) {
merge(this, options);

if (!options.message) {
Object.defineProperty(this, 'message', {
Expand All @@ -42,7 +41,7 @@ var AssertionError = function AssertionError(options) {

if (this.stackStartFunction) {
// try to strip useless frames
var fn_name = util.functionName(this.stackStartFunction);
var fn_name = functionName(this.stackStartFunction);
var idx = out.indexOf('\n' + fn_name);
if (idx >= 0) {
// once we have located the function frame
Expand All @@ -55,7 +54,7 @@ var AssertionError = function AssertionError(options) {
this.stack = out;
}
}
};
}


var indent = ' ';
Expand All @@ -79,8 +78,8 @@ AssertionError.prototype = Object.create(Error.prototype, {
if (!this.operator && this.previous) {
return this.previous.message;
}
var actual = util.format(this.actual);
var expected = 'expected' in this ? ' ' + util.format(this.expected) : '';
var actual = format(this.actual);
var expected = 'expected' in this ? ' ' + format(this.expected) : '';
var details = 'details' in this && this.details ? ' (' + this.details + ')' : '';

var previous = this.previous ? '\n' + indentLines(this.previous.message) : '';
Expand All @@ -89,5 +88,3 @@ AssertionError.prototype = Object.create(Error.prototype, {
}
}
});

module.exports = AssertionError;
13 changes: 6 additions & 7 deletions lib/assertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* MIT Licensed
*/

var AssertionError = require('./assertion-error');
import AssertionError from './assertion-error';

/**
* should Assertion
Expand All @@ -14,7 +14,7 @@ var AssertionError = require('./assertion-error');
* @memberOf should
* @static
*/
function Assertion(obj) {
export function Assertion(obj) {
this.obj = obj;

this.anyOne = false;
Expand Down Expand Up @@ -101,7 +101,7 @@ Assertion.prototype = {
*
* @param {Promise} obj
*/
function PromisedAssertion(/* obj */) {
export function PromisedAssertion(/* obj */) {
Assertion.apply(this, arguments);
}

Expand Down Expand Up @@ -242,7 +242,9 @@ Assertion.addChain = function(name, onCall) {
*/
Assertion.alias = function(from, to) {
var desc = Object.getOwnPropertyDescriptor(Assertion.prototype, from);
if (!desc) throw new Error('Alias ' + from + ' -> ' + to + ' could not be created as ' + from + ' not defined');
if (!desc) {
throw new Error('Alias ' + from + ' -> ' + to + ' could not be created as ' + from + ' not defined');
}
Object.defineProperty(Assertion.prototype, to, desc);

var desc2 = Object.getOwnPropertyDescriptor(PromisedAssertion.prototype, from);
Expand Down Expand Up @@ -273,6 +275,3 @@ Assertion.addChain('not', function() {
Assertion.addChain('any', function() {
this.anyOne = true;
});

module.exports = Assertion;
module.exports.PromisedAssertion = PromisedAssertion;
8 changes: 3 additions & 5 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
* MIT Licensed
*/

var Formatter = require('should-format').Formatter;
import format from 'should-format';

var config = {
checkProtoEql: false,

getFormatter: function(opts) {
return new Formatter(opts || config);
return new format.Formatter(opts || config);
}
};

module.exports = config;
export default config;
17 changes: 11 additions & 6 deletions lib/ext/_assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@
// when used in node, this will actually load the util module we depend on
// versus loading the builtin util module as happens otherwise
// this is a bug in node module loading as far as I am concerned
var Assertion = require('./../assertion');
import { Assertion } from './../assertion';

var _deepEqual = require('should-equal');
import _deepEqual from 'should-equal';

var pSlice = Array.prototype.slice;

// 1. The assert module provides functions that throw
// AssertionError's when particular conditions are not met. The
// assert module must conform to the following interface.

var assert = module.exports = ok;
var assert = ok;
export default assert;

// 3. All of the following functions must throw an AssertionError
// when a corresponding condition is not met, with a message that
Expand Down Expand Up @@ -84,7 +85,9 @@ assert.fail = fail;
* @param {string} [message]
*/
function ok(value, message) {
if (!value) fail(value, true, message, '==', assert.ok);
if (!value) {
fail(value, true, message, '==', assert.ok);
}
}
assert.ok = ok;

Expand All @@ -102,7 +105,9 @@ assert.ok = ok;
* @param {string} [message]
*/
assert.equal = function equal(actual, expected, message) {
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
if (actual != expected) {
fail(actual, expected, message, '==', assert.equal);
}
};

// 6. The non-equality assertion tests for whether two objects are not equal
Expand Down Expand Up @@ -136,7 +141,7 @@ assert.notEqual = function notEqual(actual, expected, message) {
* @param {string} [message]
*/
assert.deepEqual = function deepEqual(actual, expected, message) {
if (!_deepEqual(actual, expected).result) {
if (_deepEqual(actual, expected).length !== 0) {
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
}
};
Expand Down
14 changes: 7 additions & 7 deletions lib/ext/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* MIT Licensed
*/

var util = require('../util');
var assert = require('./_assert');
var AssertionError = require('../assertion-error');
import { merge, format } from '../util';
import assert from './_assert';
import AssertionError from '../assertion-error';

module.exports = function(should) {
var i = should.format;
export default function(should) {
var i = format;

/*
* Expose assert to should
Expand All @@ -21,7 +21,7 @@ module.exports = function(should) {
* should.equal(foo.bar, undefined);
*
*/
util.merge(should, assert);
merge(should, assert);

/**
* Assert _obj_ exists, with optional message.
Expand Down Expand Up @@ -68,4 +68,4 @@ module.exports = function(should) {
});
}
};
};
}
4 changes: 2 additions & 2 deletions lib/ext/bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* MIT Licensed
*/

module.exports = function(should, Assertion) {
export default function(should, Assertion) {
/**
* Assert given object is exactly `true`.
*
Expand Down Expand Up @@ -67,4 +67,4 @@ module.exports = function(should, Assertion) {

this.assert(this.obj);
});
};
}
4 changes: 2 additions & 2 deletions lib/ext/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* MIT Licensed
*/

module.exports = function(should, Assertion) {
export default function(should, Assertion) {
/**
* Simple chaining. It actually do nothing.
*
Expand All @@ -28,4 +28,4 @@ module.exports = function(should, Assertion) {
['an', 'of', 'a', 'and', 'be', 'has', 'have', 'with', 'is', 'which', 'the', 'it'].forEach(function(name) {
Assertion.addChain(name);
});
};
}
38 changes: 20 additions & 18 deletions lib/ext/contain.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* MIT Licensed
*/

var util = require('../util');
var eql = require('should-equal');
import { format, isIndexable, forEach, some, isEmptyObject, length } from '../util';
import eql from 'should-equal';

module.exports = function(should, Assertion) {
var i = should.format;
export default function(should, Assertion) {
var i = format;

/**
* Assert that given object contain something that equal to `other`. It uses `should-equal` for equality checks.
Expand Down Expand Up @@ -44,9 +44,9 @@ module.exports = function(should, Assertion) {

if (typeof obj == 'string') {
this.assert(obj.indexOf(String(other)) >= 0);
} else if (util.isIndexable(obj)) {
this.assert(util.some(obj, function(v) {
return eql(v, other).result;
} else if (isIndexable(obj)) {
this.assert(some(obj, function(v) {
return eql(v, other).length === 0;
}));
} else {
this.have.properties(other);
Expand Down Expand Up @@ -78,8 +78,8 @@ module.exports = function(should, Assertion) {
var obj = this.obj;
if (typeof obj == 'string') {// expect other to be string
this.is.equal(String(other));
} else if (util.isIndexable(obj) && util.isIndexable(other)) {
for (var objIdx = 0, otherIdx = 0, objLength = util.length(obj), otherLength = util.length(other); objIdx < objLength && otherIdx < otherLength; objIdx++) {
} else if (isIndexable(obj) && isIndexable(other)) {
for (var objIdx = 0, otherIdx = 0, objLength = length(obj), otherLength = length(other); objIdx < objLength && otherIdx < otherLength; objIdx++) {
try {
should(obj[objIdx]).containDeepOrdered(other[otherIdx]);
otherIdx++;
Expand All @@ -93,12 +93,12 @@ module.exports = function(should, Assertion) {

this.assert(otherIdx === otherLength);
} else if (obj != null && other != null && typeof obj == 'object' && typeof other == 'object') {// object contains object case
util.forEach(other, function(value, key) {
forEach(other, function(value, key) {
should(obj[key]).containDeepOrdered(value);
});

// if both objects is empty means we finish traversing - and we need to compare for hidden values
if (util.isEmptyObject(other)) {
if (isEmptyObject(other)) {
this.eql(other);
}
} else {
Expand All @@ -124,11 +124,13 @@ module.exports = function(should, Assertion) {
var obj = this.obj;
if (typeof obj == 'string') {// expect other to be string
this.is.equal(String(other));
} else if (util.isIndexable(obj) && util.isIndexable(other)) {
} else if (isIndexable(obj) && isIndexable(other)) {
var usedKeys = {};
util.forEach(other, function(otherItem) {
this.assert(util.some(obj, function(item, index) {
if (index in usedKeys) return false;
forEach(other, function(otherItem) {
this.assert(some(obj, function(item, index) {
if (index in usedKeys) {
return false;
}

try {
should(item).containDeep(otherItem);
Expand All @@ -143,17 +145,17 @@ module.exports = function(should, Assertion) {
}));
}, this);
} else if (obj != null && other != null && typeof obj == 'object' && typeof other == 'object') {// object contains object case
util.forEach(other, function(value, key) {
forEach(other, function(value, key) {
should(obj[key]).containDeep(value);
});

// if both objects is empty means we finish traversing - and we need to compare for hidden values
if (util.isEmptyObject(other)) {
if (isEmptyObject(other)) {
this.eql(other);
}
} else {
this.eql(other);
}
});

};
}
32 changes: 17 additions & 15 deletions lib/ext/eql.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
* MIT Licensed
*/

var eql = require('should-equal');
var type = require('should-type');
var util = require('../util');
import eql from 'should-equal';
import getType from 'should-type';
import { formatProp, format, forEach } from '../util';

function formatEqlResult(r, a, b) {
return ((r.path.length > 0 ? 'at ' + r.path.map(util.formatProp).join(' -> ') : '') +
(r.a === a ? '' : ', A has ' + util.format(r.a)) +
(r.b === b ? '' : ' and B has ' + util.format(r.b)) +
return ((r.path.length > 0 ? 'at ' + r.path.map(formatProp).join(' -> ') : '') +
(r.a === a ? '' : ', A has ' + format(r.a)) +
(r.b === b ? '' : ' and B has ' + format(r.b)) +
(r.showReason ? ' because ' + r.reason : '')).trim();
}

module.exports = function(should, Assertion) {
export default function(should, Assertion) {

/**
* Deep object equality comparison. For full spec see [`should-equal tests`](https://github.com/shouldjs/equal/blob/master/test.js).
Expand All @@ -40,13 +40,15 @@ module.exports = function(should, Assertion) {
*/
Assertion.add('eql', function(val, description) {
this.params = {operator: 'to equal', expected: val, message: description};
var obj = this.obj;
var fails = eql(this.obj, val, should.config);
this.params.details = fails.map(function(fail) {
return formatEqlResult(fail, obj, val);
}).join(', ');

var result = eql(this.obj, val, should.config);
this.params.details = result.result ? '' : formatEqlResult(result, this.obj, val);
this.params.showDiff = eql(getType(obj), getType(val)).length === 0;

this.params.showDiff = eql(type(this.obj), type(val)).result;

this.assert(result.result);
this.assert(fails.length === 0);
});

/**
Expand All @@ -68,7 +70,7 @@ module.exports = function(should, Assertion) {
Assertion.add('equal', function(val, description) {
this.params = {operator: 'to be', expected: val, message: description};

this.params.showDiff = eql(type(this.obj), type(val)).result;
this.params.showDiff = eql(getType(this.obj), getType(val)).length === 0;

this.assert(val === this.obj);
});
Expand All @@ -89,7 +91,7 @@ module.exports = function(should, Assertion) {
var obj = this.obj;
var found = false;

util.forEach(vals, function(val) {
forEach(vals, function(val) {
try {
should(val)[method](obj);
found = true;
Expand Down Expand Up @@ -134,4 +136,4 @@ module.exports = function(should, Assertion) {
*/
addOneOf('oneOf', 'to be one of', 'eql');

};
}
Loading

0 comments on commit cd23439

Please sign in to comment.