Skip to content

Commit

Permalink
Add draft source map spec tests
Browse files Browse the repository at this point in the history
This commit adds a submodule for the draft source map spec test repo and adds a
new test file that runs each of the test cases. Some test cases that have known
failures are skipped, with a comment explaining why.
  • Loading branch information
takikawa committed Apr 27, 2024
1 parent 60adcb0 commit 3db03cf
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "test/source-map-tests"]
path = test/source-map-tests
url = https://github.com/takikawa/source-map-tests.git
1 change: 1 addition & 0 deletions test/source-map-tests
Submodule source-map-tests added at b28522
91 changes: 91 additions & 0 deletions test/test-spec-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* -*- Mode: js; js-indent-level: 2; -*- */
/*
* Copyright 2024 Mozilla Foundation and contributors
* Licensed under the New BSD license. See LICENSE or:
* http://opensource.org/licenses/BSD-3-Clause
*/

const fs = require('node:fs/promises');
const SourceMapConsumer =
require("../lib/source-map-consumer").SourceMapConsumer;

const sourceMapSpecTests = require("./source-map-tests/source-map-spec-tests.json");

async function readJSON(path) {
const file = await fs.open(require.resolve(path));
const json = JSON.parse(await file.readFile());
file.close();
return json;
}

// Known failures due to intentional implementation choices or due to bugs.
const skippedTests = [
// Versions are explicitly checked a bit loosely.
"versionNumericString",
// Stricter sources array checking isn't implemented.
"sourcesNotStringOrNull",
"sourcesAndSourcesContentBothNull",
// Stricter names array checking isn't implemented.
"namesMissing",
"namesNotString",
// This check isn't as strict in this library.
"invalidMappingNotAString1",
// A mapping segment with no fields is technically invalid in the spec.
"invalidMappingSegmentWithZeroFields",
// These tests fail due to imprecision in the spec about the 32-bit limit.
"invalidMappingSegmentWithColumnExceeding32Bits",
"invalidMappingSegmentWithOriginalLineExceeding32Bits",
"invalidMappingSegmentWithOriginalColumnExceeding32Bits",
// A large VLQ that should parse, but currently does not.
"validMappingLargeVLQ",
// The library currently doesn't check the types of offset lines/columns.
"indexMapOffsetLineWrongType",
"indexMapOffsetColumnWrongType",
// The spec is not totally clear about this case.
"indexMapInvalidBaseMappings",
// The spec's definition of overlap can be refined
"indexMapInvalidOverlap",
// Not clear if this test makes sense, but spec isn't clear on behavior
"validMappingNullSources"
]

async function testMappingAction(assert, rawSourceMap, action) {
return SourceMapConsumer.with(rawSourceMap, null, (consumer) => {
let mappedPosition = consumer.generatedPositionFor({
source: action.originalSource,
line: action.originalLine + 1,
column: action.originalColumn
});

assert.equal(mappedPosition.line, action.generatedLine + 1, `generated line didn't match, expected ${action.generatedLine + 1} got ${mappedPosition.line}`);

Check failure on line 60 in test/test-spec-tests.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 160. Maximum allowed is 120
assert.equal(mappedPosition.column, action.generatedColumn, `generated column didn't match, expected ${action.generatedColumn} got ${mappedPosition.column}`);

Check failure on line 61 in test/test-spec-tests.js

View workflow job for this annotation

GitHub Actions / lint

This line has a length of 162. Maximum allowed is 120
});
}

for (let testCase of sourceMapSpecTests.tests) {
if (skippedTests.includes(testCase.name))
continue;
exports[`test from source map spec tests, name: ${testCase.name}`] =
async function (assert) {
const json = await readJSON(`./source-map-tests/resources/${testCase.sourceMapFile}`);
let sourceMapFailed = false;

Check failure on line 71 in test/test-spec-tests.js

View workflow job for this annotation

GitHub Actions / lint

'sourceMapFailed' is assigned a value but never used
try {
const map = await new SourceMapConsumer(json);
map.eachMapping(() => {});
map.destroy();
} catch (exn) {
if (testCase.sourceMapIsValid)
assert.fail("Expected valid source map but failed to load successfully: " + exn.message);
return;
}
if (!testCase.sourceMapIsValid)
assert.fail("Expected invalid source map but loaded successfully");
if (testCase.testActions) {
for (let testAction of testCase.testActions) {
if (testAction.actionType == "checkMapping") {
await testMappingAction(assert, json, testAction);
}
}
}
};
};

0 comments on commit 3db03cf

Please sign in to comment.