-
Notifications
You must be signed in to change notification settings - Fork 69
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
Support mocking multiple traits with same name #601
Conversation
mockall_derive/src/mock_trait.rs
Outdated
@@ -176,3 +174,45 @@ impl MockTrait { | |||
) | |||
} | |||
} | |||
|
|||
fn write_join_path( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be more idiomatic if this function allocated and returned a new String.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to avoid string allocations but then again, I couldn't figure out a way to know the required capacity ahead of time anyway, so even the current single String::new
approach keeps resizing it internally.
I've now updated the fn to return an owned string instead.
Ok(()) | ||
} | ||
|
||
fn hash_path_arguments(path: &Path) -> Option<u64> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any examples of a path that has path_args in segment that isn't the last?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have an example. This was done since I assumed processing the hash + concatenating the ident are symmetric. Whereas before both were done on the last segment, now we go over all the segments. Happy to revert this part to only process the last segment.
mockall_derive/src/mock_trait.rs
Outdated
// Similar to how the `format_ident!` constructs an `Ident`. | ||
// https://github.com/dtolnay/quote/blob/3256f897c9821f9b397cb81851ae9a1672a84cb7/src/runtime.rs#L478-L484 | ||
fn ident_maybe_raw(id: &str, span: Span) -> Ident { | ||
if let Some(id) = id.strip_prefix("r#") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch. Did you personally have a need to mock a trait with a raw identifier? This warrants a test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It caused the existing raw_identifier test to not compile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Now how about a CHANGELOG entry?
fn join_path_segments(path: &Path, sep: &str) -> String { | ||
let mut output = String::new(); | ||
for segment in &path.segments { | ||
if write!( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too bad there isn't something like a Punctuated::join
method to help with this part.
CHANGELOG.md
Outdated
@@ -3,6 +3,14 @@ | |||
All notable changes to this project will be documented in this file. | |||
This project adheres to [Semantic Versioning](http://semver.org/). | |||
|
|||
## [ unreleased ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## [ unreleased ] | |
## [ Unreleased ] - ReleaseDate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution!
An attempt to solve #600.
Not really sure about backward compatibility. Should be fine as we only change the generation of a private identifier. But it could cause issues if either a length limit is reached or a there's a name collision with a different ident.