1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
<!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.ObjectEqualsMatcher');
goog.require('goog.labs.testing.HasPropertyMatcher');
goog.require('goog.labs.testing.InstanceOfMatcher');
goog.require('goog.labs.testing.IsNullOrUndefinedMatcher');
goog.require('goog.labs.testing.IsNullMatcher');
goog.require('goog.labs.testing.IsUndefinedMatcher');
goog.require('goog.labs.testing.assertThat');
goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script>
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);
}
</script>
</body>
</html>
|