Skip to content

Commit

Permalink
Support take immutable and shared in separate APIs in test scenario (M…
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind authored Apr 28, 2022
1 parent 882566f commit 27dff72
Show file tree
Hide file tree
Showing 15 changed files with 424 additions and 224 deletions.
39 changes: 21 additions & 18 deletions sui_programmability/examples/basics/sources/Counter.move
Original file line number Diff line number Diff line change
Expand Up @@ -71,41 +71,44 @@ module Basics::CounterTest {

TestScenario::next_tx(scenario, &user1);
{
let counter = TestScenario::take_object<Counter::Counter>(scenario);
let counter_wrapper = TestScenario::take_shared_object<Counter::Counter>(scenario);
let counter = TestScenario::borrow_mut(&mut counter_wrapper);

assert!(Counter::owner(&counter) == owner, 0);
assert!(Counter::value(&counter) == 0, 1);
assert!(Counter::owner(counter) == owner, 0);
assert!(Counter::value(counter) == 0, 1);

Counter::increment(&mut counter, TestScenario::ctx(scenario));
Counter::increment(&mut counter, TestScenario::ctx(scenario));
Counter::increment(&mut counter, TestScenario::ctx(scenario));
TestScenario::return_object(scenario, counter);
Counter::increment(counter, TestScenario::ctx(scenario));
Counter::increment(counter, TestScenario::ctx(scenario));
Counter::increment(counter, TestScenario::ctx(scenario));
TestScenario::return_shared_object(scenario, counter_wrapper);
};

TestScenario::next_tx(scenario, &owner);
{
let counter = TestScenario::take_object<Counter::Counter>(scenario);
let counter_wrapper = TestScenario::take_shared_object<Counter::Counter>(scenario);
let counter = TestScenario::borrow_mut(&mut counter_wrapper);

assert!(Counter::owner(&counter) == owner, 0);
assert!(Counter::value(&counter) == 3, 1);
assert!(Counter::owner(counter) == owner, 0);
assert!(Counter::value(counter) == 3, 1);

Counter::set_value(&mut counter, 100, TestScenario::ctx(scenario));
Counter::set_value(counter, 100, TestScenario::ctx(scenario));

TestScenario::return_object(scenario, counter);
TestScenario::return_shared_object(scenario, counter_wrapper);
};

TestScenario::next_tx(scenario, &user1);
{
let counter = TestScenario::take_object<Counter::Counter>(scenario);
let counter_wrapper = TestScenario::take_shared_object<Counter::Counter>(scenario);
let counter = TestScenario::borrow_mut(&mut counter_wrapper);

assert!(Counter::owner(&counter) == owner, 0);
assert!(Counter::value(&counter) == 100, 1);
assert!(Counter::owner(counter) == owner, 0);
assert!(Counter::value(counter) == 100, 1);

Counter::increment(&mut counter, TestScenario::ctx(scenario));
Counter::increment(counter, TestScenario::ctx(scenario));

assert!(Counter::value(&counter) == 101, 2);
assert!(Counter::value(counter) == 101, 2);

TestScenario::return_object(scenario, counter);
TestScenario::return_shared_object(scenario, counter_wrapper);
};
}
}
9 changes: 5 additions & 4 deletions sui_programmability/examples/basics/sources/Lock.move
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,15 @@ module Basics::LockTest {
// User2 is impatient and he decides to take the treasure.
TestScenario::next_tx(scenario, &user2);
{
let lock = TestScenario::take_object<Lock<Treasure>>(scenario);
let lock_wrapper = TestScenario::take_shared_object<Lock<Treasure>>(scenario);
let lock = TestScenario::borrow_mut(&mut lock_wrapper);
let key = TestScenario::take_object<Key<Treasure>>(scenario);
let ctx = TestScenario::ctx(scenario);

Lock::take<Treasure>(&mut lock, &key, ctx);
Lock::take<Treasure>(lock, &key, ctx);

TestScenario::return_object<Lock<Treasure>>(scenario, lock);
TestScenario::return_object<Key<Treasure>>(scenario, key);
TestScenario::return_shared_object(scenario, lock_wrapper);
TestScenario::return_object(scenario, key);
};
}
}
48 changes: 24 additions & 24 deletions sui_programmability/examples/basics/sources/Sandwich.move
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module Basics::Sandwich {

/// On module init, create a grocery
fun init(ctx: &mut TxContext) {
Transfer::share_object(Grocery {
Transfer::share_object(Grocery {
id: TxContext::new_id(ctx),
profits: Coin::zero<SUI>(ctx)
});
Expand All @@ -58,8 +58,8 @@ module Basics::Sandwich {

/// Exchange `c` for some ham
public(script) fun buy_ham(
grocery: &mut Grocery,
c: Coin<SUI>,
grocery: &mut Grocery,
c: Coin<SUI>,
ctx: &mut TxContext
) {
assert!(Coin::value(&c) == HAM_PRICE, EINSUFFICIENT_FUNDS);
Expand All @@ -69,8 +69,8 @@ module Basics::Sandwich {

/// Exchange `c` for some bread
public(script) fun buy_bread(
grocery: &mut Grocery,
c: Coin<SUI>,
grocery: &mut Grocery,
c: Coin<SUI>,
ctx: &mut TxContext
) {
assert!(Coin::value(&c) == BREAD_PRICE, EINSUFFICIENT_FUNDS);
Expand All @@ -97,7 +97,7 @@ module Basics::Sandwich {
/// Owner of the grocery can collect profits by passing his capability
public(script) fun collect_profits(_cap: &GroceryOwnerCapability, grocery: &mut Grocery, ctx: &mut TxContext) {
let amount = Coin::value(&grocery.profits);

assert!(amount > 0, ENO_PROFITS);

let coin = Coin::withdraw(&mut grocery.profits, amount, ctx);
Expand All @@ -116,7 +116,7 @@ module Basics::TestSandwich {
use Sui::TestScenario;
use Sui::Coin::{Self};
use Sui::SUI::SUI;

#[test]
public(script) fun test_make_sandwich() {
let owner = @0x1;
Expand All @@ -130,45 +130,45 @@ module Basics::TestSandwich {

TestScenario::next_tx(scenario, &the_guy);
{
let grocery = TestScenario::take_object<Grocery>(scenario);
let grocery_wrapper = TestScenario::take_shared_object<Grocery>(scenario);
let grocery = TestScenario::borrow_mut(&mut grocery_wrapper);
let ctx = TestScenario::ctx(scenario);

Sandwich::buy_ham(
&mut grocery,
Coin::mint_for_testing<SUI>(10, ctx),
grocery,
Coin::mint_for_testing<SUI>(10, ctx),
ctx
);

Sandwich::buy_bread(
&mut grocery,
Coin::mint_for_testing<SUI>(2, ctx),
grocery,
Coin::mint_for_testing<SUI>(2, ctx),
ctx
);
TestScenario::return_object(scenario, grocery);

TestScenario::return_shared_object(scenario, grocery_wrapper);
};

TestScenario::next_tx(scenario, &the_guy);
{
let grocery = TestScenario::take_object<Grocery>(scenario);
let ham = TestScenario::take_object<Ham>(scenario);
let bread = TestScenario::take_object<Bread>(scenario);

Sandwich::make_sandwich(ham, bread, TestScenario::ctx(scenario));
TestScenario::return_object(scenario, grocery);
};

TestScenario::next_tx(scenario, &owner);
{
let grocery = TestScenario::take_object<Grocery>(scenario);
{
let grocery_wrapper = TestScenario::take_shared_object<Grocery>(scenario);
let grocery = TestScenario::borrow_mut(&mut grocery_wrapper);
let capability = TestScenario::take_object<GroceryOwnerCapability>(scenario);

assert!(Sandwich::profits(&grocery) == 12, 0);
Sandwich::collect_profits(&capability, &mut grocery, TestScenario::ctx(scenario));
assert!(Sandwich::profits(&grocery) == 0, 0);
assert!(Sandwich::profits(grocery) == 12, 0);
Sandwich::collect_profits(&capability, grocery, TestScenario::ctx(scenario));
assert!(Sandwich::profits(grocery) == 0, 0);

TestScenario::return_object(scenario, capability);
TestScenario::return_object(scenario, grocery);
TestScenario::return_shared_object(scenario, grocery_wrapper);
};
}
}
20 changes: 11 additions & 9 deletions sui_programmability/examples/defi/tests/FlashLenderTests.move
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,38 @@ module DeFi::FlashLenderTests {
// borrower requests and repays a loan of 10 coins + the fee
TestScenario::next_tx(scenario, &borrower);
{
let lender = TestScenario::take_object<FlashLender<SUI>>(scenario);
let lender_wrapper = TestScenario::take_shared_object<FlashLender<SUI>>(scenario);
let lender = TestScenario::borrow_mut(&mut lender_wrapper);
let ctx = TestScenario::ctx(scenario);

let (loan, receipt) = FlashLender::loan(&mut lender, 10, ctx);
let (loan, receipt) = FlashLender::loan(lender, 10, ctx);
// in practice, borrower does something (e.g., arbitrage) to make a profit from the loan.
// simulate this by min ting the borrower 5 coins.
let profit = Coin::mint_for_testing<SUI>(5, ctx);
Coin::join(&mut profit, loan);
let to_keep = Coin::withdraw(&mut profit, 4, ctx);
Coin::keep(to_keep, ctx);
FlashLender::repay(&mut lender, profit, receipt);
FlashLender::repay(lender, profit, receipt);

TestScenario::return_object(scenario, lender);
TestScenario::return_shared_object(scenario, lender_wrapper);
};
// admin withdraws the 1 coin profit from lending
TestScenario::next_tx(scenario, &admin);
{
let lender = TestScenario::take_object<FlashLender<SUI>>(scenario);
let lender_wrapper = TestScenario::take_shared_object<FlashLender<SUI>>(scenario);
let lender = TestScenario::borrow_mut(&mut lender_wrapper);
let admin_cap = TestScenario::take_object<AdminCap>(scenario);
let ctx = TestScenario::ctx(scenario);

// max loan size should have increased because of the fee payment
assert!(FlashLender::max_loan(&lender) == 101, 0);
assert!(FlashLender::max_loan(lender) == 101, 0);
// withdraw 1 coin from the pool available for lending
let coin = FlashLender::withdraw(&mut lender, &admin_cap, 1, ctx);
let coin = FlashLender::withdraw(lender, &admin_cap, 1, ctx);
// max loan size should decrease accordingly
assert!(FlashLender::max_loan(&lender) == 100, 0);
assert!(FlashLender::max_loan(lender) == 100, 0);
Coin::keep(coin, ctx);

TestScenario::return_object(scenario, lender);
TestScenario::return_shared_object(scenario, lender_wrapper);
TestScenario::return_object(scenario, admin_cap);
}
}
Expand Down
14 changes: 8 additions & 6 deletions sui_programmability/examples/defi/tests/SharedEscrowTest.move
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,25 @@ module DeFi::SharedEscrowTests {
public(script) fun cancel(scenario: &mut Scenario, initiator: &address) {
TestScenario::next_tx(scenario, initiator);
{
let escrow = TestScenario::take_object<EscrowedObj<ItemA, ItemB>>(scenario);
let escrow_wrapper = TestScenario::take_shared_object<EscrowedObj<ItemA, ItemB>>(scenario);
let escrow = TestScenario::borrow_mut(&mut escrow_wrapper);
let ctx = TestScenario::ctx(scenario);
SharedEscrow::cancel(&mut escrow, ctx);
TestScenario::return_object(scenario, escrow);
SharedEscrow::cancel(escrow, ctx);
TestScenario::return_shared_object(scenario, escrow_wrapper);
};
}

public(script) fun exchange(scenario: &mut Scenario, bob: &address, item_b_verioned_id: VersionedID) {
TestScenario::next_tx(scenario, bob);
{
let escrow = TestScenario::take_object<EscrowedObj<ItemA, ItemB>>(scenario);
let escrow_wrapper = TestScenario::take_shared_object<EscrowedObj<ItemA, ItemB>>(scenario);
let escrow = TestScenario::borrow_mut(&mut escrow_wrapper);
let item_b = ItemB {
id: item_b_verioned_id
};
let ctx = TestScenario::ctx(scenario);
SharedEscrow::exchange(item_b, &mut escrow, ctx);
TestScenario::return_object(scenario, escrow);
SharedEscrow::exchange(item_b, escrow, ctx);
TestScenario::return_shared_object(scenario, escrow_wrapper);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@ module FungibleTokens::BASKETTests {
};
TestScenario::next_tx(scenario, &user);
{
let reserve = TestScenario::take_object<Reserve>(scenario);
let reserve_wrapper = TestScenario::take_shared_object<Reserve>(scenario);
let reserve = TestScenario::borrow_mut(&mut reserve_wrapper);
let ctx = TestScenario::ctx(scenario);
assert!(BASKET::total_supply(&reserve) == 0, 0);
assert!(BASKET::total_supply(reserve) == 0, 0);

let num_coins = 10;
let sui = Coin::mint_for_testing<SUI>(num_coins, ctx);
let managed = Coin::mint_for_testing<MANAGED>(num_coins, ctx);
let basket = BASKET::mint(&mut reserve, sui, managed, ctx);
let basket = BASKET::mint(reserve, sui, managed, ctx);
assert!(Coin::value(&basket) == num_coins, 1);
assert!(BASKET::total_supply(&reserve) == num_coins, 2);
assert!(BASKET::total_supply(reserve) == num_coins, 2);

let (sui, managed) = BASKET::burn(&mut reserve, basket, ctx);
let (sui, managed) = BASKET::burn(reserve, basket, ctx);
assert!(Coin::value(&sui) == num_coins, 3);
assert!(Coin::value(&managed) == num_coins, 4);

Coin::keep(sui, ctx);
Coin::keep(managed, ctx);
TestScenario::return_object(scenario, reserve);
TestScenario::return_shared_object(scenario, reserve_wrapper);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,11 @@ module Games::SharedTicTacToeTests {
TestScenario::next_tx(scenario, player);
let status;
{
let game = TestScenario::take_object<TicTacToe>(scenario);
SharedTicTacToe::place_mark(&mut game, row, col, TestScenario::ctx(scenario));
status = SharedTicTacToe::get_status(&game);
TestScenario::return_object(scenario, game);
let game_wrapper = TestScenario::take_shared_object<TicTacToe>(scenario);
let game = TestScenario::borrow_mut(&mut game_wrapper);
SharedTicTacToe::place_mark(game, row, col, TestScenario::ctx(scenario));
status = SharedTicTacToe::get_status(game);
TestScenario::return_shared_object(scenario, game_wrapper);
};
status
}
Expand Down
Loading

0 comments on commit 27dff72

Please sign in to comment.