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

C++

GoogleTest TEST defines a suite/test pair. Link an installed GoogleTest target and provide a test main or gtest_main.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <gtest/gtest.h>
TEST(DoubleTest, DoublesAValue) {
    EXPECT_EQ(double_value(3), 6);
}

Run the suite with its native runner

C++

CTest runs tests registered by your CMake project. Building alone does not execute them.

Shell: cmake --build build; ctest --test-dir build --output-on-failure. Register with include(CTest) and gtest_discover_tests after finding GoogleTest.

Run a focused test

C++

GoogleTest supports suite/test filters; CTest also offers its own registered-test regex filter.

Shell: ./build/tests --gtest_filter=DoubleTest.*; ctest --test-dir build -R DoubleTest --output-on-failure

Name related cases

C++

GoogleTest parameterized tests report separate instantiations. TEST_P, fixture declaration and instantiation work together.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
class DoubleCases : public testing::TestWithParam<int> {};
TEST_P(DoubleCases, Doubles) {
    EXPECT_EQ(double_value(GetParam()), 2 * GetParam());
}
INSTANTIATE_TEST_SUITE_P(Inputs, DoubleCases,
    testing::Values(0, 3));

Choose equality with the right semantics

C++

EXPECT_EQ uses equality and keeps the test running. ASSERT_EQ aborts the current test function on failure.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
EXPECT_EQ(actual_count, 3);
ASSERT_EQ(actual_items.size(), 2u);

Compare floating-point values with a domain tolerance

C++

EXPECT_NEAR uses an absolute error bound. Choose it for the scale of the value; EXPECT_DOUBLE_EQ follows a different floating-point comparison contract.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
EXPECT_NEAR(actual, expected, 1e-9);

Check expected failures

C++

GoogleTest can check thrown exception types. Error-code/expected-return APIs need assertions on that return value instead.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
EXPECT_THROW(parse("bad"), std::invalid_argument);

Report observed values and the case identity

C++

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

main.cpp
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

C++

RAII guards release owned resources when leaving scope, including assertion-induced returns. Fixtures can supply SetUp/TearDown for repeated setup.

Fragment

Include memory; Resource is an application-defined RAII type.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
auto resource = std::make_unique<Resource>();
ASSERT_TRUE(resource->ready());

Wait for the actual asynchronous result

C++

Join/await the owned computation before the test ends. Future get propagates the stored exception to the caller.

Fragment

Include future; load is an application function.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
auto result = std::async(std::launch::async, load);
EXPECT_EQ(result.get(), "ready");

Bound a wait and stop owned work

C++

wait_for can bound waiting on a future, but does not cancel its task. A future from async may still wait at destruction; use cooperative stop and owned joining.

Fragment

This checks readiness only; it is not a cancellation implementation.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
auto status = result.wait_for(std::chrono::seconds(1));
EXPECT_EQ(status, std::future_status::ready);

Give each test its own mutable resources

C++

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

main.cpp
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

C++

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

main.cpp
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

C++

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

main.cpp
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

C++

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

main.cpp
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

C++

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

main.cpp
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

C++

Coverage-guided fuzzers such as libFuzzer require an explicit target and compatible compiler instrumentation. GoogleTest alone is not a fuzzer.

LLVM libFuzzer: https://llvm.org/docs/LibFuzzer.html

Test the real boundary with a focused integration case

C++

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

main.cpp
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

C++

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

main.cpp
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