Update wmts-hidpi, add nicer-api-docs

This commit is contained in:
Andreas Hocevar
2014-05-06 13:02:46 -05:00
parent b3ac1afd00
commit 1e25fc5585
2239 changed files with 3726515 additions and 37010 deletions

View File

@@ -0,0 +1,59 @@
// Copyright 2012 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 Provides main functionality of assertThat. assertThat calls the
* matcher's matches method to test if a matcher matches assertThat's arguments.
*/
goog.provide('goog.labs.testing.MatcherError');
goog.provide('goog.labs.testing.assertThat');
goog.require('goog.asserts');
goog.require('goog.debug.Error');
goog.require('goog.labs.testing.Matcher');
/**
* Asserts that the actual value evaluated by the matcher is true.
*
* @param {*} actual The object to assert by the matcher.
* @param {!goog.labs.testing.Matcher} matcher A matcher to verify values.
* @param {string=} opt_reason Description of what is asserted.
*
*/
goog.labs.testing.assertThat = function(actual, matcher, opt_reason) {
if (!matcher.matches(actual)) {
// Prefix the error description with a reason from the assert ?
var prefix = opt_reason ? opt_reason + ': ' : '';
var desc = prefix + matcher.describe(actual);
// some sort of failure here
throw new goog.labs.testing.MatcherError(desc);
}
};
/**
* Error thrown when a Matcher fails to match the input value.
* @param {string=} opt_message The error message.
* @constructor
* @extends {goog.debug.Error}
*/
goog.labs.testing.MatcherError = function(opt_message) {
goog.base(this, opt_message);
};
goog.inherits(goog.labs.testing.MatcherError, goog.debug.Error);

View File

@@ -0,0 +1,94 @@
// Copyright 2012 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 Provides the built-in decorators: is, describedAs, anything.
*/
goog.provide('goog.labs.testing.AnythingMatcher');
goog.require('goog.labs.testing.Matcher');
/**
* The Anything matcher. Matches all possible inputs.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.AnythingMatcher = function() {};
/**
* Matches anything. Useful if one doesn't care what the object under test is.
*
* @override
*/
goog.labs.testing.AnythingMatcher.prototype.matches =
function(actualObject) {
return true;
};
/**
* This method is never called but is needed so AnythingMatcher implements the
* Matcher interface.
*
* @override
*/
goog.labs.testing.AnythingMatcher.prototype.describe =
function(actualObject) {
throw Error('AnythingMatcher should never fail!');
};
/**
* Returns a matcher that matches anything.
*
* @return {!goog.labs.testing.AnythingMatcher} A AnythingMatcher.
*/
function anything() {
return new goog.labs.testing.AnythingMatcher();
}
/**
* Returnes any matcher that is passed to it (aids readability).
*
* @param {!goog.labs.testing.Matcher} matcher A matcher.
* @return {!goog.labs.testing.Matcher} The wrapped matcher.
*/
function is(matcher) {
return matcher;
}
/**
* Returns a matcher with a customized description for the given matcher.
*
* @param {string} description The custom description for the matcher.
* @param {!goog.labs.testing.Matcher} matcher The matcher.
*
* @return {!goog.labs.testing.Matcher} The matcher with custom description.
*/
function describedAs(description, matcher) {
matcher.describe = function(value) {
return description;
};
return matcher;
}

View File

