conditional
Version:
A preconditions package based on Google's Preconditions library
1,624 lines (1,470 loc) • 190 kB
JavaScript
// CommonJS export for browser - please do not edit directly
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var n;"undefined"!=typeof window?n=window:"undefined"!=typeof global?n=global:"undefined"!=typeof self&&(n=self),n.preconditions=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function() {
var IllegalArgumentError, IllegalStateError, checkArgument, checkState, runTests;
require('./helpers/test_helper');
checkArgument = preconditions.checkArgument, IllegalArgumentError = preconditions.IllegalArgumentError, checkState = preconditions.checkState, IllegalStateError = preconditions.IllegalStateError;
runTests = function(test) {
test('argument', checkArgument, IllegalArgumentError);
return test('state', checkState, IllegalStateError);
};
runTests(function(name, checker, errorType) {
return describe("test " + name + " precondition", function() {
it('happy path test', function() {
var wrapper;
wrapper = function() {
return checker(true, 'valid argument');
};
return assert.doesNotThrow(wrapper);
});
it("'null' condition value also results in a error", function() {
var wrapper;
wrapper = function() {
return checker(null, "'null' is not allowed");
};
return assert.throws(wrapper, errorType, "'null' is not allowed");
});
it('a non-undefined value for condition is valid', function() {
var wrapper;
wrapper = function() {
return checker('random', 'a non-undefined value is allowed');
};
return assert.doesNotThrow(wrapper);
});
it('a empty string value for condition is valid', function() {
var wrapper;
wrapper = function() {
return checker('', 'a string value is allowed');
};
return assert.doesNotThrow(wrapper);
});
return it('a numerical value 0 is valid', function() {
var wrapper;
wrapper = function() {
return checker(0, '0 (zero) value is allowed');
};
return assert.doesNotThrow(wrapper);
});
});
});
}).call(this);
},{"./helpers/test_helper":8}],2:[function(require,module,exports){
(function() {
var FORCED_ERROR, IllegalArgumentError, IllegalStateError, IllegalValueError, InvalidTypeError, UndefinedValueError, UnknownValueError, checkArgument, checkContains, checkDefined, checkDoesNotContain, checkDoesNotEqual, checkEmpty, checkEquals, checkNotEmpty, checkNotNull, checkNotNumberType, checkNull, checkNumberType, checkState, checkUndefined, executors, runTests;
require('./helpers/test_helper');
checkArgument = preconditions.checkArgument, checkNumberType = preconditions.checkNumberType, checkNotNumberType = preconditions.checkNotNumberType, checkContains = preconditions.checkContains, checkDoesNotContain = preconditions.checkDoesNotContain, checkEquals = preconditions.checkEquals, checkDoesNotEqual = preconditions.checkDoesNotEqual, checkDefined = preconditions.checkDefined, checkUndefined = preconditions.checkUndefined, checkEmpty = preconditions.checkEmpty, checkNotEmpty = preconditions.checkNotEmpty, checkState = preconditions.checkState, checkNull = preconditions.checkNull, checkNotNull = preconditions.checkNotNull, IllegalArgumentError = preconditions.IllegalArgumentError, InvalidTypeError = preconditions.InvalidTypeError, UnknownValueError = preconditions.UnknownValueError, UndefinedValueError = preconditions.UndefinedValueError, IllegalValueError = preconditions.IllegalValueError, IllegalStateError = preconditions.IllegalStateError;
FORCED_ERROR = 'this must fail';
executors = [
{
name: 'argument',
execFail: function(errorMessage, callback) {
return checkArgument(false, errorMessage, callback);
},
execPass: function(callback) {
return checkArgument(true, callback);
},
errorType: IllegalArgumentError,
defaultErrorMessage: 'invalid argument'
}, {
name: 'type',
execFail: function(errorMessage, callback) {
return checkNumberType('string', errorMessage, callback);
},
execPass: function(callback) {
return checkNumberType(123, callback);
},
errorType: InvalidTypeError,
defaultErrorMessage: 'invalid type'
}, {
name: 'not-type',
execFail: function(errorMessage, callback) {
return checkNotNumberType(123, errorMessage, callback);
},
execPass: function(callback) {
return checkNotNumberType('string', callback);
},
errorType: InvalidTypeError,
defaultErrorMessage: 'invalid type'
}, {
name: 'contains',
execFail: function(errorMessage, callback) {
return checkContains('d', ['a', 'b', 'c'], errorMessage, callback);
},
execPass: function(callback) {
return checkContains('a', ['a'], callback);
},
errorType: UnknownValueError,
defaultErrorMessage: "unknown value 'd'"
}, {
name: 'not-contains',
execFail: function(errorMessage, callback) {
return checkDoesNotContain('a', ['a'], errorMessage, callback);
},
execPass: function(callback) {
return checkDoesNotContain('d', ['a', 'b', 'c'], callback);
},
errorType: UnknownValueError,
defaultErrorMessage: "'a' is a known value"
}, {
name: 'equals',
execFail: function(errorMessage, callback) {
return checkEquals('a', 'b', errorMessage, callback);
},
execPass: function(callback) {
return checkEquals(true, true, callback);
},
errorType: UnknownValueError,
defaultErrorMessage: "expected 'b' but got 'a'"
}, {
name: 'not-equals',
execFail: function(errorMessage, callback) {
return checkDoesNotEqual(true, true, errorMessage, callback);
},
execPass: function(callback) {
return checkDoesNotEqual('a', 'b', callback);
},
errorType: UnknownValueError,
defaultErrorMessage: "did not expect value 'true'"
}, {
name: 'defined',
execFail: function(errorMessage, callback) {
return checkDefined({}.undefined, errorMessage, callback);
},
execPass: function(callback) {
return checkDefined(true, callback);
},
errorType: UndefinedValueError,
defaultErrorMessage: 'undefined value'
}, {
name: 'not-defined',
execFail: function(errorMessage, callback) {
return checkUndefined(true, errorMessage, callback);
},
execPass: function(callback) {
return checkUndefined({}.undefined, callback);
},
errorType: UndefinedValueError,
defaultErrorMessage: "'true' is a defined value"
}, {
name: 'empty',
execFail: function(errorMessage, callback) {
return checkEmpty('string', errorMessage, callback);
},
execPass: function(callback) {
return checkEmpty('', callback);
},
errorType: IllegalValueError,
defaultErrorMessage: "'string' is not empty"
}, {
name: 'not-empty',
execFail: function(errorMessage, callback) {
return checkNotEmpty('', errorMessage, callback);
},
execPass: function(callback) {
return checkNotEmpty('string', callback);
},
errorType: IllegalValueError,
defaultErrorMessage: 'illegal value'
}, {
name: 'state',
execFail: function(errorMessage, callback) {
return checkState(false, errorMessage, callback);
},
execPass: function(callback) {
return checkState(true, callback);
},
errorType: IllegalStateError,
defaultErrorMessage: 'illegal state'
}, {
name: 'null',
execFail: function(errorMessage, callback) {
return checkNull('string', errorMessage, callback);
},
execPass: function(callback) {
return checkNull(null, callback);
},
errorType: IllegalValueError,
defaultErrorMessage: "'string' is not null"
}, {
name: 'not-null',
execFail: function(errorMessage, callback) {
return checkNotNull(null, errorMessage, callback);
},
execPass: function(callback) {
return checkNotNull('string', callback);
},
errorType: IllegalValueError,
defaultErrorMessage: 'value is null'
}
];
runTests = function(test) {
var executor, _i, _len, _results;
_results = [];
for (_i = 0, _len = executors.length; _i < _len; _i++) {
executor = executors[_i];
_results.push(test(executor));
}
return _results;
};
describe('common tests for preconditions', function() {
describe('error message can be controlled by precondition call', function() {
return runTests(function(executor) {
return it("" + executor.name + " check", function() {
var wrapper;
wrapper = function() {
return executor.execFail(FORCED_ERROR);
};
return assert.throws(wrapper, executor.errorType, FORCED_ERROR);
});
});
});
describe('preconditions return a default error message if none present', function() {
return runTests(function(executor) {
return it("" + executor.name + " check", function() {
var e;
try {
executor.execFail();
return assert.fail('expected an exception to be thrown');
} catch (_error) {
e = _error;
assert.instanceOf(e, executor.errorType);
return assert.equal(e.message, executor.defaultErrorMessage);
}
});
});
});
describe('can call precondition with a callback', function() {
return runTests(function(executor) {
return it("" + executor.name + " check", function(done) {
return executor.execFail(FORCED_ERROR, function(err) {
assert.equal(FORCED_ERROR, err.message);
assert.instanceOf(err, executor.errorType);
return done();
});
});
});
});
describe('happy path test', function() {
return runTests(function(executor) {
return it("" + executor.name + " check", function() {
return assert.doesNotThrow(executor.execPass);
});
});
});
return describe('happy path test with a callback', function() {
return runTests(function(executor) {
return it("" + executor.name + " check", function(done) {
return executor.execPass(function(err) {
assert.isNull(err, 'no error should be thrown in happy path');
return done();
});
});
});
});
});
}).call(this);
},{"./helpers/test_helper":8}],3:[function(require,module,exports){
(function() {
var IllegalArgumentError, UnknownValueError, checkContains;
require('./helpers/test_helper');
checkContains = preconditions.checkContains, UnknownValueError = preconditions.UnknownValueError, IllegalArgumentError = preconditions.IllegalArgumentError;
describe('test contains precondition', function() {
describe('tests with strings', function() {
it('string array test', function() {
var wrapper;
wrapper = function() {
return checkContains('a', ['a', 'b', 'c']);
};
return assert.doesNotThrow(wrapper);
});
it('empty string test', function() {
var wrapper;
wrapper = function() {
return checkContains('', ['a', 'b', ''], 'empty string present');
};
return assert.doesNotThrow(wrapper);
});
it('string and number are different types', function() {
var wrapper;
wrapper = function() {
return checkContains('5', ['a', 'b', 5], '5 not present');
};
assert.throws(wrapper, UnknownValueError, '5 not present');
wrapper = function() {
return checkContains(5, ['a', 'b', '5'], '5 not present');
};
return assert.throws(wrapper, UnknownValueError, '5 not present');
});
it('unequal string do not contain each other', function() {
var wrapper;
wrapper = function() {
return checkContains('random', 'string');
};
return assert.throws(wrapper, UnknownValueError, "unknown value 'random'");
});
it('equal strings also contain each other', function() {
var wrapper;
wrapper = function() {
return checkContains('string', 'string');
};
return assert.doesNotThrow;
});
it('substring of a string', function() {
var wrapper;
wrapper = function() {
return checkContains('str', 'string');
};
return assert.doesNotThrow;
});
it('empty string can contain empty string', function() {
var wrapper;
wrapper = function() {
return checkContains('', '');
};
return assert.doesNotThrow(wrapper);
});
return it('empty string cannot contain non-empty string', function() {
var wrapper;
wrapper = function() {
return checkContains('str', '');
};
return assert.throws(wrapper, UnknownValueError, "unknown value 'str'");
});
});
describe('tests for null values', function() {
it('null value test', function() {
var wrapper;
wrapper = function() {
return checkContains(null, ['a', 'b', 'null'], 'null value missing');
};
assert.throws(wrapper, UnknownValueError, 'null value missing');
wrapper = function() {
return checkContains(null, ['a', 'b', null], 'null value present');
};
return assert.doesNotThrow(wrapper);
});
it('null and 0 are not same', function() {
var wrapper;
wrapper = function() {
return checkContains(null, ['a', 'b', 0], 'null not present');
};
assert.throws(wrapper, UnknownValueError, 'null not present');
wrapper = function() {
return checkContains(0, ['a', 'b', null], '0 not present');
};
return assert.throws(wrapper, UnknownValueError, '0 not present');
});
it('IllegalArgumentError is thrown for null collection object', function() {
var wrapper;
wrapper = function() {
return checkContains('a', null, 'this message is hidden');
};
return assert.throws(wrapper, IllegalArgumentError, 'invalid collection value');
});
return it('null is not same as empty string', function() {
var wrapper;
wrapper = function() {
return checkContains(null, '');
};
return assert.throws(wrapper, UnknownValueError, "unknown value 'null'");
});
});
describe('tests for objects', function() {
it('object can be checked in array', function() {
var obj1, obj2, obj3, wrapper;
obj1 = [1];
obj2 = {
'test': 'test'
};
obj3 = null;
wrapper = function() {
checkContains(obj1, [obj1, obj2, obj3]);
checkContains(obj2, [obj1, obj2, obj3]);
return checkContains(obj3, [obj1, obj2, obj3]);
};
return assert.doesNotThrow(wrapper);
});
it('a hash map can contain string keys', function() {
var map, wrapper;
map = {
val1: 1,
val2: 2,
val3: 3
};
wrapper = function() {
return checkContains('val1', map);
};
return assert.doesNotThrow(wrapper);
});
return it('a hash map can contain number keys', function() {
var map, wrapper;
map = {
1: 1,
2: 2,
3: 3
};
wrapper = function() {
return checkContains(1, map);
};
return assert.doesNotThrow(wrapper);
});
});
return describe('tests for numbers', function() {
it('check number contains another', function() {
var wrapper;
wrapper = function() {
return checkContains(1, 123);
};
return assert.doesNotThrow(wrapper);
});
it('check number not present in another number', function() {
var wrapper;
wrapper = function() {
return checkContains(4, 123);
};
return assert.throws(wrapper, UnknownValueError, "unknown value '4'");
});
it('check string contained in number', function() {
var wrapper;
wrapper = function() {
return checkContains('1', 123);
};
return assert.doesNotThrow(wrapper);
});
return it('check string missing from number', function() {
var wrapper;
wrapper = function() {
return checkContains('4', 123);
};
return assert.throws(wrapper, UnknownValueError, "unknown value '4'");
});
});
});
}).call(this);
},{"./helpers/test_helper":8}],4:[function(require,module,exports){
(function() {
var checkDefined;
require('./helpers/test_helper');
checkDefined = preconditions.checkDefined;
describe('test defined precondition', function() {
it('empty string is not undefined', function() {
var wrapper;
wrapper = function() {
return checkDefined('', 'expecting empty string');
};
return assert.doesNotThrow(wrapper);
});
it('0 is not undefined', function() {
var wrapper;
wrapper = function() {
return checkDefined(0, 'expecting zero');
};
return assert.doesNotThrow(wrapper);
});
it('empty array is not undefined', function() {
var wrapper;
wrapper = function() {
return checkDefined([], 'expecting empty array');
};
return assert.doesNotThrow(wrapper);
});
it('empty object is not undefined', function() {
var wrapper;
wrapper = function() {
return checkDefined({}, 'expecting empty object');
};
return assert.doesNotThrow(wrapper);
});
it('null is a defined value', function() {
var wrapper;
wrapper = function() {
return checkDefined(null, 'expecting null');
};
return assert.doesNotThrow(wrapper);
});
return it('test for undefined keyword', function() {
var wrapper;
wrapper = function() {
return checkDefined(void 0, 'should fail');
};
return assert.throws(wrapper, 'should fail');
});
});
}).call(this);
},{"./helpers/test_helper":8}],5:[function(require,module,exports){
(function() {
var IllegalArgumentError, UNDEFINED, UnknownValueError, checkEquals;
require('./helpers/test_helper');
checkEquals = preconditions.checkEquals, UnknownValueError = preconditions.UnknownValueError, IllegalArgumentError = preconditions.IllegalArgumentError;
UNDEFINED = {}.xyz;
describe('test equality precondition', function() {
describe('primitive type tests', function() {
it('string equality test', function() {
var wrapper;
wrapper = function() {
return checkEquals('str', 'str', "expecting 'a'");
};
assert.doesNotThrow(wrapper);
wrapper = function() {
return checkEquals('abc', 'xyz', 'invalid string');
};
return assert.throws(wrapper, UnknownValueError, 'invalid string');
});
it('number equality test', function() {
var wrapper;
wrapper = function() {
return checkEquals(5, 5, 'number should be equal');
};
assert.doesNotThrow(wrapper);
wrapper = function() {
return checkEquals(123, 234, 'invalid number');
};
return assert.throws(wrapper, UnknownValueError, 'invalid number');
});
it('numbers and string are not equal', function() {
var wrapper;
wrapper = function() {
return checkEquals('123', 123, 'expecting number');
};
return assert.throws(wrapper, UnknownValueError, 'expecting number');
});
return it('boolean equality test', function() {
var wrapper;
wrapper = function() {
return checkEquals(false, false, 'true value expected');
};
assert.doesNotThrow(wrapper);
wrapper = function() {
return checkEquals(true, false, 'invalid string');
};
return assert.throws(wrapper, UnknownValueError, 'invalid string');
});
});
describe('object equality tests', function() {
it('flat object test', function() {
var wrapper;
wrapper = function() {
return checkEquals({
val: 'a'
}, {
val: 'a'
}, 'expecting {val: a}');
};
assert.doesNotThrow(wrapper);
wrapper = function() {
return checkEquals({
val: 'a'
}, {
val: 'b'
}, 'invalid object');
};
assert.throws(wrapper, UnknownValueError, 'invalid object');
wrapper = function() {
return checkEquals({
val1: 'a'
}, {
val2: 'a'
}, 'expecting {val2: a}');
};
return assert.throws(wrapper, UnknownValueError, 'expecting {val2: a}');
});
return it('nested object test', function() {
var wrapper;
wrapper = function() {
return checkEquals({
val: 'a',
arr: [
1, 2, {
x: 'y'
}
],
inner: {
val2: 2
}
}, {
val: 'a',
arr: [
1, 2, {
x: 'y'
}
],
inner: {
val2: 2
}
});
};
assert.doesNotThrow(wrapper);
wrapper = function() {
return checkEquals({
val: 'a',
arr: [
1, 2, {
x: 'y'
}
],
inner: {
val2: 1
}
}, {
val: 'a',
arr: [
1, 2, {
x: 'y'
}
],
inner: {
val2: 2
}
}, 'expecting val2 to be 2');
};
return assert.throws(wrapper, UnknownValueError, 'expecting val2 to be 2');
});
});
describe('undefined and null values equality tests', function() {
it('undefined should not be expected', function() {
var wrapper;
wrapper = function() {
return checkEquals('val', UNDEFINED);
};
return assert.throws(wrapper, IllegalArgumentError, 'invalid value expected');
});
it('nulls are equal', function() {
var wrapper;
wrapper = function() {
return checkEquals(null, null, 'null value expected');
};
return assert.doesNotThrow(wrapper);
});
it('null is not same as undefined', function() {
var wrapper;
wrapper = function() {
return checkEquals(UNDEFINED, null, 'null value expected');
};
return assert.throws(wrapper, UnknownValueError, 'null value expected');
});
return it('boolean false is not null or undefined', function() {
var wrapper;
wrapper = function() {
return checkEquals(null, false, 'expecting false');
};
assert.throws(wrapper, UnknownValueError, 'expecting false');
wrapper = function() {
return checkEquals(UNDEFINED, false, 'expecting false');
};
return assert.throws(wrapper, UnknownValueError, 'expecting false');
});
});
return describe('array equality tests', function() {
it('flat array test', function() {
var wrapper;
wrapper = function() {
return checkEquals(['a'], ['a'], 'expecting [a]');
};
assert.doesNotThrow(wrapper);
wrapper = function() {
return checkEquals(['b'], ['a'], 'expecting [a]');
};
return assert.throws(wrapper, UnknownValueError, 'expecting [a]');
});
it('empty arrays are equal', function() {
var wrapper;
wrapper = function() {
return checkEquals([], [], 'expecting empty array');
};
return assert.doesNotThrow(wrapper);
});
it('arrays of different length are not equal', function() {
var wrapper;
wrapper = function() {
return checkEquals(['a', 'b'], ['a', 'b', 'c'], 'expecting length 3');
};
assert.throws(wrapper, UnknownValueError, 'expecting length 3');
wrapper = function() {
return checkEquals(['a', 'b', 'c'], ['a', 'b'], 'expecting length 2');
};
return assert.throws(wrapper, UnknownValueError, 'expecting length 2');
});
it('array containing null values are equal', function() {
var wrapper;
wrapper = function() {
return checkEquals([null, null], [null, null], 'expecting 2 nulls');
};
return assert.doesNotThrow(wrapper);
});
it('array containing undefined values are not equal', function() {
var wrapper;
wrapper = function() {
return checkEquals([UNDEFINED], [UNDEFINED], 'expecting undefined');
};
return assert.throws(wrapper, UnknownValueError, 'expecting undefined');
});
return it('multidimensional array equality test', function() {
var wrapper;
wrapper = function() {
return checkEquals([[1, 2], [3, 4], ['a', ['b']]], [[1, 2], [3, 4], ['a', ['b']]]);
};
assert.doesNotThrow(wrapper);
wrapper = function() {
return checkEquals([[1, 2], [3, 4], ['a', ['b']]], [[1, 2], [3, 4], ['a', ['b', 'c']]], 'expecting abc');
};
return assert.throws(wrapper, UnknownValueError, 'expecting abc');
});
});
});
}).call(this);
},{"./helpers/test_helper":8}],6:[function(require,module,exports){
(function() {
}).call(this);
},{}],7:[function(require,module,exports){
(function (process,global){
(function() {
var coverageMode, srcPath, srcType;
coverageMode = process.env['NODE_ENV'] === 'coverage';
srcPath = coverageMode ? '../../coverage/src' : '../../src';
srcType = coverageMode ? 'js' : 'coffee';
global.preconditions = require("" + srcPath + "/main");
}).call(this);
}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"_process":16}],8:[function(require,module,exports){
(function (global){
(function() {
global.assert = require('chai').assert;
if (typeof window !== "undefined" && window !== null) {
require('./browser_helper');
} else {
require('./node_helper');
}
}).call(this);
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./browser_helper":6,"./node_helper":7,"chai":17}],9:[function(require,module,exports){
(function() {
var IllegalValueError, checkNotEmpty;
require('./helpers/test_helper');
checkNotEmpty = preconditions.checkNotEmpty, IllegalValueError = preconditions.IllegalValueError;
describe('test notEmpty precondition', function() {
it('null is a empty value', function() {
var wrapper;
wrapper = function() {
return checkNotEmpty(null, 'should not be null');
};
return assert.throws(wrapper, IllegalValueError, 'should not be null');
});
it('undefined is a empty value', function() {
var wrapper;
wrapper = function() {
return checkNotEmpty({}.x, 'should not be undefined');
};
return assert.throws(wrapper, IllegalValueError, 'should not be undefined');
});
it('empty array is a empty value', function() {
var wrapper;
wrapper = function() {
return checkNotEmpty([], 'should not be empty');
};
return assert.throws(wrapper, IllegalValueError, 'should not be empty');
});
it('empty object is a empty value', function() {
var wrapper;
wrapper = function() {
return checkNotEmpty({}, 'should not be empty');
};
return assert.throws(wrapper, IllegalValueError, 'should not be empty');
});
it('empty string is a empty value', function() {
var wrapper;
wrapper = function() {
return checkNotEmpty('', 'should not be empty');
};
return assert.throws(wrapper, IllegalValueError, 'should not be empty');
});
it('0 (zero) is not empty', function() {
var wrapper;
wrapper = function() {
return checkNotEmpty(0);
};
return assert.doesNotThrow(wrapper);
});
return it('boolean false is not empty', function() {
var wrapper;
wrapper = function() {
return checkNotEmpty(false);
};
return assert.doesNotThrow(wrapper);
});
});
}).call(this);
},{"./helpers/test_helper":8}],10:[function(require,module,exports){
(function() {
var IllegalValueError, checkNotNull, checkNull;
require('./helpers/test_helper');
checkNull = preconditions.checkNull, checkNotNull = preconditions.checkNotNull, IllegalValueError = preconditions.IllegalValueError;
describe('not null precondition test', function() {
it('undefined is null', function() {
var wrapper;
wrapper = function() {
return checkNotNull({}.x, 'not expecting null');
};
assert.throws(wrapper, IllegalValueError, 'not expecting null');
wrapper = function() {
return checkNull({}.x, 'null expected');
};
return assert.doesNotThrow(wrapper);
});
it('empty string is not null', function() {
var wrapper;
wrapper = function() {
return checkNotNull('');
};
return assert.doesNotThrow(wrapper);
});
it('empty array is not null', function() {
var wrapper;
wrapper = function() {
return checkNotNull([]);
};
return assert.doesNotThrow(wrapper);
});
it('empty object is not null', function() {
var wrapper;
wrapper = function() {
return checkNotNull({});
};
return assert.doesNotThrow(wrapper);
});
it('boolean is not null', function() {
var wrapper;
wrapper = function() {
return checkNotNull(false);
};
assert.doesNotThrow(wrapper);
wrapper = function() {
return checkNull(true);
};
return assert.throws(wrapper, IllegalValueError, "'true' is not null");
});
return it('0 (zero) is not null', function() {
var wrapper;
wrapper = function() {
return checkNotNull(0);
};
assert.doesNotThrow(wrapper);
wrapper = function() {
return checkNull(0);
};
return assert.throws(wrapper, IllegalValueError, "'0' is not null");
});
});
}).call(this);
},{"./helpers/test_helper":8}],11:[function(require,module,exports){
(function() {
var InvalidTypeError, checkNumberType;
require('./helpers/test_helper');
checkNumberType = preconditions.checkNumberType, InvalidTypeError = preconditions.InvalidTypeError;
describe('test number type precondition', function() {
it("boolean 'true' values are not numbers", function() {
var wrapper;
wrapper = function() {
return checkNumberType(true, 'invalid numerical value');
};
return assert.throws(wrapper, InvalidTypeError, 'invalid numerical value');
});
it("boolean 'false' values are not numbers", function() {
var wrapper;
wrapper = function() {
return checkNumberType(false, 'invalid numerical value');
};
return assert.throws(wrapper, InvalidTypeError, 'invalid numerical value');
});
it('arrays are not numbers', function() {
var wrapper;
wrapper = function() {
return checkNumberType([5], 'invalid numerical value');
};
return assert.throws(wrapper, InvalidTypeError, 'invalid numerical value');
});
return it('objects are not numbers', function() {
var wrapper;
wrapper = function() {
return checkNumberType(new Object, 'invalid numerical value');
};
return assert.throws(wrapper, InvalidTypeError, 'invalid numerical value');
});
});
}).call(this);
},{"./helpers/test_helper":8}],12:[function(require,module,exports){
/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
* @license MIT
*/
var base64 = require('base64-js')
var ieee754 = require('ieee754')
var isArray = require('is-array')
exports.Buffer = Buffer
exports.SlowBuffer = SlowBuffer
exports.INSPECT_MAX_BYTES = 50
Buffer.poolSize = 8192 // not used by this implementation
var kMaxLength = 0x3fffffff
var rootParent = {}
/**
* If `Buffer.TYPED_ARRAY_SUPPORT`:
* === true Use Uint8Array implementation (fastest)
* === false Use Object implementation (most compatible, even IE6)
*
* Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
* Opera 11.6+, iOS 4.2+.
*
* Note:
*
* - Implementation must support adding new properties to `Uint8Array` instances.
* Firefox 4-29 lacked support, fixed in Firefox 30+.
* See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
*
* - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
*
* - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
* incorrect length in some situations.
*
* We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they will
* get the Object implementation, which is slower but will work correctly.
*/
Buffer.TYPED_ARRAY_SUPPORT = (function () {
try {
var buf = new ArrayBuffer(0)
var arr = new Uint8Array(buf)
arr.foo = function () { return 42 }
return 42 === arr.foo() && // typed array instances can be augmented
typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`
new Uint8Array(1).subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`
} catch (e) {
return false
}
})()
/**
* Class: Buffer
* =============
*
* The Buffer constructor returns instances of `Uint8Array` that are augmented
* with function properties for all the node `Buffer` API functions. We use
* `Uint8Array` so that square bracket notation works as expected -- it returns
* a single octet.
*
* By augmenting the instances, we can avoid modifying the `Uint8Array`
* prototype.
*/
function Buffer (subject, encoding, noZero) {
if (!(this instanceof Buffer))
return new Buffer(subject, encoding, noZero)
var type = typeof subject
// Find the length
var length
if (type === 'number')
length = subject > 0 ? subject >>> 0 : 0
else if (type === 'string') {
length = Buffer.byteLength(subject, encoding)
} else if (type === 'object' && subject !== null) { // assume object is array-like
if (subject.type === 'Buffer' && isArray(subject.data))
subject = subject.data
length = +subject.length > 0 ? Math.floor(+subject.length) : 0
} else
throw new TypeError('must start with number, buffer, array or string')
if (length > kMaxLength)
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
'size: 0x' + kMaxLength.toString(16) + ' bytes')
var buf
if (Buffer.TYPED_ARRAY_SUPPORT) {
// Preferred: Return an augmented `Uint8Array` instance for best performance
buf = Buffer._augment(new Uint8Array(length))
} else {
// Fallback: Return THIS instance of Buffer (created by `new`)
buf = this
buf.length = length
buf._isBuffer = true
}
var i
if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') {
// Speed optimization -- use set if we're copying from a typed array
buf._set(subject)
} else if (isArrayish(subject)) {
// Treat array-ish objects as a byte array
if (Buffer.isBuffer(subject)) {
for (i = 0; i < length; i++)
buf[i] = subject.readUInt8(i)
} else {
for (i = 0; i < length; i++)
buf[i] = ((subject[i] % 256) + 256) % 256
}
} else if (type === 'string') {
buf.write(subject, 0, encoding)
} else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT && !noZero) {
for (i = 0; i < length; i++) {
buf[i] = 0
}
}
if (length > 0 && length <= Buffer.poolSize)
buf.parent = rootParent
return buf
}
function SlowBuffer(subject, encoding, noZero) {
if (!(this instanceof SlowBuffer))
return new SlowBuffer(subject, encoding, noZero)
var buf = new Buffer(subject, encoding, noZero)
delete buf.parent
return buf
}
Buffer.isBuffer = function (b) {
return !!(b != null && b._isBuffer)
}
Buffer.compare = function (a, b) {
if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b))
throw new TypeError('Arguments must be Buffers')
var x = a.length
var y = b.length
for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {}
if (i !== len) {
x = a[i]
y = b[i]
}
if (x < y) return -1
if (y < x) return 1
return 0
}
Buffer.isEncoding = function (encoding) {
switch (String(encoding).toLowerCase()) {
case 'hex':
case 'utf8':
case 'utf-8':
case 'ascii':
case 'binary':
case 'base64':
case 'raw':
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
return true
default:
return false
}
}
Buffer.concat = function (list, totalLength) {
if (!isArray(list)) throw new TypeError('Usage: Buffer.concat(list[, length])')
if (list.length === 0) {
return new Buffer(0)
} else if (list.length === 1) {
return list[0]
}
var i
if (totalLength === undefined) {
totalLength = 0
for (i = 0; i < list.length; i++) {
totalLength += list[i].length
}
}
var buf = new Buffer(totalLength)
var pos = 0
for (i = 0; i < list.length; i++) {
var item = list[i]
item.copy(buf, pos)
pos += item.length
}
return buf
}
Buffer.byteLength = function (str, encoding) {
var ret
str = str + ''
switch (encoding || 'utf8') {
case 'ascii':
case 'binary':
case 'raw':
ret = str.length
break
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
ret = str.length * 2
break
case 'hex':
ret = str.length >>> 1
break
case 'utf8':
case 'utf-8':
ret = utf8ToBytes(str).length
break
case 'base64':
ret = base64ToBytes(str).length
break
default:
ret = str.length
}
return ret
}
// pre-set for values that may exist in the future
Buffer.prototype.length = undefined
Buffer.prototype.parent = undefined
// toString(encoding, start=0, end=buffer.length)
Buffer.prototype.toString = function (encoding, start, end) {
var loweredCase = false
start = start >>> 0
end = end === undefined || end === Infinity ? this.length : end >>> 0
if (!encoding) encoding = 'utf8'
if (start < 0) start = 0
if (end > this.length) end = this.length
if (end <= start) return ''
while (true) {
switch (encoding) {
case 'hex':
return hexSlice(this, start, end)
case 'utf8':
case 'utf-8':
return utf8Slice(this, start, end)
case 'ascii':
return asciiSlice(this, start, end)
case 'binary':
return binarySlice(this, start, end)
case 'base64':
return base64Slice(this, start, end)
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
return utf16leSlice(this, start, end)
default:
if (loweredCase)
throw new TypeError('Unknown encoding: ' + encoding)
encoding = (encoding + '').toLowerCase()
loweredCase = true
}
}
}
Buffer.prototype.equals = function (b) {
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
return Buffer.compare(this, b) === 0
}
Buffer.prototype.inspect = function () {
var str = ''
var max = exports.INSPECT_MAX_BYTES
if (this.length > 0) {
str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')
if (this.length > max)
str += ' ... '
}
return '<Buffer ' + str + '>'
}
Buffer.prototype.compare = function (b) {
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
return Buffer.compare(this, b)
}
// `get` will be removed in Node 0.13+
Buffer.prototype.get = function (offset) {
console.log('.get() is deprecated. Access using array indexes instead.')
return this.readUInt8(offset)
}
// `set` will be removed in Node 0.13+
Buffer.prototype.set = function (v, offset) {
console.log('.set() is deprecated. Access using array indexes instead.')
return this.writeUInt8(v, offset)
}
function hexWrite (buf, string, offset, length) {
offset = Number(offset) || 0
var remaining = buf.length - offset
if (!length) {
length = remaining
} else {
length = Number(length)
if (length > remaining) {
length = remaining
}
}
// must be an even number of digits
var strLen = string.length
if (strLen % 2 !== 0) throw new Error('Invalid hex string')
if (length > strLen / 2) {
length = strLen / 2
}
for (var i = 0; i < length; i++) {
var byte = parseInt(string.substr(i * 2, 2), 16)
if (isNaN(byte)) throw new Error('Invalid hex string')
buf[offset + i] = byte
}
return i
}
function utf8Write (buf, string, offset, length) {
var charsWritten = blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
return charsWritten
}
function asciiWrite (buf, string, offset, length) {
var charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length)
return charsWritten
}
function binaryWrite (buf, string, offset, length) {
return asciiWrite(buf, string, offset, length)
}
function base64Write (buf, string, offset, length) {
var charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length)
return charsWritten
}
function utf16leWrite (buf, string, offset, length) {
var charsWritten = blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length, 2)
return charsWritten
}
Buffer.prototype.write = function (string, offset, length, encoding) {
// Support both (string, offset, length, encoding)
// and the legacy (string, encoding, offset, length)
if (isFinite(offset)) {
if (!isFinite(length)) {
encoding = length
length = undefined
}
} else { // legacy
var swap = encoding
encoding = offset
offset = length
length = swap
}
offset = Number(offset) || 0
if (length < 0 || offset < 0 || offset > this.length)
throw new RangeError('attempt to write outside buffer bounds');
var remaining = this.length - offset
if (!length) {
length = remaining
} else {
length = Number(length)
if (length > remaining) {
length = remaining
}
}
encoding = String(encoding || 'utf8').toLowerCase()
var ret
switch (encoding) {
case 'hex':
ret = hexWrite(this, string, offset, length)
break
case 'utf8':
case 'utf-8':
ret = utf8Write(this, string, offset, length)
break
case 'ascii':
ret = asciiWrite(this, string, offset, length)
break
case 'binary':
ret = binaryWrite(this, string, offset, length)
break
case 'base64':
ret = base64Write(this, string, offset, length)
break
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
ret = utf16leWrite(this, string, offset, length)
break
default:
throw new TypeError('Unknown encoding: ' + encoding)
}
return ret
}
Buffer.prototype.toJSON = function () {
return {
type: 'Buffer',
data: Array.prototype.slice.call(this._arr || this, 0)
}
}
function base64Slice (buf, start, end) {
if (start === 0 && end === buf.length) {
return base64.fromByteArray(buf)
} else {
return base64.fromByteArray(buf.slice(start, end))
}
}
function utf8Slice (buf, start, end) {
var res = ''
var tmp = ''
end = Math.min(buf.length, end)
for (var i = start; i < end; i++) {
if (buf[i] <= 0x7F) {
res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i])
tmp = ''
} else {
tmp += '%' + buf[i].toString(16)
}
}
return res + decodeUtf8Char(tmp)
}
function asciiSlice (buf, start, end) {
var ret = ''
end = Math.min(buf.length, end)
for (var i = start; i < end; i++) {
ret += String.fromCharCode(buf[i] & 0x7F)
}
return ret
}
function binarySlice (buf, start, end) {
var ret = ''
end = Math.min(buf.length, end)
for (var i = start; i < end; i++) {
ret += String.fromCharCode(buf[i])
}
return ret
}
function hexSlice (buf, start, end) {
var len = buf.length
if (!start || start < 0) start = 0
if (!end || end < 0 || end > len) end = len
var out = ''
for (var i = start; i < end; i++) {
out += toHex(buf[i])
}
return out
}
function utf16leSlice (buf, start, end) {
var bytes = buf.slice(start, end)
var res = ''
for (var i = 0; i < bytes.length; i += 2) {
res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)
}
return res
}
Buffer.prototype.slice = function (start, end) {
var len = this.length
start = ~~start
end = end === undefined ? len : ~~end
if (start < 0) {
start += len;
if (start < 0)
start = 0
} else if (start > len) {
start = len
}
if (end < 0) {
end += len
if (end < 0)
end = 0
} else if (end > len) {
end = len
}
if (end < start)
end = start
var newBuf
if (Buffer.TYPED_ARRAY_SUPPORT) {
newBuf = Buffer._augment(this.subarray(start, end))
} else {
var sliceLen = end - start
newBuf = new Buffer(sliceLen, undefined, true)
for (var i = 0; i < sliceLen; i++) {
newBuf[i] = this[i + start]
}
}
if (newBuf.length)
newBuf.parent = this.parent || this
return newBuf
}
/*
* Need to make sure that buffer isn't trying to write out of bounds.
*/
function checkOffset (offset, ext, length) {
if ((offset % 1) !== 0 || offset < 0)
throw new RangeError('offset is not uint')
if (offset + ext > length)
throw new RangeError('Trying to access beyond buffer length')
}
Buffer.prototype.readUIntLE = function (offset, byteLength, noAssert) {
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert)
checkOffset(offset, byteLength, this.length)
var val = this[offset]
var mul = 1
var i = 0
while (++i < byteLength && (mul *= 0x100))
val += this[offset + i] * mul
return val
}
Buffer.prototype.readUIntBE = function (offset, byteLength, noAssert) {
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert)
checkOffset(offset, byteLength, this.length)
var val = this[offset + --byteLength]
var mul = 1
while (byteLength > 0 && (mul *= 0x100))
val += this[offset + --byteLength] * mul;
return val
}
Buffer.prototype.readUInt8 = function (offset, noAssert) {
if (!noAssert)
checkOffset(offset, 1, this.length)
return this[offset]
}
Buffer.prototype.readUInt16LE = function (offset, noAssert) {
if (!noAssert)
checkOffset(offset, 2, this.length)
return this[offset] | (this[offset + 1] << 8)
}
Buffer.prototype.readUInt16BE = function (offset, noAssert) {
if (!noAssert)
checkOffset(offset, 2, this.length)
return (this[offset] << 8) | this[offset + 1]
}
Buffer.prototype.readUInt32LE = function (offset, noAssert) {
if (!noAssert)
checkOffset(offset, 4, this.length)
return ((this[offset]) |
(this[offset + 1] << 8) |
(this[offset + 2] << 16)) +
(this[offset + 3] * 0x1000000)
}
Buffer.prototype.readUInt32BE = function (offset, noAssert) {
if (!noAssert)
checkOffset(offset, 4, this.length)
return (this[offset] * 0x1000000) +
((this[offset + 1] << 16) |
(this[offset + 2] << 8) |
this[offset + 3])
}
Buffer.prototype.readIntLE = function (offset, byteLength, noAssert) {
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert)
checkOffset(offset, byteLength, this.length)
var val = this[offset]
var mul = 1
var i = 0
while (++i < byteLength && (mul *= 0x100))
val += this[offset + i] * mul
mul *= 0x80
if (val >= mul)
val -= Math.pow(2, 8 * byteLength)
return val
}
Buffer.prototype.readIntBE = function (offset, byteLength, noAssert) {
offset = offset >>> 0
byteLength = byteLength >>> 0
if (!noAssert)
checkOffset(offset, byteLength, this.length)
var i = byteLength
var mul = 1
var val = this[offset + --i]
while (i > 0 && (mul *= 0x100))
val += this[offset + --i] * mul
mul *= 0x80
if (val >= mul)
val -= Math.pow(2, 8 * byteLength)
return val
}
Buffer.prototype.readInt8 = function (offset, noAssert) {
if (!noAssert)
checkOffset(offset, 1, this.length)
if (!(this[offset] & 0x80))
return (this[offset])
return ((0xff - this[offset] + 1) * -1)
}
Buffer.prototype.readInt16LE = function (offset, noAssert) {
if (!noAssert)
checkOffset(offset, 2, this.length)
var val = this[offset] | (this[offset + 1] << 8)
return (val & 0x8000) ? val | 0xFFFF0000 : val
}
Buffer.prototype.readInt16BE = function (offs