Skip to content

Commit

Permalink
fix: templates errors (#329)
Browse files Browse the repository at this point in the history
* fix: templates

* fix: refactor and safe name replacement
  • Loading branch information
AlexD10S authored Nov 7, 2024
1 parent c807ba0 commit 0704723
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
35 changes: 31 additions & 4 deletions crates/pop-contracts/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,19 @@ pub fn rename_contract(name: &str, path: PathBuf, template: &Contract) -> Result
// Replace name in the lib.rs file.
file_path = path.join("lib.rs");
let name_in_camel_case = name.to_upper_camel_case();
let mut replacements_in_contract = HashMap::new();
replacements_in_contract.insert(template_name.as_str(), name);
replacements_in_contract.insert(template.name(), &name_in_camel_case);
replace_in_file(file_path, replacements_in_contract)?;
replace_in_file(
file_path,
HashMap::from([(template_name.as_str(), name), (template.name(), &name_in_camel_case)]),
)?;
// Replace name in the e2e_tests.rs file if exists.
let e2e_tests = path.join("e2e_tests.rs");
if e2e_tests.exists() {
let name_in_camel_case = format!("\"{}\"", name.to_upper_camel_case());
replace_in_file(
e2e_tests,
HashMap::from([(template_name.as_str(), name), (template.name(), &name_in_camel_case)]),
)?;
}
Ok(())
}

Expand Down Expand Up @@ -187,6 +196,20 @@ mod tests {
mod erc20
"#
)?;

let e2e_code = temp_dir.path().join("e2e_tests.rs");
let mut e2e_code_file = fs::File::create(e2e_code.clone())?;
writeln!(
e2e_code_file,
r#"
#[ink_e2e::test]
let contract = client
.instantiate("erc20", &ink_e2e::alice(), &mut constructor)
.submit()
.await
.expect("erc20 instantiate failed");
"#
)?;
Ok(temp_dir)
}
#[test]
Expand All @@ -200,6 +223,10 @@ mod tests {
let generated_code =
fs::read_to_string(temp_dir.path().join("lib.rs")).expect("Could not read file");
assert!(generated_code.contains("mod my_contract"));
let generated_e2e_code =
fs::read_to_string(temp_dir.path().join("e2e_tests.rs")).expect("Could not read file");
assert!(generated_e2e_code
.contains(".instantiate(\"my_contract\", &ink_e2e::alice(), &mut constructor)"));

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/pop-contracts/src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub enum Contract {
serialize = "PSP34",
message = "Psp34",
detailed_message = "The implementation of the PSP34 standard in ink!",
props(Type = "PSP", Repository = "https://github.com/Cardinal-Cryptography/PSP34")
props(Type = "PSP", Repository = "https://github.com/r0gue-io/PSP34")
)]
PSP34,
/// Domain name service example implemented in ink!
Expand Down Expand Up @@ -162,7 +162,7 @@ mod tests {
("erc721".to_string(), "https://github.com/use-ink/ink-examples"),
("erc1155".to_string(), "https://github.com/use-ink/ink-examples"),
("PSP22".to_string(), "https://github.com/Cardinal-Cryptography/PSP22"),
("PSP34".to_string(), "https://github.com/Cardinal-Cryptography/PSP34"),
("PSP34".to_string(), "https://github.com/r0gue-io/PSP34"),
("dns".to_string(), "https://github.com/use-ink/ink-examples"),
("cross-contract-calls".to_string(), "https://github.com/use-ink/ink-examples"),
("multisig".to_string(), "https://github.com/use-ink/ink-examples"),
Expand Down

0 comments on commit 0704723

Please sign in to comment.