-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(template): add client and tests to the program template (#469)
- Loading branch information
1 parent
9215ce0
commit dc5cf13
Showing
6 changed files
with
71 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,15 @@ | ||
fn main() {} | ||
use sails_client_gen::ClientGenerator; | ||
use std::{env, path::PathBuf}; | ||
|
||
use {{ crate_name }}::{{ program-struct-name }} as ProgramType; | ||
|
||
fn main() { | ||
let idl_file_path = PathBuf::from("{{ crate_name }}.idl"); | ||
// Generate IDL file for the program | ||
sails_idl_gen::generate_idl_to_file::<ProgramType>(&idl_file_path).unwrap(); | ||
// Generate client code from IDL file | ||
ClientGenerator::from_idl_path(&idl_file_path) | ||
.with_mocks("{{ mocks-feature-name}}") | ||
.generate_to(PathBuf::from(env::var("OUT_DIR").unwrap()).join("{{ client_crate_name }}.rs")) | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![no_std] | ||
|
||
// Incorporate code generated based on the IDL file | ||
include!(concat!(env!("OUT_DIR"), "/{{ client_crate_name }}.rs")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,32 @@ | ||
use sails_rs::{calls::*, gtest::calls::*}; | ||
|
||
use {{ client_crate_name }}::traits::*; | ||
|
||
const ACTOR_ID: u64 = 42; | ||
|
||
#[tokio::test] | ||
async fn do_something_works() { | ||
println!("WASM_BINARY length: {}", {{ program-name-snake }}::WASM_BINARY.len()); | ||
let remoting = GTestRemoting::new(ACTOR_ID.into()); | ||
remoting.system().init_logger(); | ||
|
||
// Submit program code into the system | ||
let program_code_id = remoting.system().submit_code({{ crate_name }}::WASM_BINARY); | ||
|
||
let program_factory = {{ client_crate_name }}::{{ service-name }}Factory::new(remoting.clone()); | ||
|
||
let program_id = program_factory | ||
.new() // Call program's constructor (see src/lib.rs:27) | ||
.send_recv(program_code_id, b"salt") | ||
.await | ||
.unwrap(); | ||
|
||
let mut service_client = {{ client_crate_name }}::{{ service-name }}::new(remoting.clone()); | ||
|
||
let result = service_client | ||
.do_something() // Call service's method (see src/lib.rs:17) | ||
.send_recv(program_id) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(result, "Hello from {{ service-name }}!".to_string()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters