Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
macros.rs - source
[go: Go Back, main page]

snapbox/
macros.rs

1/// Check if a value is the same as an expected value
2///
3/// By default [`filters`][crate::filter] are applied, including:
4/// - `...` is a line-wildcard when on a line by itself
5/// - `[..]` is a character-wildcard when inside a line
6/// - `[EXE]` matches `.exe` on Windows
7/// - `"{...}"` is a JSON value wildcard
8/// - `"...": "{...}"` is a JSON key-value wildcard
9/// - `\` to `/`
10/// - Newlines
11///
12/// To limit this to newline normalization for text, call [`Data::raw`][crate::Data] on `expected`.
13///
14/// # Effective signature
15///
16/// ```rust
17/// # use snapbox::IntoData;
18/// fn assert_data_eq(actual: impl IntoData, expected: impl IntoData) {
19///     // ...
20/// }
21/// ```
22///
23/// # Examples
24///
25/// ```rust
26/// # use snapbox::assert_data_eq;
27/// let output = "something";
28/// let expected = "so[..]g";
29/// assert_data_eq!(output, expected);
30/// ```
31///
32/// Can combine this with [`file!`]
33/// ```rust,no_run
34/// # use snapbox::assert_data_eq;
35/// # use snapbox::file;
36/// let actual = "something";
37/// assert_data_eq!(actual, file!["output.txt"]);
38/// ```
39#[macro_export]
40macro_rules! assert_data_eq {
41    ($actual: expr, $expected: expr $(,)?) => {{
42        let actual = $crate::IntoData::into_data($actual);
43        let expected = $crate::IntoData::into_data($expected);
44        $crate::Assert::new()
45            .action_env($crate::assert::DEFAULT_ACTION_ENV)
46            .eq(actual, expected);
47    }};
48}
49
50/// Find the directory for your source file
51#[doc(hidden)] // forced to be visible in intended location
52#[macro_export]
53macro_rules! current_dir {
54    () => {{
55        let root = $crate::utils::cargo_rustc_current_dir!();
56        let file = ::std::file!();
57        let rel_path = ::std::path::Path::new(file).parent().unwrap();
58        root.join(rel_path)
59    }};
60}
61
62/// Find the directory for your source file
63#[doc(hidden)] // forced to be visible in intended location
64#[macro_export]
65macro_rules! current_rs {
66    () => {{
67        let root = $crate::utils::cargo_rustc_current_dir!();
68        let file = ::std::file!();
69        let rel_path = ::std::path::Path::new(file);
70        root.join(rel_path)
71    }};
72}
73
74/// Find the base directory for [`std::file!`]
75#[doc(hidden)] // forced to be visible in intended location
76#[macro_export]
77macro_rules! cargo_rustc_current_dir {
78    () => {{
79        if let Some(rustc_root) = ::std::option_env!("CARGO_RUSTC_CURRENT_DIR") {
80            ::std::path::Path::new(rustc_root)
81        } else {
82            let manifest_dir = ::std::path::Path::new(::std::env!("CARGO_MANIFEST_DIR"));
83            manifest_dir
84                .ancestors()
85                .filter(|it| it.join("Cargo.toml").exists())
86                .last()
87                .unwrap()
88        }
89    }};
90}
91
92/// Path to the current function
93///
94/// Closures are ignored
95#[doc(hidden)]
96#[macro_export]
97macro_rules! fn_path {
98    () => {{
99        fn f() {}
100        fn type_name_of_val<T>(_: T) -> &'static str {
101            std::any::type_name::<T>()
102        }
103        let mut name = type_name_of_val(f).strip_suffix("::f").unwrap_or("");
104        while let Some(rest) = name.strip_suffix("::{{closure}}") {
105            name = rest;
106        }
107        name
108    }};
109}
110
111#[cfg(test)]
112mod test {
113    #[test]
114    fn direct_fn_path() {
115        assert_eq!(fn_path!(), "snapbox::macros::test::direct_fn_path");
116    }
117
118    #[test]
119    #[allow(clippy::redundant_closure_call)]
120    fn closure_fn_path() {
121        (|| {
122            assert_eq!(fn_path!(), "snapbox::macros::test::closure_fn_path");
123        })();
124    }
125
126    #[test]
127    fn nested_fn_path() {
128        fn nested() {
129            assert_eq!(fn_path!(), "snapbox::macros::test::nested_fn_path::nested");
130        }
131        nested();
132    }
133}