1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
|
use morphology_img::render::{check_lilypond_available, render_ly_to_svg, render_ly_files_to_svg};
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]
#[ignore]
fn test_lilypond_available() {
let result = check_lilypond_available();
assert!(result.is_ok(), "LilyPond should be available for this test");
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: {}",
version
);
}
/// 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]
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"),
"error message should mention LilyPond or PATH"
);
}
_ => 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]
#[ignore]
fn test_render_single_svg() {
// Create a simple sequence
let seq = Sequence::new(vec![Value::origin()]);
// Generate .ly file
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();
// Render to SVG
let output_dir = TempDir::new().unwrap();
let svg_path = render_ly_to_svg(
&ly_path,
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("<svg"), "file should contain SVG markup");
assert!(!svg_content.is_empty(), "SVG file should not be empty");
}
/// Test rendering with the example sequence
#[test]
#[ignore]
fn test_render_example_sequence() {
// Create the 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("example.ly");
generate_ly_file(&seq, &ly_path).unwrap();
let output_dir = TempDir::new().unwrap();
let svg_path = render_ly_to_svg(
&ly_path,
output_dir.path(),
Path::new("lilypond"),
).unwrap();
// Verify SVG was created
assert!(svg_path.exists());
// Verify it contains SVG content
let svg_content = std::fs::read_to_string(&svg_path).unwrap();
assert!(svg_content.contains("<svg"));
assert!(svg_content.len() > 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");
}
/// Test rendering multiple different sequences
#[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();
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,
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("<svg"), "should contain <svg tag");
assert!(svg_content.contains("</svg>"), "should contain closing </svg> tag");
assert!(svg_content.contains("xmlns"), "should contain XML namespace");
// LilyPond SVGs typically contain these elements
assert!(svg_content.contains("<g"), "should contain <g> group elements");
}
/// Test rendering with relative path for lilypond binary
#[test]
#[ignore]
fn test_render_with_relative_lilypond_path() {
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();
// Use just "lilypond" as the binary name (will be found in PATH)
let svg_path = render_ly_to_svg(
&ly_path,
output_dir.path(),
Path::new("lilypond"),
).unwrap();
assert!(svg_path.exists());
}
/// Test that the function returns an error if LilyPond fails
#[test]
#[ignore]
fn test_render_error_on_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,
output_dir.path(),
Path::new("lilypond"),
);
// Should return an error
assert!(result.is_err(), "should fail on invalid .ly file");
match result {
Err(RenderError::LilyPondExecution(msg)) => {
assert!(!msg.is_empty(), "error message should not be 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]
#[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)]),
];
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(
&ly_files,
output_dir.path(),
Path::new("lilypond"),
).unwrap();
// Verify all SVGs were created
assert_eq!(svg_paths.len(), 3, "should create 3 SVG files");
for svg_path in &svg_paths {
assert!(svg_path.exists(), "each SVG should exist");
// Verify SVG has content
let content = std::fs::read_to_string(svg_path).unwrap();
assert!(content.contains("<svg"), "should contain SVG markup");
}
}
/// Test that batch rendering preserves order
#[test]
#[ignore]
fn test_batch_render_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();
write_support_files(temp_dir.path()).unwrap();
let ly_files = generate_ly_files(&sequences, temp_dir.path()).unwrap();
let output_dir = TempDir::new().unwrap();
let svg_paths = render_ly_files_to_svg(
&ly_files,
output_dir.path(),
Path::new("lilypond"),
).unwrap();
// Verify filenames are in order
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");
// Verify all files exist
for path in &svg_paths {
assert!(path.exists());
}
}
/// Test batch rendering with empty list
#[test]
#[ignore]
fn test_batch_render_empty_list() {
let ly_files: Vec<std::path::PathBuf> = vec![];
let output_dir = TempDir::new().unwrap();
let svg_paths = render_ly_files_to_svg(
&ly_files,
output_dir.path(),
Path::new("lilypond"),
).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()]);
let temp_dir = create_temp_dir().unwrap();
write_support_files(temp_dir.path()).unwrap();
let ly_path = temp_dir.path().join("single.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,
output_dir.path(),
Path::new("lilypond"),
).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"),
);
// 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"),
}
}
/// Test batch rendering with large number of files
#[test]
#[ignore]
fn test_batch_render_many_files() {
// Create 10 sequences
let sequences: Vec<Sequence> = (0..10)
.map(|i| Sequence::new(vec![Value::new(i, 0)]))
.collect();
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 output_dir = TempDir::new().unwrap();
let svg_paths = render_ly_files_to_svg(
&ly_files,
output_dir.path(),
Path::new("lilypond"),
).unwrap();
// Verify all 10 SVGs were created
assert_eq!(svg_paths.len(), 10, "should create 10 SVG files");
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("<svg"));
assert!(content.len() > 100, "SVG should have substantial content");
}
}
/// 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),
Value::new(-1, 1),
Value::new(-1, 0),
Value::new(0, 0),
]),
Sequence::new(vec![
Value::origin(),
Value::new(1, 0),
]),
Sequence::new(vec![
Value::new(2, 0),
]),
];
// 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(
&ly_files,
output_dir.path(),
Path::new("lilypond"),
).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"), "SVG {} should have svg tag", i);
assert!(content.contains("</svg>"), "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())
.collect();
assert_eq!(entries.len(), 3, "output directory should have exactly 3 files");
}
|