diff --git a/src/lib.rs b/src/lib.rs index a6d3f140..c6aabebb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -133,12 +133,6 @@ pub use crate::value::{ from_value, to_value, Index, Number, Sequence, Value, }; // Value manipulation functions -/// The `macros` module contains functions for generating macros. -pub mod macros; - -/// The `utilities` module contains utility functions for the library. -pub mod utilities; - #[doc(inline)] pub use crate::mapping::Mapping; // Re-export the Mapping type for YAML mappings @@ -151,6 +145,9 @@ pub mod libyml; /// The `loader` module contains the `Loader` type for YAML loading. pub mod loader; +/// The `macros` module contains functions for generating macros. +pub mod macros; + /// The `mapping` module contains the `Mapping` type for YAML mappings. pub mod mapping; @@ -163,6 +160,9 @@ pub mod number; /// The `ser` module contains the library's YAML serializer. pub mod ser; +/// The `utilities` module contains utility functions for the library. +pub mod utilities; + /// The `value` module contains the `Value` type for YAML values. pub mod value; diff --git a/tests/macros/mod.rs b/tests/macros/mod.rs new file mode 100644 index 00000000..4824db32 --- /dev/null +++ b/tests/macros/mod.rs @@ -0,0 +1,8 @@ +/// The `test_macro_directory` module contains tests for the directory module. +pub mod test_macro_directory; + +/// The `test_macro_file` module contains tests for the file module. +pub mod test_macro_file; + +/// The `test_macro_nested_enum_serde` module contains tests for the nested enum serde module. +pub mod test_macro_nested_enum_serde; diff --git a/tests/test_macro_directory.rs b/tests/macros/test_macro_directory.rs similarity index 54% rename from tests/test_macro_directory.rs rename to tests/macros/test_macro_directory.rs index 5c9cbba0..ddc95147 100644 --- a/tests/test_macro_directory.rs +++ b/tests/macros/test_macro_directory.rs @@ -4,47 +4,65 @@ mod tests { macro_check_directory, macro_cleanup_directories, macro_create_directories, }; - use std::fs; - use std::path::Path; + use std::{fs, path::Path}; use tempfile::tempdir; + /// Tests the `macro_check_directory` macro when the directory doesn't exist. #[test] - fn test_macro_check_directory() { + fn test_macro_check_directory_create() { let temp_dir = tempdir().unwrap(); let path = temp_dir.path().join("logs"); - // Test creating a new directory macro_check_directory!(&path, "logs"); assert!(path.exists() && path.is_dir()); + } + + /// Tests the `macro_check_directory` macro when the directory already exists. + #[test] + fn test_macro_check_directory_exists() { + let temp_dir = tempdir().unwrap(); + let path = temp_dir.path().join("logs"); - // Test directory already exists + fs::create_dir(&path).unwrap(); macro_check_directory!(&path, "logs"); assert!(path.exists() && path.is_dir()); } + /// Tests the `macro_check_directory` macro when the path is not a directory. + #[test] + #[should_panic( + expected = "❌ 'test_file' exists but is not a directory" + )] + fn test_macro_check_directory_not_dir() { + let temp_dir = tempdir().unwrap(); + let path = temp_dir.path().join("test_file"); + fs::write(&path, "test").unwrap(); + + macro_check_directory!(&path, "test_file"); + } + + /// Tests the `macro_create_directories` macro when creating multiple directories. #[test] fn test_macro_create_directories() { let temp_dir = tempdir().unwrap(); let path1 = temp_dir.path().join("logs1"); let path2 = temp_dir.path().join("logs2"); - // Test creating multiple directories macro_create_directories!(&path1, &path2).unwrap(); assert!(path1.exists() && path1.is_dir()); assert!(path2.exists() && path2.is_dir()); } + /// Tests the `macro_cleanup_directories` macro when cleaning up multiple directories. #[test] fn test_macro_cleanup_directories() { let temp_dir = tempdir().unwrap(); let path1 = temp_dir.path().join("logs1"); let path2 = temp_dir.path().join("logs2"); - // Create directories to clean up fs::create_dir_all(&path1).unwrap(); fs::create_dir_all(&path2).unwrap(); - // Test cleaning up directories macro_cleanup_directories!(&path1, &path2); assert!(!path1.exists()); assert!(!path2.exists()); diff --git a/tests/test_macro_file.rs b/tests/macros/test_macro_file.rs similarity index 100% rename from tests/test_macro_file.rs rename to tests/macros/test_macro_file.rs diff --git a/tests/test_macro_nested_enum_serde.rs b/tests/macros/test_macro_nested_enum_serde.rs similarity index 100% rename from tests/test_macro_nested_enum_serde.rs rename to tests/macros/test_macro_nested_enum_serde.rs diff --git a/tests/mod.rs b/tests/mod.rs new file mode 100644 index 00000000..bd1a15ca --- /dev/null +++ b/tests/mod.rs @@ -0,0 +1,5 @@ +/// This module contains the tests for the `macros` module. +pub mod macros; + +/// This module contains the tests for the `utilities` module. +pub mod utilities; diff --git a/tests/utilities/test_directory.rs b/tests/utilities/test_directory.rs index ac8e9307..e68c782e 100644 --- a/tests/utilities/test_directory.rs +++ b/tests/utilities/test_directory.rs @@ -2,9 +2,9 @@ mod tests { use serde_yml::utilities::directory::{ cleanup_directory, create_directory, directory, - move_output_directory, truncate, + move_output_directory, }; - use std::{fs, io::Error, path::Path}; + use std::{fs, path::Path}; use tempfile::tempdir; /// Tests that the `directory` function correctly creates a directory if it does not exist. @@ -86,19 +86,4 @@ mod tests { fs::create_dir(&dir).unwrap(); assert!(create_directory(&[&dir]).is_ok()); } - - /// Tests the `truncate` function with different path lengths. - #[test] - fn test_truncate_path() { - let path = Path::new("/a/b/c/d/e"); - - let result = truncate(&path, 3); - assert_eq!(result, Some("c/d/e".to_string())); - - let result = truncate(&path, 0); - assert_eq!(result, None); - - let result = truncate(&path, 10); - assert_eq!(result, None); - } }