@@ -0,0 +1,266 @@
// Copyright 2012 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 Provides the built-in dictionary matcher methods like
* hasEntry, hasEntries, hasKey, hasValue, etc.
*/
goog.provide('goog.labs.testing.HasEntriesMatcher');
goog.provide('goog.labs.testing.HasEntryMatcher');
goog.provide('goog.labs.testing.HasKeyMatcher');
goog.provide('goog.labs.testing.HasValueMatcher');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.labs.testing.Matcher');
goog.require('goog.string');
/**
* The HasEntries matcher.
*
* @param {!Object} entries The entries to check in the object.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.HasEntriesMatcher = function(entries) {
/**
* @type {Object}
* @private
*/
this.entries_ = entries;
};
/**
* Determines if an object has particular entries.
*
* @override
*/
goog.labs.testing.HasEntriesMatcher.prototype.matches =
function(actualObject) {
goog.asserts.assertObject(actualObject, 'Expected an Object');
var object = /** @type {!Object} */(actualObject);
return goog.object.every(this.entries_, function(value, key) {
return goog.object.containsKey(object, key) &&
object[key] === value;
});
};
/**
* @override
*/
goog.labs.testing.HasEntriesMatcher.prototype.describe =
function(actualObject) {
goog.asserts.assertObject(actualObject, 'Expected an Object');
var object = /** @type {!Object} */(actualObject);
var errorString = 'Input object did not contain the following entries:\n';
goog.object.forEach(this.entries_, function(value, key) {
if (!goog.object.containsKey(object, key) ||
object[key] !== value) {
errorString += key + ': ' + value + '\n';
}
});
return errorString;
};
/**
* The HasEntry matcher.
*
* @param {string} key The key for the entry.
* @param {*} value The value for the key.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.HasEntryMatcher = function(key, value) {
/**
* @type {string}
* @private
*/
this.key_ = key;
/**
* @type {*}
* @private
*/
this.value_ = value;
};
/**
* Determines if an object has a particular entry.
*
* @override
*/
goog.labs.testing.HasEntryMatcher.prototype.matches =
function(actualObject) {
goog.asserts.assertObject(actualObject);
return goog.object.containsKey(actualObject, this.key_) &&
actualObject[this.key_] === this.value_;
};
/**
* @override
*/
goog.labs.testing.HasEntryMatcher.prototype.describe =
function(actualObject) {
goog.asserts.assertObject(actualObject);
var errorMsg;
if (goog.object.containsKey(actualObject, this.key_)) {
errorMsg = 'Input object did not contain key: ' + this.key_;
} else {
errorMsg = 'Value for key did not match value: ' + this.value_;
}
return errorMsg;
};
/**
* The HasKey matcher.
*
* @param {string} key The key to check in the object.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.HasKeyMatcher = function(key) {
/**
* @type {string}
* @private
*/
this.key_ = key;
};
/**
* Determines if an object has a key.
*
* @override
*/
goog.labs.testing.HasKeyMatcher.prototype.matches =
function(actualObject) {
goog.asserts.assertObject(actualObject);
return goog.object.containsKey(actualObject, this.key_);
};
/**
* @override
*/
goog.labs.testing.HasKeyMatcher.prototype.describe =
function(actualObject) {
goog.asserts.assertObject(actualObject);
return 'Input object did not contain the key: ' + this.key_;
};
/**
* The HasValue matcher.
*
* @param {*} value The value to check in the object.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.HasValueMatcher = function(value) {
/**
* @type {*}
* @private
*/
this.value_ = value;
};
/**
* Determines if an object contains a value
*
* @override
*/
goog.labs.testing.HasValueMatcher.prototype.matches =
function(actualObject) {
goog.asserts.assertObject(actualObject, 'Expected an Object');
var object = /** @type {!Object} */(actualObject);
return goog.object.containsValue(object, this.value_);
};
/**
* @override
*/
goog.labs.testing.HasValueMatcher.prototype.describe =
function(actualObject) {
return 'Input object did not contain the value: ' + this.value_;
};
/**
* Gives a matcher that asserts an object contains all the given key-value pairs
* in the input object.
*
* @param {!Object} entries The entries to check for presence in the object.
*
* @return {!goog.labs.testing.HasEntriesMatcher} A HasEntriesMatcher.
*/
function hasEntries(entries) {
return new goog.labs.testing.HasEntriesMatcher(entries);
}
/**
* Gives a matcher that asserts an object contains the given key-value pair.
*
* @param {string} key The key to check for presence in the object.
* @param {*} value The value to check for presence in the object.
*
* @return {!goog.labs.testing.HasEntryMatcher} A HasEntryMatcher.
*/
function hasEntry(key, value) {
return new goog.labs.testing.HasEntryMatcher(key, value);
}
/**
* Gives a matcher that asserts an object contains the given key.
*
* @param {string} key The key to check for presence in the object.
*
* @return {!goog.labs.testing.HasKeyMatcher} A HasKeyMatcher.
*/
function hasKey(key) {
return new goog.labs.testing.HasKeyMatcher(key);
}
/**
* Gives a matcher that asserts an object contains the given value.
*
* @param {*} value The value to check for presence in the object.
*
* @return {!goog.labs.testing.HasValueMatcher} A HasValueMatcher.
*/
function hasValue(value) {
return new goog.labs.testing.HasValueMatcher(value);
}

