feat: Add testing and benchmarking capabilities

This commit introduces a new `tester` module to the `ast` crate,
enabling functional tests and performance benchmarks for MYC scripts.

Functional tests are executed by reading `.myc` files from the
`examples` directory, parsing expected output comments, and comparing
them against the actual script execution results.

Benchmarking involves measuring the execution time of scripts,
calculating median durations, and comparing them against stored
baselines. The commit also adds support for updating these baselines.

The `ast.rs` CLI and the `main.rs` GUI application have been updated to
expose these new testing and benchmarking features. The `Cargo.toml` and
`Cargo.lock` files have been updated to include the `regex` dependency
required for parsing benchmark comments.
This commit is contained in:
Michael Schimmel
2026-02-17 14:47:04 +01:00
parent b1ca16149d
commit b0f139f389
12 changed files with 283 additions and 30 deletions
+27 -2
View File
@@ -1,24 +1,49 @@
use myc::ast::environment::Environment;
use myc::ast::tester;
use clap::Parser;
use std::fs;
use std::path::PathBuf;
#[derive(Parser)]
#[command(author, version, about = "MYC AST Compiler CLI", long_about = None)]
#[command(author, version, about = "MYC AST Compiler & Benchmarker", long_about = None)]
struct Cli {
/// The script file to run
/// The script file to run or benchmark
#[arg(value_name = "FILE")]
file: Option<PathBuf>,
/// Run a script string directly
#[arg(short, long)]
eval: Option<String>,
/// Run benchmarks (Only allowed in Release mode)
#[arg(short, long)]
bench: bool,
/// Update the benchmark baseline (Only allowed in Release mode)
#[arg(short, long)]
update_bench: bool,
}
fn main() {
let cli = Cli::parse();
let env = Environment::new();
if cli.bench || cli.update_bench {
if cfg!(debug_assertions) {
eprintln!("❌ ERROR: Benchmarks must be run in Release mode!");
eprintln!(" Use: cargo run --release --bin ast -- --bench");
std::process::exit(1);
}
println!("🚀 Running benchmarks in RELEASE mode...\n");
let results = tester::run_benchmarks(cli.update_bench);
for res in results {
let diff = res.diff_pct.map_or(String::new(), |d| format!(" ({:+.1}%)", d));
println!("{}: {} - {:?}{}", res.status, res.name, res.median, diff);
}
return;
}
if let Some(script_str) = cli.eval {
execute(&env, &script_str);
} else if let Some(file_path) = cli.file {