mirror of
https://github.com/fluencelabs/gitbook-docs
synced 2025-04-25 07:52:14 +00:00
GitBook: [2.0.0] 23 pages modified
This commit is contained in:
parent
36bf9e20c4
commit
0c7f35f413
@ -330,11 +330,18 @@ MountedBinaryResult then can be used on a variety of match or conditional tests.
|
||||
|
||||
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:
|
||||
To use the `[marine-test]` macro please add `marine-rs-sdk-test` crate to the `[dev-dependencies]` section of `Config.toml`:
|
||||
|
||||
```rust
|
||||
use fluence::marine;
|
||||
use fluence::module_manifest;
|
||||
[dev-dependencies]
|
||||
marine-rs-sdk-test = "0.2.0"
|
||||
```
|
||||
|
||||
Let's have a look at an implementation example:
|
||||
|
||||
```rust
|
||||
use marine_rs_sdk::marine;
|
||||
use marine_rs_sdk::module_manifest;
|
||||
|
||||
module_manifest!();
|
||||
|
||||
@ -347,16 +354,16 @@ pub fn greeting(name: String) -> String { # 1
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use fluence_test::marine_test; # 2
|
||||
use marine_rs_sdk_test::marine_test; # 2
|
||||
|
||||
#[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts")] # 3
|
||||
fn empty_string() {
|
||||
fn empty_string(greeting: marine_test_env::greeting::ModuleInterface) {
|
||||
let actual = greeting.greeting(String::new()); # 4
|
||||
assert_eq!(actual, "Hi, ");
|
||||
}
|
||||
|
||||
#[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts")]
|
||||
fn non_empty_string() {
|
||||
fn non_empty_string(greeting: marine_test_env::greeting::ModuleInterface) {
|
||||
let actual = greeting.greeting("name".to_string());
|
||||
assert_eq!(actual, "Hi, name");
|
||||
}
|
||||
@ -364,11 +371,11 @@ mod tests {
|
||||
```
|
||||
|
||||
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.
|
||||
2. We wrap our tests as usual with `[cfg(test)]` and import the marine _test crate._ Do **not** import _super_ or the _local crate_.
|
||||
3. Instead, we apply the `[marine_test]` macro 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`. Moreover, we add the type of the test as an argument in the function signature. It is imperative that project build precedes the test runner otherwise the required Wasm file will ne missing.
|
||||
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 as specified in the function argument.
|
||||
|
||||
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.
|
||||
Now that we have our Wasm module and tests in place, we can proceed with `cargo test --release.` Note that using the `release`flag vastly improves the import speed of the necessary Wasm modules.
|
||||
|
||||
### Features
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user