Comments (4)
That's exactly what I were pasting right now 😄
Yesterday I saw something in divan
crate documentation that ring me a bell... So maybe I can introduce the following syntax:
#[rstest]
#[case(Eps1eMinus7, 0.50000001f64, 0.5, true)]
#[case(Eps1eMinus9, 0.50000001f64, 0.5, false)]
fn test_almost_equal<#[case] E: ConstEps>(
#[case] left: f64,
#[case] right: f64,
#[case] expect: bool,
) {
let result = almost_equal::<E>(left, right);
assert_eq!(result, expect);
}
The sad news is that I've no time to implement it .... 😢
from rstest.
Yes you can but you you should just use values of different types. In your example you cannot build MyStruct
without set all its inner values that constrains the generic types. I'll try to fill your example but ...
MyEnumType
should be a trait and not an enum... I remove it as generic typeconst
generic argument are not supported yet
use rstest::rstest;
struct MyStruct<T, const U: usize> {
a: [T; U],
}
impl<T, const U: usize> MyStruct<T, U> {
const u: usize = U;
fn do_something(&self) -> usize {
U + 1
}
}
#[rstest]
#[case(1_f64)]
fn test_do_something<T: Copy>(#[case] t: T) {
let a = MyStruct { a: [t; 5] };
assert!(6 == a.do_something());
}
If you can provide to me some more concrete examples I'll happy to help you. I can think to introduce something to define a const
argument but I would like some examples were that is really necessary.
from rstest.
I have the following use case of specifying generics explicitly:
I have a trait that defines a constant epsilon value on the type level:
pub trait ConstEps {
fn eps() -> f64;
}
then I have a function that does some calculation and uses a generic epsilon value for numerical precision, for example:
fn almost_equal<E: ConstEps>(left: f64, right: f64) -> bool {
(right-left).abs() < E::eps()
}
I can have now many different epsilon types like:
struct Eps1eMinus7;
impl ConstEps for Eps1eMinus7 {
fn eps() -> f64 {
1e-7
}
}
struct Eps1eMinus8;
impl ConstEps for Eps1eMinus8 {
fn eps() -> f64 {
1e-8
}
}
struct Eps1eMinus9;
impl ConstEps for Eps1eMinus9 {
fn eps() -> f64 {
1e-9
}
}
...
Now I want to test this function with various different epsilons (and left and right values). I don't know how I could do this at the moment without writing separate tests for each epsilon.
Any ideas? Note that the given example function almost_equal
is of course just a simple example, my real use case has much more complex numerical calculations that use the generic epsilon value.
Edit: I found a solution with a slight hack, if there would be native support it could be much nicer I think:
use rstest::rstest;
use std::marker::PhantomData;
#[rstest]
#[case(PhantomData::<Eps1eMinus7>, 0.50000001f64, 0.5, true)]
#[case(PhantomData::<Eps1eMinus9>, 0.50000001f64, 0.5, false)]
fn test_almost_equal<E: ConstEps>(
#[case] _e: PhantomData<E>,
#[case] left: f64,
#[case] right: f64,
#[case] expect: bool,
) {
let result = almost_equal::<E>(left, right);
assert_eq!(result, expect);
}
from rstest.
Thank you for the quick answer! That would be nice to have this syntax, although I understand it is not high priority (the PhantomData
version works well enough).
Btw. there is an interesting edge case if one uses _
as variable name the macro fails:
If I use
#[rstest]
#[case(PhantomData::<Eps1eMinus7>, 0.50000001f64, 0.5, true)]
#[case(PhantomData::<Eps1eMinus9>, 0.50000001f64, 0.5, false)]
fn test_almost_equal<E: ConstEps>(
#[case] _: PhantomData<E>,
#[case] left: f64,
#[case] right: f64,
#[case] expect: bool,
) {
let result = almost_equal::<E>(left, right);
assert_eq!(result, expect);
}
compilation fails with:
error: Wrong case signature: should match the given parameters list.
--> src/investigate_rstest.rs:51:16
|
51 | #[case(PhantomData::<Eps1eMinus7>, 0.50000001f64, 0.5, true)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: Wrong case signature: should match the given parameters list.
--> src/investigate_rstest.rs:52:16
|
52 | #[case(PhantomData::<Eps1eMinus9>, 0.50000001f64, 0.5, false)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
from rstest.
Related Issues (20)
- Injecting static types HOT 5
- Create a fixture from `files` attribute HOT 3
- skip cases based on feature flag? HOT 5
- `#[once]` fixtures are unsound for types that are not `Sync` HOT 4
- clippy::duplicated_attributes warning with rust 1.79.0-nightly HOT 3
- By-reference fixtures HOT 12
- Update `rstest_reuse` docs and release new version
- `#[from(source)]` should accept path also and not just idents
- rstest_reuse: "the `async` keyword is missing from the function declaration" HOT 1
- replace return <expr>; with <expr> [needless_return] HOT 3
- Support dynamic `#[values]` HOT 1
- Feature: Optional label in the `#[case...]` HOT 1
- Dynamically set #[values(..)] HOT 1
- New test files are only recognized after `cargo clean` HOT 4
- feat: provide a cargo feature to disable the proc macro inference in #221 HOT 2
- consider adding a `wasm-bindgen` feature HOT 1
- Recommendation on choosing between different sets of `#[values(...)]`?
- Mixing `#[rstest]` and `#[serial]` breaks VS Code's IntelliSense with rust-analyzer HOT 3
- Possibility of resolving a fixture only once per test?
- Automatically test all variants of an enum HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rstest.