Skip to content

Commit

Permalink
add method assert.isUndefinedOrNull in assert object
Browse files Browse the repository at this point in the history
  • Loading branch information
fcaballero committed Feb 18, 2015
1 parent c638c39 commit e987897
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ module.exports = {
isFunction: _.partial(isOptionalValueOrFulfilsConstraint, _.isFunction, 'Function', undefined),
isNumber: _.partial(isOptionalValueOrFulfilsConstraint, _.isNumber, 'Number', undefined),
isNotObject: _.partial(isOptionalValueOrFulfilsConstraint, not(_.isPlainObject), undefined, 'The argument should not be of type Object.'),
isDefinedOrNotNull: isDefinedOrNotNull,
isNonEmptyString: _.partial(isOptionalValueOrFulfilsConstraint, isNonEmptyString, undefined, 'The argument should be a non empty String.')
isNonEmptyString: _.partial(isOptionalValueOrFulfilsConstraint, isNonEmptyString, undefined, 'The argument should be a non empty String.'),
isDefinedOrNotNull: _.partial(fulfilsConstraint, not(isUndefinedOrNull), 'The argument should not be undefined or null.'),
isUndefinedOrNull: _.partial(fulfilsConstraint, isUndefinedOrNull, 'The argument should be undefined or null.')
};

function not(fn) {
Expand All @@ -25,12 +26,16 @@ function isOptionalValueOrFulfilsConstraint(constraint, typeName, defaultErrorMe
}
}

function isDefinedOrNotNull (value, errorMessage) {
if(_.isUndefined(value) || _.isNull(value)){
throw new Error(errorMessage || 'The argument should not be undefined or null.');
function fulfilsConstraint(constraint, defaultErrorMessage, value, errorMessage){
if(!constraint(value)){
throw new Error(errorMessage || defaultErrorMessage);
}
}

function isUndefinedOrNull(value) {
return _.isUndefined(value) || _.isNull(value);
}

function isNonEmptyString(value){
return _.isString(value) && !_.isEmpty(value);
}
25 changes: 24 additions & 1 deletion src/assert.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ describe('assert', function () {
});

describe('isDefinedOrNotNull method', function () {

it('should not throw an exception when value is defined', function () {
testWithAllValuesBut(['undefined', 'null'], function (value) {
expect(function () {
Expand Down Expand Up @@ -216,7 +215,31 @@ describe('assert', function () {
assert.isDefinedOrNotNull(null, 'custom error message');
}).toThrow('custom error message');
});
});

describe('isUndefinedOrNull', function () {
it('should not throw an exception when value is undefined or null', function () {
expect(function () {
assert.isUndefinedOrNull(undefined);
assert.isUndefinedOrNull(null);
}).not.toThrow();
});

it('should throw an exception when value is not undefined', function () {
testWithAllValuesBut(['undefined', 'null'], function (value) {
expect(function(){
assert.isUndefinedOrNull(value);
}).toThrow('The argument should be undefined or null.');
});
});

it('should throw an exception whit a custom message', function () {
testWithAllValuesBut(['undefined', 'null'], function (value) {
expect(function(){
assert.isUndefinedOrNull(value, 'custom message');
}).toThrow('custom message');
});
});
});

describe('isNonEmptyString method', function () {
Expand Down

0 comments on commit e987897

Please sign in to comment.