Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added/updated tests for matchAll #1

Merged
merged 1 commit into from
Mar 20, 2018

Conversation

peterwmwong
Copy link
Collaborator

Tests were updated and assume tc39/proposal-string-matchall#33 will be merged.

In an attempt to make it easier to see the coverage these tests provides, here's a version of the specification annotated with links to the related test from this PR: https://gist.github.com/peterwmwong/2a9d80d3533c0ab165bc8c675ac99e45 (Example below)

screen shot 2018-03-18 at 7 53 35 pm

I haven't had the honor of contributing to Test262, so I'm not sure if I've made some bad cross cutting decisions...

  • const/let over var
  • Using arrow function
  • Frontmatter features, I'm adding String.prototype.matchAll and Symbol.matchAll

@ljharb I totally defer to you as you're the expert, let me know whatever I can do to make these tests better!

Thanks!

PS - If you'd like to try out my in-progress V8 implementation, checkout my branch or download the pre-built MacOS X binary here (zip): https://gist.github.com/peterwmwong/e38b50689d7717870b2cff7b68574b87

…matchall], and %RegExpStringIteratorPrototype%

Tests were updated and assuming tc39/proposal-string-matchall#33 will be merged.
@peterwmwong peterwmwong requested a review from ljharb March 19, 2018 00:59
@@ -1,22 +0,0 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -1,36 +0,0 @@
// Copyright (C) 2018 Jordan Harband. All rights reserved.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -1,22 +0,0 @@
// Copyright (C) 2018 Jordan Harband. All rights reserved.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -1,26 +0,0 @@
// Copyright (C) 2018 Jordan Harband. All rights reserved.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -1,43 +0,0 @@
// Copyright (C) 2018 Jordan Harband. All rights reserved.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This test was removed as per changes from tc39/proposal-string-matchall#33

  • Given a string, @@matchall is no longer executed on the internally created
    RegExp.

Copy link
Owner

@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.

Thanks, this is amazing!

Definitely please use var and normal functions (and no destructuring); these tests should work in a fully polyfilled ES5 implementation as much as they can.

RegExp.prototype[Symbol.matchAll].call({}, '');
});
} finally {
Object.defineProperty(RegExp.prototype, Symbol.match, {
Copy link
Owner

Choose a reason for hiding this comment

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

Tests generally don’t need to clean up after themselves; each test is run in its own environment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Great! Removed: 7decf6a


assert.sameValue(RegExpStringIteratorProto.next.length, 0);

verifyNotEnumerable(RegExpStringIteratorProto.next, "length");
Copy link
Owner

Choose a reason for hiding this comment

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

We should try to use consistent quotes in this pr; probably single over double?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Absolutely, fixed: 0c1492d

const str = 'a*b';
const iter = regexp[Symbol.matchAll](str);

let { value, done } = iter.next();
Copy link
Owner

Choose a reason for hiding this comment

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

We should probably make a helper for working with iterator so; maybe giving it the iterator and an array of expected values (or an array of functions to test each value), and have it test everything? Seems like it would DRY up a lot of these tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, sounds good to me. Is the following kinda what you had in mind?

// harness/compareIterator.js
assert.compareIterator = function compareIterator(iter, validators) {
  var i = 0;
  var result = iter.next();
  while (result.done !== false) {
    validators[i](result.value);
    result = iter.next();
    i++;
  }
}

// harness/regExpUtils.js
function matchComparator(expectedEntries, expectedIndex, expectedInput) {
  return function(match) {
    assert.compareArray(match, expectedEntries);
    assert.sameValue(match.index, expectedIndex);
    assert.sameValue(match.input, expectedInput);
  }
}

// Example usage
assert.compareIterator(regexp[Symbol.matchAll](str), [
  matchComparator(['a'], 0, str),
  matchComparator(['b'], 2, str)
]);

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, that looks great! Perhaps also with something that fails if the iterator is not "done" precisely after validators.length iterations?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah yes! I have implemented and applied the helpers (with your suggested "done" check) in the latest commit: 74aac5b

@peterwmwong
Copy link
Collaborator Author

@ljharb I believe I've addressed your initial round of review comments:

  • Remove cleanup code (undo prototype modifications)
  • Consistent quoting, use single quotes
  • ES5-ify: removed arrrow functions, let/const, and destructuring
  • Added assert.compareIterator and matchValidator

... and am ready for another round of review!
Thank you!

Copy link
Owner

@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.

Thank you!!

return function(match) {
assert.compareArray(match, expectedEntries, 'Match entries');
assert.sameValue(match.index, expectedIndex, 'Match index');
assert.sameValue(match.input, expectedInput, 'Match input');
Copy link
Owner

Choose a reason for hiding this comment

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

This should also take an expectedGroups argument, since match will either lack a groups property, or have one that's a null object when named capture group syntax is used.

Copy link
Owner

Choose a reason for hiding this comment

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

I've rebased your branch and am going to merge it; so we can do that in a followup.

@ljharb ljharb merged commit ab1e265 into ljharb:matchall Mar 20, 2018
ljharb pushed a commit that referenced this pull request Oct 3, 2019
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.

2 participants