frondeus / test-case

Rust procedural macro attribute for adding test cases easily
MIT License
610 stars 38 forks source link

Specify type parameters #55

Closed l4l closed 2 years ago

l4l commented 3 years ago

It seems that test-case able to handle generic parameters, i.e.:

#[test_case(1i8; "xyz")]
fn my_test<T>(x: T)
where T: Debug
{
  println!("{:?}", x); 
}

but is it possible to specify a type parameter explicitly? That's in particular useful for cases, where you need to check conversion for specific trait, e.g.:

#[test_case(MyType { .. }; "xyz")]
fn my_test<T>(x: MyType)
where T: From<MyType>
{
  let _: T = x.into();
}
luke-biel commented 2 years ago

I'm sorry that this was left unattended for so long.

TBH, I don't really undestand this use case. T: From<M> doesn't really say anything about type T. Declaring my_test isn't forbiden as is because you are required to specify T at function call:

fn my_test<T>(x: MyType)
    where T: From<MyType>
{
    let _: T = x.into();
}

fn main() {
    my_test::<SomeType>();
}

Moreover, in this case validation of conversion from SomeType is done at compile time, no test_case needed.

test_case can't and won't provide your method with concrete implementations of T as it lacks information of what types implement Into<T>.

I'm gonna close this, but if you have extra insights feel free to reopen.