mirror of
https://github.com/fluencelabs/gitbook-docs
synced 2025-04-25 07:52:14 +00:00
GitBook: [main] 54 pages modified
This commit is contained in:
parent
6d83f78837
commit
ccf7c7d19f
@ -164,11 +164,47 @@ Where
|
|||||||
|
|
||||||
#### Testing
|
#### 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
|
### Features
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user