View File

@@ -0,0 +1,206 @@
// Copyright 2012 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 Provides the built-in logic matchers: anyOf, allOf, and isNot.
*
*/
goog.provide('goog.labs.testing.AllOfMatcher');
goog.provide('goog.labs.testing.AnyOfMatcher');
goog.provide('goog.labs.testing.IsNotMatcher');
goog.require('goog.array');
goog.require('goog.labs.testing.Matcher');
/**
* The AllOf matcher.
*
* @param {!Array.<!goog.labs.testing.Matcher>} matchers Input matchers.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.AllOfMatcher = function(matchers) {
/**
* @type {!Array.<!goog.labs.testing.Matcher>}
* @private
*/
this.matchers_ = matchers;
};
/**
* Determines if all of the matchers match the input value.
*
* @override
*/
goog.labs.testing.AllOfMatcher.prototype.matches = function(actualValue) {
return goog.array.every(this.matchers_, function(matcher) {
return matcher.matches(actualValue);
});
};
/**
* Describes why the matcher failed. The returned string is a concatenation of
* all the failed matchers' error strings.
*
* @override
*/
goog.labs.testing.AllOfMatcher.prototype.describe =
function(actualValue) {
// TODO(user) : Optimize this to remove duplication with matches ?
var errorString = '';
goog.array.forEach(this.matchers_, function(matcher) {
if (!matcher.matches(actualValue)) {
errorString += matcher.describe(actualValue) + '\n';
}
});
return errorString;
};
/**
* The AnyOf matcher.
*
* @param {!Array.<!goog.labs.testing.Matcher>} matchers Input matchers.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.AnyOfMatcher = function(matchers) {
/**
* @type {!Array.<!goog.labs.testing.Matcher>}
* @private
*/
this.matchers_ = matchers;
};
/**
* Determines if any of the matchers matches the input value.
*
* @override
*/
goog.labs.testing.AnyOfMatcher.prototype.matches = function(actualValue) {
return goog.array.some(this.matchers_, function(matcher) {
return matcher.matches(actualValue);
});
};
/**
* Describes why the matcher failed.
*
* @override
*/
goog.labs.testing.AnyOfMatcher.prototype.describe =
function(actualValue) {
// TODO(user) : Optimize this to remove duplication with matches ?
var errorString = '';
goog.array.forEach(this.matchers_, function(matcher) {
if (!matcher.matches(actualValue)) {
errorString += matcher.describe(actualValue) + '\n';
}
});
return errorString;
};
/**
* The IsNot matcher.
*
* @param {!goog.labs.testing.Matcher} matcher The matcher to negate.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.IsNotMatcher = function(matcher) {
/**
* @type {!goog.labs.testing.Matcher}
* @private
*/
this.matcher_ = matcher;
};
/**
* Determines if the input value doesn't satisfy a matcher.
*
* @override
*/
goog.labs.testing.IsNotMatcher.prototype.matches = function(actualValue) {
return !this.matcher_.matches(actualValue);
};
/**
* Describes why the matcher failed.
*
* @override
*/
goog.labs.testing.IsNotMatcher.prototype.describe =
function(actualValue) {
return 'The following is false: ' + this.matcher_.describe(actualValue);
};
/**
* Creates a matcher that will succeed only if all of the given matchers
* succeed.
*
* @param {...goog.labs.testing.Matcher} var_args The matchers to test
* against.
*
* @return {!goog.labs.testing.AllOfMatcher} The AllOf matcher.
*/
function allOf(var_args) {
var matchers = goog.array.toArray(arguments);
return new goog.labs.testing.AllOfMatcher(matchers);
}
/**
* Accepts a set of matchers and returns a matcher which matches
* values which satisfy the constraints of any of the given matchers.
*
* @param {...goog.labs.testing.Matcher} var_args The matchers to test
* against.
*
* @return {!goog.labs.testing.AnyOfMatcher} The AnyOf matcher.
*/
function anyOf(var_args) {
var matchers = goog.array.toArray(arguments);
return new goog.labs.testing.AnyOfMatcher(matchers);
}
/**
* Returns a matcher that negates the input matcher. The returned
* matcher matches the values not matched by the input matcher and vice-versa.
*
* @param {!goog.labs.testing.Matcher} matcher The matcher to test against.
*
* @return {!goog.labs.testing.IsNotMatcher} The IsNot matcher.
*/
function isNot(matcher) {
return new goog.labs.testing.IsNotMatcher(matcher);
}

