Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add unit test for pop new pallet #84

Merged
merged 7 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
Loading