Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(455)

Unified Diff: remoting/webapp/crd/js/typecheck.js

Issue 1015553003: Added more typechecking functions and unit tests for existing code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: remoting/webapp/crd/js/typecheck.js
diff --git a/remoting/webapp/crd/js/typecheck.js b/remoting/webapp/crd/js/typecheck.js
index b44df12c4fe6e51aad0d4b765e5783edd33dd929..1329f4834a2e976f719ba241763cf03fb02fdbc4 100644
--- a/remoting/webapp/crd/js/typecheck.js
+++ b/remoting/webapp/crd/js/typecheck.js
@@ -3,6 +3,107 @@
// found in the LICENSE file.
/**
+ * @param {*} value the value to check; must be an object
+ * @return {*} the argument
+ */
+function assertDefined(value) {
+ if (value !== undefined) {
+ return value;
+ } else {
+ throw new Error('Undefined value');
kelvinp 2015/03/17 21:41:48 we have a function base.debug.assert which current
John Williams 2015/03/18 17:23:01 I tried rewriting in terms of assert, but it won't
+ }
+}
+
+/**
+ * @param {*} value the value to check; must be an object
+ * @return {!Array} the argument
+ */
+function assertArray(value) {
+ if (value instanceof Array) {
kelvinp 2015/03/17 21:41:48 Nit: Array.isArray(value) as it is more robust acr
John Williams 2015/03/18 17:23:01 Done.
+ return /** @type {!Array} */ (value);
+ } else {
+ throw new Error('Invalid data type' +
+ ' (expected: array, actual: ' + typeof value + ')');
+ }
+}
+
+/**
+ * @param {*} value the value to check; must be a boolean
+ * @return {boolean} the argument
+ */
+function assertBoolean(value) {
+ if (typeof value == 'boolean') {
+ return /** @type {boolean} */ (value);
+ } else {
+ throw new Error('Invalid data type' +
+ ' (expected: boolean, actual: ' + typeof value + ')');
+ }
+}
+
+/**
+ * @param {*} value the value to check; must be a number
+ * @return {number} the argument
+ */
+function assertNumber(value) {
+ if (typeof value == 'number') {
+ return /** @type {number} */ (value);
+ } else {
+ throw new Error('Invalid data type' +
+ ' (expected: number, actual: ' + typeof value + ')');
+ }
+}
+
+/**
+ * @param {*} value the value to check; must be an object
+ * @return {!Object} the argument
+ */
+function assertObject(value) {
+ if (value instanceof Object && !(value instanceof Array)) {
+ return /** @type {!Object} */ (value);
+ } else {
+ throw new Error('Invalid data type' +
+ ' (expected: object, actual: ' + typeof value + ')');
+ }
+}
+
+/**
+ * @param {*} value the value to check; must be a string
+ * @return {string} the argument
+ */
+function assertString(value) {
+ if (typeof value == 'string') {
+ return /** @type {string} */ (value);
+ } else {
+ throw new Error('Invalid data type' +
+ ' (expected: string, actual: ' + typeof value + ')');
+ }
+}
+
+/**
+ * Get the |key| attribute in the given |dict| and verify that it is an
+ * array value.
kelvinp 2015/03/17 21:41:48 comment needs update. array value no longer applie
John Williams 2015/03/18 17:23:01 I just got rid of this function entirely.
+ *
+ * If the attribute is not an array, then an exception will be thrown unless
+ * a default value is specified in |opt_default|.
+ *
+ * @param {Object<string,*>} dict The dictionary containing the |key|
+ * @param {string} key The key to typecheck in the |dict|.
+ * @param {*=} opt_default The value to return if the key is not a bool.
+ * @return {*} The |key| attribute value as an object.
+ */
+function getAttr(dict, key, opt_default) {
+ var value = /** @type {*} */ (dict[key]);
+ if (value === undefined) {
+ if (opt_default === undefined) {
+ throw new Error('Undefined value for ' + key);
+ } else {
+ return opt_default;
+ }
+ }
+ return value;
+}
+
+/**
* Get the |key| attribute in the given |dict| and verify that it is an
* array value.
*
@@ -18,8 +119,8 @@ function getArrayAttr(dict, key, opt_default) {
var value = /** @type {Array} */ (dict[key]);
if (!(value instanceof Array)) {
if (opt_default === undefined) {
- throw 'Invalid data type for ' + key +
- ' (expected: array, actual: ' + typeof value + ')';
+ throw new Error('Invalid data type for ' + key +
+ ' (expected: array, actual: ' + typeof value + ')');
} else {
return opt_default;
}
@@ -46,8 +147,8 @@ function getBooleanAttr(dict, key, opt_default) {
}
if (typeof value !== 'boolean') {
if (opt_default === undefined) {
- throw 'Invalid data type for ' + key +
- ' (expected: boolean, actual: ' + typeof value + ')';
+ throw new Error('Invalid data type for ' + key +
+ ' (expected: boolean, actual: ' + typeof value + ')');
} else {
return opt_default;
}
@@ -71,8 +172,8 @@ function getNumberAttr(dict, key, opt_default) {
var value = /** @type {number} */(dict[key]);
if (typeof value != 'number') {
if (opt_default === undefined) {
- throw 'Invalid data type for ' + key +
- ' (expected: number, actual: ' + typeof value + ')';
+ throw new Error('Invalid data type for ' + key +
+ ' (expected: number, actual: ' + typeof value + ')');
} else {
return opt_default;
}
@@ -94,10 +195,10 @@ function getNumberAttr(dict, key, opt_default) {
*/
function getObjectAttr(dict, key, opt_default) {
var value = /** @type {Object} */ (dict[key]);
- if (typeof value != 'object') {
+ if (!(value instanceof Object) || value instanceof Array) {
if (opt_default === undefined) {
- throw 'Invalid data type for ' + key +
- ' (expected: object, actual: ' + typeof value + ')';
+ throw new Error('Invalid data type for ' + key +
+ ' (expected: object, actual: ' + typeof value + ')');
} else {
return opt_default;
}
@@ -121,8 +222,8 @@ function getStringAttr(dict, key, opt_default) {
var value = /** @type {string} */ (dict[key]);
if (typeof value != 'string') {
if (opt_default === undefined) {
- throw 'Invalid data type for ' + key +
- ' (expected: string, actual: ' + typeof value + ')';
+ throw new Error('Invalid data type for ' + key +
+ ' (expected: string, actual: ' + typeof value + ')');
} else {
return opt_default;
}
@@ -142,7 +243,8 @@ function getStringAttr(dict, key, opt_default) {
function getJsonObjectFromString(jsonString) {
var value = base.jsonParseSafe(jsonString);
if (typeof value != 'object') {
- throw 'Invalid data type (expected: Object, actual: ' + typeof value + ')';
+ throw new Error('Invalid data type (expected: Object, actual: ' +
+ typeof value + ')');
}
return value;
}

Powered by Google App Engine
This is Rietveld 408576698