Skip to content

Commit

Permalink
feat(prog): state queries
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Feb 22, 2024
1 parent 16d9b95 commit c672a5f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions alpha/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ publish.workspace = true
gstd.workspace = true
alpha-io.workspace = true

[dev-dependencies]
gtest.workspace = true

[build-dependencies]
alpha-io.workspace = true
gear-wasm-builder.workspace = true
6 changes: 5 additions & 1 deletion alpha/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ static mut STATE: Option<State> = None;

// do nothing atm.
#[no_mangle]
extern fn init() {}
extern fn init() {
unsafe {
STATE = Some(vec![]);
}
}

// The `handle()` entry point.
#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion alpha/state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod metafns {
Label::list()
}

/// Search all resouces matching labels
/// Search all resouces matching the input label.
pub fn search(state: State, label: Label) -> Vec<Resource> {
state
.into_iter()
Expand Down
37 changes: 37 additions & 0 deletions alpha/tests/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use alpha_io::{Label, Resource};
use gtest::{state_args, Program, System};
use std::{fs, path::PathBuf};

fn get_state_binary() -> Vec<u8> {
let bin = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../target/wasm32-unknown-unknown/debug/alpha_state.meta.wasm");
fs::read(bin).unwrap()
}

#[test]
fn test() {
let system = System::new();
system.init_logger();

let program = Program::current(&system);
let mut result = program.send_bytes(2, []);
assert!(!result.main_failed());

let input = Resource {
labels: Label::list(),
domain: "vara-go".into(),
description: "test test".into(),
html: "<div>hello, world<div>".into(),
styles: "".into(),
};
result = program.send(2, input);
assert!(!result.main_failed());

let state_bin = get_state_binary();
let search = Label::Vara;
let output: Vec<Resource> = program
.read_state_using_wasm(b"", "search", state_bin, state_args!(search))
.expect("Failed to search source");
// assert_eq!(output.get(&input.domain), Some(input.src).as_ref());
println!("{output:?}");
}
1 change: 0 additions & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ fn test() {
assert_eq!(output.get(&input.domain), Some(input.src).as_ref());
}

#[allow(unused)]
fn get_state_binary() -> Vec<u8> {
fs::read("target/wasm32-unknown-unknown/debug/template_state.meta.wasm").unwrap()
}

0 comments on commit c672a5f

Please sign in to comment.