diff options
Diffstat (limited to 'morphology-img/tests/file_gen.rs')
| -rw-r--r-- | morphology-img/tests/file_gen.rs | 533 |
1 files changed, 533 insertions, 0 deletions
diff --git a/morphology-img/tests/file_gen.rs b/morphology-img/tests/file_gen.rs new file mode 100644 index 0000000..e459688 --- /dev/null +++ b/morphology-img/tests/file_gen.rs @@ -0,0 +1,533 @@ +use morphology_img::file_gen::{generate_filename, create_temp_dir, write_support_files, generate_ly_file, generate_ly_files}; +use morphology::{Sequence, Value}; +use std::fs; +use std::path::PathBuf; + +// Tests for generate_filename + +#[test] +fn test_generate_filename_single_digit_padding() { + // With 10 sequences (1-9, plus 0), we need 2-digit padding + let filename = generate_filename(0, 10); + assert_eq!(filename.to_str().unwrap(), "sequence-00.ly"); + + let filename = generate_filename(5, 10); + assert_eq!(filename.to_str().unwrap(), "sequence-05.ly"); + + let filename = generate_filename(9, 10); + assert_eq!(filename.to_str().unwrap(), "sequence-09.ly"); +} + +#[test] +fn test_generate_filename_double_digit_padding() { + // With 100 sequences, we need 3-digit padding + let filename = generate_filename(0, 100); + assert_eq!(filename.to_str().unwrap(), "sequence-000.ly"); + + let filename = generate_filename(42, 100); + assert_eq!(filename.to_str().unwrap(), "sequence-042.ly"); + + let filename = generate_filename(99, 100); + assert_eq!(filename.to_str().unwrap(), "sequence-099.ly"); +} + +#[test] +fn test_generate_filename_triple_digit_padding() { + // With 1000 sequences, we need 4-digit padding + let filename = generate_filename(0, 1000); + assert_eq!(filename.to_str().unwrap(), "sequence-0000.ly"); + + let filename = generate_filename(42, 1000); + assert_eq!(filename.to_str().unwrap(), "sequence-0042.ly"); + + let filename = generate_filename(123, 1000); + assert_eq!(filename.to_str().unwrap(), "sequence-0123.ly"); + + let filename = generate_filename(999, 1000); + assert_eq!(filename.to_str().unwrap(), "sequence-0999.ly"); +} + +#[test] +fn test_generate_filename_single_sequence() { + // With just 1 sequence, we only need 1-digit padding + let filename = generate_filename(0, 1); + assert_eq!(filename.to_str().unwrap(), "sequence-0.ly"); +} + +#[test] +fn test_generate_filename_ten_sequences_boundary() { + // At exactly 10, we need 2 digits + let filename = generate_filename(0, 10); + assert_eq!(filename.to_str().unwrap(), "sequence-00.ly"); +} + +#[test] +fn test_generate_filename_lexicographic_ordering() { + // Verify that filenames sort correctly + let names: Vec<PathBuf> = (0..15).map(|i| generate_filename(i, 15)).collect(); + + let mut sorted_names = names.clone(); + sorted_names.sort(); + + // If padding is correct, the sorted order should be the same as generated order + assert_eq!(names, sorted_names); +} + +#[test] +fn test_generate_filename_has_ly_extension() { + let filename = generate_filename(5, 10); + assert!(filename.to_str().unwrap().ends_with(".ly")); +} + +#[test] +fn test_generate_filename_different_counts_same_index() { + // Same index but different total counts should have different padding + let name_10 = generate_filename(5, 10); + let name_100 = generate_filename(5, 100); + let name_1000 = generate_filename(5, 1000); + + assert_eq!(name_10.to_str().unwrap(), "sequence-05.ly"); + assert_eq!(name_100.to_str().unwrap(), "sequence-005.ly"); + assert_eq!(name_1000.to_str().unwrap(), "sequence-0005.ly"); +} + +// Tests for create_temp_dir + +#[test] +fn test_create_temp_dir_success() { + // Test that we can create a temp directory + let temp_dir = create_temp_dir(); + assert!(temp_dir.is_ok(), "should successfully create temp directory"); + + let dir = temp_dir.unwrap(); + let path = dir.path(); + + // Verify the directory exists + assert!(path.exists(), "temp directory should exist"); + assert!(path.is_dir(), "temp path should be a directory"); + + // TempDir will be dropped here and automatically cleaned up +} + +#[test] +fn test_create_temp_dir_is_empty() { + // Test that the newly created temp directory is empty + let temp_dir = create_temp_dir().unwrap(); + let entries = fs::read_dir(temp_dir.path()).unwrap(); + + assert_eq!(entries.count(), 0, "new temp directory should be empty"); +} + +#[test] +fn test_create_temp_dir_automatic_cleanup() { + let path; + { + let temp_dir = create_temp_dir().unwrap(); + path = temp_dir.path().to_path_buf(); + assert!(path.exists(), "temp directory should exist while TempDir is in scope"); + } // TempDir dropped here + + // After TempDir is dropped, directory should be cleaned up + assert!(!path.exists(), "temp directory should be cleaned up after drop"); +} + +#[test] +fn test_create_temp_dir_multiple_independent() { + // Test that we can create multiple independent temp directories + let dir1 = create_temp_dir().unwrap(); + let dir2 = create_temp_dir().unwrap(); + + assert_ne!(dir1.path(), dir2.path(), "temp directories should be different"); + assert!(dir1.path().exists()); + assert!(dir2.path().exists()); +} + +// Tests for write_support_files + +#[test] +fn test_write_support_files_success() { + let temp_dir = create_temp_dir().unwrap(); + + // Write support files + let result = write_support_files(temp_dir.path()); + assert!(result.is_ok(), "should successfully write support files"); + + // Verify all three files exist + let format_path = temp_dir.path().join("format.ily"); + let entry_path = temp_dir.path().join("entry.ily"); + let elements_path = temp_dir.path().join("elements.ily"); + + assert!(format_path.exists(), "format.ily should exist"); + assert!(entry_path.exists(), "entry.ily should exist"); + assert!(elements_path.exists(), "elements.ily should exist"); +} + +#[test] +fn test_write_support_files_content_correctness() { + let temp_dir = create_temp_dir().unwrap(); + + write_support_files(temp_dir.path()).unwrap(); + + // Read back the files and verify they have the correct content + let format_content = fs::read_to_string(temp_dir.path().join("format.ily")).unwrap(); + let entry_content = fs::read_to_string(temp_dir.path().join("entry.ily")).unwrap(); + let elements_content = fs::read_to_string(temp_dir.path().join("elements.ily")).unwrap(); + + // Verify content is non-empty + assert!(!format_content.is_empty(), "format.ily should have content"); + assert!(!entry_content.is_empty(), "entry.ily should have content"); + assert!(!elements_content.is_empty(), "elements.ily should have content"); + + // Verify content matches the embedded templates + use morphology_img::template::{get_format_ily, get_entry_ily, get_elements_ily}; + + assert_eq!(format_content, get_format_ily(), "format.ily content should match template"); + assert_eq!(entry_content, get_entry_ily(), "entry.ily content should match template"); + assert_eq!(elements_content, get_elements_ily(), "elements.ily content should match template"); +} + +#[test] +fn test_write_support_files_are_readable() { + let temp_dir = create_temp_dir().unwrap(); + write_support_files(temp_dir.path()).unwrap(); + + // Verify files are readable + let format_path = temp_dir.path().join("format.ily"); + let entry_path = temp_dir.path().join("entry.ily"); + let elements_path = temp_dir.path().join("elements.ily"); + + assert!(fs::read_to_string(&format_path).is_ok(), "format.ily should be readable"); + assert!(fs::read_to_string(&entry_path).is_ok(), "entry.ily should be readable"); + assert!(fs::read_to_string(&elements_path).is_ok(), "elements.ily should be readable"); +} + +#[test] +fn test_write_support_files_count() { + let temp_dir = create_temp_dir().unwrap(); + write_support_files(temp_dir.path()).unwrap(); + + // Count files in directory + let entries: Vec<_> = fs::read_dir(temp_dir.path()) + .unwrap() + .filter_map(|e| e.ok()) + .collect(); + + assert_eq!(entries.len(), 3, "should have exactly 3 support files"); +} + +#[test] +fn test_write_support_files_overwrite() { + let temp_dir = create_temp_dir().unwrap(); + + // Write support files first time + write_support_files(temp_dir.path()).unwrap(); + + // Write again (should overwrite) + let result = write_support_files(temp_dir.path()); + assert!(result.is_ok(), "should be able to overwrite existing files"); + + // Verify files still exist and have correct content + let format_content = fs::read_to_string(temp_dir.path().join("format.ily")).unwrap(); + assert!(!format_content.is_empty()); +} + +// Integration test + +#[test] +fn test_complete_file_generation_workflow() { + // Test the complete workflow of creating temp dir and writing support files + let temp_dir = create_temp_dir().unwrap(); + let temp_path = temp_dir.path(); + + // Directory should be empty initially + assert_eq!(fs::read_dir(temp_path).unwrap().count(), 0); + + // Write support files + write_support_files(temp_path).unwrap(); + + // Directory should now have 3 files + assert_eq!(fs::read_dir(temp_path).unwrap().count(), 3); + + // Generate some filenames + let filenames: Vec<_> = (0..5).map(|i| generate_filename(i, 5)).collect(); + + // Verify filenames are correctly padded + assert_eq!(filenames[0].to_str().unwrap(), "sequence-0.ly"); + assert_eq!(filenames[4].to_str().unwrap(), "sequence-4.ly"); + + // Simulate writing .ly files + for filename in &filenames { + let path = temp_path.join(filename); + fs::write(&path, "test content").unwrap(); + } + + // Now we should have 3 .ily files + 5 .ly files = 8 files + assert_eq!(fs::read_dir(temp_path).unwrap().count(), 8); +} + +// Tests for generate_ly_file + +#[test] +fn test_generate_ly_file_simple_sequence() { + // Create a simple test sequence + let seq = Sequence::new(vec![Value::origin()]); + + let temp_dir = create_temp_dir().unwrap(); + let output_path = temp_dir.path().join("test.ly"); + + // Generate .ly file + let result = generate_ly_file(&seq, &output_path); + assert!(result.is_ok(), "should successfully generate .ly file"); + + // Verify file exists + assert!(output_path.exists(), "generated file should exist"); + + // Verify file is readable + let content = fs::read_to_string(&output_path).unwrap(); + assert!(!content.is_empty(), "file should not be empty"); +} + +#[test] +fn test_generate_ly_file_content_structure() { + // Create the example sequence from the preliminary overview + 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(); + let output_path = temp_dir.path().join("test.ly"); + + generate_ly_file(&seq, &output_path).unwrap(); + + // Read the file and verify its content + let content = fs::read_to_string(&output_path).unwrap(); + + // Verify it contains the template structure + assert!(content.contains(r#"\version "2.24.0""#), "should have version declaration"); + assert!(content.contains(r#"\score"#), "should have score block"); + assert!(content.contains(r#"\entry"#), "should have entry block"); + assert!(content.contains(r#"\include "format.ily""#), "should include format.ily"); + assert!(content.contains(r#"\include "entry.ily""#), "should include entry.ily"); + assert!(content.contains(r#"\include "elements.ily""#), "should include elements.ily"); + + // Verify it contains the expected markup + assert!(content.contains("f'4 f4 e4 b4"), "should contain the generated markup"); + + // Verify the placeholder is gone + assert!(!content.contains("%%%"), "should not contain placeholder"); +} + +#[test] +fn test_generate_ly_file_is_valid_utf8() { + let seq = Sequence::new(vec![Value::new(1, 0)]); + + let temp_dir = create_temp_dir().unwrap(); + let output_path = temp_dir.path().join("test.ly"); + + generate_ly_file(&seq, &output_path).unwrap(); + + // Verify file is valid UTF-8 + let result = fs::read_to_string(&output_path); + assert!(result.is_ok(), "file should be valid UTF-8"); +} + +#[test] +fn test_generate_ly_file_overwrite() { + let seq1 = Sequence::new(vec![Value::origin()]); + let seq2 = Sequence::new(vec![Value::new(1, 0)]); + + let temp_dir = create_temp_dir().unwrap(); + let output_path = temp_dir.path().join("test.ly"); + + // Generate first file + generate_ly_file(&seq1, &output_path).unwrap(); + let content1 = fs::read_to_string(&output_path).unwrap(); + + // Generate second file (overwrite) + generate_ly_file(&seq2, &output_path).unwrap(); + let content2 = fs::read_to_string(&output_path).unwrap(); + + // Contents should be different + assert_ne!(content1, content2, "overwrite should change file content"); +} + +// Tests for generate_ly_files (batch) + +#[test] +fn test_generate_ly_files_multiple_sequences() { + // Create multiple test 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(); + + // Generate .ly files + let result = generate_ly_files(&sequences, temp_dir.path()); + assert!(result.is_ok(), "should successfully generate all files"); + + let paths = result.unwrap(); + + // Verify we got the correct number of paths + assert_eq!(paths.len(), 3, "should return 3 paths"); + + // Verify all files exist + for path in &paths { + assert!(path.exists(), "each generated file should exist"); + } +} + +#[test] +fn test_generate_ly_files_correct_filenames() { + 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(); + let paths = generate_ly_files(&sequences, temp_dir.path()).unwrap(); + + // Verify filenames are correctly generated + assert!(paths[0].ends_with("sequence-0.ly")); + assert!(paths[1].ends_with("sequence-1.ly")); + assert!(paths[2].ends_with("sequence-2.ly")); +} + +#[test] +fn test_generate_ly_files_each_has_unique_content() { + let sequences = vec![ + Sequence::new(vec![Value::origin()]), + Sequence::new(vec![Value::new(1, 0)]), + ]; + + let temp_dir = create_temp_dir().unwrap(); + let paths = generate_ly_files(&sequences, temp_dir.path()).unwrap(); + + // Read content of both files + let content1 = fs::read_to_string(&paths[0]).unwrap(); + let content2 = fs::read_to_string(&paths[1]).unwrap(); + + // Contents should be different + assert_ne!(content1, content2, "each file should have unique content based on its sequence"); +} + +#[test] +fn test_generate_ly_files_empty_slice() { + let sequences: Vec<Sequence> = vec![]; + + let temp_dir = create_temp_dir().unwrap(); + let result = generate_ly_files(&sequences, temp_dir.path()); + + assert!(result.is_ok(), "should handle empty slice"); + assert_eq!(result.unwrap().len(), 0, "should return empty vector"); +} + +#[test] +fn test_generate_ly_files_single_sequence() { + let sequences = vec![Sequence::new(vec![Value::origin()])]; + + let temp_dir = create_temp_dir().unwrap(); + let paths = generate_ly_files(&sequences, temp_dir.path()).unwrap(); + + assert_eq!(paths.len(), 1); + assert!(paths[0].exists()); + assert!(paths[0].ends_with("sequence-0.ly")); +} + +#[test] +fn test_generate_ly_files_preserves_order() { + let sequences = vec![ + Sequence::new(vec![Value::new(5, 0)]), + Sequence::new(vec![Value::new(3, 0)]), + Sequence::new(vec![Value::new(1, 0)]), + ]; + + let temp_dir = create_temp_dir().unwrap(); + let paths = generate_ly_files(&sequences, temp_dir.path()).unwrap(); + + // Read the markup from each file + for (i, path) in paths.iter().enumerate() { + let content = fs::read_to_string(path).unwrap(); + + // Verify the content corresponds to the right sequence + // Sequence with Value::new(outer, 0) and length 1 gives pitch = outer * 1 + 0 = outer + // Then integer_to_pitch_name: 5→g', 3→e', 1→c' + // (5+6=11, 11%7=4→"g", 11/7=1→') (3+6=9, 9%7=2→"e", 9/7=1→') (1+6=7, 7%7=0→"c", 7/7=1→') + match i { + 0 => assert!(content.contains("g'4"), "first file should have sequence 0 content"), + 1 => assert!(content.contains("e'4"), "second file should have sequence 1 content"), + 2 => assert!(content.contains("c'4"), "third file should have sequence 2 content"), + _ => panic!("unexpected index"), + } + } +} + +// Integration test: Complete pipeline + +#[test] +fn test_complete_ly_generation_pipeline() { + // Test the complete workflow from sequence to .ly file + + // Create test sequences + let sequences = vec![ + Sequence::new(vec![ + Value::new(1, 0), + Value::new(-1, 1), + Value::new(-1, 0), + Value::new(0, 0), + ]), + Sequence::new(vec![ + Value::origin(), + Value::new(1, 0), + ]), + ]; + + // Create temp directory + let temp_dir = create_temp_dir().unwrap(); + + // Write support files + write_support_files(temp_dir.path()).unwrap(); + + // Generate .ly files + let ly_paths = generate_ly_files(&sequences, temp_dir.path()).unwrap(); + + // Verify we have 2 .ly files + assert_eq!(ly_paths.len(), 2); + + // Verify directory now has 5 files total (3 .ily + 2 .ly) + assert_eq!(fs::read_dir(temp_dir.path()).unwrap().count(), 5); + + // Verify each .ly file has correct structure + for path in &ly_paths { + let content = fs::read_to_string(path).unwrap(); + + // All files should have the template structure + assert!(content.contains(r#"\version"#)); + assert!(content.contains(r#"\score"#)); + assert!(content.contains(r#"\entry"#)); + + // All files should reference the .ily files + assert!(content.contains(r#"\include "format.ily""#)); + assert!(content.contains(r#"\include "entry.ily""#)); + assert!(content.contains(r#"\include "elements.ily""#)); + + // Should not contain placeholder + assert!(!content.contains("%%%")); + } + + // Verify specific content for first file + let content0 = fs::read_to_string(&ly_paths[0]).unwrap(); + assert!(content0.contains("f'4 f4 e4 b4"), "first file should have correct markup"); + + // Verify specific content for second file + // Sequence has length 2: [(0,0), (1,0)] + // Pitches: 0*2+0=0→b4, 1*2+0=2→d'4 + let content1 = fs::read_to_string(&ly_paths[1]).unwrap(); + assert!(content1.contains("b4 d'4"), "second file should have correct markup"); +} |