| Crates.io | arg_ripper |
| lib.rs | arg_ripper |
| version | 0.1.0 |
| created_at | 2023-06-17 00:12:29.89198+00 |
| updated_at | 2023-06-17 00:12:29.89198+00 |
| description | A proc macro for pulling local variables out of functions as arguments |
| homepage | https://gitlab.com/yocally/arg_ripper |
| repository | https://gitlab.com/yocally/arg_ripper |
| max_upload_size | |
| id | 892630 |
| size | 46,222 |
arg_ripper is a macro that allows for easy Dependency Injection for the purposes of mocking
internal variables in functions. It contains a single macro, rip that takes in a list of
local bindings (let statements) that occur within the annotated function, and generates a new
function with those as arguments instead of local bindings.
#[rip(inner: i32)]
fn my_func() -> i32 {
let inner = 42;
inner
}
fn main() {
assert_eq!(my_func(), 42);
assert_eq!(ripped_my_func(69), 69);
}
The key feature that makes this useful for unit testing is the ability to change the type of
the argument you are ripping. Note the the type hint on line 1 of the example above, in this
case it's the same as the original version of inner, but it doesn't have to be. The only
restriction is that the new code must still compile, making things like this possible:
#[rip(answer: &str)]
fn print_answer() {
let answer: i32 = 42;
println!("{answer}");
}
fn main() {
print_answer();
ripped_print_answer("Fourty Two");
}
The repository has more in depth examples of how to use this with mockall if you're curious.