diff --git a/src/engines/pallet_engine.rs b/src/engines/pallet_engine.rs index 7a7e3e5e..f3d06ea4 100644 --- a/src/engines/pallet_engine.rs +++ b/src/engines/pallet_engine.rs @@ -62,3 +62,60 @@ fn render_pallet( } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_pallet_create_template() { + let temp_dir = tempfile::tempdir().expect("Failed to create temp dir"); + let pallet_name = "MyPallet"; + let config = TemplatePalletConfig { + name: pallet_name.to_string(), + authors: "Alice".to_string(), + description: "A sample pallet".to_string(), + }; + + // Call the function being tested + let result = + create_pallet_template(Some(temp_dir.path().to_str().unwrap().to_string()), config); + + // Assert that the result is Ok + assert!(result.is_ok(), "Result should be Ok"); + + // Assert that the pallet structure is generated + let pallet_path = temp_dir.path().join(pallet_name); + assert!(pallet_path.exists(), "Pallet folder should be created"); + assert!(pallet_path.join("src").exists(), "src folder should be created"); + assert!(pallet_path.join("Cargo.toml").exists(), "Cargo.toml should be created"); + assert!(pallet_path.join("src").join("lib.rs").exists(), "lib.rs should be created"); + assert!( + pallet_path.join("src").join("benchmarking.rs").exists(), + "benchmarking.rs should be created" + ); + assert!(pallet_path.join("src").join("tests.rs").exists(), "tests.rs should be created"); + assert!(pallet_path.join("src").join("mock.rs").exists(), "mock.rs should be created"); + + let lib_rs_content = fs::read_to_string(pallet_path.join("src").join("lib.rs")) + .expect("Failed to read lib.rs"); + assert!(lib_rs_content.contains("pub mod pallet"), "lib.rs should contain pub mod pallet"); + } + + #[test] + fn test_pallet_create_template_invalid_path() { + let invalid_path = "/invalid/path/that/does/not/exist"; + let pallet_name = "MyPallet"; + let config = TemplatePalletConfig { + name: pallet_name.to_string(), + authors: "Alice".to_string(), + description: "A sample pallet".to_string(), + }; + + // Call the function being tested with an invalid path + let result = create_pallet_template(Some(invalid_path.to_string()), config); + + // Assert that the result is an error + assert!(result.is_err(), "Result should be an error"); + } +} diff --git a/src/helpers.rs b/src/helpers.rs index 71299d32..293b3626 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -99,3 +99,25 @@ pub(crate) fn resolve_pallet_path(path: Option) -> PathBuf { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_resolve_pallet_path_with_no_path() { + let result = resolve_pallet_path(None); + let working_path = std::env::current_dir().unwrap().join("pallets"); + assert_eq!(result, working_path); + } + + #[test] + fn test_resolve_pallet_path_with_custom_path() { + let custom_path = tempfile::tempdir().expect("Failed to create temp dir"); + let custom_path_str = custom_path.path().join("my_pallets").to_str().unwrap().to_string(); + + let result = resolve_pallet_path(Some(custom_path_str.clone())); + + assert_eq!(result, custom_path.path().join("my_pallets"), "Unexpected result path"); + } +}