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