macros_rs/
lib.rs

1pub mod exp;
2pub mod fmt;
3pub mod fnc;
4pub mod fs;
5pub mod obj;
6pub mod os;
7
8/// Enable actix-web macros
9#[cfg(feature = "actix-web")]
10pub mod actix;
11
12/// Enable everything
13pub mod everything {
14    pub use super::exp::*;
15    pub use super::fmt::*;
16    pub use super::fnc::*;
17    pub use super::fs::*;
18    pub use super::obj::*;
19    pub use super::os::*;
20
21    #[cfg(feature = "actix-web")]
22    pub use super::actix::*;
23}
24
25#[cfg(test)]
26mod tests {
27    #[test]
28    fn inc() {
29        let mut num = 3;
30
31        crate::exp::inc!(num);
32        assert_eq!(num, 4);
33
34        crate::exp::inc!(num);
35        assert_eq!(num, 5);
36    }
37
38    #[test]
39    fn dec() {
40        let mut num = 3;
41
42        crate::exp::dec!(num);
43        assert_eq!(num, 2);
44
45        crate::exp::dec!(num);
46        assert_eq!(num, 1);
47    }
48
49    #[test]
50    fn ternary() {
51        let test = |val: bool| crate::exp::ternary!(val, "something", "nothing");
52
53        assert_eq!(test(true), "something");
54        assert_eq!(test(false), "nothing");
55    }
56
57    #[test]
58    fn then() {
59        let mut var = "";
60
61        crate::exp::then!(true, var = "something");
62        assert_eq!(var, "something");
63    }
64
65    #[test]
66    fn string() {
67        assert_eq!(crate::fmt::string!(), "");
68        assert_eq!(crate::fmt::string!("something"), "something".to_string());
69    }
70
71    #[test]
72    fn str() {
73        assert_eq!(crate::fmt::str!("something".to_string()), "something");
74    }
75
76    #[test]
77    fn set_env() {
78        let mut rng = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos() as u64;
79
80        let test_value = std::iter::repeat_with(|| {
81            rng = rng.wrapping_mul(0x5DEECE66D).wrapping_add(11) & ((1 << 48) - 1);
82            let byte = (rng >> 24) as u8;
83
84            match byte % 3 {
85                0 => (b'A' + (byte % 26)) as char,
86                1 => (b'a' + (byte % 26)) as char,
87                _ => (b'0' + (byte % 10)) as char,
88            }
89        })
90        .take(16)
91        .collect::<String>();
92
93        crate::os::set_env_sync!(MACROS_ENV_1 = test_value.clone());
94        assert_eq!(std::env::var("MACROS_ENV_1"), Ok(test_value));
95    }
96
97    #[test]
98    fn fmtstr() {
99        let data = 123;
100        let var = "hello";
101
102        assert_eq!(crate::fmt::fmtstr!("{var} world {data}{}", 45), "hello world 12345");
103    }
104
105    #[test]
106    fn fn_name() {
107        assert_eq!(crate::fnc::function_name!(), "fn_name");
108    }
109
110    #[test]
111    fn fn_path() {
112        assert_eq!(crate::fnc::function_path!(), "fn_path");
113    }
114
115    #[test]
116    fn crash_test_facade() {
117        use colored::Colorize;
118        use std::io::Write;
119
120        write!(&mut std::io::stdout(), "-------------------------\n").unwrap();
121        crate::fmt::error!("error! {} {}", "print-test".bright_white(), "ok\n".green());
122        crate::fmt::errorln!("errorln! {} {}", "print-test".bright_white(), "ok".green());
123        write!(&mut std::io::stdout(), "-------------------------\n\n").unwrap();
124    }
125
126    #[test]
127    fn clone() {
128        let var: String = Default::default();
129
130        assert_eq!(crate::obj::clone!(var), "");
131    }
132
133    #[test]
134    fn lazy_lock() {
135        crate::obj::lazy_lock! {
136            static TEST: String = Default::default();
137        }
138
139        assert_eq!(&*TEST, "");
140    }
141
142    #[test]
143    fn file_exists() { assert_eq!(crate::fs::file_exists!("tests/file.txt"), true) }
144
145    #[test]
146    fn folder_exists() { assert_eq!(crate::fs::folder_exists!("tests/dir"), true) }
147
148    #[test]
149    fn path_empty() { assert_eq!(crate::fs::path_empty!("tests/empty_file.txt"), true) }
150
151    #[test]
152    fn path_content() { assert_eq!(crate::fs::path_empty!("tests/file.txt"), false) }
153
154    #[actix_web::test]
155    #[cfg(feature = "actix-web")]
156    async fn actix_web_html() {
157        use actix_http::{HttpService, Request};
158        use actix_http_test::test_server;
159        use actix_utils::future::ok;
160        use actix_web::http::header::HeaderValue;
161        use std::convert::Infallible;
162
163        let mut srv = test_server(|| {
164            HttpService::build()
165                .h1(|req: Request| {
166                    assert!(req.peer_addr().is_some());
167                    ok::<_, Infallible>(crate::actix::send_html!().finish())
168                })
169                .tcp()
170        })
171        .await;
172
173        let response = srv.get("/").send().await.unwrap();
174        assert_eq!(response.headers().get("Content-Type"), Some(&HeaderValue::from_static("text/html; charset=utf-8")));
175
176        srv.stop().await;
177    }
178
179    #[actix_web::test]
180    #[cfg(feature = "actix-web")]
181    async fn actix_web_ok() {
182        use actix_http::{HttpService, Request};
183        use actix_http_test::test_server;
184        use actix_utils::future::ok;
185        use std::convert::Infallible;
186
187        let mut srv = test_server(|| {
188            HttpService::build()
189                .h1(|req: Request| {
190                    assert!(req.peer_addr().is_some());
191                    ok::<_, Infallible>(crate::actix::ok!().finish())
192                })
193                .tcp()
194        })
195        .await;
196
197        let response = srv.get("/").send().await.unwrap();
198        assert!(response.status().is_success());
199
200        srv.stop().await;
201    }
202}