Skip to content

Commit 2958fa8

Browse files
committed
Create a test to run readme example
1 parent a261c08 commit 2958fa8

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

examples/readme/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ tokio = { version = "1.44.2", default-features = false, features = [
1414
"rt-multi-thread",
1515
"time",
1616
] }
17+
18+
[dev-dependencies]
19+
assert_cmd = "2.0.17"
20+
predicates = "3.1.3"

examples/readme/src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{Context, Result};
22
use serde::{Deserialize, Serialize};
3-
use std::collections::HashMap;
3+
use std::collections::BTreeMap;
44
use std::time::Duration;
55
use tokio::time::sleep;
66

@@ -12,7 +12,7 @@ use mahler::worker::{init_logging, Worker};
1212
// since the library uses JSON internally to access parts
1313
// of the state
1414
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
15-
struct Counters(HashMap<String, i32>);
15+
struct Counters(BTreeMap<String, i32>);
1616

1717
// `plus_one` defines a job that updates a counter if it is below some target.
1818
// The job makes use of two extractors:
@@ -72,7 +72,7 @@ async fn main() -> Result<()> {
7272
.job("/{counter}", update(plus_two))
7373
// We initialize the worker with two counters
7474
// `a` and `b` with value 0
75-
.initial_state(Counters(HashMap::from([
75+
.initial_state(Counters(BTreeMap::from([
7676
("a".to_string(), 0),
7777
("b".to_string(), 0),
7878
])))
@@ -81,7 +81,7 @@ async fn main() -> Result<()> {
8181
// Tell the worker to find a plan from the initial state (a:0, b:0)
8282
// to the target state (a:1, b:2) and execute it
8383
let worker = worker
84-
.seek_target(Counters(HashMap::from([
84+
.seek_target(Counters(BTreeMap::from([
8585
("a".to_string(), 1),
8686
("b".to_string(), 2),
8787
])))
@@ -98,7 +98,9 @@ async fn main() -> Result<()> {
9898

9999
assert_eq!(
100100
state,
101-
Counters(HashMap::from([("a".to_string(), 1), ("b".to_string(), 2),]))
101+
Counters(BTreeMap::from(
102+
[("a".to_string(), 1), ("b".to_string(), 2),]
103+
))
102104
);
103105

104106
println!("The system state is now {:?}", state);

examples/readme/tests/cli.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use anyhow::Result;
2+
use assert_cmd::prelude::*; // Add methods on commands
3+
use predicates::prelude::*;
4+
use std::process::{Command, Output}; // Run programs
5+
6+
#[test]
7+
fn run_example() -> Result<()> {
8+
let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))?;
9+
let assert = cmd.env("RUST_LOG", "debug").assert();
10+
11+
let Output { stdout, stderr, .. } = cmd.unwrap();
12+
println!("{}", String::from_utf8_lossy(&stderr));
13+
println!("{}", String::from_utf8_lossy(&stdout));
14+
15+
assert.success().stdout(predicate::str::diff(
16+
"The system state is now Counters({\"a\": 1, \"b\": 2})\n",
17+
));
18+
19+
Ok(())
20+
}

examples/run-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export RUST_LOG="${RUST_LOG},bollard=off,hyper=off"
99
find ./examples -name 'Cargo.toml' | while read -r file; do
1010
dir=$(dirname "$file")
1111
echo "Running tests in $dir"
12-
(cd "$dir" && cargo test -- --test-threads 1) || {
12+
(cd "$dir" && cargo test -- --nocapture --test-threads 1) || {
1313
echo "Tests failed in $dir"
1414
exit 1
1515
}

0 commit comments

Comments
 (0)