2015-12-03 20:43:42 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
let chai = require('chai'),
|
|
|
|
assert = chai.assert,
|
|
|
|
expect = chai.expect,
|
|
|
|
should = chai.should();
|
|
|
|
|
|
|
|
let helpers = require('../lib/helpers');
|
|
|
|
|
2016-01-26 19:29:12 -05:00
|
|
|
suite('Helper Module Tests -', () => {
|
|
|
|
suite('Type-checking methods -', () => {
|
2015-12-03 20:43:42 -05:00
|
|
|
suite('Object wrappers are listed as their native type', () => {
|
2015-12-07 17:03:36 -05:00
|
|
|
test('Boolean Wrapper returns \'boolean\' not \'object\'', () => {
|
2015-12-03 20:43:42 -05:00
|
|
|
let item = Boolean(true);
|
|
|
|
expect(helpers.type(item)).to.deep.equal('boolean');
|
|
|
|
});
|
2015-12-07 17:03:36 -05:00
|
|
|
test('Number Wrapper returns \'number\' not \'object\'', () => {
|
2015-12-03 20:43:42 -05:00
|
|
|
let item = Number(4867);
|
|
|
|
expect(helpers.type(item)).to.deep.equal('number');
|
|
|
|
});
|
2015-12-07 17:03:36 -05:00
|
|
|
test('String Wrapper returns \'string\' not \'object\'', () => {
|
|
|
|
let item = String('Foo');
|
2015-12-03 20:43:42 -05:00
|
|
|
expect(helpers.type(item)).to.deep.equal('string');
|
|
|
|
});
|
|
|
|
});
|
2016-01-26 19:29:12 -05:00
|
|
|
suite('is..Method methods exist -', () => {
|
2015-12-07 17:03:36 -05:00
|
|
|
let types = [
|
|
|
|
'Null',
|
|
|
|
'Undefined',
|
|
|
|
'Object',
|
|
|
|
'Array',
|
|
|
|
'String',
|
|
|
|
'Number',
|
|
|
|
'Boolean',
|
|
|
|
'Function',
|
|
|
|
'RegExp',
|
|
|
|
'NaN',
|
|
|
|
'Infinite',
|
|
|
|
];
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
types.forEach(type => {
|
|
|
|
test(`is${type} method exists`, () => {
|
|
|
|
assert.ok(helpers[`is${type}`]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-01-26 19:29:12 -05:00
|
|
|
suite('isScalar -', () => {
|
2015-12-03 20:43:42 -05:00
|
|
|
let trueCases = {
|
|
|
|
'Strings are scalar': 'foo',
|
|
|
|
'Booleans are scalar': true,
|
2015-12-07 17:03:36 -05:00
|
|
|
'Numbers are scalar': 545,
|
2015-12-03 20:43:42 -05:00
|
|
|
};
|
|
|
|
Object.keys(trueCases).forEach(desc => {
|
|
|
|
test(desc, () => {
|
|
|
|
expect(helpers.isScalar(trueCases[desc])).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let falseCases = {
|
|
|
|
'Arrays are not scalar': [],
|
|
|
|
'Objects are not scalar': [],
|
|
|
|
};
|
|
|
|
Object.keys(falseCases).forEach(desc => {
|
|
|
|
test(desc, () => {
|
|
|
|
expect(helpers.isScalar(falseCases[desc])).to.be.false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-01-26 19:29:12 -05:00
|
|
|
suite('isInfinity -', () => {
|
2015-12-03 20:43:42 -05:00
|
|
|
test('The type of 1/0 is infinity', () => {
|
2015-12-07 17:03:36 -05:00
|
|
|
expect(helpers.type(1 / 0)).to.equal('infinity');
|
2015-12-03 20:43:42 -05:00
|
|
|
});
|
|
|
|
test('isInfinity is the same as isInfinite', () => {
|
2015-12-07 17:03:36 -05:00
|
|
|
expect(helpers.isInfinite(1 / 0)).to.be.true;
|
2015-12-03 20:43:42 -05:00
|
|
|
});
|
|
|
|
});
|
2016-01-26 19:29:12 -05:00
|
|
|
suite('isNaN -', () => {
|
2015-12-03 20:43:42 -05:00
|
|
|
test('The type of 0 / 0 is NaN', () => {
|
|
|
|
expect(helpers.type(0 / 0)).to.equal('nan');
|
|
|
|
});
|
|
|
|
test('isNaN method agrees with type', () => {
|
|
|
|
expect(helpers.isNaN(0 / 0)).to.be.true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-01-26 19:29:12 -05:00
|
|
|
suite('Other helper methods -', () => {
|
|
|
|
suite('stringTrim -', () => {
|
2015-12-03 20:43:42 -05:00
|
|
|
test('stringTrim method works as expected', () => {
|
|
|
|
let orig = [' x y ', 'z ', ' q'];
|
|
|
|
let ret = ['x y', 'z', 'q'];
|
|
|
|
|
|
|
|
expect(orig.map(helpers.stringTrim)).to.be.deep.equal(ret);
|
|
|
|
});
|
|
|
|
});
|
2016-01-26 19:29:12 -05:00
|
|
|
suite('arrayPluck -', () => {
|
2015-12-07 17:03:36 -05:00
|
|
|
let orig = [
|
|
|
|
{
|
|
|
|
foo: 1,
|
|
|
|
}, {
|
|
|
|
foo: 2,
|
|
|
|
bar: 10,
|
|
|
|
}, {
|
|
|
|
foo: 3,
|
|
|
|
bar: 15,
|
|
|
|
},
|
|
|
|
];
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
test('Finding members in all objects', () => {
|
2015-12-07 17:03:36 -05:00
|
|
|
expect(helpers.arrayPluck(orig, 'foo')).to.be.deep.equal([1, 2, 3]);
|
2015-12-03 20:43:42 -05:00
|
|
|
});
|
|
|
|
test('Some members are missing in some objects', () => {
|
|
|
|
expect(helpers.arrayPluck(orig, 'bar')).to.be.deep.equal([10, 15]);
|
|
|
|
});
|
|
|
|
test('Empty case', () => {
|
|
|
|
expect(helpers.arrayPluck([], 'apple')).to.be.deep.equal([]);
|
|
|
|
});
|
|
|
|
});
|
2016-01-26 19:29:12 -05:00
|
|
|
suite('regexInArray -', () => {
|
2015-12-03 20:43:42 -05:00
|
|
|
let orig = ['apple', ' string ', 6, 4, 7];
|
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
let cases = [
|
|
|
|
{
|
|
|
|
'Dollar sign is not in any of the array items': /\$/,
|
|
|
|
'None of the numbers in the array match /5/': /5/,
|
|
|
|
}, {
|
|
|
|
'\' string \' matches /^ ?string/': /^ ?string/,
|
|
|
|
'\'apple\' matches /APPLE/i': /APPLE/i,
|
|
|
|
},
|
|
|
|
];
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
[0, 1].forEach(i => {
|
|
|
|
let boolCase = cases[i];
|
|
|
|
Object.keys(boolCase).forEach(desc => {
|
|
|
|
test(desc, () => {
|
|
|
|
if (i) {
|
|
|
|
expect(helpers.regexInArray(orig, boolCase[desc])).to.be.true;
|
|
|
|
} else {
|
|
|
|
expect(helpers.regexInArray(orig, boolCase[desc])).to.be.false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('First argument is not an array', () => {
|
|
|
|
expect(helpers.regexInArray(5, /5/)).to.be.false;
|
|
|
|
});
|
|
|
|
test('Array is empty', () => {
|
|
|
|
expect(helpers.regexInArray([], /.*/)).to.be.false;
|
|
|
|
});
|
|
|
|
});
|
2016-03-11 16:32:38 -05:00
|
|
|
suite('upperCaseFirst -', () => {
|
|
|
|
test('Capitalizes only the first letter of the string', () => {
|
|
|
|
expect(helpers.upperCaseFirst('foobar')).to.equal('Foobar');
|
|
|
|
expect(helpers.upperCaseFirst('FOOBAR')).to.equal('FOOBAR');
|
|
|
|
});
|
|
|
|
});
|
2015-12-03 20:43:42 -05:00
|
|
|
});
|
|
|
|
});
|