Skip to content

Commit

Permalink
feat(serde_yml): 🎨 sorting unit tests for macros
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 27, 2024
1 parent e587159 commit 19696b0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;

Expand All @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions tests/macros/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 2 additions & 17 deletions tests/utilities/test_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 19696b0

Please sign in to comment.