iocore_test/
lib.rs

1//! iocore_test is a testing tool for crates that utilize the
2//! [`iocore`] crate.
3
4/// `seq_bytes` returns a [`Vec<u8>`] containing a sequence of [`u8`]
5/// bytes and applying the remainder operation if `count` is longer
6/// than `u8::MAX`
7pub fn seq_bytes(count: usize) -> Vec<u8> {
8    (0..count)
9        .map(|n| {
10            TryInto::<u8>::try_into(if n > u8::MAX.into() {
11                n % <u8 as Into<usize>>::into(u8::MAX)
12            } else {
13                n
14            })
15            .unwrap()
16        })
17        .collect()
18}
19
20/// `path_to_test_file` returns the path to an empty file within the same dir as the calling test file, creates parent directories if necessary and deletes the file if exists
21#[macro_export]
22macro_rules! path_to_test_file {
23    ($name:expr) => {{
24        let path = $crate::folder_path!("__test_files__").join($crate::test_name!()).join($name);
25        path.parent().unwrap().mkdir().map(|_| ()).unwrap_or_default();
26        path.delete().map(|_| ()).unwrap_or_default();
27        path
28    }};
29}
30
31/// `folder_path` returns the path to the parent folder of the calling test file, if called with an argument then calls [`iocore::Path::join`] on the folder path (creates folder if necessary)
32#[macro_export]
33macro_rules! folder_path {
34    () => {{
35        let path = iocore::Path::raw(file!())
36            .parent()
37            .expect(&format!("{:#?} has no parent folder!!", file!()));
38        path
39    }};
40    ($name:expr) => {{
41        let path = $crate::folder_path!();
42        let path = path.join($name);
43        path.mkdir_unchecked();
44        path
45    }};
46}
47/// `directory_path` returns the path to the parent directory of the calling test file, if called with an argument then calls [`iocore::Path::join`] on the directory path (creates directory if necessary)
48#[macro_export]
49macro_rules! directory_path {
50    () => {{
51        $crate::folder_path!()
52    }};
53    ($name:expr) => {
54        $crate::folder_path!($name)
55    };
56}
57
58/// `test_folder_parent_path` returns the parent folder of the test file which calls it joined with the given "name" (creates the directory if necessary)
59#[macro_export]
60macro_rules! test_folder_parent_path {
61    ($name:expr) => {{
62        let path = iocore::Path::raw(file!())
63            .parent()
64            .expect(&format!("parent directory of {:#?}", file!()));
65        let path = path.join($name);
66        path.mkdir_unchecked();
67        path
68    }};
69}
70/// `test_directory_parent_path` returns the parent directory of the test file which calls it joined with the given "name" (creates the directory if necessary)
71#[macro_export]
72macro_rules! test_directory_parent_path {
73    ($name:expr) => {{
74        $crate::test_folder_parent_path($name)
75    }};
76}
77/// `test_name` returns the name of the test function
78#[macro_export]
79macro_rules! test_name {
80    () => {{
81        fn f() {}
82        fn type_name_of<T>(_: T) -> &'static str {
83            std::any::type_name::<T>()
84        }
85        let name = type_name_of(f);
86        let name = name.strip_suffix("::f").unwrap().replace("::", std::path::MAIN_SEPARATOR_STR);
87        name
88    }};
89}
90
91/// `path_to_test_file` returns the path to a test directory as the test file
92#[macro_export]
93macro_rules! path_to_test_folder {
94    () => {{
95        let path = $crate::folder_path!("__test_files__").join($crate::test_name!());
96        path.mkdir_unchecked();
97        path
98    }};
99    ($name:expr) => {{
100        let path = $crate::path_to_test_folder!().join($name);
101        path.mkdir_unchecked();
102        path
103    }};
104}
105
106/// `path_to_test_file` returns the path to a test directory as the test file
107#[macro_export]
108macro_rules! path_to_test_directory {
109    () => {{
110        $crate::path_to_test_folder!()
111    }};
112    ($name:expr) => {{
113        $crate::path_to_test_folder!($name)
114    }};
115}