source_dir: The directory containing all the source files. Path resolution is relative to the project root. Default value:src.output_dir: The directory where all the doc tests will be generated. Path resolution is relative to the project root. Default value:test.extra_imports: A list of module names that will automatically be imported in every test. Unused imports will be removed in the final test. Example:["gleam/int", "gleam/otp/actor"].- You can see it in action in
dev/fixtures/store.gleam
- You can see it in action in
preserve_tests: Whether to keep the generated test files inoutput_dirafter the test run finishes. Default value:False. When usinggleedoc.run_withtogether withgleeunit, leaving this asFalsekeeps youroutput_dirclean between runs. If you are callinggleedoc.runprogrammatically (for example from adev/prepare_tests.gleamscript that only generates tests), set this toTrueso the generated files are not deleted afterwards.source_mapped_errors: Whether to annotate every generatedassertline withas "file:line", pointing back to the exact source line of the assertion in the original doc comment. Default value:True. When enabled, test failures are reported against the original source file/line (e.g.src/math.gleam:5) instead of the generated test file. Set toFalseif your snippets place theassertsomewhere that doesn't fit a single-lineasannotation well, or if you simply prefer the unannotated output. See the README for the known limitation that motivates this flag.
A gleedoc.default() helper is provided that returns a GleedocConfig with the following defaults:
source_dir: "src"output_dir: "test"extra_imports: []preserve_tests: Falsesource_mapped_errors: True
You can use it as-is or override individual fields with record update syntax:
let config = gleedoc.GleedocConfig(..gleedoc.default(), preserve_tests: True)A set of builder-style APIs is provided for more fluent configuration customization.
newwith_source_dirwith_output_dirwith_extra_importsadd_extra_importwith_preserve_testswith_source_mapped_errors
import gleedoc
import gleeunit
pub fn main() {
gleedoc.new()
|> gleedoc.with_extra_imports(["gleam/int", "gleam/string"])
|> gleedoc.with_preserve_tests(True)
|> gleedoc.with_source_mapped_errors(False)
|> gleedoc.run_with(gleeunit.main)
}Run gleedoc with gleeunit tests (or any other test functions you want to run).
It takes two arguments: a GleedocConfig object and function that will be executed after gleedoc generates all the doc tests. Typically, it should be gleeunit.main.
import gleedoc
import gleeunit
pub fn main() {
let config =
gleedoc.GleedocConfig(
output_dir: "test/integration",
source_dir: "dev/fixtures",
extra_imports: ["gleam/int", "gleam/string"],
preserve_tests: False,
source_mapped_errors: True,
)
config |> gleedoc.run_with(gleeunit.main)
}Run gleedoc on a project, extracting doc tests from source files and generating test files in the output directory. Typically, you'd want to use this in your own test preparation module:
import gleedoc
pub fn main() {
let config =
gleedoc.GleedocConfig(
output_dir: "test/integration",
source_dir: "dev/fixtures",
extra_imports: ["gleam/int"],
preserve_tests: True,
source_mapped_errors: True,
)
let assert Ok(_) = gleedoc.run(config)
}