Skip to content

Commit

Permalink
Storage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
virgil-serbanuta committed Sep 11, 2024
1 parent 29dc072 commit a647365
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 2 deletions.
1 change: 1 addition & 0 deletions mx-rust-semantics/test/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module MX-RUST-EXECUTION-TEST-CONFIGURATION
<mx-rust-test>
<rust-test/>
<mx-test/>
<test-named> .Map </test-named>
</mx-rust-test>
endmodule
Expand Down
41 changes: 41 additions & 0 deletions mx-rust-semantics/test/execution.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,54 @@
```k
module MX-RUST-TESTING-PARSING-SYNTAX
imports INT-SYNTAX
imports MX-TEST-EXECUTION-PARSING-SYNTAX
imports RUST-EXECUTION-TEST-PARSING-SYNTAX
imports RUST-SHARED-SYNTAX
imports RUST-VALUE-SYNTAX
imports STRING-SYNTAX
syntax MxRustTest ::= ExecutionTest
syntax ExecutionItem ::= "set_named" String
| "push_named" String
| "get_bigint_from_struct"
| "check_eq" Int
| TestInstruction
endmodule
module MX-RUST-TEST
imports private COMMON-K-CELL
imports private MX-RUST-EXECUTION-TEST-CONFIGURATION
imports private MX-RUST-TESTING-PARSING-SYNTAX
imports private RUST-EXECUTION-CONFIGURATION
rule
<k> set_named Name:String => .K ... </k>
<test-stack> ListItem(Value) => .List ... </test-stack>
<test-named> M:Map => M[Name <- Value] </test-named>
rule
<k> push_named Name:String => .K ... </k>
<test-stack> .List => ListItem(Value) ... </test-stack>
<test-named> Name |-> Value ... </test-named>
rule
<k>
ptrValue
( _
, struct
( #token("BigUint", "Identifier")
, #token("mx_biguint_id", "Identifier"):Identifier |-> BigUintIdId _:Map
)
)
~> get_bigint_from_struct ; Test:ExecutionTest
=> push mxIntValue(MInt2Unsigned(BigUintId))
~> get_big_int
~> Test
...
</k>
<values> BigUintIdId |-> i32(BigUintId:MInt{32}) ... </values>
endmodule
```
12 changes: 12 additions & 0 deletions tests/mx-rust/storage.1.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

addAccount("Owner");
setCallee("Owner");
new Storage;
set_named "self";
push_named "self";
push 10u64;
call Storage.set_no_arg;
push_named "self";
call Storage.get_no_arg;
get_bigint_from_struct;
check_eq mxIntValue(10)
9 changes: 9 additions & 0 deletions tests/mx-rust/storage.2.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

addAccount("Owner");
setCallee("Owner");
new Storage;
set_named "self";
push_named "self";
call Storage.get_no_arg;
get_bigint_from_struct;
check_eq mxIntValue(0)
20 changes: 20 additions & 0 deletions tests/mx-rust/storage.3.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
addAccount("Owner");
setCallee("Owner");
new Storage;
set_named "self";

push_named "self";
push 10u64;
call Storage.set_no_arg_if_empty;
push_named "self";
call Storage.get_no_arg;
get_bigint_from_struct;
check_eq mxIntValue(10);

push_named "self";
push 20u64;
call Storage.set_no_arg_if_empty;
push_named "self";
call Storage.get_no_arg;
get_bigint_from_struct;
check_eq mxIntValue(10)
33 changes: 33 additions & 0 deletions tests/mx-rust/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![no_std]

#[allow(unused_imports)]
use multiversx_sc::imports::*;

#[multiversx_sc::contract]
pub trait Storage {
#[view(noArg)]
#[storage_mapper("no_arg")]
fn no_arg_storage(&self) -> SingleValueMapper<BigUint>;

#[view(getName)]
#[storage_mapper("name")]
fn one_arg_storage(&self, key: u64) -> SingleValueMapper<BigUint>;

#[init]
fn init(&self) {}

#[upgrade]
fn upgrade(&self) {}

fn set_no_arg(&self, value: u64) {
self.no_arg_storage().set(BigUint::from(value))
}

fn get_no_arg(&self) -> BigUint {
self.no_arg_storage().get()
}

fn set_no_arg_if_empty(&self, value: u64) {
self.no_arg_storage().set_if_empty(BigUint::from(value))
}
}
2 changes: 1 addition & 1 deletion tests/mx/storage/get-existing-storage.mx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
addAccount("Owner");
setCallee("Owner");
setStorage("Owner", "MyKey", mxStringValue("Hello"));
setStorage("Owner", "MyKey", wrappedMx(mxStringValue("Hello")));

push mxIntValue(12);
push mxStringValue("MyKey");
Expand Down
2 changes: 1 addition & 1 deletion tests/mx/storage/set-existing-storage.mx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
addAccount("Owner");
setCallee("Owner");
setStorage("Owner", "MyKey", mxStringValue("Hello"));
setStorage("Owner", "MyKey", wrappedMx(mxStringValue("Hello")));

push wrappedMx(mxStringValue("World"));
push mxStringValue("MyKey");
Expand Down

0 comments on commit a647365

Please sign in to comment.