View File

@@ -0,0 +1,51 @@
// Copyright 2012 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 Provides the base Matcher interface. User code should use the
* matchers through assertThat statements and not directly.
*/
goog.provide('goog.labs.testing.Matcher');
/**
* A matcher object to be used in assertThat statements.
* @interface
*/
goog.labs.testing.Matcher = function() {};
/**
* Determines whether a value matches the constraints of the match.
*
* @param {*} value The object to match.
* @return {boolean} Whether the input value matches this matcher.
*/
goog.labs.testing.Matcher.prototype.matches = function(value) {};
/**
* Describes why the matcher failed.
*
* @param {*} value The value that didn't match.
* @param {string=} opt_description A partial description to which the reason
* will be appended.
*
* @return {string} Description of why the matcher failed.
*/
goog.labs.testing.Matcher.prototype.describe =
function(value, opt_description) {};

View File

@@ -0,0 +1,334 @@
// Copyright 2012 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 Provides the built-in number matchers like lessThan,
* greaterThan, etc.
*/
goog.provide('goog.labs.testing.CloseToMatcher');
goog.provide('goog.labs.testing.EqualToMatcher');
goog.provide('goog.labs.testing.GreaterThanEqualToMatcher');
goog.provide('goog.labs.testing.GreaterThanMatcher');
goog.provide('goog.labs.testing.LessThanEqualToMatcher');
goog.provide('goog.labs.testing.LessThanMatcher');
goog.require('goog.asserts');
goog.require('goog.labs.testing.Matcher');
/**
* The GreaterThan matcher.
*
* @param {number} value The value to compare.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.GreaterThanMatcher = function(value) {
/**
* @type {number}
* @private
*/
this.value_ = value;
};
/**
* Determines if input value is greater than the expected value.
*
* @override
*/
goog.labs.testing.GreaterThanMatcher.prototype.matches = function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue > this.value_;
};
/**
* @override
*/
goog.labs.testing.GreaterThanMatcher.prototype.describe =
function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue + ' is not greater than ' + this.value_;
};
/**
* The lessThan matcher.
*
* @param {number} value The value to compare.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.LessThanMatcher = function(value) {
/**
* @type {number}
* @private
*/
this.value_ = value;
};
/**
* Determines if the input value is less than the expected value.
*
* @override
*/
goog.labs.testing.LessThanMatcher.prototype.matches = function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue < this.value_;
};
/**
* @override
*/
goog.labs.testing.LessThanMatcher.prototype.describe =
function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue + ' is not less than ' + this.value_;
};
/**
* The GreaterThanEqualTo matcher.
*
* @param {number} value The value to compare.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.GreaterThanEqualToMatcher = function(value) {
/**
* @type {number}
* @private
*/
this.value_ = value;
};
/**
* Determines if the input value is greater than equal to the expected value.
*
* @override
*/
goog.labs.testing.GreaterThanEqualToMatcher.prototype.matches =
function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue >= this.value_;
};
/**
* @override
*/
goog.labs.testing.GreaterThanEqualToMatcher.prototype.describe =
function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue + ' is not greater than equal to ' + this.value_;
};
/**
* The LessThanEqualTo matcher.
*
* @param {number} value The value to compare.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.LessThanEqualToMatcher = function(value) {
/**
* @type {number}
* @private
*/
this.value_ = value;
};
/**
* Determines if the input value is less than or equal to the expected value.
*
* @override
*/
goog.labs.testing.LessThanEqualToMatcher.prototype.matches =
function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue <= this.value_;
};
/**
* @override
*/
goog.labs.testing.LessThanEqualToMatcher.prototype.describe =
function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue + ' is not less than equal to ' + this.value_;
};
/**
* The EqualTo matcher.
*
* @param {number} value The value to compare.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.EqualToMatcher = function(value) {
/**
* @type {number}
* @private
*/
this.value_ = value;
};
/**
* Determines if the input value is equal to the expected value.
*
* @override
*/
goog.labs.testing.EqualToMatcher.prototype.matches = function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue === this.value_;
};
/**
* @override
*/
goog.labs.testing.EqualToMatcher.prototype.describe =
function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue + ' is not equal to ' + this.value_;
};
/**
* The CloseTo matcher.
*
* @param {number} value The value to compare.
* @param {number} range The range to check within.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.CloseToMatcher = function(value, range) {
/**
* @type {number}
* @private
*/
this.value_ = value;
/**
* @type {number}
* @private
*/
this.range_ = range;
};
/**
* Determines if input value is within a certain range of the expected value.
*
* @override
*/
goog.labs.testing.CloseToMatcher.prototype.matches = function(actualValue) {
goog.asserts.assertNumber(actualValue);
return Math.abs(this.value_ - actualValue) < this.range_;
};
/**
* @override
*/
goog.labs.testing.CloseToMatcher.prototype.describe =
function(actualValue) {
goog.asserts.assertNumber(actualValue);
return actualValue + ' is not close to(' + this.range_ + ') ' + this.value_;
};
/**
* @param {number} value The expected value.
*
* @return {!goog.labs.testing.GreaterThanMatcher} A GreaterThanMatcher.
*/
function greaterThan(value) {
return new goog.labs.testing.GreaterThanMatcher(value);
}
/**
* @param {number} value The expected value.
*
* @return {!goog.labs.testing.GreaterThanEqualToMatcher} A
* GreaterThanEqualToMatcher.
*/
function greaterThanEqualTo(value) {
return new goog.labs.testing.GreaterThanEqualToMatcher(value);
}
/**
* @param {number} value The expected value.
*
* @return {!goog.labs.testing.LessThanMatcher} A LessThanMatcher.
*/
function lessThan(value) {
return new goog.labs.testing.LessThanMatcher(value);
}
/**
* @param {number} value The expected value.
*
* @return {!goog.labs.testing.LessThanEqualToMatcher} A LessThanEqualToMatcher.
*/
function lessThanEqualTo(value) {
return new goog.labs.testing.LessThanEqualToMatcher(value);
}
/**
* @param {number} value The expected value.
*
* @return {!goog.labs.testing.EqualToMatcher} An EqualToMatcher.
*/
function equalTo(value) {
return new goog.labs.testing.EqualToMatcher(value);
}
/**
* @param {number} value The expected value.
* @param {number} range The maximum allowed difference from the expected value.
*
* @return {!goog.labs.testing.CloseToMatcher} A CloseToMatcher.
*/
function closeTo(value, range) {
return new goog.labs.testing.CloseToMatcher(value, range);
}

