Commit 63cebb07 authored by Stefan Cameron's avatar Stefan Cameron

Fix the unit tests; coverage is at 97% due to needed work in impl.check()

parent 957e1fdf
......@@ -167,7 +167,7 @@ const check = function(value, typeset) {
// TODO other typeset types
throw new Error(`Missing handler for typeset="${print(typeset)}" type specified`);
throw new Error(`Missing handler for type of specified typeset="${print(typeset)}"`);
} else {
throw new Error(`Invalid typeset="${print(typeset)}" specified`);
}
......@@ -200,7 +200,7 @@ const _registerType = function(validator) {
throw new Error(`Cannot register an invalid validator for type="${print(validator && validator.type)}": missing at least one required property in [type, config, default]`);
}
_validatorMap[validator.type, validator.default];
_validatorMap[validator.type] = validator.default;
};
////////////////////////////////////////////////////////////////////////////////
......@@ -220,13 +220,13 @@ const impl = {
// make properties/methods with underscore prefix internal by making them
// non-enumerable (but otherwise, a normal property)
Object.keys(impl).forEach(function(method, name) {
if (name.indexOf('_') === 0) {
Object.defineProperty(impl, name, {
Object.keys(impl).forEach(function(prop) {
if (prop.indexOf('_') === 0) {
Object.defineProperty(impl, prop, {
enumerable: false,
configurable: true,
writable: true,
value: method
value: impl[prop]
});
}
});
......
......@@ -284,5 +284,6 @@ export default rtv;
validators.forEach(function(val) {
val.config({impl: publicImpl});
impl._registerType(val);
});
})();
import {expect} from 'chai';
import sinon from 'sinon';
import '../../src/rtv'; // so that types get registered with `impl`
import impl from '../../src/lib/impl';
import {DEFAULT_OBJECT_TYPE, default as types} from '../../src/lib/types';
import {DEFAULT_QUALIFIER, default as qualifiers} from '../../src/lib/qualifiers';
import RtvSuccess from '../../src/lib/RtvSuccess';
import RtvError from '../../src/lib/RtvError';
import isObject from '../../src/lib/validator/isObject';
import isFunction from '../../src/lib/validator/isFunction';
describe('module: lib/impl', function() {
describe('._validatorMap', function() {
......@@ -14,7 +16,8 @@ describe('module: lib/impl', function() {
expect(Object.getOwnPropertyDescriptor(impl, '_validatorMap')).to.eql({
enumerable: false,
configurable: true,
writable: true
writable: true,
value: impl._validatorMap
});
expect(isObject(impl._validatorMap)).to.be.true;
expect(Object.keys(impl._validatorMap).length).to.equal(15); // # of known types
......@@ -22,13 +25,55 @@ describe('module: lib/impl', function() {
});
describe('#_registerType()', function() {
const errorRE = /Cannot register an invalid validator/;
let validator;
let stringValidator;
beforeEach(function() {
stringValidator = impl._validatorMap[types.STRING];
validator = {
type: types.STRING,
config: function() {},
default: function() {}
};
});
afterEach(function() {
impl._validatorMap[types.STRING] = stringValidator;
});
it('should be an internal method', function() {
expect(Object.getOwnPropertyDescriptor(impl, '_registerType')).to.eql({
value: pkg.version,
enumerable: false,
configurable: true,
writable: true
writable: true,
value: impl._registerType
});
expect(isFunction(impl._registerType)).to.be.true;
});
it('should throw if validator is invalid: not an object', function() {
expect(impl._registerType.bind(impl, 'foo')).to.throw(errorRE);
});
it('should throw if validator is invalid: unknown type', function() {
validator.type = 'unknown';
expect(impl._registerType.bind(impl, validator)).to.throw(errorRE);
});
it('should throw if validator is invalid: missing .type', function() {
delete validator.type;
expect(impl._registerType.bind(impl, validator)).to.throw(errorRE);
});
it('should throw if validator is invalid: missing #config()', function() {
validator.config = 123;
expect(impl._registerType.bind(impl, validator)).to.throw(errorRE);
});
it('should throw if validator is invalid: missing #default()', function() {
validator.default = 123;
expect(impl._registerType.bind(impl, validator)).to.throw(errorRE);
});
});
......@@ -47,6 +92,10 @@ describe('module: lib/impl', function() {
it('should throw if shape is not a valid typeset', function() {
expect(impl.check.bind(impl, 1, 'foo')).to.throw(/Cannot check value: Invalid typeset/);
});
it('should throw if typeset type is not supported', function() {
expect(impl.check.bind(impl, 1, function() {})).to.throw(/Missing handler for type of specified typeset/);
});
});
describe('#checkType()', function() {
......@@ -62,7 +111,7 @@ describe('module: lib/impl', function() {
const typesVerifyStub = sinon.stub(types, 'verify'); // prevent verification of unknown/invalid type
expect(function() {
impl.checkType(2, 'foo');
}).to.throw(/Missing validator for "foo" type/);
}).to.throw(/Missing validator for type="foo"/);
typesVerifyStub.restore();
});
});
......
......@@ -3,7 +3,7 @@ import _ from 'lodash';
import * as mod from '../../src/lib/qualifiers';
import Enumeration from '../../src/lib/Enumeration';
import isString from '../../src/lib/validation/isString';
import isString from '../../src/lib/validator/isString';
describe('module: lib/qualifiers', function() {
const qualifiers = mod.default;
......
......@@ -3,7 +3,7 @@ import _ from 'lodash';
import * as mod from '../../src/lib/types';
import Enumeration from '../../src/lib/Enumeration';
import isString from '../../src/lib/validation/isString';
import isString from '../../src/lib/validator/isString';
describe('module: lib/types', function() {
const types = mod.default;
......
......@@ -16,7 +16,7 @@ describe('module: lib/validation/isPrimitive', function() {
const values = [''].concat(validValues[types.STRING],
validValues[types.BOOLEAN], validValues[types.NUMBER],
validValues[types.SYMBOL]);
expect(vtu.testValues('isPrimitive', val.isPrimitive, values).failures).to.eql([]);
expect(vtu.testValues('isPrimitive', isPrimitive, values).failures).to.eql([]);
});
});
});
......@@ -2,8 +2,8 @@
import _ from 'lodash';
import types from '../../../src/lib/types';
import * as util from '../../../src/lib/util';
import types from '../../src/lib/types';
import * as util from '../../src/lib/util';
/**
* Get a fresh copy of the valid values type map, or just one valid value type array.
......
......@@ -4,19 +4,19 @@ import * as vtu from '../validationTestUtil';
import types from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isAny';
describe('module: lib/validation/isAny', function() {
describe('module: lib/validator/isAny', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.ANY);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
// for ANY, _all_ other values should be _valid_ also
expect(vtu.testOtherValues(val.type, val.validator, true)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default, true)).to.eql([]);
});
});
});
......@@ -5,7 +5,7 @@ import * as vtu from '../validationTestUtil';
import types from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isAnyObject';
describe('module: lib/validation/isAnyObject', function() {
describe('module: lib/validator/isAnyObject', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.ANY_OBJECT);
......@@ -24,7 +24,7 @@ describe('module: lib/validation/isAnyObject', function() {
values = values.concat(validValues[type]);
});
expect(vtu.testValues(val.type, val.validator, values).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default, values).failures).to.eql([]);
});
});
});
......@@ -6,18 +6,18 @@ import types from '../../../src/lib/types';
import qualifiers from '../../../src/lib/qualifiers';
import * as val from '../../../src/lib/validator/isArray';
describe('module: lib/validation/isArray', function() {
describe('module: lib/validator/isArray', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.ARRAY);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
......
......@@ -4,18 +4,18 @@ import * as vtu from '../validationTestUtil';
import types from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isBoolean';
describe('module: lib/validation/isBoolean', function() {
describe('module: lib/validator/isBoolean', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.BOOLEAN);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
});
......@@ -6,14 +6,14 @@ import types from '../../../src/lib/types';
import qualifiers from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isFinite';
describe('module: lib/validation/isFinite', function() {
describe('module: lib/validator/isFinite', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.FINITE);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
......@@ -34,7 +34,7 @@ describe('module: lib/validation/isFinite', function() {
Number.NEGATIVE_INFINITY);
// nothing should pass
expect(vtu.testValues(val.type, val.validator, invalidValues).passes).to.eql([]);
expect(vtu.testValues(val.type, val.default, invalidValues).passes).to.eql([]);
});
});
......
......@@ -4,18 +4,18 @@ import * as vtu from '../validationTestUtil';
import types from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isFunction';
describe('module: lib/validation/isFunction', function() {
describe('module: lib/validator/isFunction', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.FUNCTION);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
});
......@@ -5,18 +5,18 @@ import types from '../../../src/lib/types';
import qualifiers from '../../../src/lib/qualifiers';
import * as val from '../../../src/lib/validator/isMap';
describe('module: lib/validation/isMap', function() {
describe('module: lib/validator/isMap', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.MAP);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
......
......@@ -6,14 +6,14 @@ import types from '../../../src/lib/types';
import qualifiers from '../../../src/lib/qualifiers';
import * as val from '../../../src/lib/validator/isNumber';
describe('module: lib/validation/isNumber', function() {
describe('module: lib/validator/isNumber', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.NUMBER);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
......@@ -30,7 +30,7 @@ describe('module: lib/validation/isNumber', function() {
});
// nothing should pass
expect(vtu.testValues(val.type, val.validator, invalidValues).passes).to.eql([]);
expect(vtu.testValues(val.type, val.default, invalidValues).passes).to.eql([]);
});
});
......
......@@ -4,18 +4,18 @@ import * as vtu from '../validationTestUtil';
import types from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isObject';
describe('module: lib/validation/isObject', function() {
describe('module: lib/validator/isObject', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.OBJECT);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
});
......@@ -4,18 +4,18 @@ import * as vtu from '../validationTestUtil';
import types from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isRegExp';
describe('module: lib/validation/isRegExp', function() {
describe('module: lib/validator/isRegExp', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.REGEXP);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
});
......@@ -4,18 +4,18 @@ import * as vtu from '../validationTestUtil';
import types from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isSet';
describe('module: lib/validation/isSet', function() {
describe('module: lib/validator/isSet', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.SET);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
......
......@@ -6,18 +6,18 @@ import types from '../../../src/lib/types';
import qualifiers from '../../../src/lib/qualifiers';
import * as val from '../../../src/lib/validator/isString';
describe('module: lib/validation/isString', function() {
describe('module: lib/validator/isString', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.STRING);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
......
......@@ -4,18 +4,18 @@ import * as vtu from '../validationTestUtil';
import types from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isSymbol';
describe('module: lib/validation/isSymbol', function() {
describe('module: lib/validator/isSymbol', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.SYMBOL);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
});
......@@ -4,18 +4,18 @@ import * as vtu from '../validationTestUtil';
import types from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isWeakMap';
describe('module: lib/validation/isWeakMap', function() {
describe('module: lib/validator/isWeakMap', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.WEAK_MAP);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
});
......@@ -4,18 +4,18 @@ import * as vtu from '../validationTestUtil';
import types from '../../../src/lib/types';
import * as val from '../../../src/lib/validator/isWeakSet';
describe('module: lib/validation/isWeakSet', function() {
describe('module: lib/validator/isWeakSet', function() {
describe('validator', function() {
it('type', function() {
expect(val.type).to.equal(types.WEAK_SET);
});
it('valid values', function() {
expect(vtu.testValues(val.type, val.validator).failures).to.eql([]);
expect(vtu.testValues(val.type, val.default).failures).to.eql([]);
});
it('other types/values', function() {
expect(vtu.testOtherValues(val.type, val.validator)).to.eql([]);
expect(vtu.testOtherValues(val.type, val.default)).to.eql([]);
});
});
});
......@@ -9,21 +9,21 @@ import * as util from '../src/lib/util';
import isTypeset from '../src/lib/validation/isTypeset';
import isPrimitive from '../src/lib/validation/isPrimitive';
import * as isAny from '../src/lib/validation/isAny';
import * as isAnyObject from '../src/lib/validation/isAnyObject';
import * as isArray from '../src/lib/validation/isArray';
import * as isBoolean from '../src/lib/validation/isBoolean';
import * as isFinite from '../src/lib/validation/isFinite';
import * as isFunction from '../src/lib/validation/isFunction';
import * as isMap from '../src/lib/validation/isMap';
import * as isNumber from '../src/lib/validation/isNumber';
import * as isObject from '../src/lib/validation/isObject';
import * as isRegExp from '../src/lib/validation/isRegExp';
import * as isSet from '../src/lib/validation/isSet';
import * as isString from '../src/lib/validation/isString';
import * as isSymbol from '../src/lib/validation/isSymbol';
import * as isWeakMap from '../src/lib/validation/isWeakMap';
import * as isWeakSet from '../src/lib/validation/isWeakSet';
import * as isAny from '../src/lib/validator/isAny';
import * as isAnyObject from '../src/lib/validator/isAnyObject';
import * as isArray from '../src/lib/validator/isArray';
import * as isBoolean from '../src/lib/validator/isBoolean';
import * as isFinite from '../src/lib/validator/isFinite';
import * as isFunction from '../src/lib/validator/isFunction';
import * as isMap from '../src/lib/validator/isMap';
import * as isNumber from '../src/lib/validator/isNumber';
import * as isObject from '../src/lib/validator/isObject';
import * as isRegExp from '../src/lib/validator/isRegExp';
import * as isSet from '../src/lib/validator/isSet';
import * as isString from '../src/lib/validator/isString';
import * as isSymbol from '../src/lib/validator/isSymbol';
import * as isWeakMap from '../src/lib/validator/isWeakMap';
import * as isWeakSet from '../src/lib/validator/isWeakSet';
global.rtvi = {
rtv,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment