-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate Initializable to component (#764)
* update Scarb * remove internal attr * update initializable * fix formatting * fix spdx doc style * use generate_trait attr * change _comp to _component * add blank line between component! and impl * Apply suggestions from code review Co-authored-by: Eric Nordelo <[email protected]> --------- Co-authored-by: Eric Nordelo <[email protected]>
- Loading branch information
1 parent
b08d0e0
commit 0b77977
Showing
4 changed files
with
42 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#[starknet::contract] | ||
mod InitializableMock { | ||
use openzeppelin::security::initializable::Initializable as initializable_component; | ||
|
||
component!(path: initializable_component, storage: initializable, event: InitializableEvent); | ||
|
||
impl InternalImpl = initializable_component::InternalImpl<ContractState>; | ||
|
||
#[storage] | ||
struct Storage { | ||
#[substorage(v0)] | ||
initializable: initializable_component::Storage | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
InitializableEvent: initializable_component::Event | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
use openzeppelin::security::initializable::Initializable::InternalImpl; | ||
use openzeppelin::security::initializable::Initializable; | ||
use openzeppelin::tests::mocks::initializable_mock::InitializableMock; | ||
|
||
fn STATE() -> Initializable::ContractState { | ||
Initializable::contract_state_for_testing() | ||
fn STATE() -> InitializableMock::ContractState { | ||
InitializableMock::contract_state_for_testing() | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
fn test_initialize() { | ||
let mut state = STATE(); | ||
assert(!InternalImpl::is_initialized(@state), 'Should not be initialized'); | ||
InternalImpl::initialize(ref state); | ||
assert(InternalImpl::is_initialized(@state), 'Should be initialized'); | ||
assert(!state.initializable.is_initialized(), 'Should not be initialized'); | ||
state.initializable.initialize(); | ||
assert(state.initializable.is_initialized(), 'Should be initialized'); | ||
} | ||
|
||
#[test] | ||
#[available_gas(2000000)] | ||
#[should_panic(expected: ('Initializable: is initialized',))] | ||
fn test_initialize_when_initialized() { | ||
let mut state = STATE(); | ||
InternalImpl::initialize(ref state); | ||
InternalImpl::initialize(ref state); | ||
state.initializable.initialize(); | ||
state.initializable.initialize(); | ||
} |