Skip to content

Commit

Permalink
Updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszDepta committed Aug 30, 2024
1 parent 52e78e1 commit a51b241
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 11 deletions.
19 changes: 17 additions & 2 deletions src/pages/cw-multi-test/getting-started/writing-tests.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
tags: ["multitest", "getting started", "writing tests"]
---

import { Cards, Card } from "nextra/components";

# Writing tests

## Compiling the smart contract
Expand All @@ -25,7 +27,7 @@ cargo build

Ok, the **counter** smart contract has been built successfully.

## Preparing directories for tests
## Preparing tests directory structure

```ansi {8-12} filename="counter"
.
Expand Down Expand Up @@ -82,7 +84,7 @@ tree
        └── test_counter.rs
```

## Running tests and measuring the code coverage
## Running tests and measuring code coverage

```shell copy
cargo test
Expand Down Expand Up @@ -130,3 +132,16 @@ cargo nextest run
```

![coverage-0](./coverage-0.png)

## Writing tests

(some introduction)

<Cards>
<Card title="Writing tests in CosmWasm" href="writing-tests/writing-tests-cosmwasm" icon="">
Writing tests in CosmWasm
</Card>
<Card title="Writing tests in Sylvia" href="writing-tests/writing-tests-sylvia" icon="">
Writing tests in Sylvia
</Card>
</Cards>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ tags: ["multitest", "getting started", "tests", "CosmWasm"]

# Writing tests in CosmWasm