View File

@@ -0,0 +1,306 @@
// Copyright 2012 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 Provides the built-in object matchers like equalsObject,
* hasProperty, instanceOf, etc.
*/
goog.provide('goog.labs.testing.HasPropertyMatcher');
goog.provide('goog.labs.testing.InstanceOfMatcher');
goog.provide('goog.labs.testing.IsNullMatcher');
goog.provide('goog.labs.testing.IsNullOrUndefinedMatcher');
goog.provide('goog.labs.testing.IsUndefinedMatcher');
goog.provide('goog.labs.testing.ObjectEqualsMatcher');
goog.require('goog.labs.testing.Matcher');
goog.require('goog.string');
/**
* The Equals matcher.
*
* @param {!Object} expectedObject The expected object.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.ObjectEqualsMatcher = function(expectedObject) {
/**
* @type {!Object}
* @private
*/
this.object_ = expectedObject;
};
/**
* Determines if two objects are the same.
*
* @override
*/
goog.labs.testing.ObjectEqualsMatcher.prototype.matches =
function(actualObject) {
return actualObject === this.object_;
};
/**
* @override
*/
goog.labs.testing.ObjectEqualsMatcher.prototype.describe =
function(actualObject) {
return 'Input object is not the same as the expected object.';
};
/**
* The HasProperty matcher.
*
* @param {string} property Name of the property to test.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.HasPropertyMatcher = function(property) {
/**
* @type {string}
* @private
*/
this.property_ = property;
};
/**
* Determines if an object has a property.
*
* @override
*/
goog.labs.testing.HasPropertyMatcher.prototype.matches =
function(actualObject) {
return this.property_ in actualObject;
};
/**
* @override
*/
goog.labs.testing.HasPropertyMatcher.prototype.describe =
function(actualObject) {
return 'Object does not have property: ' + this.property_;
};
/**
* The InstanceOf matcher.
*
* @param {!Object} object The expected class object.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.InstanceOfMatcher = function(object) {
/**
* @type {!Object}
* @private
*/
this.object_ = object;
};
/**
* Determines if an object is an instance of another object.
*
* @override
*/
goog.labs.testing.InstanceOfMatcher.prototype.matches =
function(actualObject) {
return actualObject instanceof this.object_;
};
/**
* @override
*/
goog.labs.testing.InstanceOfMatcher.prototype.describe =
function(actualObject) {
return 'Input object is not an instance of the expected object';
};
/**
* The IsNullOrUndefined matcher.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.IsNullOrUndefinedMatcher = function() {};
/**
* Determines if input value is null or undefined.
*
* @override
*/
goog.labs.testing.IsNullOrUndefinedMatcher.prototype.matches =
function(actualValue) {
return !goog.isDefAndNotNull(actualValue);
};
/**
* @override
*/
goog.labs.testing.IsNullOrUndefinedMatcher.prototype.describe =
function(actualValue) {
return actualValue + ' is not null or undefined.';
};
/**
* The IsNull matcher.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.IsNullMatcher = function() {};
/**
* Determines if input value is null.
*
* @override
*/
goog.labs.testing.IsNullMatcher.prototype.matches =
function(actualValue) {
return goog.isNull(actualValue);
};
/**
* @override
*/
goog.labs.testing.IsNullMatcher.prototype.describe =
function(actualValue) {
return actualValue + ' is not null.';
};
/**
* The IsUndefined matcher.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.IsUndefinedMatcher = function() {};
/**
* Determines if input value is undefined.
*
* @override
*/
goog.labs.testing.IsUndefinedMatcher.prototype.matches =
function(actualValue) {
return !goog.isDef(actualValue);
};
/**
* @override
*/
goog.labs.testing.IsUndefinedMatcher.prototype.describe =
function(actualValue) {
return actualValue + ' is not undefined.';
};
/**
* Returns a matcher that matches objects that are equal to the input object.
* Equality in this case means the two objects are references to the same
* object.
*
* @param {!Object} object The expected object.
*
* @return {!goog.labs.testing.ObjectEqualsMatcher} A
* ObjectEqualsMatcher.
*/
function equalsObject(object) {
return new goog.labs.testing.ObjectEqualsMatcher(object);
}
/**
* Returns a matcher that matches objects that contain the input property.
*
* @param {string} property The property name to check.
*
* @return {!goog.labs.testing.HasPropertyMatcher} A HasPropertyMatcher.
*/
function hasProperty(property) {
return new goog.labs.testing.HasPropertyMatcher(property);
}
/**
* Returns a matcher that matches instances of the input class.
*
* @param {!Object} object The class object.
*
* @return {!goog.labs.testing.InstanceOfMatcher} A
* InstanceOfMatcher.
*/
function instanceOfClass(object) {
return new goog.labs.testing.InstanceOfMatcher(object);
}
/**
* Returns a matcher that matches all null values.
*
* @return {!goog.labs.testing.IsNullMatcher} A IsNullMatcher.
*/
function isNull() {
return new goog.labs.testing.IsNullMatcher();
}
/**
* Returns a matcher that matches all null and undefined values.
*
* @return {!goog.labs.testing.IsNullOrUndefinedMatcher} A
* IsNullOrUndefinedMatcher.
*/
function isNullOrUndefined() {
return new goog.labs.testing.IsNullOrUndefinedMatcher();
}
/**
* Returns a matcher that matches undefined values.
*
* @return {!goog.labs.testing.IsUndefinedMatcher} A IsUndefinedMatcher.
*/
function isUndefined() {
return new goog.labs.testing.IsUndefinedMatcher();
}

