opy-rs 0.1.29

Standalone OverPy-compatible .opy implementation with parsing, tooling, and bounded Workshop compilation.
Documentation
//! Indexed assignment lowering and catalog numeric coercion coverage.

use std::path::{Path, PathBuf};

use crate::Compiler;
use workshop_rs::catalog::{Catalog, Locale};
use workshop_rs::roundtrip::equivalent;

fn fixture_dir(name: &str) -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures/corpus/synthetic")
        .join(name)
}

#[test]
fn nested_indexed_assignments_match_the_pinned_oracle() {
    let dir = fixture_dir("indexed-assignment-nested");
    let source = std::fs::read_to_string(dir.join("source.opy")).unwrap();
    let hir = crate::compile(&source, "source.opy", &dir).expect("fixture must resolve");
    let artifact = Compiler::new().unwrap().compile_hir(&hir).unwrap();
    let oracle: serde_json::Value =
        serde_json::from_str(&std::fs::read_to_string(dir.join("oracle.json")).unwrap()).unwrap();
    let expected = workshop_rs::parser::parse(
        oracle["compile"]["workshop"].as_str().unwrap(),
        &Catalog::builtin().unwrap(),
        &Locale::new("en-US"),
    )
    .unwrap();
    assert!(equivalent(&artifact.wir, &expected));
}

#[test]
fn indexed_assignment_indices_use_catalog_numeric_coercions() {
    let source = r#"
globalvar values = [0]
globalvar nested = [[0]]

rule "boolean indices":
    @Event global
    values[true] = 1
    values[false] += 1
    nested[true][false] = 2
"#;
    let hir = crate::compile(source, "indexed-assignment-indices.opy", Path::new(".")).unwrap();
    let artifact = Compiler::new().unwrap().compile_hir(&hir).unwrap();
    assert!(
        artifact
            .emitted
            .contains("Set Global Variable At Index(values, 1, 1);")
    );
    assert!(
        artifact
            .emitted
            .contains("Modify Global Variable At Index(values, 0, Add, 1);")
    );
    assert!(
        artifact
            .emitted
            .contains("Set Global Variable At Index(nested, 1,")
    );
}