Skip to content

Commit

Permalink
test: add unit test for pop new pallet (#84)
Browse files Browse the repository at this point in the history
* add unit test for `create_pallet_template`
* add test for `resolve_pallet_path`
  • Loading branch information
brunopgalvao authored Apr 1, 2024
1 parent 5894fac commit 104dfe0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/engines/pallet_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
22 changes: 22 additions & 0 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,25 @@ pub(crate) fn resolve_pallet_path(path: Option<String>) -> 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");
}
}

0 comments on commit 104dfe0

Please sign in to comment.