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

Fix replacement for multiple substitutions #76

Merged

Conversation

speckins
Copy link
Contributor

@speckins speckins commented Jul 4, 2018

Since SubstitutionContext#substitute! uses while/#sub! to replace '?' in
the selector, and the replacement text contains that character, e.g.,
"(?-mix:\d+)", if the selector requires more than one replacement, the
replaced text is matched by the next iteration.

# given this assertion
assert_select "input[name=?][value=?]", /item-\d+/, /\d+/

# first iteration
"input[name=\"(?-mix:item-\\d+)\"][value=?]"

# second iteration
"input[name=\"(\"(?-mix:\\d+)\"-mix:item-\\d+)\"][value=?]"

This commit uses #gsub to avoid replacing already substituted text.

Fixes #75.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@zzak
Copy link
Member

zzak commented May 27, 2021

@lastthyme Was there a reason you approved this? It doesn't seem like there has been any activity on this PR in nearly 3 years. 🤔

@speckins
Copy link
Contributor Author

@zzak What kind of activity is necessary?

@zzak
Copy link
Member

zzak commented May 28, 2021

@speckins Just wondering if you had some context I might be missing about this PR 🤔

@speckins
Copy link
Contributor Author

speckins commented May 28, 2021

Originally I submitted an issue (#75) before the PR, but it appears they've since been disabled for this repo.

The assert_select method accepts RegExp arguments, which replace placeholders ("?") in the selector. However, due to the implementation of the method which performs the replacement, it does not work correctly for multiple RegExp arguments. This is because the current implementation doesn't resume matching after the last replacement; it starts over at the beginning of the string, which means it will match any "?" in the substituted text of the previous iteration. And since RegExp#to_s always (I think) contain "?", this means that assert_select cannot be used with more than one RegExp. (The behavior is also demonstrated in the commit message in the first comment.)

irb> /\d+/.to_s
=> "(?-mix:\\d+)"

I see that master has been active. I could rebase if you'd like.

@zzak
Copy link
Member

zzak commented May 28, 2021

@speckins Yes please! Thank you for elaborating on the problem 🙇

Since SubstitutionContext#substitute! uses while/#sub! to replace '?' in
the selector, and the replacement text contains that character, e.g.,
"(?-mix:\\d+)", if the selector requires more than one replacement, the
replaced text is matched by the next iteration.

	# given this assertion
	assert_select "input[name=?][value=?]", /item-\d+/, /\d+/

	# first iteration
	"input[name=\"(?-mix:item-\\d+)\"][value=?]"

	# second iteration
	"input[name=\"(\"(?-mix:\\d+)\"-mix:item-\\d+)\"][value=?]"

This commit uses #gsub to avoid replacing already substituted text.

Fixes rails#75.
@speckins speckins force-pushed the issue-75-multiple-substitution-fix branch from 9510b67 to 117d284 Compare May 29, 2021 09:13
@speckins
Copy link
Contributor Author

@zzak - I rebased on the current master; tests pass.

@zzak
Copy link
Member

zzak commented May 31, 2021

Thanks @speckins!

TBH I'm not really sure the current state of this gem, perhaps @seanpdoyle or @rafaelfranca can enlighten me? 🙏

@rafaelfranca rafaelfranca merged commit f442cd2 into rails:master Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

SubstitutionContext#substitute! fails with multiple patterns
4 participants