Skip to content

Commit

Permalink
test: Allow multiple rounds of matching in tests
Browse files Browse the repository at this point in the history
This is to match what the real CLI does. Previously this part of
the Fixer was missing, which was not needed for any test cases
we have currently, but will be needed in my next commit.

Also drop needless '/g' from replace.js test, which was not needed
and set a bad example since none of the real patterns use the global
flag.
  • Loading branch information
Krinkle committed Oct 17, 2019
1 parent a71dbb0 commit 6e38465
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
4 changes: 2 additions & 2 deletions test/index.js
Expand Up @@ -8,8 +8,8 @@ async function main () {
require('./diff'),
require('./content'),
require('./replace'),
require('./patterns'),
require('./fixer')
require('./fixer'),
require('./patterns')
]) {
if (typeof fns === 'function') {
fns = { [fns.name]: fns };
Expand Down
27 changes: 18 additions & 9 deletions test/patterns.js
Expand Up @@ -12,15 +12,24 @@ function applyPatterns (lines) {
mainpagename: 'Main_Page'
};
patterns.forEach(function (pattern) {
var matchAllPattern = Object.create(pattern);
matchAllPattern.regex = new RegExp(
pattern.regex.source,
pattern.regex.flags.replace(/g/g, '') + 'g'
);
output.forEach(function (line, i) {
if (line !== null) {
output[i] = replace(line, matchAllPattern, siteinfo);
}
output.forEach(function (input, i) {
let result = input;
// Emulate what fixer.js does:
// Create a clone of the pattern and match multiple times until it no longer changes
do {
if (result === null) {
break;
}
const clonePattern = Object.create(pattern);
clonePattern.regex = new RegExp(
pattern.regex.source,
pattern.regex.flags
);
input = result;
result = replace(input, clonePattern, siteinfo);
} while (input !== result);

output[i] = result;
});
});
output = output.filter(function (line) {
Expand Down
10 changes: 5 additions & 5 deletions test/replace.js
Expand Up @@ -9,27 +9,27 @@ module.exports = function testReplace () {
};
var i = 1;
var cases = [ {
regex: /foo/g,
regex: /foo/,
replacement: 'bar',
input: 'input',
expected: 'input'
}, {
regex: /foo/g,
regex: /foo/,
replacement: 'bar',
input: 'input foos and bars',
expected: 'input bars and bars'
}, {
regex: /two/g,
regex: /two/,
replacement: '',
input: 'one two three',
expected: 'one three'
}, {
regex: /two/g,
regex: /two/,
replacement: '<tourbot-rm-blank>',
input: 'two',
expected: null
}, {
regex: /two/g,
regex: /two/,
replacement: '<tourbot-rm-blank>',
input: 'one two',
expected: 'one '
Expand Down

0 comments on commit 6e38465

Please sign in to comment.