fvm_mock/lib.rs
1//! fvm-mock is a tool for developer to check contract logical is correct or not locally, which is suit for hyperchain.
2//!
3//! ### Demo contract
4//!
5//! ```ignore
6//! use macros::contract;
7//! use macros::storage;
8//! use fvm_std::runtime;
9//! use scale::{Decode, Encode};
10//!
11//! #[storage]
12//! pub struct TestHello {}
13//!
14//! #[contract]
15//! impl TestHello {
16//! fn new() -> Self {
17//! Self {}
18//! }
19//!
20//! pub fn set(&mut self, key: String, value: String) {
21//! runtime::storage_write(key.as_bytes(), "test".as_bytes(), value.as_bytes())
22//! }
23//!
24//! pub fn get(&mut self, key: String) -> Vec<u8> {
25//! return if let Some(res) = runtime::storage_read(key.as_bytes(), "test".as_bytes()) {
26//! res
27//! } else {
28//! vec![]
29//! };
30//! }
31//! }
32//! ```
33//!
34//! In order to check locally, we should do as below
35//!
36//! + Add dependency in `cargo.toml`
37//!
38//! ```toml
39//! [dev-dependencies]
40//! # ... other dependencies
41//! fvm-mock = "xxx"
42//! ```
43//!
44//! + Call `build_runtime()` in unit test
45//!
46//! ```ignore
47//! #[cfg(test)]
48//! mod tests {
49//! use scale::{Decode, Encode};
50//! use crate::TestHello;
51//! use fvm_mock::build_runtime;
52//!
53//! #[test]
54//! fn test_set() {
55//! let mut contract = TestHello::new();
56//! let handle = build_runtime();
57//! contract.set("hello".to_string(), "world".to_string());
58//! assert_eq!("world".as_bytes(), contract.get("hello".to_string()).as_slice())
59//! }
60//!
61//! #[test]
62//! fn test_get() {
63//! let mut contract = TestHello::new();
64//! let handle = build_runtime();
65//! handle.storage_write("hello", "test", "world");
66//! assert_eq!("world".as_bytes(), contract.get("hello".to_string()).as_slice())
67//! }
68//! }
69//! ```
70//!
71//! ***Notice that if you could pass the unit test, it indicates the logical is correct, but can not represent it can
72//! run well on the chain.
73//! For there are specifications that WASM core not support well, so developer should ensure that you do not use them, such
74//! as `print to console`, `file read` and so on***
75
76use cfg_if::cfg_if;
77
78
79cfg_if! {
80 if #[cfg(not(target_arch = "wasm32"))] {
81 pub mod mock_env;
82 pub mod mock_runtime;
83 pub use self::mock_env::build_runtime;
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 #[test]
90 fn it_works() {
91 let result = 2 + 2;
92 assert_eq!(result, 4);
93 }
94}