Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Testing

Node’s built-in runner for JavaScript/compiled TypeScript, Go testing, Rust cargo test, and optional GoogleTest for C++. Excerpts assume a test body and named application functions unless marked as complete.

Declare a test

Rust

Mark test functions with #[test]. Unit tests commonly live in a cfg(test) module; a panic signals failure.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#[test]
fn doubles_a_value() {
    assert_eq!(double(3), 6);
}

Run the suite with its native runner

Rust

cargo test builds and runs unit, integration and documentation tests for the selected package/workspace.

Shell: cargo test; cargo test --workspace

Run a focused test

Rust

The positional filter selects matching names; arguments after -- go to the test harness.

Shell: cargo test doubles -- --nocapture

Name related cases

Rust

Standard #[test] has no dynamic subtest result tree. Prefer separate test functions or a table whose assertion names the failing case.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
for n in [0, 3] {
    assert_eq!(double(n), 2 * n, "input={n}");
}

Choose equality with the right semantics

Rust

assert_eq! uses PartialEq and prints Debug representations. Equality semantics come from the value’s implementation.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
assert_eq!(actual_items, vec![1, 2]);

Compare floating-point values with a domain tolerance

Rust

Machine epsilon alone is not a universal domain tolerance. Combine absolute/relative policy deliberately.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let tolerance = 1e-9_f64 * expected.abs().max(1.0);
assert!(actual.is_finite());
assert!((actual - expected).abs() <= tolerance);

Check expected failures

Rust

Inspect Result’s error instead of converting an expected error into an accidental unwrap panic.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
assert!(parse("bad").is_err());

Report observed values and the case identity

Rust

A useful failure says which input failed, what was observed and what was expected. Assert public behavior; avoid pinning incidental private details that are free to change.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
case: empty input
observed: one item
expected: no items

Register cleanup with ownership

Rust

Use RAII/Drop for owned resources, or explicit teardown where errors matter. A panic does not make an asynchronous cleanup future run automatically.

Fragment

Optional tempfile crate; the surrounding test returns Result. The guard removes the directory on drop.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let directory = tempfile::tempdir()?;
let path = directory.path().join("example.txt");

Wait for the actual asynchronous result

Rust

The standard test harness is synchronous. An optional runtime such as Tokio supplies an async test attribute.

Fragment

Tokio dependency needs macros and rt features.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#[tokio::test]
async fn loads() {
    assert_eq!(load().await, "ready");
}

Bound a wait and stop owned work

Rust

Tokio timeout drops the inner future on expiry; this does not automatically cancel detached tasks or an already-started blocking operation.

Fragment

Tokio time feature and a surrounding async Result test are required.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
tokio::time::timeout(
    std::time::Duration::from_secs(1), load()
).await??;

Give each test its own mutable resources

Rust

Use temporary directories, unique database names/keys, random free ports and explicit clocks/configuration. Restore global state in cleanup when isolation is impossible. Tests that run in parallel must not mutate the same process-wide state unsafely.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
test A → its own directory / namespace
test B → its own directory / namespace

Choose a double for a boundary

Rust

A stub supplies answers, a fake implements simplified behavior and a mock verifies chosen interactions. Keep essential protocol semantics in the double; separately test the real adapter against the real dependency.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
core logic → fake clock/storage
adapter test → real clock/storage boundary

Control time without arbitrary sleeps

Rust

Inject a clock/scheduler or use the runner’s documented fake-time facilities. Advance to an observable condition; sleeping a guessed duration creates slow and flaky tests. Real I/O and other threads may not use the fake clock.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
arrange a deadline
advance the controlled clock
assert the observable transition

Pick boundary and equivalence cases

Rust

Cover ordinary input, empty/min/max values, malformed input, duplicates, missing values and meaningful ordering. Derive cases from the contract rather than repeating the implementation’s branches.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
empty / one / many
just below / at / just above a threshold
valid / malformed / missing

State an invariant before generating inputs

Rust

Property testing checks a relationship over generated inputs and often shrinks a failure. The language’s test runner alone does not always supply a property framework. Define input constraints and preserve the failing seed/example.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
decode(encode(value)) = value
sort(sort(values)) = sort(values)
output length = input length

Use an explicit fuzzing tool

Rust

Property libraries such as proptest and fuzzing tools such as cargo-fuzz are optional, separate tools with different workflows.

Preserve the regression corpus and state any supported input domain; cargo test itself is not a coverage-guided fuzz engine.

Test the real boundary with a focused integration case

Rust

Start the actual dependency with a known configuration, create isolated data, exercise one external contract and tear it down. Include serialization, error mapping, timeouts and authorization where they belong to that boundary.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
unit: business rule with fake storage
integration: real driver + disposable database
end-to-end: supported user workflow

Use coverage to find unexercised paths

Rust

Coverage shows what ran under a selected instrumentation model. A high percentage does not prove correct assertions, all input combinations or absence of races. Inspect missing meaningful behavior rather than forcing a target number.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
line / branch / function coverage
covered code can still have a wrong assertion