Skip to content

Commit

Permalink
[Framework Testing] Add test to account_storage exists and move_from (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed Jun 27, 2023
1 parent 576bc58 commit 69007f4
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
processed 3 tasks

task 1 'publish'. lines 3-34:
status EXECUTED

task 2 'run'. lines 36-44:
status EXECUTED
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//# init --addresses test=0x42

//# publish
module test::m {
use std::signer;
use moveos_std::storage_context::{StorageContext};
use moveos_std::account_storage;

struct Test has key{
addr: address,
version: u64
}

fun init(ctx: &mut StorageContext, sender: &signer) {
let sender_addr = signer::address_of(sender);
account_storage::global_move_to(ctx, sender, Test{
addr: sender_addr,
version: 0,
});
}

public fun test_exists_and_move_from(ctx: &mut StorageContext, sender:&signer){
let sender_addr = signer::address_of(sender);
let test_exists = account_storage::global_exists<Test>(ctx, sender_addr);
assert!(test_exists, 1);
let test = account_storage::global_move_from<Test>(ctx, sender_addr);
let test_exists = account_storage::global_exists<Test>(ctx, sender_addr);
assert!(!test_exists, 2);
let Test{
addr: _,
version: _
} = test;
}
}

//# run --signers test
script {
use moveos_std::storage_context::{StorageContext};
use test::m;

fun main(ctx: &mut StorageContext, sender: signer) {
m::test_exists_and_move_from(ctx, &sender);
}
}
10 changes: 0 additions & 10 deletions examples/complex_struct/sources/complex_struct.move
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,6 @@ module rooch_examples::complex_struct {
fun init(ctx: &mut StorageContext, sender: signer) {

let addr = signer::address_of(&sender);
//When we try to re publish the modules, the `ComplexStruct` already inited
//so we need to remove it first.
//There some bugs in the global_exists| global_move_from | global_move_to
//Seam like the VM think the resource after move_from still exists.
//FIXME
// if (account_storage::global_exists<ComplexStruct>(ctx, addr)){
// let old_struct = account_storage::global_move_from<ComplexStruct>(ctx, addr);
// drop(old_struct);
// };

let object_id = {
let tx_ctx = storage_context::tx_context_mut(ctx);
tx_context::fresh_object_id(tx_ctx)
Expand Down
2 changes: 1 addition & 1 deletion moveos/moveos-stdlib/move-stdlib/doc/error.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ Construct a canonical error code from a category and a reason.


<pre><code><b>public</b> <b>fun</b> <a href="error.md#0x1_error_canonical">canonical</a>(category: u64, reason: u64): u64 {
(category * 10000) + reason
(category &lt;&lt; 16) + reason
}
</code></pre>

Expand Down
22 changes: 22 additions & 0 deletions moveos/moveos-stdlib/moveos-stdlib/sources/account_storage.move
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,26 @@ module moveos_std::account_storage {
assert!(exist_account_storage(&ctx , sender_addr), 1);
storage_context::drop_test_context(ctx);
}

#[test(sender=@0x42)]
fun test_ensure_move_from_and_exists(sender: signer){
let sender_addr = signer::address_of(&sender);
let ctx = storage_context::new_test_context(sender_addr);
let test_exists = global_exists<Test>(&ctx, sender_addr);
assert!(!test_exists, 1);
global_move_to(&mut ctx, &sender, Test{
addr: sender_addr,
version: 1,
});
let test_exists = global_exists<Test>(&ctx, sender_addr);
assert!(test_exists, 2);
let test = global_move_from<Test>(&mut ctx, sender_addr);
let test_exists = global_exists<Test>(&ctx, sender_addr);
assert!(!test_exists, 3);
let Test{
addr: _,
version: _
} = test;
storage_context::drop_test_context(ctx);
}
}

0 comments on commit 69007f4

Please sign in to comment.