blob: 60fd02f3acc5866f1f35a290b14e229aabf47525 [file] [log] [blame]
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
describe('argscheck', function () {
var argscheck = cordova.require('cordova/argscheck');
function createTestFunc (allowNull) {
return function testFunc (num, obj, arr, str, date, func) {
var spec = allowNull ? 'NOASDF*' : 'noasdf*';
argscheck.checkArgs(spec, 'testFunc', arguments);
};
}
afterEach(function () {
argscheck.enableChecks = true;
});
it('Test#001 : should not throw when given valid args', function () {
var testFunc = createTestFunc(false);
testFunc(0, {}, [], '', new Date(), testFunc, 1);
});
it('Test#002 : should not throw when given valid optional args', function () {
var testFunc = createTestFunc(true);
testFunc(0, {}, [], '', new Date(), testFunc, '');
});
it('Test#003 : should not throw when given missing optional args', function () {
var testFunc = createTestFunc(true);
testFunc();
});
it('Test#004 : should not throw when given null optional args', function () {
var testFunc = createTestFunc(true);
testFunc(null, null, null, null, null, null, null);
});
it('Test#005 : should throw when given invalid number', function () {
var testFunc = createTestFunc(true);
expect(function () { testFunc('foo', null, null, null, null, null); }).toThrowError('Wrong type for parameter "num" of testFunc: Expected Number, but got String.');
});
it('Test#006 : should throw when given invalid object', function () {
var testFunc = createTestFunc(true);
// Do not allow arrays for objects since we're usually dealing with JSON when expecting objects.
expect(function () { testFunc(null, [], null, null, null, null); }).toThrowError('Wrong type for parameter "obj" of testFunc: Expected Object, but got Array.');
});
it('Test#007 : should throw when given invalid array', function () {
var testFunc = createTestFunc(true);
expect(function () { testFunc(null, null, {}, null, null, null); }).toThrowError('Wrong type for parameter "arr" of testFunc: Expected Array, but got Object.');
});
it('Test#008 : should throw when given invalid string', function () {
var testFunc = createTestFunc(true);
expect(function () { testFunc(null, null, null, 5, null, null); }).toThrowError('Wrong type for parameter "str" of testFunc: Expected String, but got Number.');
});
it('Test#009 : should throw when given invalid date', function () {
var testFunc = createTestFunc(true);
expect(function () { testFunc(null, null, null, null, 233, null); }).toThrowError('Wrong type for parameter "date" of testFunc: Expected Date, but got Number.');
});
it('Test#010 : should throw when given invalid function', function () {
var testFunc = createTestFunc(true);
expect(function () { testFunc(null, null, null, null, null, new Date()); }).toThrowError('Wrong type for parameter "func" of testFunc: Expected Function, but got Date.');
});
it('Test#011 : should not throw when checking is disabled', function () {
var testFunc = createTestFunc(false);
argscheck.enableChecks = false;
testFunc();
});
it('Test#012 : should be able to extract from all kinds of parameter formats', () => {
const check = args => argscheck.checkArgs('ss', 'testFn', args);
function sparse (
a,
b
) { check(arguments); }
expect(() => sparse(null, '')).toThrowError(/parameter "a"/);
expect(() => sparse('', null)).toThrowError(/parameter "b"/);
// eslint-disable-next-line comma-spacing
function dense (a,b) { check(arguments); }
expect(() => dense('', null)).toThrowError(/parameter "b"/);
// eslint-disable-next-line comma-spacing, space-in-parens
function funky ( a ,b ) { check(arguments); }
expect(() => funky(null, '')).toThrowError(/parameter "a"/);
});
});