Skip to main content

oak_testing/
lib.rs

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