| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.testing.mockmatchersTest'); |
| 16 |
goog.setTestOnly('goog.testing.mockmatchersTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.testing.mockmatchers'); |
| 22 |
goog.require('goog.testing.mockmatchers.ArgumentMatcher'); |
| 23 |
|
| 24 |
// A local reference to the mockmatchers namespace. |
| 25 |
var matchers = goog.testing.mockmatchers; |
| 26 |
|
| 27 |
// Simple classes to test the InstanceOf matcher. |
| 28 |
var foo = function() {}; |
| 29 |
var bar = function() {}; |
| 30 |
|
| 31 |
// Simple class to test adding error messages to |
| 32 |
// MockExpectation objects |
| 33 |
function MockMock() { |
| 34 |
this.errorMessages = []; |
| 35 |
} |
| 36 |
|
| 37 |
var mockExpect = null; |
| 38 |
|
| 39 |
MockMock.prototype.addErrorMessage = function(msg) { |
| 40 |
this.errorMessages.push(msg); |
| 41 |
}; |
| 42 |
|
| 43 |
|
| 44 |
MockMock.prototype.getErrorMessageCount = function() { |
| 45 |
return this.errorMessages.length; |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
function setUp() { |
| 50 |
mockExpect = new MockMock(); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
function testNoMatchName() { |
| 55 |
// A matcher that does not fill in the match name |
| 56 |
var matcher = new goog.testing.mockmatchers.ArgumentMatcher(goog.isString); |
| 57 |
|
| 58 |
// Make sure the lack of match name doesn't affect the ability |
| 59 |
// to return True/False |
| 60 |
assertTrue(matcher.matches('hello')); |
| 61 |
assertFalse(matcher.matches(123)); |
| 62 |
|
| 63 |
// Make sure we handle the lack of a match name |
| 64 |
assertFalse(matcher.matches(456, mockExpect)); |
| 65 |
assertEquals(1, mockExpect.errorMessages.length); |
| 66 |
assertEquals( |
| 67 |
'Expected: missing mockmatcher description ' + |
| 68 |
'but was: <456> (Number)', |
| 69 |
mockExpect.errorMessages[0]); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
function testInstanceOf() { |
| 74 |
var matcher = new matchers.InstanceOf(foo); |
| 75 |
assertTrue(matcher.matches(new foo())); |
| 76 |
assertFalse(matcher.matches(new bar())); |
| 77 |
|
| 78 |
assertFalse(matcher.matches(new bar(), mockExpect)); |
| 79 |
assertEquals(1, mockExpect.errorMessages.length); |
| 80 |
assertEquals( |
| 81 |
'Expected: instanceOf() ' + |
| 82 |
'but was: <[object Object]> (Object)', |
| 83 |
mockExpect.errorMessages[0]); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
function testTypeOf() { |
| 88 |
var matcher = new matchers.TypeOf('number'); |
| 89 |
assertTrue(matcher.matches(1)); |
| 90 |
assertTrue(matcher.matches(2)); |
| 91 |
assertFalse(matcher.matches('test')); |
| 92 |
|
| 93 |
assertFalse(matcher.matches(true, mockExpect)); |
| 94 |
assertEquals(1, mockExpect.errorMessages.length); |
| 95 |
assertEquals( |
| 96 |
'Expected: typeOf(number) but was: <true> (Boolean)', |
| 97 |
mockExpect.errorMessages[0]); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
function testRegexpMatch() { |
| 102 |
var matcher = new matchers.RegexpMatch(/^cho[dtp]/); |
| 103 |
assertTrue(matcher.matches('chodhop')); |
| 104 |
assertTrue(matcher.matches('chopper')); |
| 105 |
assertFalse(matcher.matches('chocolate')); |
| 106 |
assertFalse(matcher.matches(null)); |
| 107 |
|
| 108 |
assertFalse(matcher.matches('an anger', mockExpect)); |
| 109 |
assertEquals(1, mockExpect.errorMessages.length); |
| 110 |
assertEquals( |
| 111 |
'Expected: match(/^cho[dtp]/) but was: <an anger> (String)', |
| 112 |
mockExpect.errorMessages[0]); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
function testObjectEquals() { |
| 117 |
// Test a simple match. |
| 118 |
var simpleMatcher = new matchers.ObjectEquals({name: 'Bob', age: 42}); |
| 119 |
assertTrue(simpleMatcher.matches({name: 'Bob', age: 42}, mockExpect)); |
| 120 |
assertEquals(0, mockExpect.getErrorMessageCount()); |
| 121 |
expectObjectEqualsFailure( |
| 122 |
simpleMatcher, {name: 'Bill', age: 42}, |
| 123 |
'name: Expected <Bob> (String) but was <Bill> (String)'); |
| 124 |
expectObjectEqualsFailure( |
| 125 |
simpleMatcher, {name: 'Bob', age: 21}, |
| 126 |
'age: Expected <42> (Number) but was <21> (Number)'); |
| 127 |
expectObjectEqualsFailure( |
| 128 |
simpleMatcher, {name: 'Bob'}, |
| 129 |
'property age not present in actual Object'); |
| 130 |
expectObjectEqualsFailure( |
| 131 |
simpleMatcher, {name: 'Bob', age: 42, country: 'USA'}, |
| 132 |
'property country not present in expected Object'); |
| 133 |
|
| 134 |
// Multiple mismatches should include multiple messages. |
| 135 |
expectObjectEqualsFailure( |
| 136 |
simpleMatcher, {name: 'Jim', age: 36}, |
| 137 |
'name: Expected <Bob> (String) but was <Jim> (String)\n' + |
| 138 |
' age: Expected <42> (Number) but was <36> (Number)'); |
| 139 |
} |
| 140 |
|
| 141 |
function testComplexObjectEquals() { |
| 142 |
var complexMatcher = new matchers.ObjectEquals( |
| 143 |
{a: 'foo', b: 2, c: ['bar', 3], d: {sub1: 'baz', sub2: -1}}); |
| 144 |
assertTrue( |
| 145 |
complexMatcher.matches( |
| 146 |
{a: 'foo', b: 2, c: ['bar', 3], d: {sub1: 'baz', sub2: -1}})); |
| 147 |
expectObjectEqualsFailure( |
| 148 |
complexMatcher, |
| 149 |
{a: 'foo', b: 2, c: ['bar', 3], d: {sub1: 'zap', sub2: -1}}, |
| 150 |
'sub1: Expected <baz> (String) but was <zap> (String)'); |
| 151 |
expectObjectEqualsFailure( |
| 152 |
complexMatcher, |
| 153 |
{a: 'foo', b: 2, c: ['bar', 6], d: {sub1: 'baz', sub2: -1}}, |
| 154 |
'c[1]: Expected <3> (Number) but was <6> (Number)'); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
function testSaveArgument() { |
| 159 |
var saveMatcher = new matchers.SaveArgument(); |
| 160 |
assertTrue(saveMatcher.matches(42)); |
| 161 |
assertEquals(42, saveMatcher.arg); |
| 162 |
|
| 163 |
saveMatcher = new matchers.SaveArgument(goog.isString); |
| 164 |
assertTrue(saveMatcher.matches('test')); |
| 165 |
assertEquals('test', saveMatcher.arg); |
| 166 |
assertFalse(saveMatcher.matches(17)); |
| 167 |
assertEquals(17, saveMatcher.arg); |
| 168 |
|
| 169 |
saveMatcher = |
| 170 |
new matchers.SaveArgument(new matchers.ObjectEquals({value: 'value'})); |
| 171 |
assertTrue(saveMatcher.matches({value: 'value'})); |
| 172 |
assertEquals('value', saveMatcher.arg.value); |
| 173 |
assertFalse(saveMatcher.matches('test')); |
| 174 |
assertEquals('test', saveMatcher.arg); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
function testIsArray() { |
| 179 |
assertTrue(matchers.isArray.matches([])); |
| 180 |
assertTrue(matchers.isArray.matches(new Array())); |
| 181 |
assertFalse(matchers.isArray.matches('test')); |
| 182 |
|
| 183 |
assertFalse(matchers.isArray.matches({}, mockExpect)); |
| 184 |
assertEquals(1, mockExpect.errorMessages.length); |
| 185 |
assertEquals( |
| 186 |
'Expected: isArray but was: <[object Object]> (Object)', |
| 187 |
mockExpect.errorMessages[0]); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
function testIsArrayLike() { |
| 192 |
var nodeList = (function() { |
| 193 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 194 |
div.appendChild(goog.dom.createElement(goog.dom.TagName.P)); |
| 195 |
div.appendChild(goog.dom.createElement(goog.dom.TagName.P)); |
| 196 |
return goog.dom.getElementsByTagName(goog.dom.TagName.DIV, div); |
| 197 |
})(); |
| 198 |
|
| 199 |
assertTrue(matchers.isArrayLike.matches([])); |
| 200 |
assertTrue(matchers.isArrayLike.matches(new Array())); |
| 201 |
assertTrue(matchers.isArrayLike.matches(nodeList)); |
| 202 |
assertFalse(matchers.isArrayLike.matches('test')); |
| 203 |
|
| 204 |
assertFalse(matchers.isArrayLike.matches(3, mockExpect)); |
| 205 |
assertEquals(1, mockExpect.errorMessages.length); |
| 206 |
assertEquals( |
| 207 |
'Expected: isArrayLike but was: <3> (Number)', |
| 208 |
mockExpect.errorMessages[0]); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
function testIsDateLike() { |
| 213 |
assertTrue(matchers.isDateLike.matches(new Date())); |
| 214 |
assertFalse(matchers.isDateLike.matches('test')); |
| 215 |
|
| 216 |
assertFalse(matchers.isDateLike.matches('test', mockExpect)); |
| 217 |
assertEquals(1, mockExpect.errorMessages.length); |
| 218 |
assertEquals( |
| 219 |
'Expected: isDateLike but was: <test> (String)', |
| 220 |
mockExpect.errorMessages[0]); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
function testIsString() { |
| 225 |
assertTrue(matchers.isString.matches('a')); |
| 226 |
assertTrue(matchers.isString.matches('b')); |
| 227 |
assertFalse(matchers.isString.matches(null)); |
| 228 |
|
| 229 |
assertFalse(matchers.isString.matches(null, mockExpect)); |
| 230 |
assertEquals(1, mockExpect.errorMessages.length); |
| 231 |
assertEquals( |
| 232 |
'Expected: isString but was: <null>', mockExpect.errorMessages[0]); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
function testIsBoolean() { |
| 237 |
assertTrue(matchers.isBoolean.matches(true)); |
| 238 |
assertTrue(matchers.isBoolean.matches(false)); |
| 239 |
assertFalse(matchers.isBoolean.matches(null)); |
| 240 |
|
| 241 |
assertFalse(matchers.isBoolean.matches([], mockExpect)); |
| 242 |
assertEquals(1, mockExpect.errorMessages.length); |
| 243 |
assertEquals( |
| 244 |
'Expected: isBoolean but was: <> (Array)', mockExpect.errorMessages[0]); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
function testIsNumber() { |
| 249 |
assertTrue(matchers.isNumber.matches(-1)); |
| 250 |
assertTrue(matchers.isNumber.matches(1)); |
| 251 |
assertTrue(matchers.isNumber.matches(1.25)); |
| 252 |
assertFalse(matchers.isNumber.matches(null)); |
| 253 |
|
| 254 |
assertFalse(matchers.isNumber.matches('hello', mockExpect)); |
| 255 |
assertEquals(1, mockExpect.errorMessages.length); |
| 256 |
assertEquals( |
| 257 |
'Expected: isNumber but was: <hello> (String)', |
| 258 |
mockExpect.errorMessages[0]); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
function testIsFunction() { |
| 263 |
assertTrue(matchers.isFunction.matches(function() {})); |
| 264 |
assertFalse(matchers.isFunction.matches('test')); |
| 265 |
|
| 266 |
assertFalse(matchers.isFunction.matches({}, mockExpect)); |
| 267 |
assertEquals(1, mockExpect.errorMessages.length); |
| 268 |
assertEquals( |
| 269 |
'Expected: isFunction but was: <[object Object]> (Object)', |
| 270 |
mockExpect.errorMessages[0]); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
function testIsObject() { |
| 275 |
assertTrue(matchers.isObject.matches({})); |
| 276 |
assertTrue(matchers.isObject.matches(new Object())); |
| 277 |
assertTrue(matchers.isObject.matches(new function() {})); |
| 278 |
assertTrue(matchers.isObject.matches([])); |
| 279 |
assertTrue(matchers.isObject.matches(new Array())); |
| 280 |
assertTrue(matchers.isObject.matches(function() {})); |
| 281 |
assertFalse(matchers.isObject.matches(null)); |
| 282 |
|
| 283 |
assertFalse(matchers.isObject.matches(1234, mockExpect)); |
| 284 |
assertEquals(1, mockExpect.errorMessages.length); |
| 285 |
assertEquals( |
| 286 |
'Expected: isObject but was: <1234> (Number)', |
| 287 |
mockExpect.errorMessages[0]); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
function testIsNodeLike() { |
| 292 |
assertFalse(matchers.isNodeLike.matches({})); |
| 293 |
assertFalse(matchers.isNodeLike.matches(1)); |
| 294 |
assertFalse(matchers.isNodeLike.matches(function() {})); |
| 295 |
assertFalse(matchers.isNodeLike.matches(false)); |
| 296 |
assertTrue(matchers.isNodeLike.matches(document.body)); |
| 297 |
assertTrue(matchers.isNodeLike.matches(goog.dom.getElement('someDiv'))); |
| 298 |
|
| 299 |
assertFalse(matchers.isNodeLike.matches('test', mockExpect)); |
| 300 |
assertEquals(1, mockExpect.errorMessages.length); |
| 301 |
assertEquals( |
| 302 |
'Expected: isNodeLike but was: <test> (String)', |
| 303 |
mockExpect.errorMessages[0]); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
function testIgnoreArgumentsMatcher() { |
| 308 |
// ignoreArgument always returns true: |
| 309 |
assertTrue(matchers.ignoreArgument.matches()); |
| 310 |
assertTrue(matchers.ignoreArgument.matches(356)); |
| 311 |
assertTrue(matchers.ignoreArgument.matches('str')); |
| 312 |
assertTrue(matchers.ignoreArgument.matches(['array', 123, false])); |
| 313 |
assertTrue(matchers.ignoreArgument.matches({'map': 1, key2: 'value2'})); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
function testFlexibleArrayMatcher() { |
| 318 |
// Test that basic lists are verified properly. |
| 319 |
var a1 = [1, 'test']; |
| 320 |
var a2 = [1, 'test']; |
| 321 |
var a3 = [1, 'test', 'extra']; |
| 322 |
assertTrue(matchers.flexibleArrayMatcher(a1, a2)); |
| 323 |
assertFalse(matchers.flexibleArrayMatcher(a1, a3)); |
| 324 |
|
| 325 |
// Test that basic lists with basic class instances are verified properly. |
| 326 |
var instance = new foo(); |
| 327 |
a1 = [1, 'test', instance]; |
| 328 |
a2 = [1, 'test', instance]; |
| 329 |
a3 = [1, 'test', new foo()]; |
| 330 |
assertTrue(matchers.flexibleArrayMatcher(a1, a2)); |
| 331 |
assertTrue(matchers.flexibleArrayMatcher(a1, a3)); |
| 332 |
|
| 333 |
// Create an argument verifier that returns a consistent value. |
| 334 |
var verifyValue = true; |
| 335 |
var argVerifier = function() {}; |
| 336 |
goog.inherits(argVerifier, matchers.ArgumentMatcher); |
| 337 |
argVerifier.prototype.matches = function(arg) { return verifyValue; }; |
| 338 |
|
| 339 |
// Test that the arguments are always verified when the verifier returns |
| 340 |
// true. |
| 341 |
a1 = [1, 'test', new argVerifier()]; |
| 342 |
a2 = [1, 'test', 'anything']; |
| 343 |
a3 = [1, 'test', 12345]; |
| 344 |
assertTrue(matchers.flexibleArrayMatcher(a1, a2)); |
| 345 |
assertTrue(matchers.flexibleArrayMatcher(a1, a3)); |
| 346 |
|
| 347 |
// Now test the case when then verifier returns false. |
| 348 |
verifyValue = false; |
| 349 |
assertFalse(matchers.flexibleArrayMatcher(a1, a2)); |
| 350 |
assertFalse(matchers.flexibleArrayMatcher(a1, a3)); |
| 351 |
|
| 352 |
// And test we report errors back up via the opt_expectation |
| 353 |
assertFalse(matchers.flexibleArrayMatcher(a2, a3, mockExpect)); |
| 354 |
assertEquals(1, mockExpect.errorMessages.length); |
| 355 |
assertEquals( |
| 356 |
'Expected <anything> (String) but was <12345> (Number)\n' + |
| 357 |
' Expected <anything> (String) but was <12345> (Number)', |
| 358 |
mockExpect.errorMessages[0]); |
| 359 |
|
| 360 |
// And test we report errors found via the matcher |
| 361 |
a1 = [1, goog.testing.mockmatchers.isString]; |
| 362 |
a2 = [1, 'test string']; |
| 363 |
a3 = [1, null]; |
| 364 |
assertTrue(matchers.flexibleArrayMatcher(a1, a2, mockExpect)); |
| 365 |
assertFalse(matchers.flexibleArrayMatcher(a1, a3, mockExpect)); |
| 366 |
// Old error is still there |
| 367 |
assertEquals(2, mockExpect.errorMessages.length); |
| 368 |
assertEquals( |
| 369 |
'Expected <anything> (String) but was <12345> (Number)\n' + |
| 370 |
' Expected <anything> (String) but was <12345> (Number)', |
| 371 |
mockExpect.errorMessages[0]); |
| 372 |
// plus the new error... |
| 373 |
assertEquals( |
| 374 |
'Expected: isString but was: <null>', mockExpect.errorMessages[1]); |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
/** |
| 379 |
* Utility method for checking for an ObjectEquals match failure. Checks that |
| 380 |
* the expected error message was included in the error messages appended to |
| 381 |
* the expectation object. |
| 382 |
* @param {goog.testing.mockmatchers.ArgumentMatcher.ObjectEquals} matcher |
| 383 |
* The matcher to test against. |
| 384 |
* @param {Object} matchObject The object to compare. |
| 385 |
* @param {string=} opt_errorMsg The deep object comparison failure message |
| 386 |
* to check for. |
| 387 |
*/ |
| 388 |
function expectObjectEqualsFailure(matcher, matchObject, opt_errorMsg) { |
| 389 |
mockExpect.errorMessages = []; |
| 390 |
assertFalse(matcher.matches(matchObject, mockExpect)); |
| 391 |
assertNotEquals(0, mockExpect.getErrorMessageCount()); |
| 392 |
if (opt_errorMsg) { |
| 393 |
assertContains(opt_errorMsg, mockExpect.errorMessages[0]); |
| 394 |
} |
| 395 |
} |