Skip to content

Commit

Permalink
Force test-context to be initialized with an actor (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpringle authored Oct 23, 2024
1 parent 1a3c670 commit cf6d48a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 29 deletions.
5 changes: 2 additions & 3 deletions x/contracts/examples/counter-external/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ mod tests {

#[test]
fn get_value_exists() {
let mut ctx = Context::new();
let external = Address::new([0; 33]);
let of = Address::new([1; 33]);
let [external, of] = [0, 1].map(|i| Address::new([i; 33]));
let mut ctx = Context::with_actor(external);

// mock `get_value` external contract call to return `value`
let value = 5_u64;
Expand Down
24 changes: 8 additions & 16 deletions x/contracts/examples/multisig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,7 @@ mod tests {

let voters = Voters::Addresses(HashSet::from([bob]));
let quorum = NonZeroU32::new(1).unwrap();
let mut ctx = Context::new();
ctx.set_actor(bob);
let mut ctx = Context::with_actor(bob);

propose(&mut ctx, proposal, voters, quorum);
}
Expand All @@ -301,8 +300,7 @@ mod tests {
args: Default::default(),
};

let mut ctx = Context::new();
ctx.set_actor(bob);
let mut ctx = Context::with_actor(bob);

propose(&mut ctx, proposal, voters, quorum);
}
Expand Down Expand Up @@ -330,8 +328,7 @@ mod tests {
args: vec![],
};

let mut ctx = Context::new();
ctx.set_actor(bob);
let mut ctx = Context::with_actor(bob);

let id = propose(&mut ctx, proposal, voters, NonZeroU32::new(quorum).unwrap());

Expand All @@ -357,8 +354,7 @@ mod tests {
args: vec![],
};

let ctx = &mut Context::new();
ctx.set_actor(bob);
let ctx = &mut Context::with_actor(bob);

let id = propose(ctx, proposal, voters, quorum);

Expand All @@ -381,8 +377,7 @@ mod tests {
args: args.to_vec(),
};

let mut ctx = Context::new();
ctx.set_actor(bob);
let mut ctx = Context::with_actor(bob);

let id = propose(&mut ctx, proposal, voters, quorum);

Expand Down Expand Up @@ -414,8 +409,7 @@ mod tests {
args: args.to_vec(),
};

let mut ctx = Context::new();
ctx.set_actor(bob);
let mut ctx = Context::with_actor(bob);

let id = propose(&mut ctx, proposal, voters, quorum);
let result = "hello world";
Expand Down Expand Up @@ -450,8 +444,7 @@ mod tests {
args: args.to_vec(),
};

let mut ctx = Context::new();
ctx.set_actor(bob);
let mut ctx = Context::with_actor(bob);

let id = propose(&mut ctx, proposal, voters, quorum);
ctx.set_actor(charlie);
Expand All @@ -477,8 +470,7 @@ mod tests {
args: args.to_vec(),
};

let mut ctx = Context::new();
ctx.set_actor(bob);
let mut ctx = Context::with_actor(bob);

let id = propose(&mut ctx, proposal, voters, quorum);

Expand Down
11 changes: 2 additions & 9 deletions x/contracts/wasmlanche/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ impl Context {
#[cfg(feature = "test")]
impl Context {
#[must_use]
pub fn new() -> Self {
pub fn with_actor(actor: Address) -> Self {
Self {
contract_address: Address::default(),
actor: Address::default(),
actor,
height: 0,
timestamp: 0,
action_id: Id::default(),
Expand Down Expand Up @@ -399,10 +399,3 @@ mod external {
}
}
}

#[cfg(feature = "test")]
impl Default for Context {
fn default() -> Self {
Self::new()
}
}
2 changes: 1 addition & 1 deletion x/contracts/wasmlanche/tests/test-crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ mod tests {

#[test]
fn test_balance() {
let mut context = Context::new();
let address = Address::default();
let mut context = Context::with_actor(address);
let amount: u64 = 100;

// set the balance
Expand Down

0 comments on commit cf6d48a

Please sign in to comment.