Skip to content

Commit

Permalink
Rename remove_object to take_object in TestScenario (MystenLabs#1370)
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind authored Apr 14, 2022
1 parent 7a4e7a1 commit 6c24355
Show file tree
Hide file tree
Showing 24 changed files with 169 additions and 169 deletions.
14 changes: 7 additions & 7 deletions doc/move_code/objects_tutorial/sources/Ch1_2/ColorObject.move
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ module Tutorial::ColorObjectTests {
let not_owner = @0x2;
TestScenario::next_tx(scenario, &not_owner);
{
assert!(!TestScenario::can_remove_object<ColorObject>(scenario), 0);
assert!(!TestScenario::can_take_object<ColorObject>(scenario), 0);
};
// Check that @owner indeed owns the just-created ColorObject.
// Also checks the value fields of the object.
TestScenario::next_tx(scenario, &owner);
{
let object = TestScenario::remove_object<ColorObject>(scenario);
let object = TestScenario::take_object<ColorObject>(scenario);
let (red, green, blue) = ColorObject::get_color(&object);
assert!(red == 255 && green == 0 && blue == 255, 0);
TestScenario::return_object(scenario, object);
Expand All @@ -92,14 +92,14 @@ module Tutorial::ColorObjectTests {
// Delete the ColorObject we just created.
TestScenario::next_tx(scenario, &owner);
{
let object = TestScenario::remove_object<ColorObject>(scenario);
let object = TestScenario::take_object<ColorObject>(scenario);
let ctx = TestScenario::ctx(scenario);
ColorObject::delete(object, ctx);
};
// Verify that the object was indeed deleted.
TestScenario::next_tx(scenario, &owner);
{
assert!(!TestScenario::can_remove_object<ColorObject>(scenario), 0);
assert!(!TestScenario::can_take_object<ColorObject>(scenario), 0);
}
}

Expand All @@ -116,19 +116,19 @@ module Tutorial::ColorObjectTests {
let recipient = @0x2;
TestScenario::next_tx(scenario, &owner);
{
let object = TestScenario::remove_object<ColorObject>(scenario);
let object = TestScenario::take_object<ColorObject>(scenario);
let ctx = TestScenario::ctx(scenario);
ColorObject::transfer(object, recipient, ctx);
};
// Check that owner no longer owns the object.
TestScenario::next_tx(scenario, &owner);
{
assert!(!TestScenario::can_remove_object<ColorObject>(scenario), 0);
assert!(!TestScenario::can_take_object<ColorObject>(scenario), 0);
};
// Check that recipient now owns the object.
TestScenario::next_tx(scenario, &recipient);
{
assert!(TestScenario::can_remove_object<ColorObject>(scenario), 0);
assert!(TestScenario::can_take_object<ColorObject>(scenario), 0);
};
}
}
8 changes: 4 additions & 4 deletions doc/src/build/move.md
Original file line number Diff line number Diff line change
Expand Up @@ -688,15 +688,15 @@ function:
TestScenario::next_tx(scenario, &initial_owner);
{
// extract the sword owned by the initial owner
let sword = TestScenario::remove_object<Sword>(scenario);
let sword = TestScenario::take_object<Sword>(scenario);
// transfer the sword to the final owner
sword_transfer(sword, final_owner, TestScenario::ctx(scenario));
};
// third transaction executed by the final sword owner
TestScenario::next_tx(scenario, &final_owner);
{
// extract the sword owned by the final owner
let sword = TestScenario::remove_object<Sword>(scenario);
let sword = TestScenario::take_object<Sword>(scenario);
// verify that the sword has expected properties
assert!(magic(&sword) == 42 && strength(&sword) == 7, 1);
// return the sword to the object pool (it cannot be simply "dropped")
Expand All @@ -719,7 +719,7 @@ the sword it now owns to its final owner. Please note that in *pure
Move* we do not have the notion of Sui storage and, consequently, no
easy way for the emulated Sui transaction to retrieve it from
storage. This is where the `TestScenario` module comes to help - its
`remove_object` function makes an object of a given type (in this case
`take_object` function makes an object of a given type (in this case
of type `Sword`) owned by an address executing the current transaction
available for manipulation by the Move code. (For now, we assume that
there is only one such object.) In this case, the object retrieved
Expand Down Expand Up @@ -878,7 +878,7 @@ We can now create a function to test the module initialization:
TestScenario::next_tx(scenario, &admin);
{
// extract the Forge object
let forge = TestScenario::remove_object<Forge>(scenario);
let forge = TestScenario::take_object<Forge>(scenario);
// verify number of created swords
assert!(swords_created(&forge) == 0, 1);
// return the Forge object to the object pool
Expand Down
10 changes: 5 additions & 5 deletions doc/src/build/programming-with-objects/ch1-object-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,26 @@ let not_owner = @0x2;
// Check that @not_owner does not own the just-created ColorObject.
TestScenario::next_tx(scenario, &not_owner);
{
assert!(!TestScenario::can_remove_object<ColorObject>(scenario), 0);
assert!(!TestScenario::can_take_object<ColorObject>(scenario), 0);
};
```
`TestScenario::next_tx` switches the transaction sender to `@0x2`, which is a new address than the previous one.
`TestScenario::can_remove_object` checks whether an object with the given type actually exists in the global storage owned by the current sender of the transaction. In this code, we assert that we should not be able to remove such an object, because `@0x2` does not own any object.
`TestScenario::can_take_object` checks whether an object with the given type actually exists in the global storage owned by the current sender of the transaction. In this code, we assert that we should not be able to remove such an object, because `@0x2` does not own any object.
> :bulb: The second parameter of `assert!` is the error code. In non-test code, we usually define a list of dedicated error code constants for each type of error that could happen in production. For unit tests though, it's usually unnecessary because there will be way too many assetions and the stacktrace upon error is sufficient to tell where the error happened. Hence we recommend just putting `0` there in unit tests for assertions.
Finally we check that `@0x1` owns the object and the object value is consistent:
```rust
TestScenario::next_tx(scenario, &owner);
{
let object = TestScenario::remove_object<ColorObject>(scenario);
let object = TestScenario::take_object<ColorObject>(scenario);
let (red, green, blue) = ColorObject::get_color(&object);
assert!(red == 255 && green == 0 && blue == 255, 0);
TestScenario::return_object(scenario, object);
};
```
`TestScenario::remove_object` removes the object of given type from global storage that's owned by the current transaction sender (it also implicitly checks `can_remove_object`). If this line of code succeeds, it means that `owner` indeed owns an object of type `ColorObject`.
`TestScenario::take_object` removes the object of given type from global storage that's owned by the current transaction sender (it also implicitly checks `can_take_object`). If this line of code succeeds, it means that `owner` indeed owns an object of type `ColorObject`.
We also check that the field values of the object match with what we set in creation. At the end, we must return the object back to the global storage by calling `TestScenario::return_object` so that it's back to the global storage. This also ensures that if any mutations happened to the object during the test, the global storage is aware of the changes.
You may have noticed that `remove_object` picks the object only based on the type parameter. What if there are multiple objects of the same type owned by the account? How do we retrieve each of them? In fact, if you call `remove_object` when there are more than one object of the same type in the same account, an assertion failure will be triggered. We are working on adding an API just for this. Update coming soon.
You may have noticed that `take_object` picks the object only based on the type parameter. What if there are multiple objects of the same type owned by the account? How do we retrieve each of them? In fact, if you call `take_object` when there are more than one object of the same type in the same account, an assertion failure will be triggered. We are working on adding an API just for this. Update coming soon.

Again, you can find the full code [here](../../../move_code/objects_tutorial/sources/Ch1_2/ColorObject.move).

Expand Down
10 changes: 5 additions & 5 deletions doc/src/build/programming-with-objects/ch2-using-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ let scenario = &mut TestScenario::begin(&owner);
// Delete the ColorObject we just created.
TestScenario::next_tx(scenario, &owner);
{
let object = TestScenario::remove_object<ColorObject>(scenario);
let object = TestScenario::take_object<ColorObject>(scenario);
let ctx = TestScenario::ctx(scenario);
ColorObject::delete(object, ctx);
};
// Verify that the object was indeed deleted.
TestScenario::next_tx(scenario, &owner);
{
assert!(!TestScenario::can_remove_object<ColorObject>(scenario), 0);
assert!(!TestScenario::can_take_object<ColorObject>(scenario), 0);
}
```
The first part is the same as what we have seen in [Chapter 1](./ch1-object-basics.md#writing-unit-tests), which creates a new `ColorObject` and put it to the owner's account. The second transaction is what we are testing: retrieve the object from the storage, and then delete it. Since the object is deleted, there is no need (in fact, impossible) to return it to the storage. The last part checks that the object is indeed no longer in the global storage, and hence cannot be retrieved from there.
Expand All @@ -96,7 +96,7 @@ let scenario = &mut TestScenario::begin(&owner);
let recipient = @0x2;
TestScenario::next_tx(scenario, &owner);
{
let object = TestScenario::remove_object<ColorObject>(scenario);
let object = TestScenario::take_object<ColorObject>(scenario);
let ctx = TestScenario::ctx(scenario);
ColorObject::transfer(object, recipient, ctx);
};
Expand All @@ -106,12 +106,12 @@ Note that in the second transaction, the sender of the transaction should still
// Check that owner no longer owns the object.
TestScenario::next_tx(scenario, &owner);
{
assert!(!TestScenario::can_remove_object<ColorObject>(scenario), 0);
assert!(!TestScenario::can_take_object<ColorObject>(scenario), 0);
};
// Check that recipient now owns the object.
TestScenario::next_tx(scenario, &recipient);
{
assert!(TestScenario::can_remove_object<ColorObject>(scenario), 0);
assert!(TestScenario::can_take_object<ColorObject>(scenario), 0);
};
```

Expand Down
10 changes: 5 additions & 5 deletions sui_core/src/unit_tests/data/hero/sources/Hero.move
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ module Examples::Hero {
// Admin mints 500 coins and sends them to the Player so they can buy game items
TestScenario::next_tx(scenario, &admin);
{
let treasury_cap = TestScenario::remove_object<TreasuryCap<EXAMPLE>>(scenario);
let treasury_cap = TestScenario::take_object<TreasuryCap<EXAMPLE>>(scenario);
let ctx = TestScenario::ctx(scenario);
let coins = Coin::mint(500, &mut treasury_cap, ctx);
Coin::transfer(coins, copy player);
Expand All @@ -322,21 +322,21 @@ module Examples::Hero {
// Player purchases a hero with the coins
TestScenario::next_tx(scenario, &player);
{
let coin = TestScenario::remove_object<Coin<EXAMPLE>>(scenario);
let coin = TestScenario::take_object<Coin<EXAMPLE>>(scenario);
acquire_hero(coin, TestScenario::ctx(scenario));
};
// Admin sends a boar to the Player
TestScenario::next_tx(scenario, &admin);
{
let admin_cap = TestScenario::remove_object<GameAdmin>(scenario);
let admin_cap = TestScenario::take_object<GameAdmin>(scenario);
send_boar(&mut admin_cap, 10, 10, player, TestScenario::ctx(scenario));
TestScenario::return_object(scenario, admin_cap)
};
// Player slays the boar!
TestScenario::next_tx(scenario, &player);
{
let hero = TestScenario::remove_object<Hero>(scenario);
let boar = TestScenario::remove_object<Boar>(scenario);
let hero = TestScenario::take_object<Hero>(scenario);
let boar = TestScenario::take_object<Boar>(scenario);
slay(&mut hero, boar, TestScenario::ctx(scenario));
TestScenario::return_object(scenario, hero)
};
Expand Down
6 changes: 3 additions & 3 deletions sui_programmability/examples/basics/sources/Lock.move
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,16 @@ module Basics::LockTest {
// key to User2, so that he can have access to the stored treasure.
TestScenario::next_tx(scenario, &user1);
{
let key = TestScenario::remove_object<Key<Treasure>>(scenario);
let key = TestScenario::take_object<Key<Treasure>>(scenario);

Transfer::transfer(key, user2);
};

// User2 is impatient and he decides to take the treasure.
TestScenario::next_tx(scenario, &user2);
{
let lock = TestScenario::remove_object<Lock<Treasure>>(scenario);
let key = TestScenario::remove_object<Key<Treasure>>(scenario);
let lock = TestScenario::take_object<Lock<Treasure>>(scenario);
let key = TestScenario::take_object<Key<Treasure>>(scenario);
let ctx = TestScenario::ctx(scenario);

Lock::take<Treasure>(&mut lock, &key, ctx);
Expand Down
10 changes: 5 additions & 5 deletions sui_programmability/examples/defi/tests/EscrowTests.move
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ module DeFi::EscrowTests {
// The third party returns item A to Alice, item B to Bob
TestScenario::next_tx(scenario, &THIRD_PARTY_ADDRESS);
{
let item_a = TestScenario::remove_object<EscrowedObj<ItemA, ItemB>>(scenario);
let item_a = TestScenario::take_object<EscrowedObj<ItemA, ItemB>>(scenario);
let ctx = TestScenario::ctx(scenario);
Escrow::return_to_sender<ItemA, ItemB>(item_a, ctx);

let item_b = TestScenario::remove_object<EscrowedObj<ItemB, ItemA>>(scenario);
let item_b = TestScenario::take_object<EscrowedObj<ItemB, ItemA>>(scenario);
let ctx = TestScenario::ctx(scenario);
Escrow::return_to_sender<ItemB, ItemA>(item_b, ctx);
};
Expand Down Expand Up @@ -82,8 +82,8 @@ module DeFi::EscrowTests {
fun swap(scenario: &mut Scenario, third_party: &address) {
TestScenario::next_tx(scenario, third_party);
{
let item_a = TestScenario::remove_object<EscrowedObj<ItemA, ItemB>>(scenario);
let item_b = TestScenario::remove_object<EscrowedObj<ItemB, ItemA>>(scenario);
let item_a = TestScenario::take_object<EscrowedObj<ItemA, ItemB>>(scenario);
let item_b = TestScenario::take_object<EscrowedObj<ItemB, ItemA>>(scenario);
let ctx = TestScenario::ctx(scenario);
Escrow::swap(item_a, item_b, ctx);
};
Expand Down Expand Up @@ -157,6 +157,6 @@ module DeFi::EscrowTests {

fun owns_object<T: key + store>(scenario: &mut Scenario, owner: &address): bool{
TestScenario::next_tx(scenario, owner);
TestScenario::can_remove_object<T>(scenario)
TestScenario::can_take_object<T>(scenario)
}
}
6 changes: 3 additions & 3 deletions sui_programmability/examples/defi/tests/FlashLenderTests.move
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module DeFi::FlashLenderTests {
// borrower requests and repays a loan of 10 coins + the fee
TestScenario::next_tx(scenario, &borrower);
{
let lender = TestScenario::remove_object<FlashLender<SUI>>(scenario);
let lender = TestScenario::take_object<FlashLender<SUI>>(scenario);
let ctx = TestScenario::ctx(scenario);

let (loan, receipt) = FlashLender::loan(&mut lender, 10, ctx);
Expand All @@ -40,8 +40,8 @@ module DeFi::FlashLenderTests {
// admin withdraws the 1 coin profit from lending
TestScenario::next_tx(scenario, &admin);
{
let lender = TestScenario::remove_object<FlashLender<SUI>>(scenario);
let admin_cap = TestScenario::remove_object<AdminCap>(scenario);
let lender = TestScenario::take_object<FlashLender<SUI>>(scenario);
let admin_cap = TestScenario::take_object<AdminCap>(scenario);
let ctx = TestScenario::ctx(scenario);

// max loan size should have increased because of the fee payment
Expand Down
6 changes: 3 additions & 3 deletions sui_programmability/examples/defi/tests/SharedEscrowTest.move
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ module DeFi::SharedEscrowTests {
fun cancel(scenario: &mut Scenario, initiator: &address) {
TestScenario::next_tx(scenario, initiator);
{
let escrow = TestScenario::remove_object<EscrowedObj<ItemA, ItemB>>(scenario);
let escrow = TestScenario::take_object<EscrowedObj<ItemA, ItemB>>(scenario);
let ctx = TestScenario::ctx(scenario);
SharedEscrow::cancel(&mut escrow, ctx);
TestScenario::return_object(scenario, escrow);
Expand All @@ -128,7 +128,7 @@ module DeFi::SharedEscrowTests {
fun exchange(scenario: &mut Scenario, bob: &address, item_b_verioned_id: VersionedID) {
TestScenario::next_tx(scenario, bob);
{
let escrow = TestScenario::remove_object<EscrowedObj<ItemA, ItemB>>(scenario);
let escrow = TestScenario::take_object<EscrowedObj<ItemA, ItemB>>(scenario);
let item_b = ItemB {
id: item_b_verioned_id
};
Expand Down Expand Up @@ -171,6 +171,6 @@ module DeFi::SharedEscrowTests {

fun owns_object<T: key + store>(scenario: &mut Scenario, owner: &address): bool{
TestScenario::next_tx(scenario, owner);
TestScenario::can_remove_object<T>(scenario)
TestScenario::can_take_object<T>(scenario)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#[test_only]
module FungibleTokens::BASKETTests {
module FungibleTokens::BASKETTests {
use FungibleTokens::BASKET::{Self, Reserve};
use FungibleTokens::MANAGED::MANAGED;
use Sui::Coin;
Expand All @@ -20,7 +20,7 @@ module FungibleTokens::BASKETTests {
};
TestScenario::next_tx(scenario, &user);
{
let reserve = TestScenario::remove_object<Reserve>(scenario);
let reserve = TestScenario::take_object<Reserve>(scenario);
let ctx = TestScenario::ctx(scenario);
assert!(BASKET::total_supply(&reserve) == 0, 0);

Expand All @@ -29,7 +29,7 @@ module FungibleTokens::BASKETTests {
let managed = Coin::mint_for_testing<MANAGED>(num_coins, ctx);
let basket = BASKET::mint(&mut 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);
assert!(Coin::value(&sui) == num_coins, 3);
Expand Down
6 changes: 3 additions & 3 deletions sui_programmability/examples/games/hero/sources/Hero.move
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,15 @@ module HeroGame::Hero {
// Admin sends a boar to the Player
TestScenario::next_tx(scenario, &admin);
{
let admin_cap = TestScenario::remove_object<GameAdmin>(scenario);
let admin_cap = TestScenario::take_object<GameAdmin>(scenario);
send_boar(&mut admin_cap, 10, 10, player, TestScenario::ctx(scenario));
TestScenario::return_object(scenario, admin_cap)
};
// Player slays the boar!
TestScenario::next_tx(scenario, &player);
{
let hero = TestScenario::remove_object<Hero>(scenario);
let boar = TestScenario::remove_object<Boar>(scenario);
let hero = TestScenario::take_object<Hero>(scenario);
let boar = TestScenario::take_object<Boar>(scenario);
slay(&mut hero, boar, TestScenario::ctx(scenario));
TestScenario::return_object(scenario, hero)
};
Expand Down
Loading

0 comments on commit 6c24355

Please sign in to comment.