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

Add possibility to replace assert_eq! with custom assertion #31

Closed
frondeus opened this issue Dec 13, 2019 · 6 comments
Closed

Add possibility to replace assert_eq! with custom assertion #31

frondeus opened this issue Dec 13, 2019 · 6 comments
Labels
enhancement New feature or request
Milestone

Comments

@frondeus
Copy link
Owner

When using => expected syntax it could be nice to set up what assertion shall we use.
It might be extra attribute:

#[test_case("foo" => "bar")]
#[test_case_assert_macro(assert_diff)]
fn test(input: &str) -> String { input.to_string() }

this could be expanded into:

mod test {
    use super::*;
    fn test(input: &str) -> String { input.to_string() }
    
    #[test]
    fn foo_expects_bar() {
       let actual = test("foo");
       assert_diff!("bar", actual);
    }
@frondeus frondeus added the enhancement New feature or request label Dec 13, 2019
@frondeus
Copy link
Owner Author

The question is - should this setting be active for one test_case or for all test_cases

@Ryneqq
Copy link

Ryneqq commented Dec 13, 2019

How about

#[test_case("foo" => "bar")]
#[test_case_assert_macro(assert_diff)]

to

#[test_case[assert_diff]("foo" => "bar)]

@StragaSevera
Copy link

Is there any progress on this issue? I would be very glad to be able to set the assertion macro globally or locally and have the clean => syntax.

@luke-biel
Copy link
Collaborator

Unfortunately not. Life called and neither me nor @frondeus have a lot of free time, so test-case lives in suspended state.

With this issue, idea is from some time back and wasn't ever fully fleshed out (we found no real world use case to base it on). If it is something you'd like and have some time to spare, introducing it yourself is an option (I can guarantee as much that I'm reviewing PR's actively).

As for design - I wouldn't oppose global #[test_case_assertion] marker that could be applied to a global method, which would then be used in every test_case, but local option, like #[test_case::<function_xyz>("foo" => "bar")] may be easier to introduce with how test-case is currently structured.

@luke-biel
Copy link
Collaborator

luke-biel commented Dec 12, 2021

An idea for #73 that came to my mind:

#[test_case(input1 => 1.0)]
#[test_case(input2 => with assert!($.is_nan()))]
fn tested(v: X) -> f64 {
	...
}

We could provide an option to manually map assertions in some cases.
Then, if there's too much repetition and we wanna cover whole test case with our custom assert

fn assert_nan(value: f64) {
	assert!(value.is_nan())
}

#[test_case(input1 => 1.0)]
#[test_case(input2 => use assert_nan)]
fn tested(v: X) -> f64 {
	...
}

which in turn allows:

fn assert_nan(value: f64) {
	assert!(value.is_nan())
}

#[test_case(input2)]
#[test_case_use(assert_nan)]
fn tested(v: X) -> f64 {
	...
}

Still no idea how to apprach global replacement though, but this goes with the spirit of test case in my opinion.
What do you think @frondeus ?

@luke-biel luke-biel mentioned this issue Dec 18, 2021
7 tasks
@luke-biel luke-biel added this to the 2.0.0 milestone Dec 18, 2021
github-actions bot pushed a commit to gnoliyil/fuchsia that referenced this issue Jan 25, 2022
Use //third_party/rust_crates:pretty_assertions crate pretty-prints assert_eq!
failures, to make the difference easier to spot.

We are not able to just use `assert_eq!` in `test_case` tests because it
generating test with `use super::*`
It is a known issue frondeus/test-case#31

Test: fx test input_pipeline_lib_tests
Bug: 89598

Change-Id: I73fd51765c2c5ba8d6c0f491568aaa8eb84b84a7
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/633294
Reviewed-by: Mukesh Agrawal <[email protected]>
Reviewed-by: Filip Filmar <[email protected]>
Commit-Queue: Jianpeng Chao <[email protected]>
@luke-biel
Copy link
Collaborator

I've relaxed a bit requirements on using keyword, thus users can do

    fn wrapped_pretty_assert(expected: u64) -> impl Fn(u64) {
        move |actual: u64| { pretty_assertions::assert_eq!(actual, expected) }
    }

    #[test_case(1 => using wrapped_pretty_assert(1))]
    fn pretty_assertions_usage(input: u64) -> u64 {
        input
    }

I believe this covers all initial work needed to allow custom assertions.
I'm gonna fork this issue so that #[test_case_use(assert_nan)] and global assertions can be done separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants