Adding mapbox-gl branch
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
// 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.debug.Error');
|
||||
|
||||
|
||||
/**
|
||||
* 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}
|
||||
* @final
|
||||
*/
|
||||
goog.labs.testing.MatcherError = function(opt_message) {
|
||||
goog.labs.testing.MatcherError.base(this, 'constructor', opt_message);
|
||||
};
|
||||
goog.inherits(goog.labs.testing.MatcherError, goog.debug.Error);
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<!--
|
||||
Copyright 2012 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>
|
||||
<title>
|
||||
Closure Unit Tests - goog.labs.testing.assertThat
|
||||
</title>
|
||||
<script src="../../base.js">
|
||||
</script>
|
||||
<script>
|
||||
goog.require('goog.labs.testing.assertThatTest');
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,69 @@
|
||||
// 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.
|
||||
|
||||
goog.provide('goog.labs.testing.assertThatTest');
|
||||
goog.setTestOnly('goog.labs.testing.assertThatTest');
|
||||
|
||||
goog.require('goog.labs.testing.MatcherError');
|
||||
goog.require('goog.labs.testing.assertThat');
|
||||
goog.require('goog.testing.jsunit');
|
||||
goog.require('goog.testing.recordFunction');
|
||||
|
||||
var successMatchesFn, failureMatchesFn, describeFn, successTestMatcher;
|
||||
var failureTestMatcher;
|
||||
|
||||
function setUp() {
|
||||
successMatchesFn = new goog.testing.recordFunction(function() {return true;});
|
||||
failureMatchesFn =
|
||||
new goog.testing.recordFunction(function() {return false;});
|
||||
describeFn = new goog.testing.recordFunction();
|
||||
|
||||
successTestMatcher = function() {
|
||||
return { matches: successMatchesFn, describe: describeFn };
|
||||
};
|
||||
failureTestMatcher = function() {
|
||||
return { matches: failureMatchesFn, describe: describeFn };
|
||||
};
|
||||
}
|
||||
|
||||
function testAssertthatAlwaysCallsMatches() {
|
||||
var value = 7;
|
||||
goog.labs.testing.assertThat(value, successTestMatcher(),
|
||||
'matches is called on success');
|
||||
|
||||
assertEquals(1, successMatchesFn.getCallCount());
|
||||
var matchesCall = successMatchesFn.popLastCall();
|
||||
assertEquals(value, matchesCall.getArgument(0));
|
||||
|
||||
var e = assertThrows(goog.bind(goog.labs.testing.assertThat, null,
|
||||
value, failureTestMatcher(), 'matches is called on failure'));
|
||||
|
||||
assertTrue(e instanceof goog.labs.testing.MatcherError);
|
||||
|
||||
assertEquals(1, failureMatchesFn.getCallCount());
|
||||
}
|
||||
|
||||
function testAssertthatCallsDescribeOnFailure() {
|
||||
var value = 7;
|
||||
var e = assertThrows(goog.bind(goog.labs.testing.assertThat, null,
|
||||
value, failureTestMatcher(), 'describe is called on failure'));
|
||||
|
||||
assertTrue(e instanceof goog.labs.testing.MatcherError);
|
||||
|
||||
assertEquals(1, failureMatchesFn.getCallCount());
|
||||
assertEquals(1, describeFn.getCallCount());
|
||||
|
||||
var matchesCall = describeFn.popLastCall();
|
||||
assertEquals(value, matchesCall.getArgument(0));
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// 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}
|
||||
* @final
|
||||
*/
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<!--
|
||||
Copyright 2012 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>
|
||||
<title>
|
||||
Closure Unit Tests - Decorator matchers
|
||||
</title>
|
||||
<script src="../../base.js">
|
||||
</script>
|
||||
<script>
|
||||
goog.require('goog.labs.testing.decoratorMatcherTest');
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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.
|
||||
|
||||
goog.provide('goog.labs.testing.decoratorMatcherTest');
|
||||
goog.setTestOnly('goog.labs.testing.decoratorMatcherTest');
|
||||
|
||||
/** @suppress {extraRequire} */
|
||||
goog.require('goog.labs.testing.AnythingMatcher');
|
||||
/** @suppress {extraRequire} */
|
||||
goog.require('goog.labs.testing.GreaterThanMatcher');
|
||||
goog.require('goog.labs.testing.MatcherError');
|
||||
goog.require('goog.labs.testing.assertThat');
|
||||
goog.require('goog.testing.jsunit');
|
||||
|
||||
function testAnythingMatcher() {
|
||||
goog.labs.testing.assertThat(true, anything(), 'anything matches true');
|
||||
goog.labs.testing.assertThat(false, anything(), 'false matches anything');
|
||||
}
|
||||
|
||||
function testIs() {
|
||||
goog.labs.testing.assertThat(5, is(greaterThan(4)), '5 is > 4');
|
||||
}
|
||||
|
||||
function testDescribedAs() {
|
||||
var e = assertThrows(function() {
|
||||
goog.labs.testing.assertThat(4, describedAs('this is a test',
|
||||
greaterThan(6)))});
|
||||
assertTrue(e instanceof goog.labs.testing.MatcherError);
|
||||
assertEquals('this is a test', e.message);
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
// 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.asserts');
|
||||
goog.require('goog.labs.testing.Matcher');
|
||||
goog.require('goog.object');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The HasEntries matcher.
|
||||
*
|
||||
* @param {!Object} entries The entries to check in the object.
|
||||
*
|
||||
* @constructor
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<!--
|
||||
Copyright 2012 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>
|
||||
<title>
|
||||
Closure Unit Tests - Object matchers
|
||||
</title>
|
||||
<script src="../../base.js">
|
||||
</script>
|
||||
<script>
|
||||
goog.require('goog.labs.testing.dictionaryMatcherTest');
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,65 @@
|
||||
// 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.
|
||||
|
||||
goog.provide('goog.labs.testing.dictionaryMatcherTest');
|
||||
goog.setTestOnly('goog.labs.testing.dictionaryMatcherTest');
|
||||
|
||||
/** @suppress {extraRequire} */
|
||||
goog.require('goog.labs.testing.HasEntryMatcher');
|
||||
goog.require('goog.labs.testing.MatcherError');
|
||||
goog.require('goog.labs.testing.assertThat');
|
||||
goog.require('goog.testing.jsunit');
|
||||
|
||||
function testHasEntries() {
|
||||
var obj1 = {x: 1, y: 2, z: 3};
|
||||
goog.labs.testing.assertThat(obj1, hasEntries({x: 1, y: 2}),
|
||||
'obj1 has entries: {x:1, y:2}');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(obj1, hasEntries({z: 5, a: 4}));
|
||||
}, 'hasEntries should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testHasEntry() {
|
||||
var obj1 = {x: 1, y: 2, z: 3};
|
||||
goog.labs.testing.assertThat(obj1, hasEntry('x', 1),
|
||||
'obj1 has entry: {x:1}');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(obj1, hasEntry('z', 5));
|
||||
}, 'hasEntry should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testHasKey() {
|
||||
var obj1 = {x: 1};
|
||||
goog.labs.testing.assertThat(obj1, hasKey('x'), 'obj1 has key x');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(obj1, hasKey('z'));
|
||||
}, 'hasKey should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testHasValue() {
|
||||
var obj1 = {x: 1};
|
||||
goog.labs.testing.assertThat(obj1, hasValue(1), 'obj1 has value 1');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(obj1, hasValue(2));
|
||||
}, 'hasValue should throw exception when it fails');
|
||||
}
|
||||
|
||||
function assertMatcherError(callable, errorString) {
|
||||
var e = assertThrows(errorString || 'callable throws exception', callable);
|
||||
assertTrue(e instanceof goog.labs.testing.MatcherError);
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
// Copyright 2014 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.labs.testing.Environment');
|
||||
|
||||
goog.require('goog.array');
|
||||
goog.require('goog.debug.Console');
|
||||
goog.require('goog.testing.MockClock');
|
||||
goog.require('goog.testing.MockControl');
|
||||
goog.require('goog.testing.TestCase');
|
||||
goog.require('goog.testing.jsunit');
|
||||
|
||||
|
||||
/**
|
||||
* JsUnit environments allow developers to customize the existing testing
|
||||
* lifecycle by hitching additional setUp and tearDown behaviors to tests.
|
||||
*
|
||||
* Environments will run their setUp steps in the order in which they
|
||||
* are instantiated and registered. During tearDown, the environments will
|
||||
* unwind the setUp and execute in reverse order.
|
||||
*
|
||||
* See http://go/jsunit-env for more information.
|
||||
*/
|
||||
goog.labs.testing.Environment = goog.defineClass(null, {
|
||||
/** @constructor */
|
||||
constructor: function() {
|
||||
goog.labs.testing.EnvironmentTestCase_.getInstance().
|
||||
registerEnvironment_(this);
|
||||
|
||||
/** @type {goog.testing.MockControl} */
|
||||
this.mockControl = null;
|
||||
|
||||
/** @type {goog.testing.MockClock} */
|
||||
this.mockClock = null;
|
||||
|
||||
/** @private {boolean} */
|
||||
this.shouldMakeMockControl_ = false;
|
||||
|
||||
/** @private {boolean} */
|
||||
this.shouldMakeMockClock_ = false;
|
||||
|
||||
/** @const {!goog.debug.Console} */
|
||||
this.console = goog.labs.testing.Environment.console_;
|
||||
},
|
||||
|
||||
|
||||
/** Runs immediately before the setUpPage phase of JsUnit tests. */
|
||||
setUpPage: function() {
|
||||
if (this.mockClock && this.mockClock.isDisposed()) {
|
||||
this.mockClock = new goog.testing.MockClock(true);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
/** Runs immediately after the tearDownPage phase of JsUnit tests. */
|
||||
tearDownPage: function() {
|
||||
// If we created the mockClock, we'll also dispose it.
|
||||
if (this.shouldMakeMockClock_) {
|
||||
this.mockClock.dispose();
|
||||
}
|
||||
},
|
||||
|
||||
/** Runs immediately before the setUp phase of JsUnit tests. */
|
||||
setUp: goog.nullFunction,
|
||||
|
||||
/** Runs immediately after the tearDown phase of JsUnit tests. */
|
||||
tearDown: function() {
|
||||
// Make sure promises and other stuff that may still be scheduled, get a
|
||||
// chance to run (and throw errors).
|
||||
if (this.mockClock) {
|
||||
for (var i = 0; i < 100; i++) {
|
||||
this.mockClock.tick(1000);
|
||||
}
|
||||
// If we created the mockClock, we'll also reset it.
|
||||
if (this.shouldMakeMockClock_) {
|
||||
this.mockClock.reset();
|
||||
}
|
||||
}
|
||||
// Make sure the user did not forget to call $replayAll & $verifyAll in
|
||||
// their test. This is a noop if they did.
|
||||
// This is important because:
|
||||
// - Engineers thinks that not all their tests need to replay and verify.
|
||||
// That lets tests sneak in that call mocks but never replay those calls.
|
||||
// - Then some well meaning maintenance engineer wants to update the test
|
||||
// with some new mock, adds a replayAll and BOOM the test fails
|
||||
// because completely unrelated mocks now get replayed.
|
||||
if (this.mockControl) {
|
||||
try {
|
||||
this.mockControl.$verifyAll();
|
||||
this.mockControl.$replayAll();
|
||||
this.mockControl.$verifyAll();
|
||||
} finally {
|
||||
this.mockControl.$resetAll();
|
||||
}
|
||||
if (this.shouldMakeMockControl_) {
|
||||
// If we created the mockControl, we'll also tear it down.
|
||||
this.mockControl.$tearDown();
|
||||
}
|
||||
}
|
||||
// Verifying the mockControl may throw, so if cleanup needs to happen,
|
||||
// add it further up in the function.
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Create a new {@see goog.testing.MockControl} accessible via
|
||||
* {@code env.mockControl} for each test. If your test has more than one
|
||||
* testing environment, don't call this on more than one of them.
|
||||
* @return {!goog.labs.testing.Environment} For chaining.
|
||||
*/
|
||||
withMockControl: function() {
|
||||
if (!this.shouldMakeMockControl_) {
|
||||
this.shouldMakeMockControl_ = true;
|
||||
this.mockControl = new goog.testing.MockControl();
|
||||
}
|
||||
return this;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@see goog.testing.MockClock} for each test. The clock will be
|
||||
* installed (override i.e. setTimeout) by default. It can be accessed
|
||||
* using {@code env.mockClock}. If your test has more than one testing
|
||||
* environment, don't call this on more than one of them.
|
||||
* @return {!goog.labs.testing.Environment} For chaining.
|
||||
*/
|
||||
withMockClock: function() {
|
||||
if (!this.shouldMakeMockClock_) {
|
||||
this.shouldMakeMockClock_ = true;
|
||||
this.mockClock = new goog.testing.MockClock(true);
|
||||
}
|
||||
return this;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Creates a basic strict mock of a {@code toMock}. For more advanced mocking,
|
||||
* please use the MockControl directly.
|
||||
* @param {Function} toMock
|
||||
* @return {!goog.testing.StrictMock}
|
||||
*/
|
||||
mock: function(toMock) {
|
||||
if (!this.shouldMakeMockControl_) {
|
||||
throw new Error('MockControl not available on this environment. ' +
|
||||
'Call withMockControl if this environment is expected ' +
|
||||
'to contain a MockControl.');
|
||||
}
|
||||
return this.mockControl.createStrictMock(toMock);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
/** @private @const {!goog.debug.Console} */
|
||||
goog.labs.testing.Environment.console_ = new goog.debug.Console();
|
||||
|
||||
|
||||
// Activate logging to the browser's console by default.
|
||||
goog.labs.testing.Environment.console_.setCapturing(true);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* An internal TestCase used to hook environments into the JsUnit test runner.
|
||||
* Environments cannot be used in conjunction with custom TestCases for JsUnit.
|
||||
* @private @final @constructor
|
||||
* @extends {goog.testing.TestCase}
|
||||
*/
|
||||
goog.labs.testing.EnvironmentTestCase_ = function() {
|
||||
goog.labs.testing.EnvironmentTestCase_.base(this, 'constructor');
|
||||
|
||||
/** @private {!Array<!goog.labs.testing.Environment>}> */
|
||||
this.environments_ = [];
|
||||
|
||||
// Automatically install this TestCase when any environment is used in a test.
|
||||
goog.testing.TestCase.initializeTestRunner(this);
|
||||
};
|
||||
goog.inherits(goog.labs.testing.EnvironmentTestCase_, goog.testing.TestCase);
|
||||
goog.addSingletonGetter(goog.labs.testing.EnvironmentTestCase_);
|
||||
|
||||
|
||||
/**
|
||||
* Override the default global scope discovery of lifecycle functions to prevent
|
||||
* overriding the custom environment setUp(Page)/tearDown(Page) logic.
|
||||
* @override
|
||||
*/
|
||||
goog.labs.testing.EnvironmentTestCase_.prototype.autoDiscoverLifecycle =
|
||||
function() {
|
||||
if (goog.global['runTests']) {
|
||||
this.runTests = goog.bind(goog.global['runTests'], goog.global);
|
||||
}
|
||||
if (goog.global['shouldRunTests']) {
|
||||
this.shouldRunTests = goog.bind(goog.global['shouldRunTests'], goog.global);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Adds an environment to the JsUnit test.
|
||||
* @param {!goog.labs.testing.Environment} env
|
||||
* @private
|
||||
*/
|
||||
goog.labs.testing.EnvironmentTestCase_.prototype.registerEnvironment_ =
|
||||
function(env) {
|
||||
this.environments_.push(env);
|
||||
};
|
||||
|
||||
|
||||
/** @override */
|
||||
goog.labs.testing.EnvironmentTestCase_.prototype.setUpPage = function() {
|
||||
goog.array.forEach(this.environments_, function(env) {
|
||||
env.setUpPage();
|
||||
});
|
||||
|
||||
// User defined setUpPage method.
|
||||
if (goog.global['setUpPage']) {
|
||||
goog.global['setUpPage']();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** @override */
|
||||
goog.labs.testing.EnvironmentTestCase_.prototype.setUp = function() {
|
||||
// User defined configure method.
|
||||
if (goog.global['configureEnvironment']) {
|
||||
goog.global['configureEnvironment']();
|
||||
}
|
||||
|
||||
goog.array.forEach(this.environments_, function(env) {
|
||||
env.setUp();
|
||||
}, this);
|
||||
|
||||
// User defined setUp method.
|
||||
if (goog.global['setUp']) {
|
||||
goog.global['setUp']();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** @override */
|
||||
goog.labs.testing.EnvironmentTestCase_.prototype.tearDown = function() {
|
||||
var firstException;
|
||||
// User defined tearDown method.
|
||||
if (goog.global['tearDown']) {
|
||||
try {
|
||||
goog.global['tearDown']();
|
||||
} catch (e) {
|
||||
if (!firstException) {
|
||||
firstException = e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Execute the tearDown methods for the environment in the reverse order
|
||||
// in which they were registered to "unfold" the setUp.
|
||||
goog.array.forEachRight(this.environments_, function(env) {
|
||||
// For tearDowns between tests make sure they run as much as possible to
|
||||
// avoid interference between tests.
|
||||
try {
|
||||
env.tearDown();
|
||||
} catch (e) {
|
||||
if (!firstException) {
|
||||
firstException = e;
|
||||
}
|
||||
}
|
||||
});
|
||||
if (firstException) {
|
||||
throw firstException;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** @override */
|
||||
goog.labs.testing.EnvironmentTestCase_.prototype.tearDownPage = function() {
|
||||
// User defined tearDownPage method.
|
||||
if (goog.global['tearDownPage']) {
|
||||
goog.global['tearDownPage']();
|
||||
}
|
||||
|
||||
goog.array.forEachRight(this.environments_, function(env) {
|
||||
env.tearDownPage();
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<!--
|
||||
Copyright 2014 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>
|
||||
<title>
|
||||
Closure Unit Tests - JsUnit Environments
|
||||
</title>
|
||||
<script src="../../base.js">
|
||||
</script>
|
||||
<script>
|
||||
goog.require('goog.labs.testing.environmentTest');
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,210 @@
|
||||
// Copyright 2014 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.labs.testing.environmentTest');
|
||||
goog.setTestOnly('goog.labs.testing.environmentTest');
|
||||
|
||||
goog.require('goog.labs.testing.Environment');
|
||||
goog.require('goog.testing.MockControl');
|
||||
goog.require('goog.testing.TestCase');
|
||||
goog.require('goog.testing.jsunit');
|
||||
|
||||
var testCase = null;
|
||||
var mockControl = null;
|
||||
|
||||
// Use this flag to control whether the global JsUnit lifecycle events are being
|
||||
// called as part of the test lifecycle or as part of the "mocked" environment.
|
||||
var testing = false;
|
||||
|
||||
function setUp() {
|
||||
if (testing) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Temporarily override the initializeTestRunner method to avoid installing
|
||||
// our "test" TestCase.
|
||||
var initFn = goog.testing.TestCase.initializeTestRunner;
|
||||
goog.testing.TestCase.initializeTestRunner = function() {};
|
||||
testCase = new goog.labs.testing.EnvironmentTestCase_();
|
||||
goog.labs.testing.EnvironmentTestCase_.getInstance = function() {
|
||||
return testCase;
|
||||
};
|
||||
goog.testing.TestCase.initializeTestRunner = initFn;
|
||||
|
||||
mockControl = new goog.testing.MockControl();
|
||||
}
|
||||
|
||||
function tearDown() {
|
||||
if (testing) {
|
||||
return;
|
||||
}
|
||||
|
||||
mockControl.$resetAll();
|
||||
mockControl.$tearDown();
|
||||
}
|
||||
|
||||
function testLifecycle() {
|
||||
testing = true;
|
||||
|
||||
var envOne = mockControl.createStrictMock(goog.labs.testing.Environment);
|
||||
var envTwo = mockControl.createStrictMock(goog.labs.testing.Environment);
|
||||
var envThree = mockControl.createStrictMock(goog.labs.testing.Environment);
|
||||
var testMethod = mockControl.createFunctionMock('testMethod');
|
||||
|
||||
testCase.addNewTest('testFake', testMethod);
|
||||
|
||||
testCase.registerEnvironment_(envOne);
|
||||
testCase.registerEnvironment_(envTwo);
|
||||
testCase.registerEnvironment_(envThree);
|
||||
|
||||
envOne.setUpPage();
|
||||
envTwo.setUpPage();
|
||||
envThree.setUpPage();
|
||||
|
||||
envOne.setUp();
|
||||
envTwo.setUp();
|
||||
envThree.setUp();
|
||||
|
||||
testMethod();
|
||||
|
||||
envThree.tearDown();
|
||||
envTwo.tearDown();
|
||||
envOne.tearDown();
|
||||
|
||||
envThree.tearDownPage();
|
||||
envTwo.tearDownPage();
|
||||
envOne.tearDownPage();
|
||||
|
||||
mockControl.$replayAll();
|
||||
testCase.runTests();
|
||||
mockControl.$verifyAll();
|
||||
|
||||
testing = false;
|
||||
}
|
||||
|
||||
function testTearDownWithMockControl() {
|
||||
testing = true;
|
||||
|
||||
var envWith = new goog.labs.testing.Environment();
|
||||
var envWithout = new goog.labs.testing.Environment();
|
||||
|
||||
var mockControlMock = mockControl.createStrictMock(goog.testing.MockControl);
|
||||
var mockControlCtorMock = mockControl.createMethodMock(goog.testing,
|
||||
'MockControl');
|
||||
mockControlCtorMock().$times(1).$returns(mockControlMock);
|
||||
// Expecting verify / reset calls twice since two environments use the same
|
||||
// mockControl, but only one created it and is allowed to tear it down.
|
||||
mockControlMock.$verifyAll();
|
||||
mockControlMock.$replayAll();
|
||||
mockControlMock.$verifyAll();
|
||||
mockControlMock.$resetAll();
|
||||
mockControlMock.$tearDown().$times(1);
|
||||
mockControlMock.$verifyAll();
|
||||
mockControlMock.$replayAll();
|
||||
mockControlMock.$verifyAll();
|
||||
mockControlMock.$resetAll();
|
||||
|
||||
mockControl.$replayAll();
|
||||
envWith.withMockControl();
|
||||
envWithout.mockControl = mockControlMock;
|
||||
envWith.tearDown();
|
||||
envWithout.tearDown();
|
||||
mockControl.$verifyAll();
|
||||
mockControl.$resetAll();
|
||||
|
||||
testing = false;
|
||||
}
|
||||
|
||||
function testAutoDiscoverTests() {
|
||||
testing = true;
|
||||
|
||||
var setUpPageFn = testCase.setUpPage;
|
||||
var setUpFn = testCase.setUp;
|
||||
var tearDownFn = testCase.tearDownFn;
|
||||
var tearDownPageFn = testCase.tearDownPageFn;
|
||||
|
||||
testCase.autoDiscoverTests();
|
||||
|
||||
assertEquals(setUpPageFn, testCase.setUpPage);
|
||||
assertEquals(setUpFn, testCase.setUp);
|
||||
assertEquals(tearDownFn, testCase.tearDownFn);
|
||||
assertEquals(tearDownPageFn, testCase.tearDownPageFn);
|
||||
|
||||
// Note that this number changes when more tests are added to this file as
|
||||
// the environment reflects on the window global scope for JsUnit.
|
||||
assertEquals(6, testCase.tests_.length);
|
||||
|
||||
testing = false;
|
||||
}
|
||||
|
||||
function testMockClock() {
|
||||
testing = true;
|
||||
|
||||
var env = new goog.labs.testing.Environment().withMockClock();
|
||||
|
||||
testCase.addNewTest('testThatThrowsEventually', function() {
|
||||
setTimeout(function() {
|
||||
throw new Error('LateErrorMessage');
|
||||
}, 200);
|
||||
});
|
||||
|
||||
testCase.runTests();
|
||||
assertTestFailure(testCase, 'testThatThrowsEventually', 'LateErrorMessage');
|
||||
|
||||
testing = false;
|
||||
}
|
||||
|
||||
function testMockControl() {
|
||||
testing = true;
|
||||
|
||||
var env = new goog.labs.testing.Environment().withMockControl();
|
||||
var test = env.mockControl.createFunctionMock('test');
|
||||
|
||||
testCase.addNewTest('testWithoutVerify', function() {
|
||||
test();
|
||||
env.mockControl.$replayAll();
|
||||
test();
|
||||
});
|
||||
|
||||
testCase.runTests();
|
||||
assertNull(env.mockClock);
|
||||
|
||||
testing = false;
|
||||
}
|
||||
|
||||
function testMock() {
|
||||
testing = true;
|
||||
|
||||
var env = new goog.labs.testing.Environment().withMockControl();
|
||||
var mock = env.mock({
|
||||
test: function() {}
|
||||
});
|
||||
|
||||
testCase.addNewTest('testMockCalled', function() {
|
||||
mock.test().$times(2);
|
||||
|
||||
env.mockControl.$replayAll();
|
||||
mock.test();
|
||||
mock.test();
|
||||
env.mockControl.verifyAll();
|
||||
});
|
||||
|
||||
testCase.runTests();
|
||||
|
||||
testing = false;
|
||||
}
|
||||
|
||||
function assertTestFailure(testCase, name, message) {
|
||||
assertContains(message, testCase.result_.resultsByName[name][0]);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2014 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.labs.testing.environmentUsageTest');
|
||||
goog.setTestOnly('goog.labs.testing.environmentUsageTest');
|
||||
|
||||
goog.require('goog.labs.testing.Environment');
|
||||
|
||||
var testing = false;
|
||||
var env = new goog.labs.testing.Environment();
|
||||
|
||||
function setUpPage() {
|
||||
assertFalse(testing);
|
||||
}
|
||||
|
||||
function setUp() {
|
||||
testing = true;
|
||||
}
|
||||
|
||||
function testOne() {
|
||||
assertTrue(testing);
|
||||
}
|
||||
|
||||
function testTwo() {
|
||||
assertTrue(testing);
|
||||
}
|
||||
|
||||
function tearDown() {
|
||||
testing = false;
|
||||
}
|
||||
|
||||
function tearDownPage() {
|
||||
assertFalse(testing);
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
// 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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<!--
|
||||
Copyright 2012 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>
|
||||
<title>
|
||||
Closure Unit Tests - Logic matchers
|
||||
</title>
|
||||
<script src="../../base.js">
|
||||
</script>
|
||||
<script>
|
||||
goog.require('goog.labs.testing.logicMatcherTest');
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,57 @@
|
||||
// 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.
|
||||
|
||||
goog.provide('goog.labs.testing.logicMatcherTest');
|
||||
goog.setTestOnly('goog.labs.testing.logicMatcherTest');
|
||||
|
||||
/** @suppress {extraRequire} */
|
||||
goog.require('goog.labs.testing.AllOfMatcher');
|
||||
/** @suppress {extraRequire} */
|
||||
goog.require('goog.labs.testing.GreaterThanMatcher');
|
||||
goog.require('goog.labs.testing.MatcherError');
|
||||
goog.require('goog.labs.testing.assertThat');
|
||||
goog.require('goog.testing.jsunit');
|
||||
|
||||
function testAnyOf() {
|
||||
goog.labs.testing.assertThat(5, anyOf(greaterThan(4), lessThan(3)),
|
||||
'5 > 4 || 5 < 3');
|
||||
goog.labs.testing.assertThat(2, anyOf(greaterThan(4), lessThan(3)),
|
||||
'2 > 4 || 2 < 3');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(4, anyOf(greaterThan(5), lessThan(2)));
|
||||
}, 'anyOf should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testAllOf() {
|
||||
goog.labs.testing.assertThat(5, allOf(greaterThan(4), lessThan(6)),
|
||||
'5 > 4 && 5 < 6');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(4, allOf(lessThan(5), lessThan(3)));
|
||||
}, 'allOf should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testIsNot() {
|
||||
goog.labs.testing.assertThat(5, isNot(greaterThan(6)), '5 !> 6');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(4, isNot(greaterThan(3)));
|
||||
}, 'isNot should throw exception when it fails');
|
||||
}
|
||||
|
||||
function assertMatcherError(callable, errorString) {
|
||||
var e = assertThrows(errorString || 'callable throws exception', callable);
|
||||
assertTrue(e instanceof goog.labs.testing.MatcherError);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// 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) {};
|
||||
|
||||
|
||||
/**
|
||||
* Generates a Matcher from the ‘matches’ and ‘describe’ functions passed in.
|
||||
*
|
||||
* @param {!Function} matchesFunction The ‘matches’ function.
|
||||
* @param {Function=} opt_describeFunction The ‘describe’ function.
|
||||
* @return {!Function} The custom matcher.
|
||||
*/
|
||||
goog.labs.testing.Matcher.makeMatcher =
|
||||
function(matchesFunction, opt_describeFunction) {
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
var matcherConstructor = function() {};
|
||||
|
||||
/** @override */
|
||||
matcherConstructor.prototype.matches = matchesFunction;
|
||||
|
||||
if (opt_describeFunction) {
|
||||
/** @override */
|
||||
matcherConstructor.prototype.describe = opt_describeFunction;
|
||||
}
|
||||
|
||||
return matcherConstructor;
|
||||
};
|
||||
@@ -0,0 +1,346 @@
|
||||
// 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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<!--
|
||||
Copyright 2012 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>
|
||||
<title>
|
||||
Closure Unit Tests - Number matchers
|
||||
</title>
|
||||
<script src="../../base.js">
|
||||
</script>
|
||||
<script>
|
||||
goog.require('goog.labs.testing.numberMatcherTest');
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,71 @@
|
||||
// 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.
|
||||
|
||||
goog.provide('goog.labs.testing.numberMatcherTest');
|
||||
goog.setTestOnly('goog.labs.testing.numberMatcherTest');
|
||||
|
||||
/** @suppress {extraRequire} */
|
||||
goog.require('goog.labs.testing.LessThanMatcher');
|
||||
goog.require('goog.labs.testing.MatcherError');
|
||||
goog.require('goog.labs.testing.assertThat');
|
||||
goog.require('goog.testing.jsunit');
|
||||
|
||||
function testGreaterThan() {
|
||||
goog.labs.testing.assertThat(4, greaterThan(3), '4 > 3');
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(2, greaterThan(3));
|
||||
}, '2 > 3');
|
||||
}
|
||||
|
||||
function testGreaterThanEqualTo() {
|
||||
goog.labs.testing.assertThat(5, greaterThanEqualTo(4), '5 >= 4');
|
||||
goog.labs.testing.assertThat(5, greaterThanEqualTo(5), '5 >= 5');
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(3, greaterThanEqualTo(5));
|
||||
}, '3 >= 5');
|
||||
}
|
||||
|
||||
function testLessThan() {
|
||||
goog.labs.testing.assertThat(6, lessThan(7), '6 < 7');
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(7, lessThan(5));
|
||||
}, '7 < 5');
|
||||
}
|
||||
|
||||
function testLessThanEqualTo() {
|
||||
goog.labs.testing.assertThat(8, lessThanEqualTo(8), '8 <= 8');
|
||||
goog.labs.testing.assertThat(8, lessThanEqualTo(9), '8 <= 9');
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(7, lessThanEqualTo(5));
|
||||
}, '7 <= 5');
|
||||
}
|
||||
|
||||
function testEqualTo() {
|
||||
goog.labs.testing.assertThat(7, equalTo(7), '7 equals 7');
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(7, equalTo(5));
|
||||
}, '7 == 5');
|
||||
}
|
||||
|
||||
function testCloseTo() {
|
||||
goog.labs.testing.assertThat(7, closeTo(10, 4), '7 within range(4) of 10');
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(5, closeTo(10, 3));
|
||||
}, '5 within range(3) of 10');
|
||||
}
|
||||
|
||||
function assertMatcherError(callable, errorString) {
|
||||
var e = assertThrows(errorString || 'callable throws exception', callable);
|
||||
assertTrue(e instanceof goog.labs.testing.MatcherError);
|
||||
}
|
||||
@@ -0,0 +1,317 @@
|
||||
// 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');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The Equals matcher.
|
||||
*
|
||||
* @param {!Object} expectedObject The expected object.
|
||||
*
|
||||
* @constructor
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<!--
|
||||
Copyright 2012 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>
|
||||
<title>
|
||||
Closure Unit Tests - Object matchers
|
||||
</title>
|
||||
<script src="../../base.js">
|
||||
</script>
|
||||
<script>
|
||||
goog.require('goog.labs.testing.objectMatcherTest');
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,95 @@
|
||||
// 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.
|
||||
|
||||
goog.provide('goog.labs.testing.objectMatcherTest');
|
||||
goog.setTestOnly('goog.labs.testing.objectMatcherTest');
|
||||
|
||||
goog.require('goog.labs.testing.MatcherError');
|
||||
/** @suppress {extraRequire} */
|
||||
goog.require('goog.labs.testing.ObjectEqualsMatcher');
|
||||
goog.require('goog.labs.testing.assertThat');
|
||||
goog.require('goog.testing.jsunit');
|
||||
|
||||
function testObjectEquals() {
|
||||
var obj1 = {x: 1};
|
||||
var obj2 = obj1;
|
||||
goog.labs.testing.assertThat(obj1, equalsObject(obj2), 'obj1 equals obj2');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat({x: 1}, equalsObject({}));
|
||||
}, 'equalsObject does not throw exception on failure');
|
||||
}
|
||||
|
||||
function testInstanceOf() {
|
||||
function expected() {
|
||||
this.x = 1;
|
||||
}
|
||||
var input = new expected();
|
||||
goog.labs.testing.assertThat(input, instanceOfClass(expected),
|
||||
'input is an instance of expected');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(5, instanceOfClass(function() {}));
|
||||
}, 'instanceOfClass does not throw exception on failure');
|
||||
}
|
||||
|
||||
function testHasProperty() {
|
||||
goog.labs.testing.assertThat({x: 1}, hasProperty('x'),
|
||||
'{x:1} has property x}');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat({x: 1}, hasProperty('y'));
|
||||
}, 'hasProperty does not throw exception on failure');
|
||||
}
|
||||
|
||||
function testIsNull() {
|
||||
goog.labs.testing.assertThat(null, isNull(), 'null is null');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(5, isNull());
|
||||
}, 'isNull does not throw exception on failure');
|
||||
}
|
||||
|
||||
function testIsNullOrUndefined() {
|
||||
var x;
|
||||
goog.labs.testing.assertThat(undefined, isNullOrUndefined(),
|
||||
'undefined is null or undefined');
|
||||
goog.labs.testing.assertThat(x, isNullOrUndefined(),
|
||||
'undefined is null or undefined');
|
||||
x = null;
|
||||
goog.labs.testing.assertThat(null, isNullOrUndefined(),
|
||||
'null is null or undefined');
|
||||
goog.labs.testing.assertThat(x, isNullOrUndefined(),
|
||||
'null is null or undefined');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(5, isNullOrUndefined());
|
||||
}, 'isNullOrUndefined does not throw exception on failure');
|
||||
}
|
||||
|
||||
function testIsUndefined() {
|
||||
var x;
|
||||
goog.labs.testing.assertThat(undefined, isUndefined(),
|
||||
'undefined is undefined');
|
||||
goog.labs.testing.assertThat(x, isUndefined(), 'undefined is undefined');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat(5, isUndefined());
|
||||
}, 'isUndefined does not throw exception on failure');
|
||||
}
|
||||
|
||||
function assertMatcherError(callable, errorString) {
|
||||
var e = assertThrows(errorString || 'callable throws exception', callable);
|
||||
assertTrue(e instanceof goog.labs.testing.MatcherError);
|
||||
}
|
||||
@@ -0,0 +1,415 @@
|
||||
// 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.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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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
|
||||
* @struct
|
||||
* @implements {goog.labs.testing.Matcher}
|
||||
* @final
|
||||
*/
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<!--
|
||||
Copyright 2012 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>
|
||||
<title>
|
||||
Closure Unit Tests - String matchers
|
||||
</title>
|
||||
<script src="../../base.js">
|
||||
</script>
|
||||
<script>
|
||||
goog.require('goog.labs.testing.stringMatcherTest');
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,92 @@
|
||||
// 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.
|
||||
|
||||
goog.provide('goog.labs.testing.stringMatcherTest');
|
||||
goog.setTestOnly('goog.labs.testing.stringMatcherTest');
|
||||
|
||||
goog.require('goog.labs.testing.MatcherError');
|
||||
/** @suppress {extraRequire} */
|
||||
goog.require('goog.labs.testing.StringContainsInOrderMatcher');
|
||||
goog.require('goog.labs.testing.assertThat');
|
||||
goog.require('goog.testing.jsunit');
|
||||
|
||||
function testContainsString() {
|
||||
goog.labs.testing.assertThat('hello', containsString('ell'),
|
||||
'hello contains ell');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat('hello', containsString('world!'));
|
||||
}, 'containsString should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testEndsWith() {
|
||||
goog.labs.testing.assertThat('hello', endsWith('llo'), 'hello ends with llo');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat('minutes', endsWith('midnight'));
|
||||
}, 'endsWith should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testEqualToIgnoringWhitespace() {
|
||||
goog.labs.testing.assertThat(' h\n EL L\tO',
|
||||
equalToIgnoringWhitespace('h el l o'),
|
||||
'" h EL L\tO " is equal to "h el l o"');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat('hybrid', equalToIgnoringWhitespace('theory'));
|
||||
}, 'equalToIgnoringWhitespace should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testEquals() {
|
||||
goog.labs.testing.assertThat('hello', equals('hello'),
|
||||
'hello equals hello');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat('thousand', equals('suns'));
|
||||
}, 'equals should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testStartsWith() {
|
||||
goog.labs.testing.assertThat('hello', startsWith('hel'),
|
||||
'hello starts with hel');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat('linkin', startsWith('park'));
|
||||
}, 'startsWith should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testStringContainsInOrder() {
|
||||
goog.labs.testing.assertThat('hello',
|
||||
stringContainsInOrder(['h', 'el', 'el', 'l', 'o']),
|
||||
'hello contains in order: [h, el, l, o]');
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat('hybrid', stringContainsInOrder(['hy', 'brid',
|
||||
'theory']));
|
||||
}, 'stringContainsInOrder should throw exception when it fails');
|
||||
}
|
||||
|
||||
function testMatchesRegex() {
|
||||
goog.labs.testing.assertThat('foobar', matchesRegex(/foobar/));
|
||||
goog.labs.testing.assertThat('foobar', matchesRegex(/oobar/));
|
||||
|
||||
assertMatcherError(function() {
|
||||
goog.labs.testing.assertThat('foo', matchesRegex(/^foobar$/));
|
||||
}, 'matchesRegex should throw exception when it fails');
|
||||
}
|
||||
|
||||
function assertMatcherError(callable, errorString) {
|
||||
var e = assertThrows(errorString || 'callable throws exception', callable);
|
||||
assertTrue(e instanceof goog.labs.testing.MatcherError);
|
||||
}
|
||||
Reference in New Issue
Block a user