View File

@@ -0,0 +1,402 @@
// Copyright 2012 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 Provides the built-in string matchers like containsString,
* startsWith, endsWith, etc.
*/
goog.provide('goog.labs.testing.ContainsStringMatcher');
goog.provide('goog.labs.testing.EndsWithMatcher');
goog.provide('goog.labs.testing.EqualToIgnoringCaseMatcher');
goog.provide('goog.labs.testing.EqualToIgnoringWhitespaceMatcher');
goog.provide('goog.labs.testing.EqualsMatcher');
goog.provide('goog.labs.testing.RegexMatcher');
goog.provide('goog.labs.testing.StartsWithMatcher');
goog.provide('goog.labs.testing.StringContainsInOrderMatcher');
goog.require('goog.asserts');
goog.require('goog.labs.testing.Matcher');
goog.require('goog.string');
/**
* The ContainsString matcher.
*
* @param {string} value The expected string.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.ContainsStringMatcher = function(value) {
/**
* @type {string}
* @private
*/
this.value_ = value;
};
/**
* Determines if input string contains the expected string.
*
* @override
*/
goog.labs.testing.ContainsStringMatcher.prototype.matches =
function(actualValue) {
goog.asserts.assertString(actualValue);
return goog.string.contains(actualValue, this.value_);
};
/**
* @override
*/
goog.labs.testing.ContainsStringMatcher.prototype.describe =
function(actualValue) {
return actualValue + ' does not contain ' + this.value_;
};
/**
* The EndsWith matcher.
*
* @param {string} value The expected string.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.EndsWithMatcher = function(value) {
/**
* @type {string}
* @private
*/
this.value_ = value;
};
/**
* Determines if input string ends with the expected string.
*
* @override
*/
goog.labs.testing.EndsWithMatcher.prototype.matches = function(actualValue) {
goog.asserts.assertString(actualValue);
return goog.string.endsWith(actualValue, this.value_);
};
/**
* @override
*/
goog.labs.testing.EndsWithMatcher.prototype.describe =
function(actualValue) {
return actualValue + ' does not end with ' + this.value_;
};
/**
* The EqualToIgnoringWhitespace matcher.
*
* @param {string} value The expected string.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.EqualToIgnoringWhitespaceMatcher = function(value) {
/**
* @type {string}
* @private
*/
this.value_ = value;
};
/**
* Determines if input string contains the expected string.
*
* @override
*/
goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.matches =
function(actualValue) {
goog.asserts.assertString(actualValue);
var string1 = goog.string.collapseWhitespace(actualValue);
return goog.string.caseInsensitiveCompare(this.value_, string1) === 0;
};
/**
* @override
*/
goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.describe =
function(actualValue) {
return actualValue + ' is not equal(ignoring whitespace) to ' + this.value_;
};
/**
* The Equals matcher.
*
* @param {string} value The expected string.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.EqualsMatcher = function(value) {
/**
* @type {string}
* @private
*/
this.value_ = value;
};
/**
* Determines if input string is equal to the expected string.
*
* @override
*/
goog.labs.testing.EqualsMatcher.prototype.matches = function(actualValue) {
goog.asserts.assertString(actualValue);
return this.value_ === actualValue;
};
/**
* @override
*/
goog.labs.testing.EqualsMatcher.prototype.describe =
function(actualValue) {
return actualValue + ' is not equal to ' + this.value_;
};
/**
* The MatchesRegex matcher.
*
* @param {!RegExp} regex The expected regex.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.RegexMatcher = function(regex) {
/**
* @type {!RegExp}
* @private
*/
this.regex_ = regex;
};
/**
* Determines if input string is equal to the expected string.
*
* @override
*/
goog.labs.testing.RegexMatcher.prototype.matches = function(
actualValue) {
goog.asserts.assertString(actualValue);
return this.regex_.test(actualValue);
};
/**
* @override
*/
goog.labs.testing.RegexMatcher.prototype.describe =
function(actualValue) {
return actualValue + ' does not match ' + this.regex_;
};
/**
* The StartsWith matcher.
*
* @param {string} value The expected string.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.StartsWithMatcher = function(value) {
/**
* @type {string}
* @private
*/
this.value_ = value;
};
/**
* Determines if input string starts with the expected string.
*
* @override
*/
goog.labs.testing.StartsWithMatcher.prototype.matches = function(actualValue) {
goog.asserts.assertString(actualValue);
return goog.string.startsWith(actualValue, this.value_);
};
/**
* @override
*/
goog.labs.testing.StartsWithMatcher.prototype.describe =
function(actualValue) {
return actualValue + ' does not start with ' + this.value_;
};
/**
* The StringContainsInOrdermatcher.
*
* @param {Array.<string>} values The expected string values.
*
* @constructor
* @implements {goog.labs.testing.Matcher}
*/
goog.labs.testing.StringContainsInOrderMatcher = function(values) {
/**
* @type {Array.<string>}
* @private
*/
this.values_ = values;
};
/**
* Determines if input string contains, in order, the expected array of strings.
*
* @override
*/
goog.labs.testing.StringContainsInOrderMatcher.prototype.matches =
function(actualValue) {
goog.asserts.assertString(actualValue);
var currentIndex, previousIndex = 0;
for (var i = 0; i < this.values_.length; i++) {
currentIndex = goog.string.contains(actualValue, this.values_[i]);
if (currentIndex < 0 || currentIndex < previousIndex) {
return false;
}
previousIndex = currentIndex;
}
return true;
};
/**
* @override
*/
goog.labs.testing.StringContainsInOrderMatcher.prototype.describe =
function(actualValue) {
return actualValue + ' does not contain the expected values in order.';
};
/**
* Matches a string containing the given string.
*
* @param {string} value The expected value.
*
* @return {!goog.labs.testing.ContainsStringMatcher} A
* ContainsStringMatcher.
*/
function containsString(value) {
return new goog.labs.testing.ContainsStringMatcher(value);
}
/**
* Matches a string that ends with the given string.
*
* @param {string} value The expected value.
*
* @return {!goog.labs.testing.EndsWithMatcher} A
* EndsWithMatcher.
*/
function endsWith(value) {
return new goog.labs.testing.EndsWithMatcher(value);
}
/**
* Matches a string that equals (ignoring whitespace) the given string.
*
* @param {string} value The expected value.
*
* @return {!goog.labs.testing.EqualToIgnoringWhitespaceMatcher} A
* EqualToIgnoringWhitespaceMatcher.
*/
function equalToIgnoringWhitespace(value) {
return new goog.labs.testing.EqualToIgnoringWhitespaceMatcher(value);
}
/**
* Matches a string that equals the given string.
*
* @param {string} value The expected value.
*
* @return {!goog.labs.testing.EqualsMatcher} A EqualsMatcher.
*/
function equals(value) {
return new goog.labs.testing.EqualsMatcher(value);
}
/**
* Matches a string against a regular expression.
*
* @param {!RegExp} regex The expected regex.
*
* @return {!goog.labs.testing.RegexMatcher} A RegexMatcher.
*/
function matchesRegex(regex) {
return new goog.labs.testing.RegexMatcher(regex);
}
/**
* Matches a string that starts with the given string.
*
* @param {string} value The expected value.
*
* @return {!goog.labs.testing.StartsWithMatcher} A
* StartsWithMatcher.
*/
function startsWith(value) {
return new goog.labs.testing.StartsWithMatcher(value);
}
/**
* Matches a string that contains the given strings in order.
*
* @param {Array.<string>} values The expected value.
*
* @return {!goog.labs.testing.StringContainsInOrderMatcher} A
* StringContainsInOrderMatcher.
*/
function stringContainsInOrder(values) {
return new goog.labs.testing.StringContainsInOrderMatcher(values);
}