From f9c3d04c26b40888981e75ab6e7736e1bc912097 Mon Sep 17 00:00:00 2001 From: Julien Castelain Date: Fri, 3 Mar 2017 16:55:51 +0100 Subject: [PATCH] Adds metal-assertions module to packages (#215) --- packages/metal-assertions/LICENSE.md | 30 +++ packages/metal-assertions/README.md | 3 + packages/metal-assertions/package.json | 31 +++ packages/metal-assertions/src/assertions.js | 146 +++++++++++++ packages/metal-assertions/test/.eslintrc.json | 9 + packages/metal-assertions/test/assertions.js | 193 ++++++++++++++++++ 6 files changed, 412 insertions(+) create mode 100644 packages/metal-assertions/LICENSE.md create mode 100644 packages/metal-assertions/README.md create mode 100644 packages/metal-assertions/package.json create mode 100644 packages/metal-assertions/src/assertions.js create mode 100644 packages/metal-assertions/test/.eslintrc.json create mode 100644 packages/metal-assertions/test/assertions.js diff --git a/packages/metal-assertions/LICENSE.md b/packages/metal-assertions/LICENSE.md new file mode 100644 index 00000000..809f6734 --- /dev/null +++ b/packages/metal-assertions/LICENSE.md @@ -0,0 +1,30 @@ +# Software License Agreement (BSD License) + +Copyright (c) 2017, Liferay Inc. +All rights reserved. + +Redistribution and use of this software in source and binary forms, with or without modification, are +permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above + copyright notice, this list of conditions and the + following disclaimer. + +* Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the + following disclaimer in the documentation and/or other + materials provided with the distribution. + +* The name of Liferay Inc. may not be used to endorse or promote products + derived from this software without specific prior + written permission of Liferay Inc. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR +TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/packages/metal-assertions/README.md b/packages/metal-assertions/README.md new file mode 100644 index 00000000..09545e31 --- /dev/null +++ b/packages/metal-assertions/README.md @@ -0,0 +1,3 @@ +# metal-assertions + +A collection of assertion methods for metal diff --git a/packages/metal-assertions/package.json b/packages/metal-assertions/package.json new file mode 100644 index 00000000..66e6b940 --- /dev/null +++ b/packages/metal-assertions/package.json @@ -0,0 +1,31 @@ +{ + "name": "metal-assertions", + "version": "2.6.4", + "description": "A collection of assertion methods for metal", + "license": "BSD-3-Clause", + "repository": "https://github.com/metal/metal.js/tree/master/packages/metal-assertions", + "engines": { + "node": ">=0.10.0", + "npm": ">=3.0.0" + }, + "jsnext:main": "src/assertions.js", + "main": "lib/assertions.js", + "files": [ + "lib", + "src" + ], + "scripts": { + "compile": "babel --presets es2015 -d lib/ src/", + "prepublish": "npm run compile" + }, + "keywords": [ + "metal" + ], + "dependencies": { + "metal": "^2.6.4" + }, + "devDependencies": { + "babel-cli": "^6.4.5", + "babel-preset-es2015": "^6.0.0" + } +} diff --git a/packages/metal-assertions/src/assertions.js b/packages/metal-assertions/src/assertions.js new file mode 100644 index 00000000..826cd72f --- /dev/null +++ b/packages/metal-assertions/src/assertions.js @@ -0,0 +1,146 @@ +import { + isBoolean, + isDef, + isDefAndNotNull, + isDocument, + isDocumentFragment, + isElement, + isFunction, + isNull, + isNumber, + isObject, + isString, + isWindow, +} from 'metal'; + +/** + * Asserts value is a boolean. + * @param {*} value + * @param {string} errorMessage Error message + */ +export function assertBoolean(value, errorMessage) { + if (!isBoolean(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is defined. + * @param {Object} value + * @param {string} errorMessage Error message + */ +export function assertDef(value, errorMessage) { + if (!isDef(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is defined and not null. + * @param {Object} value + * @param {string} errorMessage Error message + */ +export function assertDefAndNotNull(value, errorMessage) { + if (!isDefAndNotNull(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is a function. + * @param {Function} value + * @param {string} errorMessage Error message + */ +export function assertFunction(value, errorMessage) { + if (!isFunction(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is not null. + * @param {Object} value + * @param {string} errorMessage Error message + */ +export function assertNotNull(value, errorMessage) { + if (isNull(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is a number. + * @param {Number} value + * @param {string} errorMessage Error message + */ +export function assertNumber(value, errorMessage) { + if (!isNumber(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is an object. + * @param {Object} value + * @param {string} errorMessage Error message + */ +export function assertObject(value, errorMessage) { + if (!isObject(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is a string. + * @param {String} value + * @param {string} errorMessage Error message + */ +export function assertString(value, errorMessage) { + if (!isString(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is a document. + * @param {Document} value + * @param {string} errorMessage Error message + */ +export function assertDocument(value, errorMessage) { + if (!isDocument(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is a document fragment. + * @param {DocumentFragment} value + * @param {string} errorMessage Error message + */ +export function assertDocumentFragment(value, errorMessage) { + if (!isDocumentFragment(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is an element. + * @param {Element} value + * @param {string} errorMessage Error message + */ +export function assertElement(value, errorMessage) { + if (!isElement(value)) { + throw new Error(errorMessage); + } +} + +/** + * Asserts value is a window. + * @param {Window} value + * @param {string} errorMessage Error message + */ +export function assertWindow(value, errorMessage) { + if (!isWindow(value)) { + throw new Error(errorMessage); + } +} diff --git a/packages/metal-assertions/test/.eslintrc.json b/packages/metal-assertions/test/.eslintrc.json new file mode 100644 index 00000000..92b466f1 --- /dev/null +++ b/packages/metal-assertions/test/.eslintrc.json @@ -0,0 +1,9 @@ +{ + "env": { + "mocha": true + }, + "globals": { + "assert": true, + "sinon": true + } +} diff --git a/packages/metal-assertions/test/assertions.js b/packages/metal-assertions/test/assertions.js new file mode 100644 index 00000000..e215c059 --- /dev/null +++ b/packages/metal-assertions/test/assertions.js @@ -0,0 +1,193 @@ +import { + assertBoolean, + assertDef, + assertDefAndNotNull, + assertDocument, + assertDocumentFragment, + assertElement, + assertFunction, + assertNotNull, + assertNumber, + assertObject, + assertString, + assertWindow, +} from '../src/assertions'; + +describe('assertions', () => { + describe('assertBoolean', () => { + it('should throw error when it\'s not a boolean', () => { + assert.throws(() => { + assertBoolean(undefined, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s a boolean', () => { + assert.doesNotThrow(() => { + assertBoolean(true, /message/); + assertBoolean(false, /message/); + }); + }); + }); + + describe('assertDef', () => { + it('should throw error when it\'s not defined', () => { + assert.throws(() => { + assertDef(undefined, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s defined', () => { + assert.doesNotThrow(() => { + assertDef('', 'message'); + }); + }); + }); + + describe('assertDefAndNotNull', () => { + it('should throw error when it\'s not defined', () => { + assert.throws(() => { + assertDefAndNotNull(undefined, 'message'); + }, /message/); + }); + + it('should throw error when it\'s null', () => { + assert.throws(() => { + assertDefAndNotNull(null, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s def and not null', () => { + assert.doesNotThrow(() => { + assertDefAndNotNull('', 'message'); + }); + }); + }); + + describe('assertNotNull', () => { + it('should throw error when it\'s null', () => { + assert.throws(() => { + assertNotNull(null, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s not null', () => { + assert.doesNotThrow(() => { + assertNotNull(undefined, 'message'); + }); + }); + }); + + describe('assertNumber', () => { + it('should throw error when it\'s not a number', () => { + assert.throws(() => { + assertNumber(null, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s a number', () => { + assert.doesNotThrow(() => { + assertNumber(3, 'message'); + assertNumber(Math.PI, 'message'); + }); + }); + }); + + describe('assertFunction', () => { + it('should throw error when it\'s not a function', () => { + assert.throws(() => { + assertFunction(null, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s a function', () => { + assert.doesNotThrow(() => { + assertFunction(function() {}, /message/); + }); + }); + }); + + describe('assertObject', () => { + it('should throw error when it\'s not an object', () => { + assert.throws(() => { + assertObject(null, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s a object', () => { + assert.doesNotThrow(() => { + assertObject({}, /message/); + }); + }); + }); + + describe('assertString', () => { + it('should throw error when it\'s not an string', () => { + assert.throws(() => { + assertString(null, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s a string', () => { + assert.doesNotThrow(() => { + assertString('', 'message'); + }); + }); + }); + + describe('assertDocument', () => { + it('should throw error when it\'s not a document', () => { + assert.throws(() => { + assertDocument(null, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s a document', () => { + assert.doesNotThrow(() => { + assertDocument(document, 'message'); + }); + }); + }); + + describe('assertDocumentFragment', () => { + it('should throw error when it\'s not a document fragment', () => { + assert.throws(() => { + assertDocumentFragment(null, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s a document fragment', () => { + assert.doesNotThrow(() => { + assertDocumentFragment(document.createDocumentFragment(), 'message'); + }); + }); + }); + + describe('assertElement', () => { + it('should throw error when it\'s not an element', () => { + assert.throws(() => { + assertElement(null, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s a window', () => { + assert.doesNotThrow(() => { + assertElement(document.createElement('div'), 'message'); + }); + }); + }); + + describe('assertWindow', () => { + it('should throw error when it\'s not a window', () => { + assert.throws(() => { + assertWindow(null, 'message'); + }, /message/); + }); + + it('should not throw error when it\'s a window', () => { + assert.doesNotThrow(() => { + assertWindow(window, 'message'); + }); + }); + }); + +});