Skip to content

Commit

Permalink
feat: add warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnordelo committed Jan 8, 2025
1 parent 6814d0b commit 44c6491
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
source: src/tests/test_with_components.rs
expression: result
snapshot_kind: text
---
TokenStream:

#[starknet::contract]
pub mod Contract {
use openzeppelin_access::accesscontrol::DEFAULT_ADMIN_ROLE;
use starknet::ContractAddress;

#[storage]
pub struct Storage {
#[substorage(v0)]
accesscontrol: AccessControlComponent::Storage,
}

#[constructor]
fn constructor(ref self: ContractState, default_admin: ContractAddress) {
self.accesscontrol.initializer();

self.accesscontrol._grant_role(DEFAULT_ADMIN_ROLE, default_admin);
}

use openzeppelin_access::accesscontrol::AccessControlComponent;

component!(path: AccessControlComponent, storage: accesscontrol, event: AccessControlEvent);

impl AccessControlInternalImpl = AccessControlComponent::InternalImpl<ContractState>;

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
AccessControlEvent: AccessControlComponent::Event,
}
}


Diagnostics:

None

AuxData:

None
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
source: src/tests/test_with_components.rs
expression: result
snapshot_kind: text
---
TokenStream:

#[starknet::contract]
pub mod Contract {
use openzeppelin_access::accesscontrol::DEFAULT_ADMIN_ROLE;
use starknet::ContractAddress;

#[storage]
pub struct Storage {
#[substorage(v0)]
accesscontrol: AccessControlComponent::Storage,
}

#[constructor]
fn constructor(ref self: ContractState) {}

use openzeppelin_access::accesscontrol::AccessControlComponent;

component!(path: AccessControlComponent, storage: accesscontrol, event: AccessControlEvent);

impl AccessControlInternalImpl = AccessControlComponent::InternalImpl<ContractState>;

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
AccessControlEvent: AccessControlComponent::Event,
}
}


Diagnostics:

====
Warning: It looks like the initilizers for the following components are missing:

AccessControl

This may lead to unexpected behavior. We recommend adding the corresponding initializer calls to the constructor.
====

AuxData:

None
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
source: src/tests/test_with_components.rs
expression: result
snapshot_kind: text
---
TokenStream:

#[starknet::contract]
pub mod VestingWallet {
use openzeppelin_finance::vesting::LinearVestingSchedule;
use starknet::ContractAddress;

#[storage]
pub struct Storage {
#[substorage(v0)]
vesting: VestingComponent::Storage,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
}

#[constructor]
fn constructor(
ref self: ContractState,
beneficiary: ContractAddress,
start: u64,
duration: u64,
cliff_duration: u64,
) {
self.ownable.initializer(beneficiary);
self.vesting.initializer(start, duration, cliff_duration);
}

use openzeppelin_finance::vesting::VestingComponent;
use openzeppelin_access::ownable::OwnableComponent;

component!(path: VestingComponent, storage: vesting, event: VestingEvent);
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);

impl VestingInternalImpl = VestingComponent::InternalImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
VestingEvent: VestingComponent::Event,
#[flat]
OwnableEvent: OwnableComponent::Event,
}
}


Diagnostics:

None

AuxData:

None
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
source: src/tests/test_with_components.rs
expression: result
snapshot_kind: text
---
TokenStream:

#[starknet::contract]
pub mod VestingWallet {
use openzeppelin_finance::vesting::LinearVestingSchedule;
use starknet::ContractAddress;

#[storage]
pub struct Storage {
#[substorage(v0)]
vesting: VestingComponent::Storage,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
}

#[constructor]
fn constructor(
ref self: ContractState,
beneficiary: ContractAddress,
start: u64,
duration: u64,
cliff_duration: u64,
) {
self.ownable.initializer(beneficiary);
}

use openzeppelin_finance::vesting::VestingComponent;
use openzeppelin_access::ownable::OwnableComponent;

component!(path: VestingComponent, storage: vesting, event: VestingEvent);
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);

impl VestingInternalImpl = VestingComponent::InternalImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
VestingEvent: VestingComponent::Event,
#[flat]
OwnableEvent: OwnableComponent::Event,
}
}


Diagnostics:

====
Warning: It looks like the initilizers for the following components are missing:

Vesting

This may lead to unexpected behavior. We recommend adding the corresponding initializer calls to the constructor.
====

AuxData:

None
110 changes: 110 additions & 0 deletions packages/macros/src/tests/test_with_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,116 @@ fn test_with_two_components_no_constructor() {
assert_snapshot!(result);
}

#[test]
fn test_with_access_control() {
let attribute = "(AccessControl)";
let item = indoc!(
"
#[starknet::contract]
pub mod Contract {
use openzeppelin_access::accesscontrol::DEFAULT_ADMIN_ROLE;
use starknet::ContractAddress;
#[storage]
pub struct Storage {}
#[constructor]
fn constructor(ref self: ContractState, default_admin: ContractAddress) {
self.accesscontrol.initializer();
self.accesscontrol._grant_role(DEFAULT_ADMIN_ROLE, default_admin);
}
}
"
);
let result = get_string_result(attribute, item);
assert_snapshot!(result);
}

#[test]
fn test_with_access_control_no_initializer() {
let attribute = "(AccessControl)";
let item = indoc!(
"
#[starknet::contract]
pub mod Contract {
use openzeppelin_access::accesscontrol::DEFAULT_ADMIN_ROLE;
use starknet::ContractAddress;
#[storage]
pub struct Storage {}
#[constructor]
fn constructor(ref self: ContractState) {
}
}
"
);
let result = get_string_result(attribute, item);
assert_snapshot!(result);
}

#[test]
fn test_with_vesting() {
let attribute = "(Vesting, Ownable)";
let item = indoc!(
"
#[starknet::contract]
pub mod VestingWallet {
use openzeppelin_finance::vesting::LinearVestingSchedule;
use starknet::ContractAddress;
#[storage]
pub struct Storage {}
#[constructor]
fn constructor(
ref self: ContractState,
beneficiary: ContractAddress,
start: u64,
duration: u64,
cliff_duration: u64,
) {
self.ownable.initializer(beneficiary);
self.vesting.initializer(start, duration, cliff_duration);
}
}
"
);
let result = get_string_result(attribute, item);
assert_snapshot!(result);
}

#[test]
fn test_with_vesting_no_initializer() {
let attribute = "(Vesting, Ownable)";
let item = indoc!(
"
#[starknet::contract]
pub mod VestingWallet {
use openzeppelin_finance::vesting::LinearVestingSchedule;
use starknet::ContractAddress;
#[storage]
pub struct Storage {}
#[constructor]
fn constructor(
ref self: ContractState,
beneficiary: ContractAddress,
start: u64,
duration: u64,
cliff_duration: u64,
) {
self.ownable.initializer(beneficiary);
}
}
"
);
let result = get_string_result(attribute, item);
assert_snapshot!(result);
}

#[test]
fn test_with_no_contract_attribute() {
let attribute = "(Ownable)";
Expand Down
Loading

0 comments on commit 44c6491

Please sign in to comment.