Skip to content

Commit

Permalink
Update forc init to include a more realistic test and contract skel…
Browse files Browse the repository at this point in the history
…eton (FuelLabs#1194)

* updated forc init

* Fixed fmt and warning

* fix clippy

* fmt all?

* fmt the templates

* Fix default contract fmt

* TODO comment

* Added Test Function so things work in 3 cmds now
  • Loading branch information
ControlCplusControlV authored Apr 11, 2022
1 parent a88ea43 commit 08fc150
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion forc/src/ops/forc_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub(crate) fn init_new_project(project_name: String) -> Result<()> {
// Insert default test function
fs::write(
Path::new(&project_name).join("tests").join("harness.rs"),
defaults::default_test_program(),
defaults::default_test_program(&project_name),
)?;

// Ignore default `out` and `target` directories created by forc and cargo.
Expand Down
56 changes: 47 additions & 9 deletions forc/src/utils/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,62 @@ path = "tests/harness.rs"
}

pub(crate) fn default_program() -> String {
r#"script;
r#"contract;
fn main() {
abi MyContract {
fn test_function() -> bool;
}
impl MyContract for Contract {
fn test_function() -> bool {
true
}
}
"#
.into()
}

pub(crate) fn default_test_program() -> String {
r#"
// TODO Ideally after (instance, id) it should link to the The Fuels-rs Book
// to provide further information for writing tests/working with sway
pub(crate) fn default_test_program(project_name: &str) -> String {
format!(
"{}{}{}{}{}",
r#"use fuel_tx::{ContractId, Salt};
use fuels_abigen_macro::abigen;
use fuels_contract::{contract::Contract, parameters::TxParameters};
use fuels_signers::util::test_helpers;
// Load abi from json
abigen!(MyContract, "out/debug/"#,
project_name,
r#"-abi.json");
#[tokio::test]
async fn harness() {
assert_eq!(true, true);
async fn get_contract_instance() -> (MyContract, ContractId) {
// Deploy the compiled contract
let salt = Salt::from([0u8; 32]);
let compiled = Contract::load_sway_contract("./out/debug/"#,
project_name,
r#".bin", salt).unwrap();
// Launch a local network and deploy the contract
let (provider, wallet) = test_helpers::setup_test_provider_and_wallet().await;
let id = Contract::deploy(&compiled, &provider, &wallet, TxParameters::default())
.await
.unwrap();
let instance = MyContract::new(id.to_string(), provider, wallet);
(instance, id)
}
"#
.into()
#[tokio::test]
async fn can_get_contract_id() {
let (_instance, _id) = get_contract_instance().await;
// Now you have an instance of your contract you can use to test each function
}"#
)
}

pub(crate) fn default_gitignore() -> String {
Expand Down

0 comments on commit 08fc150

Please sign in to comment.