Adding mapbox-gl branch

This commit is contained in:
Andreas Hocevar
2015-03-16 18:50:27 +01:00
parent 7985f030fa
commit 57ee7f52fd
3109 changed files with 943365 additions and 0 deletions
@@ -0,0 +1,365 @@
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed 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.
/**
* @fileoverview Utilities to check the preconditions, postconditions and
* invariants runtime.
*
* Methods in this package should be given special treatment by the compiler
* for type-inference. For example, <code>goog.asserts.assert(foo)</code>
* will restrict <code>foo</code> to a truthy value.
*
* The compiler has an option to disable asserts. So code like:
* <code>
* var x = goog.asserts.assert(foo()); goog.asserts.assert(bar());
* </code>
* will be transformed into:
* <code>
* var x = foo();
* </code>
* The compiler will leave in foo() (because its return value is used),
* but it will remove bar() because it assumes it does not have side-effects.
*
* @author agrieve@google.com (Andrew Grieve)
*/
goog.provide('goog.asserts');
goog.provide('goog.asserts.AssertionError');
goog.require('goog.debug.Error');
goog.require('goog.dom.NodeType');
goog.require('goog.string');
/**
* @define {boolean} Whether to strip out asserts or to leave them in.
*/
goog.define('goog.asserts.ENABLE_ASSERTS', goog.DEBUG);
/**
* Error object for failed assertions.
* @param {string} messagePattern The pattern that was used to form message.
* @param {!Array<*>} messageArgs The items to substitute into the pattern.
* @constructor
* @extends {goog.debug.Error}
* @final
*/
goog.asserts.AssertionError = function(messagePattern, messageArgs) {
messageArgs.unshift(messagePattern);
goog.debug.Error.call(this, goog.string.subs.apply(null, messageArgs));
// Remove the messagePattern afterwards to avoid permenantly modifying the
// passed in array.
messageArgs.shift();
/**
* The message pattern used to format the error message. Error handlers can
* use this to uniquely identify the assertion.
* @type {string}
*/
this.messagePattern = messagePattern;
};
goog.inherits(goog.asserts.AssertionError, goog.debug.Error);
/** @override */
goog.asserts.AssertionError.prototype.name = 'AssertionError';
/**
* The default error handler.
* @param {!goog.asserts.AssertionError} e The exception to be handled.
*/
goog.asserts.DEFAULT_ERROR_HANDLER = function(e) { throw e; };
/**
* The handler responsible for throwing or logging assertion errors.
* @private {function(!goog.asserts.AssertionError)}
*/
goog.asserts.errorHandler_ = goog.asserts.DEFAULT_ERROR_HANDLER;
/**
* Throws an exception with the given message and "Assertion failed" prefixed
* onto it.
* @param {string} defaultMessage The message to use if givenMessage is empty.
* @param {Array<*>} defaultArgs The substitution arguments for defaultMessage.
* @param {string|undefined} givenMessage Message supplied by the caller.
* @param {Array<*>} givenArgs The substitution arguments for givenMessage.
* @throws {goog.asserts.AssertionError} When the value is not a number.
* @private
*/
goog.asserts.doAssertFailure_ =
function(defaultMessage, defaultArgs, givenMessage, givenArgs) {
var message = 'Assertion failed';
if (givenMessage) {
message += ': ' + givenMessage;
var args = givenArgs;
} else if (defaultMessage) {
message += ': ' + defaultMessage;
args = defaultArgs;
}
// The '' + works around an Opera 10 bug in the unit tests. Without it,
// a stack trace is added to var message above. With this, a stack trace is
// not added until this line (it causes the extra garbage to be added after
// the assertion message instead of in the middle of it).
var e = new goog.asserts.AssertionError('' + message, args || []);
goog.asserts.errorHandler_(e);
};
/**
* Sets a custom error handler that can be used to customize the behavior of
* assertion failures, for example by turning all assertion failures into log
* messages.
* @param {function(!goog.asserts.AssertionError)} errorHandler
*/
goog.asserts.setErrorHandler = function(errorHandler) {
if (goog.asserts.ENABLE_ASSERTS) {
goog.asserts.errorHandler_ = errorHandler;
}
};
/**
* Checks if the condition evaluates to true if goog.asserts.ENABLE_ASSERTS is
* true.
* @template T
* @param {T} condition The condition to check.
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @return {T} The value of the condition.
* @throws {goog.asserts.AssertionError} When the condition evaluates to false.
*/
goog.asserts.assert = function(condition, opt_message, var_args) {
if (goog.asserts.ENABLE_ASSERTS && !condition) {
goog.asserts.doAssertFailure_('', null, opt_message,
Array.prototype.slice.call(arguments, 2));
}
return condition;
};
/**
* Fails if goog.asserts.ENABLE_ASSERTS is true. This function is useful in case
* when we want to add a check in the unreachable area like switch-case
* statement:
*
* <pre>
* switch(type) {
* case FOO: doSomething(); break;
* case BAR: doSomethingElse(); break;
* default: goog.assert.fail('Unrecognized type: ' + type);
* // We have only 2 types - "default:" section is unreachable code.
* }
* </pre>
*
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @throws {goog.asserts.AssertionError} Failure.
*/
goog.asserts.fail = function(opt_message, var_args) {
if (goog.asserts.ENABLE_ASSERTS) {
goog.asserts.errorHandler_(new goog.asserts.AssertionError(
'Failure' + (opt_message ? ': ' + opt_message : ''),
Array.prototype.slice.call(arguments, 1)));
}
};
/**
* Checks if the value is a number if goog.asserts.ENABLE_ASSERTS is true.
* @param {*} value The value to check.
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @return {number} The value, guaranteed to be a number when asserts enabled.
* @throws {goog.asserts.AssertionError} When the value is not a number.
*/
goog.asserts.assertNumber = function(value, opt_message, var_args) {
if (goog.asserts.ENABLE_ASSERTS && !goog.isNumber(value)) {
goog.asserts.doAssertFailure_('Expected number but got %s: %s.',
[goog.typeOf(value), value], opt_message,
Array.prototype.slice.call(arguments, 2));
}
return /** @type {number} */ (value);
};
/**
* Checks if the value is a string if goog.asserts.ENABLE_ASSERTS is true.
* @param {*} value The value to check.
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @return {string} The value, guaranteed to be a string when asserts enabled.
* @throws {goog.asserts.AssertionError} When the value is not a string.
*/
goog.asserts.assertString = function(value, opt_message, var_args) {
if (goog.asserts.ENABLE_ASSERTS && !goog.isString(value)) {
goog.asserts.doAssertFailure_('Expected string but got %s: %s.',
[goog.typeOf(value), value], opt_message,
Array.prototype.slice.call(arguments, 2));
}
return /** @type {string} */ (value);
};
/**
* Checks if the value is a function if goog.asserts.ENABLE_ASSERTS is true.
* @param {*} value The value to check.
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @return {!Function} The value, guaranteed to be a function when asserts
* enabled.
* @throws {goog.asserts.AssertionError} When the value is not a function.
*/
goog.asserts.assertFunction = function(value, opt_message, var_args) {
if (goog.asserts.ENABLE_ASSERTS && !goog.isFunction(value)) {
goog.asserts.doAssertFailure_('Expected function but got %s: %s.',
[goog.typeOf(value), value], opt_message,
Array.prototype.slice.call(arguments, 2));
}
return /** @type {!Function} */ (value);
};
/**
* Checks if the value is an Object if goog.asserts.ENABLE_ASSERTS is true.
* @param {*} value The value to check.
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @return {!Object} The value, guaranteed to be a non-null object.
* @throws {goog.asserts.AssertionError} When the value is not an object.
*/
goog.asserts.assertObject = function(value, opt_message, var_args) {
if (goog.asserts.ENABLE_ASSERTS && !goog.isObject(value)) {
goog.asserts.doAssertFailure_('Expected object but got %s: %s.',
[goog.typeOf(value), value],
opt_message, Array.prototype.slice.call(arguments, 2));
}
return /** @type {!Object} */ (value);
};
/**
* Checks if the value is an Array if goog.asserts.ENABLE_ASSERTS is true.
* @param {*} value The value to check.
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @return {!Array<?>} The value, guaranteed to be a non-null array.
* @throws {goog.asserts.AssertionError} When the value is not an array.
*/
goog.asserts.assertArray = function(value, opt_message, var_args) {
if (goog.asserts.ENABLE_ASSERTS && !goog.isArray(value)) {
goog.asserts.doAssertFailure_('Expected array but got %s: %s.',
[goog.typeOf(value), value], opt_message,
Array.prototype.slice.call(arguments, 2));
}
return /** @type {!Array<?>} */ (value);
};
/**
* Checks if the value is a boolean if goog.asserts.ENABLE_ASSERTS is true.
* @param {*} value The value to check.
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @return {boolean} The value, guaranteed to be a boolean when asserts are
* enabled.
* @throws {goog.asserts.AssertionError} When the value is not a boolean.
*/
goog.asserts.assertBoolean = function(value, opt_message, var_args) {
if (goog.asserts.ENABLE_ASSERTS && !goog.isBoolean(value)) {
goog.asserts.doAssertFailure_('Expected boolean but got %s: %s.',
[goog.typeOf(value), value], opt_message,
Array.prototype.slice.call(arguments, 2));
}
return /** @type {boolean} */ (value);
};
/**
* Checks if the value is a DOM Element if goog.asserts.ENABLE_ASSERTS is true.
* @param {*} value The value to check.
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @return {!Element} The value, likely to be a DOM Element when asserts are
* enabled.
* @throws {goog.asserts.AssertionError} When the value is not a boolean.
*/
goog.asserts.assertElement = function(value, opt_message, var_args) {
if (goog.asserts.ENABLE_ASSERTS && (!goog.isObject(value) ||
value.nodeType != goog.dom.NodeType.ELEMENT)) {
goog.asserts.doAssertFailure_('Expected Element but got %s: %s.',
[goog.typeOf(value), value], opt_message,
Array.prototype.slice.call(arguments, 2));
}
return /** @type {!Element} */ (value);
};
/**
* Checks if the value is an instance of the user-defined type if
* goog.asserts.ENABLE_ASSERTS is true.
*
* The compiler may tighten the type returned by this function.
*
* @param {*} value The value to check.
* @param {function(new: T, ...)} type A user-defined constructor.
* @param {string=} opt_message Error message in case of failure.
* @param {...*} var_args The items to substitute into the failure message.
* @throws {goog.asserts.AssertionError} When the value is not an instance of
* type.
* @return {T}
* @template T
*/
goog.asserts.assertInstanceof = function(value, type, opt_message, var_args) {
if (goog.asserts.ENABLE_ASSERTS && !(value instanceof type)) {
goog.asserts.doAssertFailure_('Expected instanceof %s but got %s.',
[goog.asserts.getType_(type), goog.asserts.getType_(value)],
opt_message, Array.prototype.slice.call(arguments, 3));
}
return value;
};
/**
* Checks that no enumerable keys are present in Object.prototype. Such keys
* would break most code that use {@code for (var ... in ...)} loops.
*/
goog.asserts.assertObjectPrototypeIsIntact = function() {
for (var key in Object.prototype) {
goog.asserts.fail(key + ' should not be enumerable in Object.prototype.');
}
};
/**
* Returns the type of a value. If a constructor is passed, and a suitable
* string cannot be found, 'unknown type name' will be returned.
* @param {*} value A constructor, object, or primitive.
* @return {string} The best display name for the value, or 'unknown type name'.
* @private
*/
goog.asserts.getType_ = function(value) {
if (value instanceof Function) {
return value.displayName || value.name || 'unknown type name';
} else if (value instanceof Object) {
return value.constructor.displayName || value.constructor.name ||
Object.prototype.toString.call(value);
} else {
return value === null ? 'null' : typeof value;
}
};
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.
Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>
Closure Unit Tests - goog.asserts.assert
</title>
<script src="../base.js">
</script>
<script>
goog.require('goog.assertsTest');
</script>
</head>
<body>
</body>
</html>
@@ -0,0 +1,242 @@
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed 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.
goog.provide('goog.assertsTest');
goog.setTestOnly('goog.assertsTest');
goog.require('goog.asserts');
goog.require('goog.asserts.AssertionError');
goog.require('goog.dom');
goog.require('goog.string');
goog.require('goog.testing.jsunit');
goog.require('goog.userAgent');
function doTestMessage(failFunc, expectedMsg) {
var error = assertThrows('failFunc should throw.', failFunc);
// Test error message.
// Opera 10 adds cruft to the end of the message, so do a startsWith check.
assertTrue('Message check failed. Expected: ' + expectedMsg + ' Actual: ' +
error.message, goog.string.startsWith(error.message, expectedMsg));
}
function testAssert() {
// None of them may throw exception
goog.asserts.assert(true);
goog.asserts.assert(1);
goog.asserts.assert([]);
goog.asserts.assert({});
assertThrows('assert(false)', goog.partial(goog.asserts.assert, false));
assertThrows('assert(0)', goog.partial(goog.asserts.assert, 0));
assertThrows('assert(null)', goog.partial(goog.asserts.assert, null));
assertThrows('assert(undefined)',
goog.partial(goog.asserts.assert, undefined));
// Test error messages.
doTestMessage(goog.partial(goog.asserts.assert, false), 'Assertion failed');
doTestMessage(goog.partial(goog.asserts.assert, false, 'ouch %s', 1),
'Assertion failed: ouch 1');
}
function testFail() {
assertThrows('fail()', goog.asserts.fail);
// Test error messages.
doTestMessage(goog.partial(goog.asserts.fail, false), 'Failure');
doTestMessage(goog.partial(goog.asserts.fail, 'ouch %s', 1),
'Failure: ouch 1');
}
function testNumber() {
goog.asserts.assertNumber(1);
assertThrows('assertNumber(null)',
goog.partial(goog.asserts.assertNumber, null));
// Test error messages.
doTestMessage(goog.partial(goog.asserts.assertNumber, null),
'Assertion failed: Expected number but got null: null.');
doTestMessage(goog.partial(goog.asserts.assertNumber, '1234'),
'Assertion failed: Expected number but got string: 1234.');
doTestMessage(goog.partial(goog.asserts.assertNumber, null, 'ouch %s', 1),
'Assertion failed: ouch 1');
}
function testString() {
assertEquals('1', goog.asserts.assertString('1'));
assertThrows('assertString(null)',
goog.partial(goog.asserts.assertString, null));
// Test error messages.
doTestMessage(goog.partial(goog.asserts.assertString, null),
'Assertion failed: Expected string but got null: null.');
doTestMessage(goog.partial(goog.asserts.assertString, 1234),
'Assertion failed: Expected string but got number: 1234.');
doTestMessage(goog.partial(goog.asserts.assertString, null, 'ouch %s', 1),
'Assertion failed: ouch 1');
}
function testFunction() {
function f() {};
assertEquals(f, goog.asserts.assertFunction(f));
assertThrows('assertFunction(null)',
goog.partial(goog.asserts.assertFunction, null));
// Test error messages.
doTestMessage(goog.partial(goog.asserts.assertFunction, null),
'Assertion failed: Expected function but got null: null.');
doTestMessage(goog.partial(goog.asserts.assertFunction, 1234),
'Assertion failed: Expected function but got number: 1234.');
doTestMessage(goog.partial(goog.asserts.assertFunction, null, 'ouch %s', 1),
'Assertion failed: ouch 1');
}
function testObject() {
var o = {};
assertEquals(o, goog.asserts.assertObject(o));
assertThrows('assertObject(null)',
goog.partial(goog.asserts.assertObject, null));
// Test error messages.
doTestMessage(goog.partial(goog.asserts.assertObject, null),
'Assertion failed: Expected object but got null: null.');
doTestMessage(goog.partial(goog.asserts.assertObject, 1234),
'Assertion failed: Expected object but got number: 1234.');
doTestMessage(goog.partial(goog.asserts.assertObject, null, 'ouch %s', 1),
'Assertion failed: ouch 1');
}
function testArray() {
var a = [];
assertEquals(a, goog.asserts.assertArray(a));
assertThrows('assertArray({})',
goog.partial(goog.asserts.assertArray, {}));
// Test error messages.
doTestMessage(goog.partial(goog.asserts.assertArray, null),
'Assertion failed: Expected array but got null: null.');
doTestMessage(goog.partial(goog.asserts.assertArray, 1234),
'Assertion failed: Expected array but got number: 1234.');
doTestMessage(goog.partial(goog.asserts.assertArray, null, 'ouch %s', 1),
'Assertion failed: ouch 1');
}
function testBoolean() {
assertEquals(true, goog.asserts.assertBoolean(true));
assertEquals(false, goog.asserts.assertBoolean(false));
assertThrows(goog.partial(goog.asserts.assertBoolean, null));
assertThrows(goog.partial(goog.asserts.assertBoolean, 'foo'));
// Test error messages.
doTestMessage(goog.partial(goog.asserts.assertBoolean, null),
'Assertion failed: Expected boolean but got null: null.');
doTestMessage(goog.partial(goog.asserts.assertBoolean, 1234),
'Assertion failed: Expected boolean but got number: 1234.');
doTestMessage(goog.partial(goog.asserts.assertBoolean, null, 'ouch %s', 1),
'Assertion failed: ouch 1');
}
function testElement() {
assertThrows(goog.partial(goog.asserts.assertElement, null));
assertThrows(goog.partial(goog.asserts.assertElement, 'foo'));
assertThrows(goog.partial(goog.asserts.assertElement,
goog.dom.createTextNode('foo')));
var elem = goog.dom.createElement('div');
assertEquals(elem, goog.asserts.assertElement(elem));
}
function testInstanceof() {
/** @constructor */
var F = function() {};
goog.asserts.assertInstanceof(new F(), F);
assertThrows('assertInstanceof({}, F)',
goog.partial(goog.asserts.assertInstanceof, {}, F));
// IE lacks support for function.name and will fallback to toString().
var object = goog.userAgent.IE ? '[object Object]' : 'Object';
// Test error messages.
doTestMessage(goog.partial(goog.asserts.assertInstanceof, {}, F),
'Assertion failed: Expected instanceof unknown type name but got ' +
object + '.');
doTestMessage(goog.partial(goog.asserts.assertInstanceof, {}, F, 'a %s', 1),
'Assertion failed: a 1');
doTestMessage(goog.partial(goog.asserts.assertInstanceof, null, F),
'Assertion failed: Expected instanceof unknown type name but got null.');
doTestMessage(goog.partial(goog.asserts.assertInstanceof, 5, F),
'Assertion failed: ' +
'Expected instanceof unknown type name but got number.');
// Test a constructor a with a name (IE does not support function.name).
if (!goog.userAgent.IE) {
F = function foo() {};
doTestMessage(goog.partial(goog.asserts.assertInstanceof, {}, F),
'Assertion failed: Expected instanceof foo but got ' + object + '.');
}
// Test a constructor with a displayName.
F.displayName = 'bar';
doTestMessage(goog.partial(goog.asserts.assertInstanceof, {}, F),
'Assertion failed: Expected instanceof bar but got ' + object + '.');
}
function testObjectPrototypeIsIntact() {
goog.asserts.assertObjectPrototypeIsIntact();
var originalToString = Object.prototype.toString;
Object.prototype.toString = goog.nullFunction;
try {
goog.asserts.assertObjectPrototypeIsIntact();
Object.prototype.foo = 1;
doTestMessage(goog.asserts.assertObjectPrototypeIsIntact,
'Failure: foo should not be enumerable in Object.prototype.');
} finally {
Object.prototype.toString = originalToString;
delete Object.prototype.foo;
}
}
function testAssertionError() {
var error = new goog.asserts.AssertionError('foo %s %s', [1, 'two']);
assertEquals('Wrong message', 'foo 1 two', error.message);
assertEquals('Wrong messagePattern', 'foo %s %s', error.messagePattern);
}
function testFailWithCustomErrorHandler() {
try {
var handledException;
goog.asserts.setErrorHandler(
function(e) { handledException = e; });
var expectedMessage = 'Failure: Gevalt!';
goog.asserts.fail('Gevalt!');
assertTrue('handledException is null.', handledException != null);
assertTrue('Message check failed. Expected: ' + expectedMessage +
' Actual: ' + handledException.message,
goog.string.startsWith(expectedMessage, handledException.message));
} finally {
goog.asserts.setErrorHandler(goog.asserts.DEFAULT_ERROR_HANDLER);
}
}
function testAssertWithCustomErrorHandler() {
try {
var handledException;
goog.asserts.setErrorHandler(
function(e) { handledException = e; });
var expectedMessage = 'Assertion failed: Gevalt!';
goog.asserts.assert(false, 'Gevalt!');
assertTrue('handledException is null.', handledException != null);
assertTrue('Message check failed. Expected: ' + expectedMessage +
' Actual: ' + handledException.message,
goog.string.startsWith(expectedMessage, handledException.message));
} finally {
goog.asserts.setErrorHandler(goog.asserts.DEFAULT_ERROR_HANDLER);
}
}