| 1 | use std::error::Error; |
| 2 | use std::path::PathBuf; |
| 3 | |
| 4 | #[cfg(feature = "debug")] |
| 5 | #[rstest::rstest] |
| 6 | #[case("complex")] |
| 7 | pub fn test_export(#[case] fixture: &str) -> Result<(), Box<dyn Error>> { |
| 8 | let mut fixture_dir = PathBuf::new(); |
| 9 | fixture_dir.push(env!("CARGO_MANIFEST_DIR")); |
| 10 | fixture_dir.push("tests"); |
| 11 | fixture_dir.push("data"); |
| 12 | fixture_dir.push("export"); |
| 13 | fixture_dir.push(fixture); |
| 14 | |
| 15 | let input = fixture_dir.join("input.rs"); |
| 16 | let output_file_dot = fixture_dir.join("output.dot"); |
| 17 | let output_file_mermaid = fixture_dir.join("output.mmd"); |
| 18 | |
| 19 | let input = std::fs::read_to_string(input)?; |
| 20 | let output_dot = std::fs::read_to_string(&output_file_dot)?; |
| 21 | let output_mermaid = std::fs::read_to_string(&output_file_mermaid)?; |
| 22 | |
| 23 | let _ = logos_codegen::generate(input.parse()?); |
| 24 | |
| 25 | let generated_dot = std::fs::read_to_string(format!("export_tmp/{}.dot", fixture))?; |
| 26 | let generated_mermaid = std::fs::read_to_string(format!("export_tmp/{}.mmd", fixture))?; |
| 27 | |
| 28 | if std::env::var("BLESS_EXPORT").is_ok_and(|value| value == "1") { |
| 29 | std::fs::write(&output_file_dot, &generated_dot)?; |
| 30 | std::fs::write(&output_file_mermaid, &generated_mermaid)?; |
| 31 | |
| 32 | // cleanup |
| 33 | let _ = std::fs::remove_dir_all("export_tmp"); |
| 34 | |
| 35 | return Ok(()); |
| 36 | } |
| 37 | |
| 38 | assert_eq!(generated_dot, output_dot, "Export test failed: `{fixture}`, run tests again with env var `BLESS_EXPORT=1` to bless these changes"); |
| 39 | assert_eq!(generated_mermaid.replace("\r\n", "\n"), output_mermaid.replace("\r\n", "\n"), "Export test failed: `{fixture}`, run tests again with env var `BLESS_EXPORT=1` to bless these changes"); |
| 40 | |
| 41 | // cleanup |
| 42 | let _ = std::fs::remove_dir_all("export_tmp"); |
| 43 | |
| 44 | Ok(()) |
| 45 | } |