Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. 'use strict';var _require$plugins =
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. require('pretty-format').plugins;const ReactElement = _require$plugins.ReactElement,ReactTestComponent = _require$plugins.ReactTestComponent,AsymmetricMatcher = _require$plugins.AsymmetricMatcher,HTMLElement = _require$plugins.HTMLElement,Immutable = _require$plugins.Immutable; /**
  20. * Copyright (c) 2014, Facebook, Inc. All rights reserved.
  21. *
  22. * This source code is licensed under the BSD-style license found in the
  23. * LICENSE file in the root directory of this source tree. An additional grant
  24. * of patent rights can be found in the PATENTS file in the same directory.
  25. *
  26. *
  27. */const chalk = require('chalk');var _require = require('jest-matcher-utils');const getType = _require.getType;const prettyFormat = require('pretty-format');const diffStrings = require('./diffStrings');var _require2 = require('./constants');const NO_DIFF_MESSAGE = _require2.NO_DIFF_MESSAGE,SIMILAR_MESSAGE = _require2.SIMILAR_MESSAGE;
  28. const PLUGINS = [
  29. ReactTestComponent,
  30. ReactElement,
  31. AsymmetricMatcher,
  32. HTMLElement].
  33. concat(Immutable);
  34. const FORMAT_OPTIONS = {
  35. plugins: PLUGINS };
  36.  
  37. const FALLBACK_FORMAT_OPTIONS = {
  38. callToJSON: false,
  39. maxDepth: 10,
  40. plugins: PLUGINS };
  41.  
  42.  
  43. // Generate a string that will highlight the difference between two values
  44. // with green and red. (similar to how github does code diffing)
  45. function diff(a, b, options) {
  46. if (a === b) {
  47. return NO_DIFF_MESSAGE;
  48. }
  49.  
  50. const aType = getType(a);
  51. let expectedType = aType;
  52. let omitDifference = false;
  53. if (aType === 'object' && typeof a.asymmetricMatch === 'function') {
  54. if (a.$$typeof !== Symbol.for('jest.asymmetricMatcher')) {
  55. // Do not know expected type of user-defined asymmetric matcher.
  56. return null;
  57. }
  58. if (typeof a.getExpectedType !== 'function') {
  59. // For example, expect.anything() matches either null or undefined
  60. return null;
  61. }
  62. expectedType = a.getExpectedType();
  63. // Primitive types boolean and number omit difference below.
  64. // For example, omit difference for expect.stringMatching(regexp)
  65. omitDifference = expectedType === 'string';
  66. }
  67.  
  68. if (expectedType !== getType(b)) {
  69. return (
  70. ' Comparing two different types of values.' +
  71. ` Expected ${chalk.green(expectedType)} but ` +
  72. `received ${chalk.red(getType(b))}.`);
  73.  
  74. }
  75.  
  76. if (omitDifference) {
  77. return null;
  78. }
  79.  
  80. switch (aType) {
  81. case 'string':
  82. const multiline = a.match(/[\r\n]/) !== -1 && b.indexOf('\n') !== -1;
  83. if (multiline) {
  84. return diffStrings(String(a), String(b), options);
  85. }
  86. return null;
  87. case 'number':
  88. case 'boolean':
  89. return null;
  90. case 'map':
  91. return compareObjects(sortMap(a), sortMap(b), options);
  92. case 'set':
  93. return compareObjects(sortSet(a), sortSet(b), options);
  94. default:
  95. return compareObjects(a, b, options);}
  96.  
  97. }
  98.  
  99. function sortMap(map) {
  100. return new Map(Array.from(map.entries()).sort());
  101. }
  102.  
  103. function sortSet(set) {
  104. return new Set(Array.from(set.values()).sort());
  105. }
  106.  
  107. function compareObjects(a, b, options) {
  108. let diffMessage;
  109. let hasThrown = false;
  110.  
  111. try {
  112. diffMessage = diffStrings(
  113. prettyFormat(a, FORMAT_OPTIONS),
  114. prettyFormat(b, FORMAT_OPTIONS),
  115. options);
  116.  
  117. } catch (e) {
  118. hasThrown = true;
  119. }
  120.  
  121. // If the comparison yields no results, compare again but this time
  122. // without calling `toJSON`. It's also possible that toJSON might throw.
  123. if (!diffMessage || diffMessage === NO_DIFF_MESSAGE) {
  124. diffMessage = diffStrings(
  125. prettyFormat(a, FALLBACK_FORMAT_OPTIONS),
  126. prettyFormat(b, FALLBACK_FORMAT_OPTIONS),
  127. options);
  128.  
  129. if (diffMessage !== NO_DIFF_MESSAGE && !hasThrown) {
  130. diffMessage = SIMILAR_MESSAGE + '\n\n' + diffMessage;
  131. }
  132. }
  133.  
  134. return diffMessage;
  135. }
  136.  
  137. module.exports = diff;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement