From 73febb9177d86596522a4b7017ba6c3004826970 Mon Sep 17 00:00:00 2001 From: jacksonfu14 Date: Mon, 17 Nov 2025 17:21:40 +0100 Subject: refactor: call lilypond once per batch --- morphology-img/tests/render.rs | 428 +++++++++-------------------------------- 1 file changed, 90 insertions(+), 338 deletions(-) (limited to 'morphology-img/tests/render.rs') diff --git a/morphology-img/tests/render.rs b/morphology-img/tests/render.rs index b6538ab..3e86f97 100644 --- a/morphology-img/tests/render.rs +++ b/morphology-img/tests/render.rs @@ -1,12 +1,11 @@ -use morphology_img::render::{check_lilypond_available, render_ly_to_svg, render_ly_files_to_svg}; +use morphology_img::render::{check_lilypond_available, render_all_ly_files_batch}; use morphology_img::error::RenderError; use morphology_img::file_gen::{create_temp_dir, generate_ly_file, write_support_files, generate_ly_files}; use morphology::{Sequence, Value}; use std::path::Path; use tempfile::TempDir; -/// This test requires LilyPond to be installed on the system. -/// Run with: cargo test -- --ignored +/// Test that LilyPond is available (requires LilyPond installation) #[test] #[ignore] fn test_lilypond_available() { @@ -17,10 +16,7 @@ fn test_lilypond_available() { let version = result.unwrap(); println!("LilyPond version: {}", version); - // Verify the version string is not empty assert!(!version.is_empty(), "version string should not be empty"); - - // Verify it contains "LilyPond" or starts with "GNU" assert!( version.contains("LilyPond") || version.starts_with("GNU"), "version string should contain 'LilyPond' or start with 'GNU', got: {}", @@ -28,18 +24,14 @@ fn test_lilypond_available() { ); } -/// Test that verifies the error type when LilyPond is not found. -/// This test will pass if LilyPond is NOT installed, or will be skipped if it is. +/// Test error type when LilyPond is not found #[test] fn test_lilypond_not_found_error_type() { - // Try to check for LilyPond let result = check_lilypond_available(); - // If it fails, verify it's the right error type if let Err(e) = result { match e { RenderError::LilyPondNotFound(msg) => { - // Verify the error message is helpful assert!(!msg.is_empty(), "error message should not be empty"); assert!( msg.contains("LilyPond") || msg.contains("PATH"), @@ -49,450 +41,221 @@ fn test_lilypond_not_found_error_type() { _ => panic!("Expected RenderError::LilyPondNotFound, got: {:?}", e), } } else { - // If LilyPond is available, we can't test the error case, so skip println!("LilyPond is installed, skipping error test"); } } -/// Test that we handle the case where the command exists but fails -#[test] -fn test_command_failure_handling() { - use std::process::Command; - - // Use a command that exists but will fail with --version - // (most systems have 'false' which always returns non-zero) - let output = Command::new("false") - .output(); - - if output.is_ok() { - let out = output.unwrap(); - // If false command exists, verify we can detect failure status - assert!(!out.status.success(), "false command should have non-success status"); - } -} - -// Tests for render_ly_to_svg (single file SVG generation) - -/// Test rendering a single .ly file to SVG +/// Test rendering multiple .ly files to SVGs in a single batch #[test] #[ignore] -fn test_render_single_svg() { - // Create a simple sequence - let seq = Sequence::new(vec![Value::origin()]); +fn test_render_batch_basic() { + let sequences = vec![ + Sequence::new(vec![Value::origin()]), + Sequence::new(vec![Value::new(1, 0)]), + Sequence::new(vec![Value::new(-1, 0)]), + ]; - // Generate .ly file let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); + let ly_files = generate_ly_files(&sequences, temp_dir.path()).unwrap(); - let ly_path = temp_dir.path().join("test.ly"); - generate_ly_file(&seq, &ly_path).unwrap(); - - // Render to SVG let output_dir = TempDir::new().unwrap(); - let svg_path = render_ly_to_svg( - &ly_path, + let svg_paths = render_all_ly_files_batch( + &ly_files, output_dir.path(), Path::new("lilypond"), ).unwrap(); - // Verify SVG exists - assert!(svg_path.exists(), "SVG file should exist"); - - // Verify SVG has content - let svg_content = std::fs::read_to_string(&svg_path).unwrap(); - assert!(svg_content.contains(" 1000, "SVG should have substantial content"); -} - -/// Test that output directory is created if it doesn't exist -#[test] -#[ignore] -fn test_creates_output_directory() { - let seq = Sequence::new(vec![Value::origin()]); - - let temp_dir = create_temp_dir().unwrap(); - write_support_files(temp_dir.path()).unwrap(); - - let ly_path = temp_dir.path().join("test.ly"); - generate_ly_file(&seq, &ly_path).unwrap(); - - // Use a nested directory that doesn't exist yet - let output_base = TempDir::new().unwrap(); - let output_dir = output_base.path().join("nested").join("output"); - - assert!(!output_dir.exists(), "output directory should not exist yet"); - - let svg_path = render_ly_to_svg( - &ly_path, - &output_dir, - Path::new("lilypond"), - ).unwrap(); - - // Verify directory was created - assert!(output_dir.exists(), "output directory should be created"); - assert!(svg_path.exists(), "SVG should be in created directory"); + assert_eq!(svg_paths[0].file_name().unwrap().to_str().unwrap(), "sequence-0.svg"); + assert_eq!(svg_paths[1].file_name().unwrap().to_str().unwrap(), "sequence-1.svg"); + assert_eq!(svg_paths[2].file_name().unwrap().to_str().unwrap(), "sequence-2.svg"); } -/// Test rendering multiple different sequences +/// Test batch rendering with empty list #[test] -#[ignore] -fn test_render_multiple_different_sequences() { - let sequences = vec![ - Sequence::new(vec![Value::origin()]), - Sequence::new(vec![Value::new(1, 0)]), - Sequence::new(vec![Value::new(-1, 0)]), - ]; - - let temp_dir = create_temp_dir().unwrap(); - write_support_files(temp_dir.path()).unwrap(); - +fn test_render_batch_empty() { + let ly_files: Vec = vec![]; let output_dir = TempDir::new().unwrap(); - for (i, seq) in sequences.iter().enumerate() { - let ly_path = temp_dir.path().join(format!("seq-{}.ly", i)); - generate_ly_file(seq, &ly_path).unwrap(); - - let svg_path = render_ly_to_svg( - &ly_path, - output_dir.path(), - Path::new("lilypond"), - ).unwrap(); - - assert!(svg_path.exists()); - assert_eq!(svg_path.file_name().unwrap().to_str().unwrap(), format!("seq-{}.svg", i)); - } - - // Verify all 3 SVGs were created - let entries: Vec<_> = std::fs::read_dir(output_dir.path()) - .unwrap() - .filter_map(|e| e.ok()) - .collect(); - assert_eq!(entries.len(), 3, "should have 3 SVG files"); -} - -/// Test that SVG content is valid and contains expected elements -#[test] -#[ignore] -fn test_svg_content_validity() { - let seq = Sequence::new(vec![Value::origin()]); - - let temp_dir = create_temp_dir().unwrap(); - write_support_files(temp_dir.path()).unwrap(); - - let ly_path = temp_dir.path().join("test.ly"); - generate_ly_file(&seq, &ly_path).unwrap(); - - let output_dir = TempDir::new().unwrap(); - let svg_path = render_ly_to_svg( - &ly_path, + let svg_paths = render_all_ly_files_batch( + &ly_files, output_dir.path(), Path::new("lilypond"), ).unwrap(); - let svg_content = std::fs::read_to_string(&svg_path).unwrap(); - - // Verify SVG structure - assert!(svg_content.contains(""), "should contain closing tag"); - assert!(svg_content.contains("xmlns"), "should contain XML namespace"); - - // LilyPond SVGs typically contain these elements - assert!(svg_content.contains(" group elements"); + assert_eq!(svg_paths.len(), 0); } -/// Test rendering with relative path for lilypond binary +/// Test batch rendering creates output directory #[test] #[ignore] -fn test_render_with_relative_lilypond_path() { +fn test_render_batch_creates_output_dir() { let seq = Sequence::new(vec![Value::origin()]); - let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); let ly_path = temp_dir.path().join("test.ly"); generate_ly_file(&seq, &ly_path).unwrap(); - let output_dir = TempDir::new().unwrap(); + let output_base = TempDir::new().unwrap(); + let output_dir = output_base.path().join("nested").join("output"); + assert!(!output_dir.exists()); - // Use just "lilypond" as the binary name (will be found in PATH) - let svg_path = render_ly_to_svg( - &ly_path, - output_dir.path(), + let svg_paths = render_all_ly_files_batch( + &[ly_path], + &output_dir, Path::new("lilypond"), ).unwrap(); - assert!(svg_path.exists()); + assert!(output_dir.exists()); + assert_eq!(svg_paths.len(), 1); } -/// Test that the function returns an error if LilyPond fails +/// Test with invalid .ly file #[test] #[ignore] -fn test_render_error_on_invalid_ly_file() { +fn test_render_batch_invalid_ly_file() { use std::fs; let temp_dir = create_temp_dir().unwrap(); - - // Create an invalid .ly file (missing required structure) let ly_path = temp_dir.path().join("invalid.ly"); fs::write(&ly_path, "this is not valid lilypond syntax!!!").unwrap(); let output_dir = TempDir::new().unwrap(); - - let result = render_ly_to_svg( - &ly_path, + let result = render_all_ly_files_batch( + &[ly_path], output_dir.path(), Path::new("lilypond"), ); - // Should return an error - assert!(result.is_err(), "should fail on invalid .ly file"); - + assert!(result.is_err()); match result { Err(RenderError::LilyPondExecution(msg)) => { - assert!(!msg.is_empty(), "error message should not be empty"); + assert!(!msg.is_empty()); } _ => panic!("expected LilyPondExecution error"), } } -// Tests for render_ly_files_to_svg (batch SVG generation) - -/// Test rendering multiple .ly files to SVGs in batch +/// Test batch rendering with many files #[test] #[ignore] -fn test_render_multiple_svgs_batch() { - // Create multiple sequences - let sequences = vec![ - Sequence::new(vec![Value::origin()]), - Sequence::new(vec![Value::new(1, 0)]), - Sequence::new(vec![Value::new(-1, 0)]), - ]; +fn test_render_batch_many_files() { + let sequences: Vec = (0..20) + .map(|i| Sequence::new(vec![Value::new(i, 0)])) + .collect(); let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); - - // Generate .ly files let ly_files = generate_ly_files(&sequences, temp_dir.path()).unwrap(); - // Render all to SVGs let output_dir = TempDir::new().unwrap(); - let svg_paths = render_ly_files_to_svg( + let svg_paths = render_all_ly_files_batch( &ly_files, output_dir.path(), Path::new("lilypond"), - None, ).unwrap(); - // Verify all SVGs were created - assert_eq!(svg_paths.len(), 3, "should create 3 SVG files"); - + assert_eq!(svg_paths.len(), 20); for svg_path in &svg_paths { - assert!(svg_path.exists(), "each SVG should exist"); - - // Verify SVG has content + assert!(svg_path.exists()); let content = std::fs::read_to_string(svg_path).unwrap(); - assert!(content.contains(" = vec![]; - - let output_dir = TempDir::new().unwrap(); - let svg_paths = render_ly_files_to_svg( - &ly_files, - output_dir.path(), - Path::new("lilypond"), - None, - ).unwrap(); - - assert_eq!(svg_paths.len(), 0, "should return empty vector for empty input"); -} - -/// Test batch rendering with single file -#[test] -#[ignore] -fn test_batch_render_single_file() { - let seq = Sequence::new(vec![Value::origin()]); +fn test_render_example_sequence() { + let seq = Sequence::new(vec![ + Value::new(1, 0), + Value::new(-1, 1), + Value::new(-1, 0), + Value::new(0, 0), + ]); let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); - let ly_path = temp_dir.path().join("single.ly"); + let ly_path = temp_dir.path().join("example.ly"); generate_ly_file(&seq, &ly_path).unwrap(); - let ly_files = vec![ly_path]; - let output_dir = TempDir::new().unwrap(); - let svg_paths = render_ly_files_to_svg( - &ly_files, + let svg_paths = render_all_ly_files_batch( + &[ly_path], output_dir.path(), Path::new("lilypond"), - None, ).unwrap(); assert_eq!(svg_paths.len(), 1); assert!(svg_paths[0].exists()); - assert_eq!(svg_paths[0].file_name().unwrap().to_str().unwrap(), "single.svg"); -} - -/// Test that batch rendering stops on first error -#[test] -#[ignore] -fn test_batch_render_stops_on_error() { - use std::fs; - - let temp_dir = create_temp_dir().unwrap(); - // Create one valid and one invalid .ly file - let seq = Sequence::new(vec![Value::origin()]); - write_support_files(temp_dir.path()).unwrap(); - - let valid_ly = temp_dir.path().join("valid.ly"); - generate_ly_file(&seq, &valid_ly).unwrap(); - - let invalid_ly = temp_dir.path().join("invalid.ly"); - fs::write(&invalid_ly, "invalid lilypond syntax!!!").unwrap(); - - let ly_files = vec![valid_ly, invalid_ly]; - - let output_dir = TempDir::new().unwrap(); - let result = render_ly_files_to_svg( - &ly_files, - output_dir.path(), - Path::new("lilypond"), - None, - ); - - // Should fail due to invalid file - assert!(result.is_err(), "should fail on invalid file"); - - match result { - Err(RenderError::LilyPondExecution(_)) => { - // Expected error type - } - _ => panic!("expected LilyPondExecution error"), - } + let svg_content = std::fs::read_to_string(&svg_paths[0]).unwrap(); + assert!(svg_content.contains(" 1000, "SVG should have substantial content"); } -/// Test batch rendering with large number of files +/// Test that SVG content is valid #[test] #[ignore] -fn test_batch_render_many_files() { - // Create 10 sequences - let sequences: Vec = (0..10) - .map(|i| Sequence::new(vec![Value::new(i, 0)])) - .collect(); +fn test_svg_content_validity() { + let seq = Sequence::new(vec![Value::origin()]); let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); - let ly_files = generate_ly_files(&sequences, temp_dir.path()).unwrap(); + let ly_path = temp_dir.path().join("test.ly"); + generate_ly_file(&seq, &ly_path).unwrap(); let output_dir = TempDir::new().unwrap(); - let svg_paths = render_ly_files_to_svg( - &ly_files, + let svg_paths = render_all_ly_files_batch( + &[ly_path], output_dir.path(), Path::new("lilypond"), - None, ).unwrap(); - // Verify all 10 SVGs were created - assert_eq!(svg_paths.len(), 10, "should create 10 SVG files"); + let svg_content = std::fs::read_to_string(&svg_paths[0]).unwrap(); - for (i, svg_path) in svg_paths.iter().enumerate() { - assert!(svg_path.exists()); - // With 10 files, we need 2-digit padding: 00, 01, 02, ..., 09 - assert_eq!( - svg_path.file_name().unwrap().to_str().unwrap(), - format!("sequence-{:02}.svg", i) - ); - } - - // Verify all files have content - for svg_path in &svg_paths { - let content = std::fs::read_to_string(svg_path).unwrap(); - assert!(content.contains(" 100, "SVG should have substantial content"); - } + assert!(svg_content.contains(""), "should contain closing tag"); + assert!(svg_content.contains("xmlns"), "should contain XML namespace"); + assert!(svg_content.contains(" group elements"); } /// Integration test: Complete pipeline from sequences to SVGs #[test] #[ignore] fn test_complete_pipeline_batch_rendering() { - // Create test sequences let sequences = vec![ Sequence::new(vec![ Value::new(1, 0), @@ -509,44 +272,33 @@ fn test_complete_pipeline_batch_rendering() { ]), ]; - // Create temp directory and write support files let temp_dir = create_temp_dir().unwrap(); write_support_files(temp_dir.path()).unwrap(); - // Generate .ly files let ly_files = generate_ly_files(&sequences, temp_dir.path()).unwrap(); assert_eq!(ly_files.len(), 3); - // Render all to SVGs let output_dir = TempDir::new().unwrap(); - let svg_paths = render_ly_files_to_svg( + let svg_paths = render_all_ly_files_batch( &ly_files, output_dir.path(), Path::new("lilypond"), - None, ).unwrap(); - // Verify complete pipeline assert_eq!(svg_paths.len(), 3); - // Verify each SVG for (i, svg_path) in svg_paths.iter().enumerate() { - // File exists assert!(svg_path.exists(), "SVG {} should exist", i); - - // Correct filename assert_eq!( svg_path.file_name().unwrap().to_str().unwrap(), format!("sequence-{}.svg", i) ); - // Has valid SVG content let content = std::fs::read_to_string(svg_path).unwrap(); assert!(content.contains(""), "SVG {} should have closing tag", i); } - // Verify all files are in output directory let entries: Vec<_> = std::fs::read_dir(output_dir.path()) .unwrap() .filter_map(|e| e.ok()) -- cgit v1.2.3