From 2e1390bd652b23d96110cbd5bc998d1dc0951221 Mon Sep 17 00:00:00 2001 From: WGB5445 <919603023@qq.com> Date: Mon, 26 Jun 2023 14:06:13 +0800 Subject: [PATCH] [Testing] moveos_stdlib::account_storage --- crates/rooch-framework/sources/account.move | 2 +- .../sources/account_storage.move | 150 ++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) diff --git a/crates/rooch-framework/sources/account.move b/crates/rooch-framework/sources/account.move index d562e1a981..553356f899 100644 --- a/crates/rooch-framework/sources/account.move +++ b/crates/rooch-framework/sources/account.move @@ -339,7 +339,7 @@ module rooch_framework::account{ #[test(sender=@0x0)] #[expected_failure(abort_code = 0x10005, location = Self)] - fun test_failur_entry_account_creation_reserved(sender: address){ + fun test_failure_entry_account_creation_reserved(sender: address){ let ctx = storage_context::new_test_context(sender); create_account_entry(&mut ctx, sender); storage_context::drop_test_context(ctx); diff --git a/moveos/moveos-stdlib/moveos-stdlib/sources/account_storage.move b/moveos/moveos-stdlib/moveos-stdlib/sources/account_storage.move index 3d988b44f3..347ea3e2b2 100644 --- a/moveos/moveos-stdlib/moveos-stdlib/sources/account_storage.move +++ b/moveos/moveos-stdlib/moveos-stdlib/sources/account_storage.move @@ -204,4 +204,154 @@ module moveos_std::account_storage { }); storage_context::drop_test_context(ctx); } + + #[test(sender=@0x42)] + fun test_move_to_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + create_account_storage(&mut ctx, sender_addr); + global_move_to(&mut ctx, &sender, Test{ + addr: sender_addr, + version: 1, + }); + storage_context::drop_test_context(ctx); + } + + #[test(sender=@0x42)] + fun test_move_from_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + create_account_storage(&mut ctx, sender_addr); + global_move_to(&mut ctx, &sender, Test{ + addr: sender_addr, + version: 1, + }); + let Test { + addr, + version + } = global_move_from(&mut ctx, sender_addr); + assert!(addr == sender_addr, 0x10); + assert!(version == 1, 0x11); + storage_context::drop_test_context(ctx); + } + + #[test(sender=@0x42)] + #[expected_failure(abort_code = 0x0, location = Self)] + fun test_failure_repeatedly_create_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + create_account_storage(&mut ctx, sender_addr); + create_account_storage(&mut ctx, sender_addr); + storage_context::drop_test_context(ctx); + } + + #[test(sender=@0x42)] + #[expected_failure(abort_code = 0x1, location = Self)] + fun test_failure_repeatedly_move_to_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + create_account_storage(&mut ctx, sender_addr); + global_move_to(&mut ctx, &sender, Test{ + addr: sender_addr, + version: 1, + }); + global_move_to(&mut ctx, &sender, Test{ + addr: sender_addr, + version: 1, + }); + storage_context::drop_test_context(ctx); + } + + #[test(sender=@0x42)] + #[expected_failure(abort_code = 0x2, location = Self)] + fun test_failure_repeatedly_move_from_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + create_account_storage(&mut ctx, sender_addr); + global_move_to(&mut ctx, &sender, Test{ + addr: sender_addr, + version: 1, + }); + let Test { + addr: _, + version: _ + } = global_move_from(&mut ctx, sender_addr); + let Test { + addr: _, + version: _ + } = global_move_from(&mut ctx, sender_addr); + storage_context::drop_test_context(ctx); + } + + #[test(sender=@0x42)] + fun test_global_borrow_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + create_account_storage(&mut ctx, sender_addr); + global_move_to(&mut ctx, &sender, Test{ + addr: sender_addr, + version: 1, + }); + + let ref_test = global_borrow(& ctx, sender_addr); + assert!( ref_test.version == 1, 1); + assert!( ref_test.addr == sender_addr, 2); + storage_context::drop_test_context(ctx); + } + + #[test(sender=@0x42)] + fun test_global_borrow_mut_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + create_account_storage(&mut ctx, sender_addr); + global_move_to(&mut ctx, &sender, Test{ + addr: sender_addr, + version: 1, + }); + + let ref_test = global_borrow_mut(&mut ctx, sender_addr); + assert!( ref_test.version == 1, 1); + assert!( ref_test.addr == sender_addr, 2); + + ref_test.version = 2; + assert!( ref_test.version == 2, 3); + storage_context::drop_test_context(ctx); + } + + #[test(sender=@0x42)] + #[expected_failure(abort_code = 0x6507, location = moveos_std::raw_table)] + fun test_failure_global_borrow_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + create_account_storage(&mut ctx, sender_addr); + global_borrow(&mut ctx, sender_addr); + storage_context::drop_test_context(ctx); + } + + #[test(sender=@0x42)] + #[expected_failure(abort_code = 0x6507, location = moveos_std::raw_table)] + fun test_failure_global_borrow_mut_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + create_account_storage(&mut ctx, sender_addr); + global_borrow_mut(&mut ctx, sender_addr); + storage_context::drop_test_context(ctx); + } + + #[test(sender=@0x42)] + fun test_exist_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + assert!(exist_account_storage(&ctx , sender_addr) == false, 1); + storage_context::drop_test_context(ctx); + } + + #[test(sender=@0x42)] + fun test_ensure_account_storage(sender: signer){ + let sender_addr = signer::address_of(&sender); + let ctx = storage_context::new_test_context(sender_addr); + ensure_account_storage(&mut ctx , sender_addr); + assert!(exist_account_storage(&ctx , sender_addr), 1); + storage_context::drop_test_context(ctx); + } } \ No newline at end of file