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.

Open in playground
//// 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,
    ),
  )
}

This lesson's code is a module of a small Bea project, compiled and run by bea for this page: content/features/src/testing.bea