oxiforge 0.4.0

YAML-to-Rust code generator for oxivgl LVGL UIs
Documentation
// SPDX-License-Identifier: GPL-3.0-only
//! Snapshot tests for generated Rust code.
//!
//! Each YAML fixture in `tests/fixtures/compile_check/` is run through
//! `generate_from_str()` and the output is snapshot-tested with insta.
//! Any codegen change produces a visible diff for review.

use std::fs;

/// Map a fixture file stem to the Cargo feature it requires, or `None` if it
/// only uses core widgets.
fn required_feature(stem: &str) -> Option<&'static str> {
    // The `feat_<widget>.yaml` fixtures exercise optional widget codegen; with
    // the matching feature off, parsing fails because the widget tag is unknown.
    match stem {
        "feat_animimg" => Some("widget-animimg"),
        "feat_arclabel" => Some("widget-arc-label"),
        "feat_buttonmatrix" => Some("widget-buttonmatrix"),
        "feat_calendar" => Some("widget-calendar"),
        "feat_canvas" => Some("widget-canvas"),
        "feat_chart" => Some("widget-chart"),
        "feat_imagebutton" => Some("widget-imagebutton"),
        "feat_keyboard" => Some("widget-keyboard"),
        "feat_list" => Some("widget-list"),
        "feat_menu" => Some("widget-menu"),
        "feat_msgbox" => Some("widget-msgbox"),
        "feat_spangroup" => Some("widget-spangroup"),
        "feat_spinbox" => Some("widget-spinbox"),
        "feat_spinner" => Some("widget-spinner"),
        "feat_table" => Some("widget-table"),
        "feat_tabview" => Some("widget-tabview"),
        "feat_textarea" => Some("widget-textarea"),
        "feat_tileview" => Some("widget-tileview"),
        "feat_win" => Some("widget-win"),
        _ => None,
    }
}

/// Returns true when the named Cargo feature is enabled for this test build.
fn feature_enabled(feature: &str) -> bool {
    match feature {
        "widget-animimg" => cfg!(feature = "widget-animimg"),
        "widget-arc-label" => cfg!(feature = "widget-arc-label"),
        "widget-buttonmatrix" => cfg!(feature = "widget-buttonmatrix"),
        "widget-calendar" => cfg!(feature = "widget-calendar"),
        "widget-canvas" => cfg!(feature = "widget-canvas"),
        "widget-chart" => cfg!(feature = "widget-chart"),
        "widget-imagebutton" => cfg!(feature = "widget-imagebutton"),
        "widget-keyboard" => cfg!(feature = "widget-keyboard"),
        "widget-list" => cfg!(feature = "widget-list"),
        "widget-menu" => cfg!(feature = "widget-menu"),
        "widget-msgbox" => cfg!(feature = "widget-msgbox"),
        "widget-spangroup" => cfg!(feature = "widget-spangroup"),
        "widget-spinbox" => cfg!(feature = "widget-spinbox"),
        "widget-spinner" => cfg!(feature = "widget-spinner"),
        "widget-table" => cfg!(feature = "widget-table"),
        "widget-tabview" => cfg!(feature = "widget-tabview"),
        "widget-textarea" => cfg!(feature = "widget-textarea"),
        "widget-tileview" => cfg!(feature = "widget-tileview"),
        "widget-win" => cfg!(feature = "widget-win"),
        _ => false,
    }
}

#[test]
fn snapshot_codegen() {
    insta::glob!("fixtures/compile_check/*.yaml", |path| {
        let stem = path.file_stem().and_then(|s| s.to_str()).expect("fixture path has stem");
        if let Some(feat) = required_feature(stem)
            && !feature_enabled(feat)
        {
            return;
        }
        let yaml = fs::read_to_string(path).expect("fixture should be readable");
        let code = oxiforge::generate_from_str(&yaml).expect("codegen should succeed");
        insta::assert_snapshot!(code);
    });
}