Skip to content

Modules, tests and docs

Each file is a module. import geometry loads geometry.brass next to the importing file, and import shapes.circle loads shapes/circle.brass. Only items marked pub can be used from outside.

because geometry.brass
pub struct Point {
pub x: f64
pub y: f64
}
pub fn distance(a: &Point, b: &Point) -> f64 {
return (a.x - b.x).hypot(a.y - b.y)
}
because main.brass
import geometry
let d = geometry.distance(&geometry.Point { x: 0.0, y: 0.0 }, &geometry.Point { x: 3.0, y: 4.0 })
print(f"{d}")

The standard library is imported the same way: import fs, import json, import http. A larger program starts with brass new app, which makes a brass.toml project; brass add name <git url> <version> adds a package.

Tests live next to the code they test, in test blocks. brass test file.brass runs them; a normal brass run skips them.

fn slugify(title: &Str) -> Str {
return title.trim().to_lower().replace(" ", "-")
}
test "spaces become dashes" {
assert_eq(slugify(&" Hello World "), "hello-world")
}
test "already a slug" for word: Str {
let once = slugify(&word)
assert_eq(slugify(&once), once)
}

The second test is a property test: Brass makes up many values for word and, when one fails, shrinks it to a small example. brass test --coverage shows which lines the tests never ran, and bench "name" { ... } blocks run with brass test --bench.

brass doc file.brass writes a Markdown page for a module: every public item with its signature and the because lines above it. Longer text and examples go into a Markdown file next to the module, geometry.md beside geometry.brass. Its text before the first ## heading opens the page, and a section ## distance (or ## Point.x for a method) is placed under that item. A section for a name the module does not have is an error, so docs cannot quietly go stale when code is renamed.

Code blocks in Markdown files are tested: brass test notes.md compiles every ```brass block, runs ```brass run blocks and compares their output with the ```text block after them, and checks that ```brass fails blocks fail with the error shown. This page is tested that way.