Skip to content

Comments

rewrite some tests to use verifyProperty#4023

Merged
Ms2ger merged 1 commit intomainfrom
verify-property
Mar 25, 2024
Merged

rewrite some tests to use verifyProperty#4023
Ms2ger merged 1 commit intomainfrom
verify-property

Conversation

@bakkot
Copy link
Member

@bakkot bakkot commented Mar 23, 2024

Per #4017 (comment), this (automatically) rewrites a bunch of tests to use verifyProperty.

There's still ~400 of remaining uses of verifyNotEnumerable and friends, many of which could be rewritten automatically, but this gets almost all of the "name" and "length" ones migrated.

rewrite-test262.mjs
import fs from 'node:fs';
import path from 'node:path';

if (process.argv.length !== 3) {
  console.log('Usage: node rewrite-test262.mjs path-to-test262');
  process.exit(1);
}

let dir = process.argv[2];

let matches = 0;
for (let file of fs.readdirSync(dir, { recursive: true, withFileTypes: true })) {
  if (!file.isFile()) continue;
  let fname = path.join(file.parentPath, file.name);
  let content = fs.readFileSync(fname, 'utf8');

  let regexpSrc = String.raw`
assert.sameValue\( (?<obj>[a-zA-Z0-9.\[\]]+)\.(?<nameOrLength>name|length), (?<value>"[a-zA-Z0-9 .\[\]]*"|[0-9]+)(, "[^\n]+")? \);
verify(?<firstNot>Not)?(?<firstAttr>Enumerable|Writable|Configurable)\(\k<obj>, "\k<nameOrLength>"\);
verify(?<secondNot>Not)?(?<secondAttr>Enumerable|Writable|Configurable)\(\k<obj>, "\k<nameOrLength>"\);
verify(?<thirdNot>Not)?(?<thirdAttr>Enumerable|Writable|Configurable)\(\k<obj>, "\k<nameOrLength>"\);`
    .trim()
    .replace(/\s+/g, '\\s*')
    .replace(/"/g, '["\']');
  let regexp = new RegExp(regexpSrc);

  let match = regexp.exec(content);
  if (match) {
    let { groups } = match;
    let attrs = Object.fromEntries([
      [groups.firstAttr.toLowerCase(), !groups.firstNot],
      [groups.secondAttr.toLowerCase(), !groups.secondNot],
      [groups.thirdAttr.toLowerCase(), !groups.thirdNot],
    ]);
    if (JSON.stringify(Object.keys(attrs).sort()) !== JSON.stringify(['configurable', 'enumerable', 'writable'])) {
      throw new Error('malformed test ' + fname);
    }
    ++matches;
    let { value } = groups;
    if (value.startsWith("'") && value.endsWith("'") && !value.includes('"')) {
      value = '"' + value.slice(1, -1) + '"';
    }

    let replacement = `
verifyProperty(${groups.obj}, "${groups.nameOrLength}", {
  value: ${value},
  writable: ${attrs.writable},
  enumerable: ${attrs.enumerable},
  configurable: ${attrs.configurable}
});`.trim();
    content = content.slice(0, match.index) + replacement + content.slice(match.index + match[0].length);

    fs.writeFileSync(fname, content, 'utf8');
  }
}
console.log(`rewrote ${matches} files`);

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems good, i didn't check every file ofc

@Ms2ger Ms2ger merged commit 5424d7e into main Mar 25, 2024
@Ms2ger Ms2ger deleted the verify-property branch March 25, 2024 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants