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

MockBuilder: Add support for generic traits in Locations #15

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions mock-builder/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ impl FunctionLocation {
.strip_suffix('>')
.unwrap();

// Remove generic from trait name
let trait_name = trait_name
.split_once('<')
.map(|(fst, _)| fst)
.unwrap_or(trait_name);

(struct_path, Some(trait_name.to_owned()))
}
None => (path, None),
Expand Down Expand Up @@ -149,6 +155,10 @@ mod tests {
fn generic_method<A: Into<i32>>(_: impl Into<u32>) -> FunctionLocation;
}

trait TraitExampleGen<G1, G2> {
fn foo() -> FunctionLocation;
}

struct Example;

impl Example {
Expand Down Expand Up @@ -176,6 +186,12 @@ mod tests {
}
}

impl TraitExampleGen<u32, bool> for Example {
fn foo() -> FunctionLocation {
FunctionLocation::from(|| ())
}
}

#[test]
fn function_location() {
assert_eq!(
Expand Down Expand Up @@ -217,6 +233,16 @@ mod tests {
trait_info: None,
}
);

assert_eq!(
Example::foo(),
FunctionLocation {
location: format!(
"<{PREFIX}::Example as {PREFIX}::TraitExampleGen<u32, bool>>::foo"
),
trait_info: None,
}
);
}

#[test]
Expand Down Expand Up @@ -244,6 +270,14 @@ mod tests {
trait_info: Some("TraitExample".into()),
}
);

assert_eq!(
Example::foo().normalize(),
FunctionLocation {
location: format!("{PREFIX}::Example::foo"),
trait_info: Some("TraitExampleGen".into()),
}
);
}

#[test]
Expand Down
Loading