Skip to content

Commit

Permalink
add rust test for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
bbyalcinkaya committed Jul 31, 2024
1 parent 4190b1c commit 42d2ad6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "test_storage"
version = "0.0.0"
edition = "2021"
publish = false

[lib]
crate-type = ["cdylib"]
doctest = false

[dependencies]
soroban-sdk = { workspace = true }

[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
A quick example of a contract that can be ran with `ksoroban test`

You will need to have the stellar cli utils installed:
https://developers.stellar.org/docs/build/smart-contracts/getting-started/setup

And the soroban semantics kompiled:
```
kdist build soroban-semantics.llvm
```

And then (from this directory):

```sh
soroban contract build --out-dir output
ksoroban test output/test_storage.wasm
```

`ksoroban test` should exit successfully
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![no_std]
use soroban_sdk::{contract, contractimpl, Env, Symbol, symbol_short};

#[contract]
pub struct StorageContract;

const INST_KEY: Symbol = symbol_short!("INST");
const TEMP_KEY: Symbol = symbol_short!("TEMP");
const PRST_KEY: Symbol = symbol_short!("PRST");

#[contractimpl]
impl StorageContract {
pub fn test_u32(env: Env, num1: u32, num2: u32, num3: u32) -> bool {
env.storage().instance().set(&INST_KEY, &num1);
env.storage().temporary().set(&TEMP_KEY, &num2);
env.storage().persistent().set(&PRST_KEY, &num3);

let snum1: u32 = env.storage().instance().get(&INST_KEY).unwrap();
let snum2: u32 = env.storage().temporary().get(&TEMP_KEY).unwrap();
let snum3: u32 = env.storage().persistent().get(&PRST_KEY).unwrap();

num1 == snum1 && num2 == snum2 && num3 == snum3
}

pub fn test_u32_overwrite(env: Env, num1: u32, num2: u32) -> bool {
env.storage().instance().set(&INST_KEY, &num1);
env.storage().instance().set(&INST_KEY, &num2);

let num: u32 = env.storage().instance().get(&INST_KEY).unwrap();

num == num2
}
}

0 comments on commit 42d2ad6

Please sign in to comment.