Skip to main content

lox_test_utils/
lib.rs

1// SPDX-FileCopyrightText: 2025 Helge Eichhorn <git@helgeeichhorn.de>
2//
3// SPDX-License-Identifier: MPL-2.0
4
5//! Test utilities for the Lox ecosystem.
6//!
7//! Provides approximate equality testing (`approx_eq`), test data helpers, and the
8//! `#[derive(ApproxEq)]` macro (behind the `derive` feature).
9
10#![warn(missing_docs)]
11
12use std::{
13    fs::read_to_string,
14    path::{Path, PathBuf},
15};
16
17pub mod approx_eq;
18
19/// Returns a [PathBuf] to the test fixture directory.
20pub fn data_dir() -> PathBuf {
21    PathBuf::from(format!("{}/../../data", env!("CARGO_MANIFEST_DIR")))
22}
23
24/// Returns a [PathBuf] to a file in the test fixture directory.
25pub fn data_file(path: impl AsRef<Path>) -> PathBuf {
26    data_dir().join(path)
27}
28
29/// Returns the contents of the data file at `path`.
30///
31/// # Panics
32/// This function will panic if the file does not exist or is otherwise unreadable.
33pub fn read_data_file(path: impl AsRef<Path>) -> String {
34    read_to_string(data_file(path)).expect("data file should be readable")
35}
36
37#[cfg(feature = "derive")]
38#[doc(inline)]
39pub use lox_derive::ApproxEq;