Chapter 7: Records, guards and deriving
Testing
- bea test
- gleeunit
- bea pack
A Bea project's tests live in test/, written in
Gleam, and bea test runs them: the main of
test/<package>_test.gleam, here gleeunit's
(bea add --dev gleeunit). They are built with the rest of the
project and run from the project root, so a test can read files relative
to it.
Tests call Bea modules like any other module:
testing.slug(…). A type error in a test points at the test
file.
When a program is ready to share, bea pack turns it into a
single executable that runs without Erlang installed. The
toolchain page has the details.
//// A module of the project and its tests: `bea test` runs
//// test/features_test.gleam, which tests Bea code from Gleam.
pub def slug(title: String) -> String {
title
|> string.lowercase
|> string.split(" ")
|> list.filter(fn(word) { word != "" })
|> string.join("-")
}
/// The reading time of a text, in whole minutes, at least one.
pub def minutes(text: String, words_per_minute: Int) -> Result(Int, String) {
words_per_minute > 0 else Error("words per minute must be positive")
words = text |> string.split(" ") |> list.count(fn(w) { w != "" })
Ok(
int.max(
1,
{
words + words_per_minute - 1
} / words_per_minute,
),
)
}//// A module of the project and its tests: `bea test` runs
//// test/features_test.gleam, which tests Bea code from Gleam.
import gleam/int
import gleam/list
import gleam/string
pub fn slug(title: String) -> String {
title
|> string.lowercase
|> string.split(" ")
|> list.filter(fn(word) { word != "" })
|> string.join("-")
}
/// The reading time of a text, in whole minutes, at least one.
pub fn minutes(text: String, words_per_minute: Int) -> Result(Int, String) {
case words_per_minute > 0 {
True -> {
let words = text |> string.split(" ") |> list.count(fn(w) { w != "" })
Ok(int.max(1, { words + words_per_minute - 1 } / words_per_minute))
}
False -> Error("words per minute must be positive")
}
}import gleeunit
import testing
pub fn main() -> Nil {
gleeunit.main()
}
pub fn slug_test() {
assert testing.slug("Hello, Bea World") == "hello,-bea-world"
assert testing.slug(" spaces everywhere ") == "spaces-everywhere"
}
pub fn minutes_test() {
assert testing.minutes("one two three", 2) == Ok(2)
assert testing.minutes("", 200) == Ok(1)
assert testing.minutes("words", 0)
== Error("words per minute must be positive")
}$ bea test
..
2 passed, no failuresThis lesson's code is a module of a small Bea project, compiled and run by bea for this page: content/features/src/testing.bea