postscriptum 0.1.1

Multi-interpreted-language scripting engine for Rust with Rhai and Python backends
Documentation
//! G1 acceptance tests: fuel/resource budgets on `BoundedRhaiEngine`
//! and explicit fuel refusal on `PythonEngine`.
//!
//! Conformance mirror: quartertone `fuel_cap_fires`
//! (`src/views/expressions.rs`) — same shape, same assertion text.

#![cfg(feature = "rhai")]

use postscriptum::{BoundedRhaiEngine, Error, LimitKind};

#[test]
fn while_true_aborts_with_matchable_fuel_error() {
    let mut engine = BoundedRhaiEngine::with_defaults();
    let err = engine.eval("while true {}").unwrap_err();
    assert!(
        matches!(
            err,
            Error::ResourceExhausted {
                kind: LimitKind::Operations,
                ..
            }
        ),
        "expected matchable fuel exhaustion, got: {err:?}"
    );
}

#[test]
fn oversized_string_build_aborts_with_string_kind() {
    // Build a 70 KiB string at runtime (a literal that long is
    // rejected by the parser instead — that stays a plain syntax
    // error for G7 validation to report). Generous op budget so the
    // loop itself never trips fuel before the string cap fires; the
    // range is an iterator, not an array, so no array cap involved.
    let mut engine = BoundedRhaiEngine::with_defaults()
        .with_max_source_len(1024 * 1024)
        .with_max_operations(10_000_000);
    let err = engine
        .eval(r#"let s = ""; for i in 0..100000 { s += "0123456789"; }"#)
        .unwrap_err();
    assert!(
        matches!(
            err,
            Error::ResourceExhausted {
                kind: LimitKind::String,
                ..
            }
        ),
        "expected string-size exhaustion, got: {err:?}"
    );
}

#[test]
fn hungry_row_does_not_starve_the_next_row() {
    // Tiny budget so the test runs in milliseconds; the point is the
    // per-eval reset, not the default numbers.
    let mut engine = BoundedRhaiEngine::new().with_max_operations(100);
    let err = engine.eval("while true {}").unwrap_err();
    assert!(
        matches!(
            err,
            Error::ResourceExhausted {
                kind: LimitKind::Operations,
                ..
            }
        ),
        "expected fuel exhaustion, got: {err:?}"
    );
    // Fresh budget on the next top-level eval: ordinary code runs.
    assert_eq!(engine.eval("40 + 2").unwrap(), "42");
}

#[test]
fn call_depth_cap_fires() {
    let mut engine = BoundedRhaiEngine::new().with_max_call_levels(8);
    let err = engine
        .eval("fn down(n) { if n <= 0 { 0 } else { down(n - 1) } } down(100)")
        .unwrap_err();
    assert!(
        matches!(
            err,
            Error::ResourceExhausted {
                kind: LimitKind::CallDepth,
                ..
            }
        ),
        "expected call-depth exhaustion, got: {err:?}"
    );
}

#[test]
#[cfg(feature = "python")]
fn python_bounded_entry_returns_limit_unsupported() {
    let err = match postscriptum::python::PythonEngine::with_limits() {
        Ok(_) => panic!("expected LimitUnsupported refusal, engine was built"),
        Err(e) => e,
    };
    assert!(
        matches!(err, Error::LimitUnsupported { .. }),
        "expected LimitUnsupported refusal, got: {err:?}"
    );
}