Skip to content

Inner function cannot be tested #36629

Description

@kineticz

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);
}

Activity

  1. Cobrand commented on Sep 21, 2016

    @Cobrand
    Contributor

    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.

  2. kineticz commented on Sep 21, 2016

    @kineticz
    Author

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

  3. kineticz commented on Sep 21, 2016

    @kineticz
    Author

    @Cobrand Yeah, something like this will be great:

    assert!
    assert_eq!
    debug_assert!
    debug_assert_eq!
    test_assert!
    test_assert_eq!
    
  4. Cobrand commented on Sep 21, 2016

    @Cobrand
    Contributor

    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.

  5. Aatch commented on Sep 22, 2016

    @Aatch
    Contributor

    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.

  6. Mark-Simulacrum commented on May 13, 2017

    @Mark-Simulacrum
    Member

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

  7. changed the title [-]Test attribute inside an fn[/-] [+]Inner function cannot be tested[/+] on May 13, 2017
  8. added a commit that references this issue on Jul 3, 2018
    739320a
  9. eddyb commented on Jul 13, 2018

    @eddyb
    Contributor

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

    I disagree with the approach taken (a new lint), we should either (rust-lang/rfcs#2471 (comment)):

  10. reopened this on Jul 13, 2018
  11. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    A-diagnosticsArea: Messages for errors, warnings, and lints
    on Oct 11, 2019
  12. PaulDotSH commented on Jun 12, 2022

    @PaulDotSH
  13. added a commit that references this issue on Aug 4, 2023
  14. sffc commented on Apr 25, 2026

    @sffc

    I would like this in order for a macro to be able to generate tests without having to come up with a name for the tests.

    Example: something like

    macro_rules! tested_varule {
        ($type:path, $bytes:expr) => {{
            const BYTES: &[u8] = $bytes;
            #[test]
            fn test_tested_varule() {
                use $crate::ule::VarULE;
                assert!(<$type>::validate_bytes(BYTES).is_ok());
            }
            // Safety: If the above test passes, then these are valid bytes
            unsafe {
                &*(BYTES as *const [u8] as *const $type)
            }
        }};
    }
    
    const MY_STR_1: &str = tested_varule!(str, &[0x61, 0x62, 0x63]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions