jasmine_utils.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.equals = equals;
  6. exports.isA = isA;
  7. exports.fnNameFor = fnNameFor;
  8. exports.isUndefined = isUndefined;
  9. exports.hasProperty = hasProperty;
  10. exports.isImmutableUnorderedKeyed = isImmutableUnorderedKeyed;
  11. exports.isImmutableUnorderedSet = isImmutableUnorderedSet;
  12. // Extracted out of jasmine 2.5.2
  13. function equals(a, b, customTesters, strictCheck) {
  14. customTesters = customTesters || [];
  15. return eq(a, b, [], [], customTesters, strictCheck ? hasKey : hasDefinedKey);
  16. } /*
  17. Copyright (c) 2008-2016 Pivotal Labs
  18. Permission is hereby granted, free of charge, to any person obtaining
  19. a copy of this software and associated documentation files (the
  20. "Software"), to deal in the Software without restriction, including
  21. without limitation the rights to use, copy, modify, merge, publish,
  22. distribute, sublicense, and/or sell copies of the Software, and to
  23. permit persons to whom the Software is furnished to do so, subject to
  24. the following conditions:
  25. The above copyright notice and this permission notice shall be
  26. included in all copies or substantial portions of the Software.
  27. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. */
  35. /* eslint-disable */
  36. function isAsymmetric(obj) {
  37. return obj && isA('Function', obj.asymmetricMatch);
  38. }
  39. function asymmetricMatch(a, b) {
  40. var asymmetricA = isAsymmetric(a),
  41. asymmetricB = isAsymmetric(b);
  42. if (asymmetricA && asymmetricB) {
  43. return undefined;
  44. }
  45. if (asymmetricA) {
  46. return a.asymmetricMatch(b);
  47. }
  48. if (asymmetricB) {
  49. return b.asymmetricMatch(a);
  50. }
  51. }
  52. // Equality function lovingly adapted from isEqual in
  53. // [Underscore](http://underscorejs.org)
  54. function eq(a, b, aStack, bStack, customTesters, hasKey) {
  55. var result = true;
  56. var asymmetricResult = asymmetricMatch(a, b);
  57. if (asymmetricResult !== undefined) {
  58. return asymmetricResult;
  59. }
  60. for (var i = 0; i < customTesters.length; i++) {
  61. var customTesterResult = customTesters[i](a, b);
  62. if (customTesterResult !== undefined) {
  63. return customTesterResult;
  64. }
  65. }
  66. if (a instanceof Error && b instanceof Error) {
  67. return a.message == b.message;
  68. }
  69. if (Object.is(a, b)) {
  70. return true;
  71. }
  72. // A strict comparison is necessary because `null == undefined`.
  73. if (a === null || b === null) {
  74. return a === b;
  75. }
  76. var className = Object.prototype.toString.call(a);
  77. if (className != Object.prototype.toString.call(b)) {
  78. return false;
  79. }
  80. switch (className) {
  81. // Strings, numbers, dates, and booleans are compared by value.
  82. case '[object String]':
  83. // Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
  84. // equivalent to `new String("5")`.
  85. return a == String(b);
  86. case '[object Number]':
  87. // `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
  88. // other numeric values.
  89. return a != +a ? b != +b : a === 0 ? 1 / a == 1 / b : a == +b;
  90. case '[object Date]':
  91. case '[object Boolean]':
  92. // Coerce dates and booleans to numeric primitive values. Dates are compared by their
  93. // millisecond representations. Note that invalid dates with millisecond representations
  94. // of `NaN` are not equivalent.
  95. return +a == +b;
  96. // RegExps are compared by their source patterns and flags.
  97. case '[object RegExp]':
  98. return (
  99. a.source == b.source &&
  100. a.global == b.global &&
  101. a.multiline == b.multiline &&
  102. a.ignoreCase == b.ignoreCase
  103. );
  104. }
  105. if (typeof a != 'object' || typeof b != 'object') {
  106. return false;
  107. }
  108. var aIsDomNode = isDomNode(a);
  109. var bIsDomNode = isDomNode(b);
  110. if (aIsDomNode && bIsDomNode) {
  111. // At first try to use DOM3 method isEqualNode
  112. if (a.isEqualNode) {
  113. return a.isEqualNode(b);
  114. }
  115. // IE8 doesn't support isEqualNode, try to use outerHTML && innerText
  116. var aIsElement = a instanceof Element;
  117. var bIsElement = b instanceof Element;
  118. if (aIsElement && bIsElement) {
  119. return a.outerHTML == b.outerHTML;
  120. }
  121. if (aIsElement || bIsElement) {
  122. return false;
  123. }
  124. return a.innerText == b.innerText && a.textContent == b.textContent;
  125. }
  126. if (aIsDomNode || bIsDomNode) {
  127. return false;
  128. }
  129. // Assume equality for cyclic structures. The algorithm for detecting cyclic
  130. // structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
  131. var length = aStack.length;
  132. while (length--) {
  133. // Linear search. Performance is inversely proportional to the number of
  134. // unique nested structures.
  135. if (aStack[length] == a) {
  136. return bStack[length] == b;
  137. }
  138. }
  139. // Add the first object to the stack of traversed objects.
  140. aStack.push(a);
  141. bStack.push(b);
  142. var size = 0;
  143. // Recursively compare objects and arrays.
  144. // Compare array lengths to determine if a deep comparison is necessary.
  145. if (className == '[object Array]') {
  146. size = a.length;
  147. if (size !== b.length) {
  148. return false;
  149. }
  150. while (size--) {
  151. result = eq(a[size], b[size], aStack, bStack, customTesters, hasKey);
  152. if (!result) {
  153. return false;
  154. }
  155. }
  156. }
  157. // Deep compare objects.
  158. var aKeys = keys(a, className == '[object Array]', hasKey),
  159. key;
  160. size = aKeys.length;
  161. // Ensure that both objects contain the same number of properties before comparing deep equality.
  162. if (keys(b, className == '[object Array]', hasKey).length !== size) {
  163. return false;
  164. }
  165. while (size--) {
  166. key = aKeys[size];
  167. // Deep compare each member
  168. result =
  169. hasKey(b, key) &&
  170. eq(a[key], b[key], aStack, bStack, customTesters, hasKey);
  171. if (!result) {
  172. return false;
  173. }
  174. }
  175. // Remove the first object from the stack of traversed objects.
  176. aStack.pop();
  177. bStack.pop();
  178. return result;
  179. }
  180. function keys(obj, isArray, hasKey) {
  181. var allKeys = (function(o) {
  182. var keys = [];
  183. for (var key in o) {
  184. if (hasKey(o, key)) {
  185. keys.push(key);
  186. }
  187. }
  188. return keys.concat(
  189. Object.getOwnPropertySymbols(o).filter(
  190. //$FlowFixMe Jest complains about nullability, but we know for sure that property 'symbol' does exist.
  191. symbol => Object.getOwnPropertyDescriptor(o, symbol).enumerable
  192. )
  193. );
  194. })(obj);
  195. if (!isArray) {
  196. return allKeys;
  197. }
  198. var extraKeys = [];
  199. if (allKeys.length === 0) {
  200. return allKeys;
  201. }
  202. for (var x = 0; x < allKeys.length; x++) {
  203. //$FlowFixMe
  204. if (typeof allKeys[x] === 'symbol' || !allKeys[x].match(/^[0-9]+$/)) {
  205. extraKeys.push(allKeys[x]);
  206. }
  207. }
  208. return extraKeys;
  209. }
  210. function hasDefinedKey(obj, key) {
  211. return hasKey(obj, key) && obj[key] !== undefined;
  212. }
  213. function hasKey(obj, key) {
  214. return Object.prototype.hasOwnProperty.call(obj, key);
  215. }
  216. function isA(typeName, value) {
  217. return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
  218. }
  219. function isDomNode(obj) {
  220. return (
  221. obj !== null &&
  222. typeof obj === 'object' &&
  223. typeof obj.nodeType === 'number' &&
  224. typeof obj.nodeName === 'string'
  225. );
  226. }
  227. function fnNameFor(func) {
  228. if (func.name) {
  229. return func.name;
  230. }
  231. const matches = func.toString().match(/^\s*function\s*(\w*)\s*\(/);
  232. return matches ? matches[1] : '<anonymous>';
  233. }
  234. function isUndefined(obj) {
  235. return obj === void 0;
  236. }
  237. function getPrototype(obj) {
  238. if (Object.getPrototypeOf) {
  239. return Object.getPrototypeOf(obj);
  240. }
  241. if (obj.constructor.prototype == obj) {
  242. return null;
  243. }
  244. return obj.constructor.prototype;
  245. }
  246. function hasProperty(obj, property) {
  247. if (!obj) {
  248. return false;
  249. }
  250. if (Object.prototype.hasOwnProperty.call(obj, property)) {
  251. return true;
  252. }
  253. return hasProperty(getPrototype(obj), property);
  254. }
  255. // SENTINEL constants are from https://github.com/facebook/immutable-js
  256. const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
  257. const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
  258. const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';
  259. function isImmutableUnorderedKeyed(maybeKeyed) {
  260. return !!(
  261. maybeKeyed &&
  262. maybeKeyed[IS_KEYED_SENTINEL] &&
  263. !maybeKeyed[IS_ORDERED_SENTINEL]
  264. );
  265. }
  266. function isImmutableUnorderedSet(maybeSet) {
  267. return !!(
  268. maybeSet &&
  269. maybeSet[IS_SET_SENTINEL] &&
  270. !maybeSet[IS_ORDERED_SENTINEL]
  271. );
  272. }