1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 // Equality function based on isEqual in
8 // http://underscorejs.org
9 // (c) 2009-2013 Jeremy Ashkenas,
11 // and Investigative Reporters & Editors
12 // Underscore may be freely distributed under the MIT license.
14 function has(obj
, key
) {
15 return obj
.hasOwnProperty(key
);
17 function isFunction(obj
) {
18 return typeof obj
=== 'function';
20 function isArrayBufferClass(className
) {
21 return className
== '[object ArrayBuffer]' ||
22 className
.match(/\[object \w+\d+(Clamped)?Array\]/);
24 // Internal recursive comparison function for `isEqual`.
25 function eq(a
, b
, aStack
, bStack
) {
26 // Identical objects are equal. `0 === -0`, but they aren't identical.
27 // See the Harmony `egal` proposal:
28 // http://wiki.ecmascript.org/doku.php?id=harmony:egal.
30 return a
!== 0 || 1 / a
== 1 / b
;
31 // A strict comparison is necessary because `null == undefined`.
32 if (a
== null || b
== null)
34 // Compare `[[Class]]` names.
35 var className
= toString
.call(a
);
36 if (className
!= toString
.call(b
))
39 // Strings, numbers, dates, and booleans are compared by value.
40 case '[object String]':
41 // Primitives and their corresponding object wrappers are equivalent;
42 // thus, `"5"` is equivalent to `new String("5")`.
43 return a
== String(b
);
44 case '[object Number]':
45 // `NaN`s are equivalent, but non-reflexive. An `egal` comparison is
46 // performed for other numeric values.
47 return a
!= +a
? b
!= +b
: (a
== 0 ? 1 / a
== 1 / b
: a
== +b
);
49 case '[object Boolean]':
50 // Coerce dates and booleans to numeric primitive values. Dates are
51 // compared by their millisecond representations. Note that invalid
52 // dates with millisecond representations of `NaN` are not equivalent.
54 // RegExps are compared by their source patterns and flags.
55 case '[object RegExp]':
56 return a
.source
== b
.source
&&
57 a
.global
== b
.global
&&
58 a
.multiline
== b
.multiline
&&
59 a
.ignoreCase
== b
.ignoreCase
;
61 if (typeof a
!= 'object' || typeof b
!= 'object')
63 // Assume equality for cyclic structures. The algorithm for detecting
64 // cyclic structures is adapted from ES 5.1 section 15.12.3, abstract
66 var length
= aStack
.length
;
68 // Linear search. Performance is inversely proportional to the number of
69 // unique nested structures.
70 if (aStack
[length
] == a
)
71 return bStack
[length
] == b
;
73 // Objects with different constructors are not equivalent, but `Object`s
74 // from different frames are.
75 var aCtor
= a
.constructor, bCtor
= b
.constructor;
76 if (aCtor
!== bCtor
&& !(isFunction(aCtor
) && (aCtor
instanceof aCtor
) &&
77 isFunction(bCtor
) && (bCtor
instanceof bCtor
))
78 && ('constructor' in a
&& 'constructor' in b
)) {
81 // Add the first object to the stack of traversed objects.
84 var size
= 0, result
= true;
85 // Recursively compare objects and arrays.
86 if (className
== '[object Array]' || isArrayBufferClass(className
)) {
87 // Compare array lengths to determine if a deep comparison is necessary.
89 result
= size
== b
.length
;
91 // Deep compare the contents, ignoring non-numeric properties.
93 if (!(result
= eq(a
[size
], b
[size
], aStack
, bStack
)))
98 // Deep compare objects.
101 // Count the expected number of properties.
103 // Deep compare each member.
104 if (!(result
= has(b
, key
) && eq(a
[key
], b
[key
], aStack
, bStack
)))
108 // Ensure that both objects contain the same number of properties.
111 if (has(b
, key
) && !(size
--))
117 // Remove the first object from the stack of traversed objects.
123 function describe(subjects
) {
124 var descriptions
= [];
125 Object
.getOwnPropertyNames(subjects
).forEach(function(name
) {
126 if (name
=== "Description")
127 descriptions
.push(subjects
[name
]);
129 descriptions
.push(name
+ ": " + JSON
.stringify(subjects
[name
]));
131 return descriptions
.join(" ");
136 predicates
.toBe = function(actual
, expected
) {
138 "result": actual
=== expected
,
139 "message": describe({
141 "Expected": expected
,
146 predicates
.toEqual = function(actual
, expected
) {
148 "result": eq(actual
, expected
, [], []),
149 "message": describe({
151 "Expected": expected
,
156 predicates
.toBeDefined = function(actual
) {
158 "result": typeof actual
!== "undefined",
159 "message": describe({
161 "Description": "Expected a defined value",
166 predicates
.toBeUndefined = function(actual
) {
167 // Recall: undefined is just a global variable. :)
169 "result": typeof actual
=== "undefined",
170 "message": describe({
172 "Description": "Expected an undefined value",
177 predicates
.toBeNull = function(actual
) {
178 // Recall: typeof null === "object".
180 "result": actual
=== null,
181 "message": describe({
188 predicates
.toBeTruthy = function(actual
) {
191 "message": describe({
193 "Description": "Expected a truthy value",
198 predicates
.toBeFalsy = function(actual
) {
201 "message": describe({
203 "Description": "Expected a falsy value",
208 predicates
.toContain = function(actual
, element
) {
210 "result": (function () {
211 for (var i
= 0; i
< actual
.length
; ++i
) {
212 if (eq(actual
[i
], element
, [], []))
217 "message": describe({
224 predicates
.toBeLessThan = function(actual
, reference
) {
226 "result": actual
< reference
,
227 "message": describe({
229 "Reference": reference
,
234 predicates
.toBeGreaterThan = function(actual
, reference
) {
236 "result": actual
> reference
,
237 "message": describe({
239 "Reference": reference
,
244 predicates
.toThrow = function(actual
) {
246 "result": (function () {
247 if (!isFunction(actual
))
256 "message": "Expected function to throw",
260 function negate(predicate
) {
262 var outcome
= predicate
.apply(null, arguments
);
263 outcome
.result
= !outcome
.result
;
268 function check(predicate
) {
270 var outcome
= predicate
.apply(null, arguments
);
273 throw outcome
.message
;
277 function Condition(actual
) {
279 Object
.getOwnPropertyNames(predicates
).forEach(function(name
) {
280 var bound
= predicates
[name
].bind(null, actual
);
281 this[name
] = check(bound
);
282 this.not
[name
] = check(negate(bound
));
286 return function(actual
) {
287 return new Condition(actual
);