asymmetric_matchers.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.stringNotMatching = exports.stringMatching = exports.stringNotContaining = exports.stringContaining = exports.objectNotContaining = exports.objectContaining = exports.arrayNotContaining = exports.arrayContaining = exports.anything = exports.any = exports.AsymmetricMatcher = undefined;
  6. var _jasmine_utils = require('./jasmine_utils');
  7. var _utils = require('./utils');
  8. /**
  9. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  10. *
  11. * This source code is licensed under the MIT license found in the
  12. * LICENSE file in the root directory of this source tree.
  13. *
  14. *
  15. */
  16. class AsymmetricMatcher {
  17. constructor() {
  18. this.$$typeof = Symbol.for('jest.asymmetricMatcher');
  19. }
  20. }
  21. exports.AsymmetricMatcher = AsymmetricMatcher;
  22. class Any extends AsymmetricMatcher {
  23. constructor(sample) {
  24. super();
  25. if (typeof sample === 'undefined') {
  26. throw new TypeError(
  27. 'any() expects to be passed a constructor function. ' +
  28. 'Please pass one or use anything() to match any object.'
  29. );
  30. }
  31. this.sample = sample;
  32. }
  33. asymmetricMatch(other) {
  34. if (this.sample == String) {
  35. return typeof other == 'string' || other instanceof String;
  36. }
  37. if (this.sample == Number) {
  38. return typeof other == 'number' || other instanceof Number;
  39. }
  40. if (this.sample == Function) {
  41. return typeof other == 'function' || other instanceof Function;
  42. }
  43. if (this.sample == Object) {
  44. return typeof other == 'object';
  45. }
  46. if (this.sample == Boolean) {
  47. return typeof other == 'boolean';
  48. }
  49. return other instanceof this.sample;
  50. }
  51. toString() {
  52. return 'Any';
  53. }
  54. getExpectedType() {
  55. if (this.sample == String) {
  56. return 'string';
  57. }
  58. if (this.sample == Number) {
  59. return 'number';
  60. }
  61. if (this.sample == Function) {
  62. return 'function';
  63. }
  64. if (this.sample == Object) {
  65. return 'object';
  66. }
  67. if (this.sample == Boolean) {
  68. return 'boolean';
  69. }
  70. return (0, _jasmine_utils.fnNameFor)(this.sample);
  71. }
  72. toAsymmetricMatcher() {
  73. return 'Any<' + (0, _jasmine_utils.fnNameFor)(this.sample) + '>';
  74. }
  75. }
  76. class Anything extends AsymmetricMatcher {
  77. asymmetricMatch(other) {
  78. return !(0, _jasmine_utils.isUndefined)(other) && other !== null;
  79. }
  80. toString() {
  81. return 'Anything';
  82. }
  83. // No getExpectedType method, because it matches either null or undefined.
  84. toAsymmetricMatcher() {
  85. return 'Anything';
  86. }
  87. }
  88. class ArrayContaining extends AsymmetricMatcher {
  89. constructor(sample) {
  90. let inverse =
  91. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  92. super();
  93. this.sample = sample;
  94. this.inverse = inverse;
  95. }
  96. asymmetricMatch(other) {
  97. if (!Array.isArray(this.sample)) {
  98. throw new Error(
  99. `You must provide an array to ${this.toString()}, not '` +
  100. typeof this.sample +
  101. "'."
  102. );
  103. }
  104. const result =
  105. this.sample.length === 0 ||
  106. (Array.isArray(other) &&
  107. this.sample.every(item =>
  108. other.some(another => (0, _jasmine_utils.equals)(item, another))
  109. ));
  110. return this.inverse ? !result : result;
  111. }
  112. toString() {
  113. return `Array${this.inverse ? 'Not' : ''}Containing`;
  114. }
  115. getExpectedType() {
  116. return 'array';
  117. }
  118. }
  119. class ObjectContaining extends AsymmetricMatcher {
  120. constructor(sample) {
  121. let inverse =
  122. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  123. super();
  124. this.sample = sample;
  125. this.inverse = inverse;
  126. }
  127. asymmetricMatch(other) {
  128. if (typeof this.sample !== 'object') {
  129. throw new Error(
  130. `You must provide an object to ${this.toString()}, not '` +
  131. typeof this.sample +
  132. "'."
  133. );
  134. }
  135. if (this.inverse) {
  136. for (const property in this.sample) {
  137. if (
  138. (0, _jasmine_utils.hasProperty)(other, property) &&
  139. (0, _jasmine_utils.equals)(this.sample[property], other[property]) &&
  140. !(0, _utils.emptyObject)(this.sample[property]) &&
  141. !(0, _utils.emptyObject)(other[property])
  142. ) {
  143. return false;
  144. }
  145. }
  146. return true;
  147. } else {
  148. for (const property in this.sample) {
  149. if (
  150. !(0, _jasmine_utils.hasProperty)(other, property) ||
  151. !(0, _jasmine_utils.equals)(this.sample[property], other[property])
  152. ) {
  153. return false;
  154. }
  155. }
  156. return true;
  157. }
  158. }
  159. toString() {
  160. return `Object${this.inverse ? 'Not' : ''}Containing`;
  161. }
  162. getExpectedType() {
  163. return 'object';
  164. }
  165. }
  166. class StringContaining extends AsymmetricMatcher {
  167. constructor(sample) {
  168. let inverse =
  169. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  170. super();
  171. if (!(0, _jasmine_utils.isA)('String', sample)) {
  172. throw new Error('Expected is not a string');
  173. }
  174. this.sample = sample;
  175. this.inverse = inverse;
  176. }
  177. asymmetricMatch(other) {
  178. if (!(0, _jasmine_utils.isA)('String', other)) {
  179. throw new Error('Actual is not a string');
  180. }
  181. const result = other.includes(this.sample);
  182. return this.inverse ? !result : result;
  183. }
  184. toString() {
  185. return `String${this.inverse ? 'Not' : ''}Containing`;
  186. }
  187. getExpectedType() {
  188. return 'string';
  189. }
  190. }
  191. class StringMatching extends AsymmetricMatcher {
  192. constructor(sample) {
  193. let inverse =
  194. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  195. super();
  196. if (
  197. !(0, _jasmine_utils.isA)('String', sample) &&
  198. !(0, _jasmine_utils.isA)('RegExp', sample)
  199. ) {
  200. throw new Error('Expected is not a String or a RegExp');
  201. }
  202. this.sample = new RegExp(sample);
  203. this.inverse = inverse;
  204. }
  205. asymmetricMatch(other) {
  206. if (!(0, _jasmine_utils.isA)('String', other)) {
  207. throw new Error('Actual is not a string');
  208. }
  209. const result = this.sample.test(other);
  210. return this.inverse ? !result : result;
  211. }
  212. toString() {
  213. return `String${this.inverse ? 'Not' : ''}Matching`;
  214. }
  215. getExpectedType() {
  216. return 'string';
  217. }
  218. }
  219. const any = (exports.any = expectedObject => new Any(expectedObject));
  220. const anything = (exports.anything = () => new Anything());
  221. const arrayContaining = (exports.arrayContaining = sample =>
  222. new ArrayContaining(sample));
  223. const arrayNotContaining = (exports.arrayNotContaining = sample =>
  224. new ArrayContaining(sample, true));
  225. const objectContaining = (exports.objectContaining = sample =>
  226. new ObjectContaining(sample));
  227. const objectNotContaining = (exports.objectNotContaining = sample =>
  228. new ObjectContaining(sample, true));
  229. const stringContaining = (exports.stringContaining = expected =>
  230. new StringContaining(expected));
  231. const stringNotContaining = (exports.stringNotContaining = expected =>
  232. new StringContaining(expected, true));
  233. const stringMatching = (exports.stringMatching = expected =>
  234. new StringMatching(expected));
  235. const stringNotMatching = (exports.stringNotMatching = expected =>
  236. new StringMatching(expected, true));