toMatchText.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.toExpectedTextValues = toExpectedTextValues;
  6. exports.toMatchText = toMatchText;
  7. var _utils = require("playwright-core/lib/utils");
  8. var _util = require("../util");
  9. var _expect = require("./expect");
  10. var _matcherHint = require("./matcherHint");
  11. var _globals = require("../common/globals");
  12. /**
  13. * Copyright Microsoft Corporation. All rights reserved.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. async function toMatchText(matcherName, receiver, receiverType, query, expected, options = {}) {
  28. (0, _util.expectTypes)(receiver, [receiverType], matcherName);
  29. const matcherOptions = {
  30. isNot: this.isNot,
  31. promise: this.promise
  32. };
  33. if (!(typeof expected === 'string') && !(expected && typeof expected.test === 'function')) {
  34. throw new Error(this.utils.matcherErrorMessage((0, _matcherHint.matcherHint)(this, receiver, matcherName, receiver, expected, matcherOptions), `${this.utils.EXPECTED_COLOR('expected')} value must be a string or regular expression`, this.utils.printWithType('Expected', expected, this.utils.printExpected)));
  35. }
  36. const timeout = (0, _globals.currentExpectTimeout)(options);
  37. const {
  38. matches: pass,
  39. received,
  40. log,
  41. timedOut
  42. } = await query(!!this.isNot, timeout);
  43. const stringSubstring = options.matchSubstring ? 'substring' : 'string';
  44. const receivedString = received || '';
  45. const message = pass ? () => typeof expected === 'string' ? (0, _matcherHint.matcherHint)(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined) + `Expected ${stringSubstring}: not ${this.utils.printExpected(expected)}\n` + `Received string: ${(0, _expect.printReceivedStringContainExpectedSubstring)(receivedString, receivedString.indexOf(expected), expected.length)}` + (0, _util.callLogText)(log) : (0, _matcherHint.matcherHint)(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined) + `Expected pattern: not ${this.utils.printExpected(expected)}\n` + `Received string: ${(0, _expect.printReceivedStringContainExpectedResult)(receivedString, typeof expected.exec === 'function' ? expected.exec(receivedString) : null)}` + (0, _util.callLogText)(log) : () => {
  46. const labelExpected = `Expected ${typeof expected === 'string' ? stringSubstring : 'pattern'}`;
  47. const labelReceived = 'Received string';
  48. return (0, _matcherHint.matcherHint)(this, receiver, matcherName, 'locator', undefined, matcherOptions, timedOut ? timeout : undefined) + this.utils.printDiffOrStringify(expected, receivedString, labelExpected, labelReceived, this.expand !== false) + (0, _util.callLogText)(log);
  49. };
  50. return {
  51. name: matcherName,
  52. expected,
  53. message,
  54. pass,
  55. actual: received,
  56. log,
  57. timeout: timedOut ? timeout : undefined
  58. };
  59. }
  60. function toExpectedTextValues(items, options = {}) {
  61. return items.map(i => ({
  62. string: (0, _utils.isString)(i) ? i : undefined,
  63. regexSource: (0, _utils.isRegExp)(i) ? i.source : undefined,
  64. regexFlags: (0, _utils.isRegExp)(i) ? i.flags : undefined,
  65. matchSubstring: options.matchSubstring,
  66. ignoreCase: options.ignoreCase,
  67. normalizeWhiteSpace: options.normalizeWhiteSpace
  68. }));
  69. }