Rust: Inner function cannot be tested

Created on 21 Sep 2016  路  7Comments  路  Source: rust-lang/rust

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);
}
A-libtest C-feature-request T-compiler

Most helpful comment

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

All 7 comments

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.

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

@Cobrand Yeah, something like this will be great:

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

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.

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.

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

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):

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pedrohjordao picture pedrohjordao  路  3Comments

dtolnay picture dtolnay  路  3Comments

mcarton picture mcarton  路  3Comments

dwrensha picture dwrensha  路  3Comments

behnam picture behnam  路  3Comments