You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2012/11/22 21:47:47 UTC
[7/9] js commit: [all] Add argscheck module.
[all] Add argscheck module.
This will allow us to check parameter types more succinctly.
https://issues.apache.org/jira/browse/CB-1892
Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/1dfa2ac5
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/1dfa2ac5
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/1dfa2ac5
Branch: refs/heads/master
Commit: 1dfa2ac54a41287279e6726d2b748060e1cc44bf
Parents: 6798d21
Author: Andrew Grieve <ag...@chromium.org>
Authored: Thu Nov 22 13:05:33 2012 -0500
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Thu Nov 22 15:45:05 2012 -0500
----------------------------------------------------------------------
lib/common/argscheck.js | 74 ++++++++++++++++++++++++++++++++++++++
test/test.argscheck.js | 81 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 155 insertions(+), 0 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-js/blob/1dfa2ac5/lib/common/argscheck.js
----------------------------------------------------------------------
diff --git a/lib/common/argscheck.js b/lib/common/argscheck.js
new file mode 100644
index 0000000..27bb5a1
--- /dev/null
+++ b/lib/common/argscheck.js
@@ -0,0 +1,74 @@
+/*
+ *
+ * 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.
+ *
+*/
+
+var exec = require('cordova/exec');
+var moduleExports = module.exports;
+
+var typeMap = {
+ 'A': 'Array',
+ 'D': 'Date',
+ 'N': 'Number',
+ 'S': 'String',
+ 'F': 'Function',
+ 'O': 'Object'
+};
+
+function extractParamName(callee, argIndex) {
+ return (/.*?\((.*?)\)/).exec(callee)[1].split(', ')[argIndex];
+}
+
+function checkArgs(spec, functionName, args, opt_callee) {
+ if (!moduleExports.enableChecks) {
+ return;
+ }
+ var errMsg = null;
+ var type;
+ for (var i = 0; i < spec.length; ++i) {
+ var c = spec.charAt(i),
+ cUpper = c.toUpperCase(),
+ arg = args[i];
+ // Asterix means allow anything.
+ if (c == '*') {
+ continue;
+ }
+ type = Object.prototype.toString.call(arg).slice(8, -1);
+ if ((arg === null || arg === undefined) && c == cUpper) {
+ continue;
+ }
+ if (type != typeMap[cUpper]) {
+ errMsg = 'Expected ' + typeMap[cUpper];
+ break;
+ }
+ }
+ if (errMsg) {
+ errMsg += ', but got ' + type + '.';
+ errMsg = 'Wrong type for parameter "' + extractParamName(opt_callee || args.callee, i) + '" of ' + functionName + ': ' + errMsg;
+ // Don't log when running jake test.
+ if (typeof jasmine == 'undefined') {
+ console.error(errMsg);
+ }
+ throw TypeError(errMsg);
+ }
+}
+
+moduleExports.checkArgs = checkArgs;
+moduleExports.enableChecks = true;
+
http://git-wip-us.apache.org/repos/asf/cordova-js/blob/1dfa2ac5/test/test.argscheck.js
----------------------------------------------------------------------
diff --git a/test/test.argscheck.js b/test/test.argscheck.js
new file mode 100644
index 0000000..10a4175
--- /dev/null
+++ b/test/test.argscheck.js
@@ -0,0 +1,81 @@
+/*
+ *
+ * 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 = 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('should not throw when given valid args', function() {
+ var testFunc = createTestFunc(false);
+ testFunc(0, {}, [], '', new Date(), testFunc, 1);
+ });
+ it('should not throw when given valid optional args', function() {
+ var testFunc = createTestFunc(true);
+ testFunc(0, {}, [], '', new Date(), testFunc, '');
+ });
+ it('should not throw when given missing optional args', function() {
+ var testFunc = createTestFunc(true);
+ testFunc();
+ });
+ it('should not throw when given null optional args', function() {
+ var testFunc = createTestFunc(true);
+ testFunc(null, null, null, null, null, null, null);
+ });
+ it('should throw when given invalid number', function() {
+ var testFunc = createTestFunc(true);
+ expect(function() { testFunc('foo', null, null, null, null, null) }).toThrow('Wrong type for parameter "num" of testFunc: Expected Number, but got String.');
+ });
+ it('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) }).toThrow('Wrong type for parameter "obj" of testFunc: Expected Object, but got Array.');
+ });
+ it('should throw when given invalid array', function() {
+ var testFunc = createTestFunc(true);
+ expect(function() { testFunc(null, null, {}, null, null, null) }).toThrow('Wrong type for parameter "arr" of testFunc: Expected Array, but got Object.');
+ });
+ it('should throw when given invalid string', function() {
+ var testFunc = createTestFunc(true);
+ expect(function() { testFunc(null, null, null, 5, null, null) }).toThrow('Wrong type for parameter "str" of testFunc: Expected String, but got Number.');
+ });
+ it('should throw when given invalid date', function() {
+ var testFunc = createTestFunc(true);
+ expect(function() { testFunc(null, null, null, null, 233, null) }).toThrow('Wrong type for parameter "date" of testFunc: Expected Date, but got Number.');
+ });
+ it('should throw when given invalid function', function() {
+ var testFunc = createTestFunc(true);
+ expect(function() { testFunc(null, null, null, null, null, new Date) }).toThrow('Wrong type for parameter "func" of testFunc: Expected Function, but got Date.');
+ });
+ it('should not throw when checking is disabled', function() {
+ var testFunc = createTestFunc(false);
+ argscheck.enableChecks = false;
+ testFunc();
+ });
+});