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

Add stream matchers. #532

Merged
merged 2 commits into from Feb 7, 2017
Merged

Add stream matchers. #532

merged 2 commits into from Feb 7, 2017

Conversation

nex3
Copy link
Member

@nex3 nex3 commented Feb 6, 2017

No description provided.

Copy link
Member

@munificent munificent left a comment

Choose a reason for hiding this comment

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

Couple small things then 👍

/// If any matchers match the stream, no errors from other matchers are thrown.
/// If no matchers match and multiple matchers threw errors, the first error is
/// re-thrown.
StreamMatcher emitsAnyOf(Iterable matchers) {
Copy link
Member

Choose a reason for hiding this comment

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

Error if the iterable is empty?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

try {
result = await streamMatchers[i].matchQueue(copy);
} catch (error, stackTrace) {
if (error != null) {
Copy link
Member

Choose a reason for hiding this comment

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

error should never be null. Is this case needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, this was supposed to check firstError.

}

/// Returns a [StreamMatcher] that matches any number of
/// events followed by events that match [matcher].
Copy link
Member

Choose a reason for hiding this comment

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

Nit: The wrapping here looks arbitrary.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

while (await queue.hasNext) {
if (await tryHere()) return null;
await queue.next;
}
Copy link
Member

Choose a reason for hiding this comment

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

This function is quadratic, right? Or I guess, more specifically O(mn) where m is the stream length and n is the matcher's length.

That's OK, but it would probably be good to mention it since it can get slow with long streams and matchers.

Or you could adapt Boyer-Moore to this. 😛

Copy link
Member Author

@nex3 nex3 Feb 7, 2017

Choose a reason for hiding this comment

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

The quadradic behavior is only relevant when the matcher can match long strings of events and when in practice it matches many long prefixes without satisfying the entire matcher. I don't think that's likely enough to come up in practice to warrant documenting.

@nex3 nex3 merged commit 192b6c4 into master Feb 7, 2017
@nex3 nex3 deleted the stream-matchers branch February 7, 2017 23:06
@brianegan
Copy link

Late to the party, but yay! Nice work @nex3!

@nex3 nex3 mentioned this pull request May 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants