Something went wrong. Try again.
A human-friendly DSL for ATProto Lexicons forked from stavola.xyz/mlf
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465// Workspace-level integration tests for code generation.// Each subdirectory of `codegen/lexicon/` is a test fixture containing// `input.mlf`, optionally `test.toml`, and insta snapshot files// (`lexicon.snap`, `warnings.snap`) checked into the fixture directory.//// To update snapshots after an intentional change:// just test-codegen # produces .snap.new files// just review # interactive accept/reject
use mlf_codegen::generate_lexicon;use mlf_integration_tests::test_utils;use mlf_lang::{parser::parse_lexicon, Workspace};use std::fs;use std::path::Path;
fn run_lexicon_test(input_path: &Path) -> datatest_stable::Result<()> { let test_dir = input_path .parent() .ok_or("input.mlf has no parent directory")?; let test_name = test_dir .file_name() .and_then(|s| s.to_str()) .ok_or("input.mlf parent has no name")? .to_string();
let namespace = test_utils::load_test_config(test_dir, |name| format!("com.example.{}", name)) .ok() .and_then(|c| c.test.namespace) .unwrap_or_else(|| format!("com.example.{}", test_name));
let input = fs::read_to_string(input_path)?; let lexicon = parse_lexicon(&input).map_err(|e| format!("Failed to parse: {:?}", e))?;
let mut ws = Workspace::with_std().map_err(|e| format!("Failed to create workspace: {:?}", e))?; ws.add_module(namespace.clone(), lexicon) .map_err(|e| format!("Failed to add module: {:?}", e))?; ws.resolve() .map_err(|e| format!("Failed to resolve: {:?}", e))?;
let lexicon = ws.get_lexicon(&namespace).ok_or("Module not found")?; let output = generate_lexicon(&namespace, lexicon, &ws);
let formatted_warnings: Vec<String> = output .warnings .iter() .map(|w| format!("{}: {}", w.namespace, w.message)) .collect();
insta::with_settings!({ snapshot_path => test_dir.to_path_buf(), prepend_module_to_snapshot => false, snapshot_suffix => test_name, }, { insta::assert_json_snapshot!("lexicon", output.json); insta::assert_debug_snapshot!("warnings", formatted_warnings); });
Ok(())}
datatest_stable::harness! { { test = run_lexicon_test, root = "codegen/lexicon", pattern = r"^.*/input\.mlf$" },}