Skip to main content

oak_testing/
lib.rs

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