Skip to content

Commit

Permalink
Add support for generic traits (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Dec 13, 2023
1 parent 19cc238 commit 92d7902
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions mock-builder/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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 @@ -160,6 +166,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 @@ -187,6 +197,12 @@ mod tests {
}
}

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

#[test]
fn function_location() {
assert_eq!(
Expand Down Expand Up @@ -228,6 +244,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 @@ -255,6 +281,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

0 comments on commit 92d7902

Please sign in to comment.