Inner function cannot be tested

199dbeb
Opened by keenz at 2022-06-12 10:57:19

I really would like to be able to this:

fn outer(x: i32) -> i32 {
    fn inner(a: i32, b: i32) -> i32 {
        a + b
    }
    #[test]
    fn test_inner() {
        assert_eq!(inner(3, 5), 8);
    }
    inner(x, 5)
}

#[test]
fn test_outer() {
    assert_eq!(outer(3), 8);
}
  1. But why though ? Do you want to be able to test things inner to your function ? How about an assert_test! that would compile / trigger only when in test mode ? Just an idea.

    Cobrand at 2016-09-21 18:36:38

  2. Because this is convenient for testing nested functions. Currently I'd have to move the fn outside to be able to test it separately.

    keenz at 2016-09-21 19:18:56

  3. @Cobrand Yeah, something like this will be great:

    assert!
    assert_eq!
    debug_assert!
    debug_assert_eq!
    test_assert!
    test_assert_eq!
    

    keenz at 2016-09-21 19:31:10

  4. I'm just arguing against my own idea here, but that would require either :

    • Require to recompile everything related for "test", which I don't know which is a good idea or not
    • Compile this only in debug mode, but only run it when doing test (so it's still in the binary but there is a conditional for whether or not this is in test mode)

    My code is horrible so it doesn't have that much tests, is it possible to run tests in release mode ? If that is the case, the second option is a no-no I guess.

    Cobrand at 2016-09-21 19:38:09

  5. The problem with this is that the test harness needs to access the inner functions, which it can't do because you can't name them.

    James Miller at 2016-09-22 01:18:21

  6. I'm marking as a diagnostics issue so that we print a warning in this case at least.

    Mark Rousskov at 2017-05-13 19:46:13

  7. Why was the #51450 PR accepted, while the RFC is still unmerged?

    I disagree with the approach taken (a new lint), we should either (https://github.com/rust-lang/rfcs/pull/2471#issuecomment-397418564):

    • figure out why unused_attribute isn't firing, and let that handle the lint side
    • properly support #[test] functions anywhere, as per https://github.com/rust-lang/rfcs/pull/2471#issuecomment-397241123

    Eduard-Mihai Burtescu at 2018-07-13 07:43:33

  8. Is this still not fixed?

    PaulDotSH at 2022-06-12 10:57:19