From ccf7c7d19fc24deaf1ba3dc0f40f97599e441816 Mon Sep 17 00:00:00 2001 From: boneyard93501 Date: Fri, 11 Jun 2021 00:42:32 +0000 Subject: [PATCH] GitBook: [main] 54 pages modified --- .../marine/marine-rs-sdk.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/knowledge_knowledge/knowledge_aquamarine/marine/marine-rs-sdk.md b/knowledge_knowledge/knowledge_aquamarine/marine/marine-rs-sdk.md index 7394872..59d2e6e 100644 --- a/knowledge_knowledge/knowledge_aquamarine/marine/marine-rs-sdk.md +++ b/knowledge_knowledge/knowledge_aquamarine/marine/marine-rs-sdk.md @@ -164,11 +164,47 @@ Where #### Testing +Since we are compiling to a wasm32-wasi target with `ftype` constrains, the basic `cargo test` is not all that useful or even usable for our purposes. To alleviate that limitation, Fluence has introduced the [`[marine-test]` macro ](https://github.com/fluencelabs/marine-rs-sdk/tree/master/crates/marine-test-macro)that does a lot of the heavy lifting to allow developers to use `cargo test` as intended. That is, `[marine-test]` macro generates the necessary code to call Marine, one instance per test function, based on the Wasm module and associated configuration file so that the actual test function is run against the Wasm module not the native code. +Let's have a look at an implementation example: +```rust +use fluence::marine; +use fluence::module_manifest; +module_manifest!(); +pub fn main() {} +#[marine] +pub fn greeting(name: String) -> String { # 1 + format!("Hi, {}", name) +} + +#[cfg(test)] +mod tests { + use fluence_test::marine_test; # 2 + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts")] # 3 + fn empty_string() { + let actual = greeting.greeting(String::new()); # 4 + assert_eq!(actual, "Hi, "); + } + + #[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts")] + fn non_empty_string() { + let actual = greeting.greeting("name".to_string()); + assert_eq!(actual, "Hi, name"); + } +} +``` + +1. We wrap a basic _greeting_ function with the `[marine`\] macro which results in the greeting.wasm module +2. We wrap our tests as usual with `[cfg(test)]` and import the fluence_test crate._ Do **not** import _super_ or the _local crate_. +3. Instead, we apply the `[marine_test]` to each of the test functions by providing the path to the config file, e.g., Config.toml, and the directory containing the Wasm module we obtained after compiling our project with `marine build`. It is imperative that project compilation proceeds the test runner otherwise there won't be the required Wasm file. +4. The target of our tests is the `pub fn greeting` function. Since we are calling the function from the Wasm module we must prefix the function name with the module namespace -- `greeting` in this example case. + +Now that we have our Wasm module and tests in place, we can proceed with `cargo test --release.` Note that using the `release`vastly improves the import speed of the necessary Wasm modules. ### Features