From db164f48cb0557429533e2f5708093e61a722b15 Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 19 Sep 2024 11:46:05 +0200 Subject: [PATCH 1/2] docs(code-style-guide): Add unit test function name rule --- .../contributor/pages/code-style-guide.adoc | 56 ++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/modules/contributor/pages/code-style-guide.adoc b/modules/contributor/pages/code-style-guide.adoc index 861964e10..f76967e42 100644 --- a/modules/contributor/pages/code-style-guide.adoc +++ b/modules/contributor/pages/code-style-guide.adoc @@ -296,7 +296,7 @@ The usage of `thiserror` is considered invalid. ---- #[derive(Snafu)] enum Error { - #[snafu(display("failed to read config file of user {user_name}"))] + #[snafu(display("failed to read config file of user {user_name:?}"))] FileRead { source: std::io::Error, user_name: String, @@ -595,3 +595,57 @@ let memory: CpuQuantity = "2".parse(); ---- ==== + +== Writing tests + +=== Unit test function names + +Function names of unit tests must not include the `test_` prefix, because otherwise `cargo test` will produce superfluous output like `memory::test::test_addition::case_1`. +Instead, use an appropriate name to describe what is being tested. +The previously mentioned name could for example be renamed to `memory::test::addition::case_1`. + +[TIP.code-rule,caption=Examples of correct code for this rule] +==== + +[source,rust] +---- +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn parse_valid_api_version() { + todo!() + } + + #[test] + fn parse_invalid_api_version() { + todo!() + } +} +---- + +==== + +[WARNING.code-rule,caption=Examples of incorrect code for this rule] +==== + +[source,rust] +---- +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_valid() { + todo!() + } + + #[test] + fn test_invalid() { + todo!() + } +} +---- + +==== From 2b493e61302d1bb7ced9cb4cb8622fccda03aa8f Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 19 Sep 2024 14:47:20 +0200 Subject: [PATCH 2/2] Update modules/contributor/pages/code-style-guide.adoc Co-authored-by: Nick <10092581+NickLarsenNZ@users.noreply.github.com> --- modules/contributor/pages/code-style-guide.adoc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/contributor/pages/code-style-guide.adoc b/modules/contributor/pages/code-style-guide.adoc index f76967e42..24ff5a545 100644 --- a/modules/contributor/pages/code-style-guide.adoc +++ b/modules/contributor/pages/code-style-guide.adoc @@ -600,9 +600,15 @@ let memory: CpuQuantity = "2".parse(); === Unit test function names -Function names of unit tests must not include the `test_` prefix, because otherwise `cargo test` will produce superfluous output like `memory::test::test_addition::case_1`. -Instead, use an appropriate name to describe what is being tested. -The previously mentioned name could for example be renamed to `memory::test::addition::case_1`. +Function names of unit tests must not include a redundant `test` prefix or +suffix. + +It results in the output of `cargo test` containing superfluous mentions of +"test", especially when the containing module is called `test`. For example: +`my_crate::test::test_valid`. + +Instead, use an appropriate name to describe what is being tested. The previous +example could then become: `my_crate::test::parse_valid_api_version`. [TIP.code-rule,caption=Examples of correct code for this rule] ====