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

Type parameters for fixtures #4

Open
vorner opened this issue Oct 17, 2017 · 2 comments
Open

Type parameters for fixtures #4

vorner opened this issue Oct 17, 2017 · 2 comments

Comments

@vorner
Copy link

vorner commented Oct 17, 2017

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
Copy link
Owner

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
Copy link
Author

vorner commented Oct 19, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants