Repository navigation
Inner function cannot be tested #36629
Description
Activity
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.
Reacted by Alex Kladov, Esteban Kuber and Colin- addedA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lints
on May 13, 2017 - changed the title
[-]Test attribute inside an fn[/-][+]Inner function cannot be tested[/+]on May 13, 2017 - addedA-libtestArea: `#[test]` / the `test` libraryArea: `#[test]` / the `test` library
on Jun 22, 2017 - addedC-feature-requestCategory: A feature request, i.e: not implemented / a PR.Category: A feature request, i.e: not implemented / a PR.
on Jul 26, 2017 - added a commit that references this issue
on Jul 3, 2018 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)):
- figure out why
unused_attributeisn't firing, and let that handle the lint side - properly support
#[test]functions anywhere, as per Add lint warning for inner function marked as#[test]rfcs#2471 (comment)
Reacted by Joe ST, scottmcm, vacore and J Wong- figure out why
- addedT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.and removedA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lints
on Oct 11, 2019 - added a commit that references this issue
on Aug 5, 2023 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]);
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
I really would like to be able to this: