mindsbackyard / galvanic-test

A test environment for rust: test cases & suites, fixtures, and parameterised test cases
Apache License 2.0
52 stars 3 forks source link

Type parameters for fixtures #4

Open vorner opened 7 years ago

vorner commented 7 years ago

Hello

I don't know if it's possible and not documented or not currently supported, but it would be great if the type fixtures could be parametric not only over values, but over types. What I mean is some way of running the same test with u8, String and something else.

Or, currently, I wanted to run bunch of tests with different closures.

I have no idea if this is easy to support so I understand if this is too hard and won't be implemented.

mindsbackyard commented 7 years ago

First of all: no this is currently not supported. Abstracting test cases over types is an issue although only relevant in a few specific situations---I'd be interested in yours.

I've got something planned for such abstractions but I don't know if it would suit your situation. Let me explain. When you try to do something like that all the type need to adhere to a specific interface, e.g., some common method which exits on all the types. This should usually be expressed as a trait in Rust. The use case I've encountered is as a library developer. Let's say a library exposes some interface in the form of a trait. Often the library developer knows that some invariants have to hold for all implementations, e.g., adding something to a list and retrieving the item from the list again. For that use case I plan to add a test theory or protocol test feature where you can write a generic test case.

// defines the theory for all types implementing `TableIndex<usize,i32>`
pub fn get_on_empty_index_returns_err<T: TableIndex<usize,i32>>() {
 ....
}
// implements the theory for `DenseIndex<usize,i32>`
theory!(get_on_empty_index_returns_err for DenseIndex<usize,i32>);

Making a fixture generic is a completely separate issue. With the current setup I don't think that it can be done easily as you'd have to do some kind of type specialization inside of the macros to generate the test cases.

vorner commented 7 years ago

In the end, I worked around that in slightly different way: https://github.com/vorner/corona/blob/tls/tests/prelude_api.rs. I use the fact I can turn the closures into function pointers. But my first thought was to generalize the test over the closure type (which is an anonymous type) and have it called with each.

I think your abstraction would work there.