```rust copy
```rust copy showLineNumbers{1} filename="test_counter.rs"
use cosmwasm_std::Empty;
use counter::msg::{CounterActionMsg, CounterInitMsg, CounterQuery, CounterResponse};
use cw_multi_test::{App, Contract, ContractWrapper, Executor, IntoAddr};
```

```rust copy showLineNumbers{5} filename="test_counter.rs"
fn counter_contract() -> Box<dyn Contract<Empty>> {
Box::new(ContractWrapper::new_with_empty(
counter::contract::execute,
Expand All @@ -18,7 +20,7 @@ fn counter_contract() -> Box<dyn Contract<Empty>> {
}
```

```rust copy
```rust copy showLineNumbers{13} filename="test_counter.rs"
#[test]
fn instantiating_with_zero_should_work() {
let mut app = App::default();
Expand Down Expand Up @@ -83,7 +85,7 @@ cargo nextest run

![coverage-cosmwasm-01](./coverage-cosmwasm-01.png)

```rust copy
```rust copy showLineNumbers{39} filename="test_counter.rs"
#[test]
fn instantiating_with_value_should_work() {
let mut app = App::default();
Expand Down Expand Up @@ -139,7 +141,7 @@ cargo nextest run
![coverage-cosmwasm-02](./coverage-cosmwasm-02.png)
</details>

```rust copy
```rust copy showLineNumbers{65} filename="test_counter.rs"
#[test]
fn incrementing_should_work() {
let mut app = App::default();
Expand Down Expand Up @@ -200,7 +202,7 @@ cargo nextest run

</details>

```rust copy
```rust copy showLineNumbers{94} filename="test_counter.rs"
#[test]
fn incrementing_should_stop_at_maximum() {
let mut app = App::default();
Expand Down Expand Up @@ -263,7 +265,7 @@ cargo nextest run
88.89% coverage, 16/18 lines covered, +0.00% change in coverage
```
```rust copy
```rust copy showLineNumbers{130} filename="test_counter.rs"
#[test]
fn decrementing_should_work() {
let mut app = App::default();
Expand Down Expand Up @@ -294,7 +296,7 @@ fn decrementing_should_work() {
}
```
```rust copy
```rust copy showLineNumbers{159} filename="test_counter.rs"
#[test]
fn decrementing_should_stop_at_minimum() {
let mut app = App::default();
Expand Down Expand Up @@ -332,7 +334,7 @@ fn decrementing_should_stop_at_minimum() {
}
```
```rust copy
```rust copy showLineNumbers{195} filename="test_counter.rs"
#[test]
fn setting_value_should_work() {
let mut app = App::default();
Expand Down Expand Up @@ -368,7 +370,10 @@ fn setting_value_should_work() {
}
```
## All tests together
## All tests put together
Below is the final content of the `test_counter.rs` file, containing all previously presented test
cases for smart contract written in CosmWasm.
```rust copy showLineNumbers filename="test_counter.rs"
use cosmwasm_std::Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,260 @@ tags: ["multitest", "getting started", "tests", "CosmWasm"]
---

# Writing tests in Sylvia

```rust copy showLineNumbers filename="test_counter.rs"
use counter::contract::sv::mt::{CodeId, CounterContractProxy};
use counter::msg::CounterInitMsg;
use sylvia::cw_multi_test::IntoAddr;
use sylvia::multitest::App;
```

```rust copy showLineNumbers{6} filename="test_counter.rs"
#[test]
fn instantiating_with_zero_should_work() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Zero)
.call(&owner)
.unwrap();

assert_eq!(0, contract.count().unwrap().count);
}
```

```rust copy showLineNumbers{20} filename="test_counter.rs"
#[test]
fn instantiating_with_value_should_work() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Set(12))
.call(&owner)
.unwrap();

assert_eq!(12, contract.count().unwrap().count);
}
```

```rust copy showLineNumbers{34} filename="test_counter.rs"
#[test]
fn incrementing_should_work() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Zero)
.call(&owner)
.unwrap();

contract.inc().call(&owner).unwrap();

assert_eq!(1, contract.count().unwrap().count);
}
```

```rust copy showLineNumbers{50} filename="test_counter.rs"
#[test]
fn incrementing_should_stop_at_maximum() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Set(250))
.call(&owner)
.unwrap();

for _ in 1..=10 {
contract.inc().call(&owner).unwrap();
}

assert_eq!(255, contract.count().unwrap().count);
}
```

```rust copy showLineNumbers{68} filename="test_counter.rs"
#[test]
fn decrementing_should_work() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Set(126))
.call(&owner)
.unwrap();

contract.dec().call(&owner).unwrap();

assert_eq!(125, contract.count().unwrap().count);
}
```

```rust copy showLineNumbers{84} filename="test_counter.rs"
#[test]
fn decrementing_should_stop_at_minimum() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Set(5))
.call(&owner)
.unwrap();

for _ in 1..=10 {
contract.dec().call(&owner).unwrap();
}

assert_eq!(0, contract.count().unwrap().count);
}
```

```rust copy showLineNumbers{102} filename="test_counter.rs"
#[test]
fn setting_value_should_work() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Set(5))
.call(&owner)
.unwrap();

contract.set(125).call(&owner).unwrap();

assert_eq!(125, contract.count().unwrap().count);
}
```

## All tests put together

Below is the final content of the `test_counter.rs` file, containing all previously presented test
cases for smart contract written in Sylvia.

```rust copy showLineNumbers filename="test_counter.rs"
use counter::contract::sv::mt::{CodeId, CounterContractProxy};
use counter::msg::CounterInitMsg;
use sylvia::cw_multi_test::IntoAddr;
use sylvia::multitest::App;

#[test]
fn instantiating_with_zero_should_work() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Zero)
.call(&owner)
.unwrap();

assert_eq!(0, contract.count().unwrap().count);
}

#[test]
fn instantiating_with_value_should_work() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Set(12))
.call(&owner)
.unwrap();

assert_eq!(12, contract.count().unwrap().count);
}

#[test]
fn incrementing_should_work() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Zero)
.call(&owner)
.unwrap();

contract.inc().call(&owner).unwrap();

assert_eq!(1, contract.count().unwrap().count);
}

#[test]
fn incrementing_should_stop_at_maximum() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Set(250))
.call(&owner)
.unwrap();

for _ in 1..=10 {
contract.inc().call(&owner).unwrap();
}

assert_eq!(255, contract.count().unwrap().count);
}

#[test]
fn decrementing_should_work() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Set(126))
.call(&owner)
.unwrap();

contract.dec().call(&owner).unwrap();

assert_eq!(125, contract.count().unwrap().count);
}

#[test]
fn decrementing_should_stop_at_minimum() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Set(5))
.call(&owner)
.unwrap();

for _ in 1..=10 {
contract.dec().call(&owner).unwrap();
}

assert_eq!(0, contract.count().unwrap().count);
}

#[test]
fn setting_value_should_work() {
let app = App::default();
let code_id = CodeId::store_code(&app);

let owner = "owner".into_addr();
let contract = code_id
.instantiate(CounterInitMsg::Set(5))
.call(&owner)
.unwrap();

contract.set(125).call(&owner).unwrap();

assert_eq!(125, contract.count().unwrap().count);
}
```

0 comments on commit a51b241

Please sign in to comment.