Skip to content

Commit

Permalink
Merge pull request #119 from webpack/bugfix/concat
Browse files Browse the repository at this point in the history
more fuzzy testing and bugfix
  • Loading branch information
sokra committed Jul 27, 2021
2 parents 657f0c0 + 20e5a85 commit 4f7b148
Show file tree
Hide file tree
Showing 5 changed files with 62,943 additions and 158,710 deletions.
2 changes: 1 addition & 1 deletion lib/ConcatSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class ConcatSource extends Source {
);
}
return {
generatedLine: currentLineOffset,
generatedLine: currentLineOffset + 1,
generatedColumn: currentColumnOffset,
source: finalSource ? code : undefined
};
Expand Down
14 changes: 6 additions & 8 deletions lib/helpers/createMappingsSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,12 @@ const createLinesOnlyMappingsSerializer = () => {
}
};
str += "A";
if (sourceIndex >= 0) {
lastWrittenLine = currentLine;
writeValue(sourceIndex - currentSourceIndex);
currentSourceIndex = sourceIndex;
writeValue(originalLine - currentOriginalLine);
currentOriginalLine = originalLine;
str += "A";
}
lastWrittenLine = currentLine;
writeValue(sourceIndex - currentSourceIndex);
currentSourceIndex = sourceIndex;
writeValue(originalLine - currentOriginalLine);
currentOriginalLine = originalLine;
str += "A";
return str;
};
};
Expand Down
99 changes: 75 additions & 24 deletions test/Snapshots.js → test/Fuzzy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const LOREM =

const LOREM_LINES = LOREM.replace(/(.{20,}?)\s/g, "$1\n");

describe("Snapshots", () => {
describe("Fuzzy", () => {
const variants = {
CompatSource: source => new CompatSource(source),
PrefixSource: source => new PrefixSource("lorem: ", source),
Expand All @@ -38,53 +38,86 @@ describe("Snapshots", () => {
const map = source.map();
return map
? new SourceMapSource(source.source(), "source-map.txt", source.map())
: new OriginalSource(source.source(), "origina.txt");
: new OriginalSource(source.source(), "original.txt");
},
SourceMapSourceInner: source => {
const code = source.source();
const replaceSource = new ReplaceSource(
new OriginalSource(code, "source-map.txt"),
"replaced.txt"
);
const input = source.source();
const regexp = /\w{6,}(\n\w{6,})?/g;
let match = regexp.exec(input);
while (match !== null) {
replaceSource.replace(
match.index,
match.index + match[0].length - 1,
match[0].length % 4 === 0 ? "XXX\n" : "XXX",
match[0].trim()
);
match = regexp.exec(input);
}
const sourceAndMap = replaceSource.sourceAndMap();

const map = source.map();
return map
? new SourceMapSource(
sourceAndMap.source,
"source-map.txt",
sourceAndMap.map,
code,
map,
true
)
: new SourceMapSource(
sourceAndMap.source,
"source-map.txt",
sourceAndMap.map
);
},
CachedSource: source => new CachedSource(source)
};
const variantsCount = Object.keys(variants).length;

const createTests = (keys, current) => {
for (const key of keys) {
const remainingKeys = keys.filter(k => k !== key);
// if (remainingKeys.length > 3) createTests(remainingKeys, current);
const withVariant = {};
const fn = variants[key];
for (const k of Object.keys(current)) {
const sourceFn = current[k];
withVariant[k] = () => fn(sourceFn());
}
describe(key, () => {
createTests(remainingKeys, withVariant);
});
}
if (keys.length === variantsCount - 3) {
const createTests = (remaining, snapshot, current) => {
if (remaining === 0) {
for (const key of Object.keys(current)) {
const sourceFn = current[key];
it(`${key} should return correct .source()`, () => {
const source = sourceFn();
const result = source.source();
expect(source.source()).toEqual(result);
expect(result).toMatchSnapshot();
if (snapshot) {
expect(result).toMatchSnapshot();
}
});
it(`${key} should return correct .size()`, () => {
const source = sourceFn();
const result = source.size();
expect(source.size()).toEqual(result);
expect(result).toMatchSnapshot();
if (snapshot) {
expect(result).toMatchSnapshot();
}
});
for (const options of [undefined, { columns: false }]) {
const o = JSON.stringify(options);
it(`${key} should return correct .map(${o})`, () => {
const source = sourceFn();
const result = withReadableMappings(source.map(options));
expect(withReadableMappings(source.map(options))).toEqual(result);
if (key.includes("original")) {
expect(result).toBeTruthy();
if (!result.sources.includes("lorem.txt"))
console.log(result.sources);
}
if (result) {
expect(result.mappings).toMatch(
/^[A-Za-z0-9+/]{1,10}((,|;+)[A-Za-z0-9+/]{1,10})*$/
);
}
expect(result).toMatchSnapshot();
if (snapshot) {
expect(result).toMatchSnapshot();
}
});
it(`${key} should return correct .sourceAndMap(${o})`, () => {
const source = sourceFn();
Expand All @@ -101,16 +134,34 @@ describe("Snapshots", () => {
const result2 = source.sourceAndMap(options);
result2.map = withReadableMappings(result.map);
expect(result).toEqual(result2);
expect(result).toMatchSnapshot();
if (snapshot) {
expect(result).toMatchSnapshot();
}
});
}
}
} else {
for (const key of Object.keys(variants)) {
const withVariant = {};
const fn = variants[key];
for (const k of Object.keys(current)) {
const sourceFn = current[k];
withVariant[k] = () => fn(sourceFn());
}
describe(key, () => {
createTests(remaining - 1, snapshot, withVariant);
});
}
}
};
createTests(Object.keys(variants), {
const base = {
raw: () => new RawSource(LOREM),
"raw lines": () => new RawSource(LOREM_LINES),
original: () => new OriginalSource(LOREM, "lorem.txt"),
"original lines": () => new OriginalSource(LOREM_LINES, "lorem.txt")
});
};
createTests(1, true, base);
createTests(2, true, base);
createTests(3, false, base);
createTests(4, false, base);
});

0 comments on commit 4f7b148

Please sign in to comment.