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 deploy cheatcode #2132

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
11 changes: 11 additions & 0 deletions protostar-rust/tests/data/deploy_test/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "deploy_test"
version = "0.1.0"

[[target.starknet-contract]]
sierra = true

[dependencies]
starknet = "2.0.0-rc2"

[tool.protostar]
19 changes: 19 additions & 0 deletions protostar-rust/tests/data/deploy_test/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[starknet::contract]
mod HelloStarknet {
#[storage]
struct Storage {
balance: felt252,
}

// Increases the balance by the given amount.
#[external]
fn increase_balance(ref self: ContractState, amount: felt252) {
self.balance.write(self.balance.read() + amount);
}

// Returns the current balance.
#[external]
fn get_balance(self: @ContractState) -> felt252 {
self.balance.read()
}
}
19 changes: 19 additions & 0 deletions protostar-rust/tests/data/deploy_test/tests/test_deploy.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use result::ResultTrait;
use protostar_print::PrintTrait;
use array::ArrayTrait;
use cheatcodes::PreparedContract;

#[test]
fn test_deploy_simple() {
let calldata = ArrayTrait::<felt252>::new();
let class_hash = declare('deploy_test').expect('Could not declare');

let contract_address = deploy(
PreparedContract {
class_hash,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen if non-existing class_hash is passed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess panic, but this can be tested

constructor_calldata: @calldata,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen if error is raised by the constructor?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure constructor is executed, can we have test case for that?

Copy link
Member Author

@Arcticae Arcticae Jul 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen if error is raised by the constructor

For now it panics, but it's not the target behavior

Are we sure constructor is executed, can we have test case for that?

OK.

contract_address: 'addr'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So deploy will use whatever contract address it gets passed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sir

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you can see here

Copy link
Contributor

@MaksymilianDemitraszek MaksymilianDemitraszek Jul 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if he had some simplification here so user can write something like this

PreparedContract::from_class_hash(class_hash);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally user would use deploy_contract for the simplest flow

Copy link
Contributor

@MaksymilianDemitraszek MaksymilianDemitraszek Jul 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally user would use deploy_contract for the simplest flow

I think deploy_contract will be obsolete when we remove declare cheatcode, and predeclare all contracts instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was not aware of such idea, fair point. Although this is out of scope for this PR.

}
).expect('Could not deploy');
assert(contract_address == 'addr', 'incorrect contract_address');
}
19 changes: 19 additions & 0 deletions protostar-rust/tests/e2e/running.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ fn with_declare() {
"#});
}

#[test]
fn with_deploy() {
let temp = assert_fs::TempDir::new().unwrap();
temp.copy_from("tests/data/deploy_test", &["**/*"]).unwrap();

let snapbox = runner();

snapbox
.current_dir(&temp)
.assert()
.success()
.stdout_matches(indoc! {r#"Collected 1 test(s) and 2 test file(s)
Running 0 test(s) from src/lib.cairo
Running 1 test(s) from tests/test_deploy.cairo
[PASS] test_deploy::test_deploy::test_deploy_simple
Tests: 1 passed, 0 failed
"#});
}

#[test]
fn with_print() {
let temp = assert_fs::TempDir::new().unwrap();
Expand Down