Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 124 additions & 34 deletions test/test-ecma426-conformance.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
* Each test links the part of the spec it checks. There are three kinds:
*
* - `todo`: a gap. It fails today without failing the run. Drop `todo` once
* the library is fixed.
* the library is fixed. A todo made with `major()` waits for a major
* release, because the fix would change results for valid source maps,
* make maps that load today throw, or take away leniency that tools rely
* on: numbers read as strings, or a map with one stray comma keeping its
* mappings. Its reason says which, and the test runner prints it next to
* the test. The other todos change results only for invalid maps, without
* new throws.
* - guard: already conformant, kept to catch regressions.
* - lock-in: pins current non-conformant behaviour, so that fixing a gap
* fails a test as a reminder to drop the matching `todo`.
Expand Down Expand Up @@ -82,6 +88,12 @@ function decode(map, sourceMapURL) {
return mappings;
}

// Options for a todo test whose fix needs a major release. `reason` says
// why.
function major(reason) {
return { todo: 'needs a major release: ' + reason };
}

// For steps that "optionally report an error": the library may throw;
// otherwise `check` gets the result of `fn`.
function assertThrowsOr(fn, check) {
Expand Down Expand Up @@ -196,7 +208,9 @@ test('an ignoreList that is not an array is left out', () => {

test(
'null entries in `sources` should not be coerced to the string "null"',
{ todo: true },
major(
'null is a valid entry, so `sources` would hold null for valid maps, where callers expect only strings'
),
() => {
var map = new SourceMapConsumer({
version: 3,
Expand Down Expand Up @@ -242,7 +256,9 @@ test('current behaviour: null in sources is stringified (regression lock-in)', (

test(
'a `sources` entry that is not a string or null becomes null',
{ todo: true },
major(
'numbers in `sources` are read as strings on purpose; see "consuming names and sources that are numbers" in test-source-map-consumer.js'
),
() => {
assertThrowsOr(
() => new SourceMapConsumer(basicMap({ sources: [1] })).sources,
Expand All @@ -253,7 +269,9 @@ test(

test(
'a `names` entry that is not a string becomes the empty string',
{ todo: true },
major(
'numbers in `names` are read as strings on purpose; see "consuming names and sources that are numbers" in test-source-map-consumer.js'
),
() => {
assertThrowsOr(
() => decode(basicMap({ names: [1], mappings: 'AAAAA' })),
Expand All @@ -264,7 +282,9 @@ test(

test(
'a `sourcesContent` entry that is not a string or null becomes null',
{ todo: true },
major(
"an index map's sourceContentFor() would throw for such a source, where it returns the value today"
),
() => {
assertThrowsOr(
() =>
Expand All @@ -276,10 +296,48 @@ test(
}
);

test('a `file` that is not a string becomes null', { todo: true }, () => {
assertThrowsOr(
() => new SourceMapConsumer(basicMap({ file: 42 })).file,
(file) => assert.equal(file, null)
test(
'a `file` that is not a string becomes null',
major(
'applySourceMap() without a source file would throw for such a map, where it runs today'
),
() => {
assertThrowsOr(
() => new SourceMapConsumer(basicMap({ file: 42 })).file,
(file) => assert.equal(file, null)
);
}
);

// Lock in the current results for a non-string `sourcesContent` entry and
// `file`. Reading them as null, as the spec does, makes other calls throw for
// maps that work today, so it waits for a major release.
test('current behaviour: a `sourcesContent` entry that is not a string is kept (regression lock-in)', () => {
var map = basicMap({ sourcesContent: [1] });
var message =
'A non-string sourcesContent entry is no longer kept. Reading it as null ' +
"makes an index map's sourceContentFor() throw for that source, so it " +
'needs a major release. Then remove this lock-in and the major() above.';
assert.equal(new SourceMapConsumer(map).sourceContentFor('a.js'), 1, message);
assert.equal(
new SourceMapConsumer(
indexMap([{ offset: { line: 0, column: 0 }, map: map }])
).sourceContentFor('a.js'),
1,
message
);
});

test('current behaviour: a `file` that is not a string is kept (regression lock-in)', () => {
var consumer = new SourceMapConsumer(basicMap({ file: 42 }));
var message =
'A non-string file is no longer kept. Reading it as null makes ' +
'applySourceMap() without a source file throw, so it needs a major ' +
'release. Then remove this lock-in and the major() above.';
assert.equal(consumer.file, 42, message);
assert.doesNotThrow(
() => new SourceMapGenerator({ file: 'out.js' }).applySourceMap(consumer),
message
);
});

Expand All @@ -295,7 +353,9 @@ test('a `file` that is not a string becomes null', { todo: true }, () => {
// range above loses them silently: encode(2^31) is "A", which decodes to 0.
test(
'VLQ encode should throw on values outside [-2^31, 2^31 - 1]',
{ todo: true },
major(
'toJSON() and toString() would throw for mappings they write (wrongly) today'
),
() => {
assert.throws(() => base64VLQ.encode(Math.pow(2, 31)));
assert.throws(() => base64VLQ.encode(-Math.pow(2, 31) - 1));
Expand All @@ -306,7 +366,9 @@ test(
// the unsigned VLQ value: the magnitude shifted left, plus the sign bit.
test(
'VLQ decode should throw when the unsigned VLQ value is 2^32 or more',
{ todo: true },
major(
'the spec requires this error, so lookups would throw on maps that work today'
),
() => {
// 9 continuation digits and a terminator encode 2^44, far above 2^32.
var tooBig = 'gggggggggB';
Expand All @@ -315,11 +377,15 @@ test(
}
);

test('VLQ "negative zero" (B) decodes to -2^31', { todo: true }, () => {
var out = {};
base64VLQ.decode('B', 0, out);
assert.equal(out.value, -Math.pow(2, 31));
});
test(
'VLQ "negative zero" (B) decodes to -2^31',
major('maps that use B would decode to -2^31 instead of 0'),
() => {
var out = {};
base64VLQ.decode('B', 0, out);
assert.equal(out.value, -Math.pow(2, 31));
}
);

// Lock in the current result, so that changing it, which changes results for
// maps that decode today, is a deliberate decision.
Expand Down Expand Up @@ -364,9 +430,15 @@ test('a missing `mappings` is rejected', () => {
assert.throws(() => decode(map));
});

test('a `mappings` that is not a string is rejected', { todo: true }, () => {
assert.throws(() => decode(basicMap({ mappings: 42 })));
});
test(
'a `mappings` that is not a string is rejected',
major(
'the spec requires this error, so maps that load today (with no mappings) would throw'
),
() => {
assert.throws(() => decode(basicMap({ mappings: 42 })));
}
);

test('a missing `sources` is rejected', () => {
var map = basicMap({});
Expand Down Expand Up @@ -411,14 +483,26 @@ test('a character outside base64 is rejected', () => {
assertRejected(basicMap({ mappings: 'AA!A' }));
});

test('a segment with more than 5 fields is rejected', { todo: true }, () => {
assertRejected(basicMap({ mappings: 'AAAAAA' }));
});
test(
'a segment with more than 5 fields is rejected',
major(
'maps that work today, with the extra fields ignored, would throw or lose every mapping'
),
() => {
assertRejected(basicMap({ mappings: 'AAAAAA' }));
}
);

test('an empty segment is rejected', { todo: true }, () => {
assertRejected(basicMap({ mappings: 'A,,C' }));
assertRejected(basicMap({ mappings: 'A,' }));
});
test(
'an empty segment is rejected',
major(
'maps that work today, with empty segments such as a trailing comma skipped, would throw or lose every mapping'
),
() => {
assertRejected(basicMap({ mappings: 'A,,C' }));
assertRejected(basicMap({ mappings: 'A,' }));
}
);

// ---------------------------------------------------------------------------
// 6. Values out of range in a segment
Expand Down Expand Up @@ -489,15 +573,21 @@ test('sources are joined to `sourceRoot` and resolved against the map URL', () =
assert.equal(resolve('/root', '../a.js'), 'https://example.com/a.js');
});

test('URL parsing keeps "//" inside a source path', { todo: true }, () => {
function resolve(source) {
return new SourceMapConsumer(basicMap({ sources: [source] }), MAP_URL)
.sources[0];
test(
'URL parsing keeps "//" inside a source path',
major(
'`sources` and originalPositionFor().source would change for valid maps, and callers look sources up by those strings'
),
() => {
function resolve(source) {
return new SourceMapConsumer(basicMap({ sources: [source] }), MAP_URL)
.sources[0];
}
// Today both lose a slash: .../js/a/b.js and http://y.com/a/b.js.
assert.equal(resolve('a//b.js'), 'https://example.com/js/a//b.js');
assert.equal(resolve('http://y.com/a//b.js'), 'http://y.com/a//b.js');
}
// Today both lose a slash: .../js/a/b.js and http://y.com/a/b.js.
assert.equal(resolve('a//b.js'), 'https://example.com/js/a//b.js');
assert.equal(resolve('http://y.com/a//b.js'), 'http://y.com/a//b.js');
});
);

// ---------------------------------------------------------------------------
// 8. Unrecognized properties
Expand Down