| 1 |
// Copyright 2012 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.labs.testing.objectMatcherTest'); |
| 16 |
goog.setTestOnly('goog.labs.testing.objectMatcherTest'); |
| 17 |
|
| 18 |
goog.require('goog.labs.testing.MatcherError'); |
| 19 |
/** @suppress {extraRequire} */ |
| 20 |
goog.require('goog.labs.testing.ObjectEqualsMatcher'); |
| 21 |
goog.require('goog.labs.testing.assertThat'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
|
| 24 |
function testAnyObject() { |
| 25 |
goog.labs.testing.assertThat({}, anyObject(), 'typeof {} == "object"'); |
| 26 |
assertMatcherError(function() { |
| 27 |
goog.labs.testing.assertThat(null, anyObject()); |
| 28 |
}, 'typeof null == "object"'); |
| 29 |
} |
| 30 |
|
| 31 |
function testObjectEquals() { |
| 32 |
var obj1 = {x: 1}; |
| 33 |
var obj2 = obj1; |
| 34 |
goog.labs.testing.assertThat(obj1, equalsObject(obj2), 'obj1 equals obj2'); |
| 35 |
|
| 36 |
assertMatcherError(function() { |
| 37 |
goog.labs.testing.assertThat({x: 1}, equalsObject({})); |
| 38 |
}, 'equalsObject does not throw exception on failure'); |
| 39 |
} |
| 40 |
|
| 41 |
function testInstanceOf() { |
| 42 |
function expected() { this.x = 1; } |
| 43 |
var input = new expected(); |
| 44 |
goog.labs.testing.assertThat( |
| 45 |
input, instanceOfClass(expected), 'input is an instance of expected'); |
| 46 |
|
| 47 |
assertMatcherError(function() { |
| 48 |
goog.labs.testing.assertThat(5, instanceOfClass(function() {})); |
| 49 |
}, 'instanceOfClass does not throw exception on failure'); |
| 50 |
} |
| 51 |
|
| 52 |
function testHasProperty() { |
| 53 |
goog.labs.testing.assertThat( |
| 54 |
{x: 1}, hasProperty('x'), '{x:1} has property x}'); |
| 55 |
|
| 56 |
assertMatcherError(function() { |
| 57 |
goog.labs.testing.assertThat({x: 1}, hasProperty('y')); |
| 58 |
}, 'hasProperty does not throw exception on failure'); |
| 59 |
} |
| 60 |
|
| 61 |
function testIsNull() { |
| 62 |
goog.labs.testing.assertThat(null, isNull(), 'null is null'); |
| 63 |
|
| 64 |
assertMatcherError(function() { |
| 65 |
goog.labs.testing.assertThat(5, isNull()); |
| 66 |
}, 'isNull does not throw exception on failure'); |
| 67 |
} |
| 68 |
|
| 69 |
function testIsNullOrUndefined() { |
| 70 |
var x; |
| 71 |
goog.labs.testing.assertThat( |
| 72 |
undefined, isNullOrUndefined(), 'undefined is null or undefined'); |
| 73 |
goog.labs.testing.assertThat( |
| 74 |
x, isNullOrUndefined(), 'undefined is null or undefined'); |
| 75 |
x = null; |
| 76 |
goog.labs.testing.assertThat( |
| 77 |
null, isNullOrUndefined(), 'null is null or undefined'); |
| 78 |
goog.labs.testing.assertThat( |
| 79 |
x, isNullOrUndefined(), 'null is null or undefined'); |
| 80 |
|
| 81 |
assertMatcherError(function() { |
| 82 |
goog.labs.testing.assertThat(5, isNullOrUndefined()); |
| 83 |
}, 'isNullOrUndefined does not throw exception on failure'); |
| 84 |
} |
| 85 |
|
| 86 |
function testIsUndefined() { |
| 87 |
var x; |
| 88 |
goog.labs.testing.assertThat( |
| 89 |
undefined, isUndefined(), 'undefined is undefined'); |
| 90 |
goog.labs.testing.assertThat(x, isUndefined(), 'undefined is undefined'); |
| 91 |
|
| 92 |
assertMatcherError(function() { |
| 93 |
goog.labs.testing.assertThat(5, isUndefined()); |
| 94 |
}, 'isUndefined does not throw exception on failure'); |
| 95 |
} |
| 96 |
|
| 97 |
function assertMatcherError(callable, errorString) { |
| 98 |
var e = assertThrows(errorString || 'callable throws exception', callable); |
| 99 |
assertTrue(e instanceof goog.labs.testing.MatcherError); |
| 100 |
} |