Skip to content

Commit

Permalink
test: ch00/constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Apr 12, 2024
1 parent 12bb167 commit a768477
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
34 changes: 34 additions & 0 deletions listings/getting-started/constructor/src/constructor.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ANCHOR: contract
#[starknet::contract]
pub mod ExampleConstructor {
use starknet::ContractAddress;
Expand All @@ -14,3 +15,36 @@ pub mod ExampleConstructor {
self.names.write(address, name);
}
}
// ANCHOR_END: contract

#[cfg(test)]
mod tests {
use starknet::testing::set_contract_address;
use starknet::{ContractAddress, contract_address_const};
use starknet::SyscallResultTrait;
use starknet::syscalls::deploy_syscall;

use super::{ExampleConstructor, ExampleConstructor::namesContractMemberStateTrait};

#[test]
fn should_deploy_with_constructor_init_value() {
let name: felt252 = 'bob';
let address: ContractAddress = contract_address_const::<'caller'>();

let mut calldata: Array::<felt252> = array![];
calldata.append(name);
calldata.append(address.into());

let (address_0, _) = deploy_syscall(
ExampleConstructor::TEST_CLASS_HASH.try_into().unwrap(), 0, calldata.span(), false
)
.unwrap_syscall();

let state = ExampleConstructor::unsafe_new_contract_state();
set_contract_address(address_0);

let name = state.names.read(address);

assert(name == 'bob', 'name should be bob');
}
}
3 changes: 0 additions & 3 deletions listings/getting-started/constructor/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
mod constructor;

#[cfg(test)]
mod tests;
34 changes: 0 additions & 34 deletions listings/getting-started/constructor/src/tests.cairo

This file was deleted.

2 changes: 1 addition & 1 deletion src/ch00/basics/constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Constructors are a special type of function that runs only once when deploying a
Here's a simple example that demonstrates how to initialize the state of a contract on deployment by defining logic inside a constructor.

```rust
{{#include ../../../listings/getting-started/constructor/src/constructor.cairo}}
{{#rustdoc_include ../../../listings/getting-started/constructor/src/constructor.cairo:contract}}
```

0 comments on commit a768477

Please sign in to comment.