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 test contract #70

Merged
merged 12 commits into from
Mar 22, 2024
42 changes: 35 additions & 7 deletions src/engines/contract_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,48 @@ pub async fn dry_run_call(
mod tests {
use super::*;
use std::fs;
use std::path::PathBuf;
brunopgalvao marked this conversation as resolved.
Show resolved Hide resolved

fn setup_test_environment() -> anyhow::Result<tempfile::TempDir, anyhow::Error> {
brunopgalvao marked this conversation as resolved.
Show resolved Hide resolved
let temp_contract_dir = tempfile::tempdir().expect("Could not create temp dir");
let result: anyhow::Result<()> = create_smart_contract(
"test_contract".to_string(),
&Some(PathBuf::from(temp_contract_dir.path())),
);

assert!(result.is_ok(), "Result should be Ok");

Ok(temp_contract_dir)
}

#[test]
fn test_create_smart_contract() -> Result<(), Box<dyn std::error::Error>> {
let temp_dir = tempfile::tempdir()?;
let result: anyhow::Result<()> =
create_smart_contract("test".to_string(), &Some(PathBuf::from(temp_dir.path())));
assert!(result.is_ok());
fn test_contract_create() -> anyhow::Result<(), anyhow::Error> {
let temp_contract_dir = setup_test_environment()?;

// Verify that the generated smart contract contains the expected content
let generated_file_content = fs::read_to_string(temp_dir.path().join("test/lib.rs"))?;
let generated_file_content =
fs::read_to_string(temp_contract_dir.path().join("test_contract/lib.rs"))
.expect("Could not read file");

assert!(generated_file_content.contains("#[ink::contract]"));
assert!(generated_file_content.contains("mod test {"));
assert!(generated_file_content.contains("mod test_contract {"));

// Verify that the generated Cargo.toml file contains the expected content
let generated_file_content =
fs::read_to_string(temp_contract_dir.path().join("test_contract/Cargo.toml"))
.expect("Could not read file");
assert!(generated_file_content.contains("[package]"));
brunopgalvao marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

#[test]
fn test_contract_test() -> anyhow::Result<(), anyhow::Error> {
let temp_contract_dir = setup_test_environment()?;

let result = test_smart_contract(&Some(temp_contract_dir.path().join("test_contract")));

assert!(result.is_ok(), "Result should be Ok");
Ok(())
}
}
Loading