From f7d83a4a6683d325f00720da499e000f2d3dde40 Mon Sep 17 00:00:00 2001 From: Ben Worline Date: Tue, 9 May 2023 11:58:57 -0700 Subject: [PATCH] move expectSourceToContain, expectSourceToMatch, regexEscape to helper files --- .../import-export-format/index.js | 17 +++++------------ test/helpers/expectSource.js | 17 +++++++++++++++++ test/helpers/regexEscape.js | 3 +++ 3 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 test/helpers/expectSource.js create mode 100644 test/helpers/regexEscape.js diff --git a/test/configCases/code-generation/import-export-format/index.js b/test/configCases/code-generation/import-export-format/index.js index 8775e50f4..836850d59 100644 --- a/test/configCases/code-generation/import-export-format/index.js +++ b/test/configCases/code-generation/import-export-format/index.js @@ -6,17 +6,8 @@ import { baz as harmonyexport_harmonyimport_2 } from "./harmony-module-2"; import * as mod3 from "./harmony-module-3"; export { mod3 }; -// This is necessary because 'source' contains the code for this test file, which will always contain the string -// being tested for, so we have to use negative lookahead/lookbehind to exclude the actual testing code from the test. -function expectSourceToContain(source, str) { - expect(source).toMatch(new RegExp(`^.*?(? (/* reexport */ harmony_module_3_namespaceObject)"); diff --git a/test/helpers/expectSource.js b/test/helpers/expectSource.js new file mode 100644 index 000000000..583b16b90 --- /dev/null +++ b/test/helpers/expectSource.js @@ -0,0 +1,17 @@ +var regexEscape = require("./regexEscape.js"); + +// These expect* methods are necessary because 'source' contains the code for this test file, which will always contain the string +// being tested for, so we have to use the "DO NOT MATCH BELOW..." technique to exclude the actual testing code from the test. +// Place your jest 'expect' calls below a line containing the DO NOT MATCH BELOW... string constructed below. See other tests for examples. + +// Break up the match string so we don't match it in these expect* funtions either. +const doNotMatch = ["DO", "NOT", "MATCH", "BELOW", "THIS", "LINE"].join(" "); + +function expectSourceToContain(source, str) { + expect(source).toMatch(new RegExp(regexEscape(str) + ".*" + doNotMatch, "s")); +} +function expectSourceToMatch(source, regexStr) { + expect(source).toMatch(new RegExp(regexStr + ".*" + doNotMatch, "s")); +} + +module.exports = { expectSourceToContain, expectSourceToMatch }; diff --git a/test/helpers/regexEscape.js b/test/helpers/regexEscape.js new file mode 100644 index 000000000..11f4b6f00 --- /dev/null +++ b/test/helpers/regexEscape.js @@ -0,0 +1,3 @@ +module.exports = function regexEscape(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string +};