Skip to main content

oak_testing/
lib.rs

1#![feature(new_range_api)]
2#![warn(missing_docs)]
3//! Testing utilities for the Oak ecosystem.
4//!
5//! This crate provides comprehensive testing infrastructure for lexers, parsers,
6//! and builders, including file-based testing, expected output comparison,
7//! timeout handling, and test result serialization.
8
9/// Testing utilities for tree building.
10pub mod building;
11/// Testing utilities for lexing.
12pub mod lexing;
13/// Testing utilities for parsing.
14pub mod parsing;
15
16use oak_core::{errors::OakError, source::SourceText};
17use std::{fs::File, path::Path};
18
19/// Reads source text from a file path.
20pub fn source_from_path(path: &Path) -> Result<SourceText, OakError> {
21    match std::fs::read_to_string(path) {
22        Ok(o) => Ok(SourceText::new(o)),
23        Err(e) => Err(OakError::io_error(e, 0)),
24    }
25}
26
27/// Reads JSON data from a file path.
28#[cfg(feature = "serde")]
29pub fn json_from_path(path: &Path) -> Result<serde_json::Value, OakError> {
30    let content = std::fs::read_to_string(path).map_err(|e| OakError::io_error(e, 0))?;
31    serde_json::from_str(&content).map_err(|e| OakError::custom_error(e.to_string()))
32}
33
34/// Opens a file and returns a file handle.
35pub fn open_file(path: &Path) -> Result<File, OakError> {
36    match File::open(path) {
37        Ok(o) => Ok(o),
38        Err(e) => Err(OakError::io_error(e, 0)),
39    }
40}
41
42/// Creates a file and its parent directories.
43pub fn create_file(path: &Path) -> Result<std::fs::File, OakError> {
44    if let Some(parent) = path.parent() {
45        std::fs::create_dir_all(parent).map_err(|e| OakError::custom_error(e.to_string()))?
46    }
47    std::fs::File::create(path).map_err(|e| OakError::custom_error(e.to_string()))
48}