mithril_common/test/
mod.rs

1//! Test utilities
2//!
3//! A collection of testing utilities and helpers:
4//!
5//! * `assert`: Custom assertions for comparing directory structures, iterables, JSON strings and more
6//! * `builder`: Test data builders and fixtures
7//! * `crypto_helper`: Cryptographic utilities for testing
8//! * `double`: Test doubles (mocks, fakes, dummies, ...)
9//! * `entities_extensions`: Extension traits adding test-specific methods for [crate::entities]
10//! * `logging`: Test logging infrastructure
11//! * `mock_extensions`: Additional mocking utilities
12//! * `temp_dir`: Temporary directory management for tests
13//!
14
15pub mod builder;
16pub mod crypto_helper;
17pub mod double;
18pub mod entities_extensions;
19pub mod logging;
20pub mod mock_extensions;
21
22mod assert;
23mod temp_dir;
24
25pub use assert::*;
26pub use temp_dir::*;
27
28#[cfg(test)]
29logging::define_test_logger!();
30
31/// Return the path of the given function.
32/// If the last function is `f`, it is removed.
33/// The last `{{closure}}` is also removed.
34pub fn format_current_function_module<T>(f: T) -> &'static str {
35    fn type_name_of<T>(_: T) -> &'static str {
36        std::any::type_name::<T>()
37    }
38
39    let name = type_name_of(f);
40    let name = name.strip_suffix("::f").unwrap_or(name);
41    name.strip_suffix("::{{closure}}").unwrap_or(name)
42}
43
44/// Return a string representing the path of the given function.
45pub fn format_current_function_path<T>(f: T) -> String {
46    let name = format_current_function_module(f);
47    name.replace("::", "/")
48}
49
50/// Returns the name of the function that called this macro.
51#[macro_export]
52macro_rules! current_function {
53    () => {{
54        fn f() {}
55        let name = $crate::test::format_current_function_module(f);
56        // The index found is the beginning of the '..', this is why we add 2.
57        let function_name_index = name.rfind("::").map(|index| index + 2).unwrap_or(0);
58
59        &name[function_name_index..]
60    }};
61}
62pub use current_function;
63
64/// Returns the path of the function that called this macro.
65#[macro_export]
66macro_rules! current_function_path {
67    () => {{
68        fn f() {}
69
70        std::path::PathBuf::from($crate::test::format_current_function_path(f))
71    }};
72}
73pub use current_function_path;
74
75#[cfg(test)]
76mod utils {
77    use std::path::Path;
78
79    use super::*;
80
81    #[test]
82    fn test_current_function_extract_function_name() {
83        let name = current_function!();
84
85        assert_eq!("test_current_function_extract_function_name", name);
86    }
87
88    #[tokio::test]
89    async fn test_current_function_extract_async_function_name() {
90        let name = current_function!();
91
92        assert_eq!("test_current_function_extract_async_function_name", name);
93    }
94
95    #[test]
96    fn test_format_function_path_from_given_function() {
97        assert_eq!(
98            "mithril_common/test/utils/test_format_function_path_from_given_function",
99            format_current_function_path(test_format_function_path_from_given_function)
100        );
101    }
102
103    #[test]
104    fn test_format_function_path_from_given_pseudo_function_f() {
105        fn f() {}
106        assert_eq!(
107            "mithril_common/test/utils/test_format_function_path_from_given_pseudo_function_f",
108            format_current_function_path(f)
109        );
110    }
111
112    #[tokio::test]
113    async fn test_format_function_path_from_given_async_function_f() {
114        fn f() {}
115        assert_eq!(
116            "mithril_common/test/utils/test_format_function_path_from_given_async_function_f",
117            format_current_function_path(f)
118        );
119    }
120
121    #[test]
122    fn test_build_current_function_path_using_macros() {
123        assert_eq!(
124            Path::new("mithril_common")
125                .join("test")
126                .join("utils")
127                .join("test_build_current_function_path_using_macros"),
128            current_function_path!()
129        );
130    }
131
132    #[tokio::test]
133    async fn test_build_current_async_function_path_using_macros() {
134        assert_eq!(
135            Path::new("mithril_common")
136                .join("test")
137                .join("utils")
138                .join("test_build_current_async_function_path_using_macros"),
139            current_function_path!()
140        );
141    }
142}