Skip to content

Commit

Permalink
Merge pull request #17 from CosmWasm/chipshort/docs-tests-template
Browse files Browse the repository at this point in the history
Docs Test Changes
  • Loading branch information
chipshort authored May 3, 2024
2 parents 694228b + e0fe15c commit 18f70dc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs-test-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ strum = { version = "0.26.2", features = ["derive"] }

[dev-dependencies]
cosmwasm-schema = "*"
cosmwasm-std = "*"
cosmwasm-std = { version = "*", features = ["stargate", "staking", "cosmwasm_2_0"] }
1 change: 1 addition & 0 deletions docs-test-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use strum::{AsRefStr, EnumIter, IntoEnumIterator};

static TEMPLATES: phf::Map<&'static str, &'static str> = phf_map! {
"core" => include_str!("../templates/core.tpl"),
"execute" => include_str!("../templates/execute.tpl"),
};

#[inline]
Expand Down
32 changes: 32 additions & 0 deletions docs-test-gen/templates/execute.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use cosmwasm_std::*;
use cosmwasm_schema::cw_serde;

#[cw_serde]
struct ExecuteMsg {}

#[entry_point]
pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> StdResult<Response> {
// this weird construction allows us to return a StdResult<Response> or () in the code
// it tricks the compiler into infering the correct generics
trait ValidReturn {}
impl ValidReturn for StdResult<Response<Empty>> {}
impl ValidReturn for () {}
fn assert_valid_return(_: &impl ValidReturn) {}

let ret = { {{code}} };
assert_valid_return(&ret);

Ok(Response::default())
}

#[test]
fn doctest() {
use cosmwasm_std::testing::*;
let mut deps = mock_dependencies();
let env = mock_env();
let info = mock_info("sender", &[]);
let msg = ExecuteMsg {};

let res = execute(deps.as_mut(), env, info, msg).unwrap();
}
9 changes: 9 additions & 0 deletions scripts/test-rust.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail

(
cd docs-test-gen && \
rm -rf ./tests && \
cargo run &&
cargo test
)

0 comments on commit 18f70dc

Please sign in to comment.