summaryrefslogtreecommitdiff
path: root/morphology-img/tests/template.rs
blob: b53689c95fd1ff00a708ab592613a9e3adb48516 (plain)
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
use morphology_img::template::{
    get_template, get_format_ily, get_entry_ily, get_elements_ily, substitute_template
};
use morphology_img::error::RenderError;

#[test]
fn test_embedded_templates_non_empty() {
    // Test that all embedded templates are non-empty
    assert!(!get_template().is_empty(), "template.ly should not be empty");
    assert!(!get_format_ily().is_empty(), "format.ily should not be empty");
    assert!(!get_entry_ily().is_empty(), "entry.ily should not be empty");
    assert!(!get_elements_ily().is_empty(), "elements.ily should not be empty");
}

#[test]
fn test_template_contains_placeholders() {
    // Verify the template contains all three placeholders
    let template = get_template();
    assert!(template.contains("%MORPHEME%"), "template should contain '%MORPHEME%' placeholder");
    assert!(template.contains("%RUNGS%"), "template should contain '%RUNGS%' placeholder");
    assert!(template.contains("%LEDGERS%"), "template should contain '%LEDGERS%' placeholder");
}

#[test]
fn test_successful_substitution() {
    // Test successful substitution with simple markup
    let morpheme = "c'4 d'4 e'4";
    let rungs = "#'(-5 0 5)";
    let ledgers = "#'(-3 3)";
    let result = substitute_template(morpheme, rungs, ledgers);

    assert!(result.is_ok(), "substitution should succeed");

    let substituted = result.unwrap();

    // Verify all three values are present in the result
    assert!(substituted.contains(morpheme), "result should contain the morpheme");
    assert!(substituted.contains(rungs), "result should contain the rungs");
    assert!(substituted.contains(ledgers), "result should contain the ledgers");

    // Verify the placeholders are no longer present
    assert!(!substituted.contains("%MORPHEME%"), "result should not contain morpheme placeholder");
    assert!(!substituted.contains("%RUNGS%"), "result should not contain rungs placeholder");
    assert!(!substituted.contains("%LEDGERS%"), "result should not contain ledgers placeholder");

    // Verify the template structure is preserved
    assert!(substituted.contains(r#"\version "2.24.0""#), "version should be preserved");
    assert!(substituted.contains(r#"\score"#), "score block should be preserved");
    assert!(substituted.contains(r#"\entry"#), "entry should be preserved");
}

#[test]
fn test_substitution_with_empty_markup() {
    // Test with empty morpheme string
    let morpheme = "";
    let rungs = "#'(0)";
    let ledgers = "#'(0)";
    let result = substitute_template(morpheme, rungs, ledgers);

    assert!(result.is_ok(), "substitution with empty morpheme should succeed");

    let substituted = result.unwrap();

    // Placeholders should be replaced
    assert!(!substituted.contains("%MORPHEME%"));
    assert!(!substituted.contains("%RUNGS%"));
    assert!(!substituted.contains("%LEDGERS%"));
    assert!(substituted.contains(rungs));
    assert!(substituted.contains(ledgers));
}

#[test]
fn test_error_when_placeholder_missing() {
    // Verify error handling works by checking that our current template
    // has all required placeholders
    let template = get_template();

    // Current template should have all three placeholders
    assert!(template.contains("%MORPHEME%"), "template should have morpheme placeholder");
    assert!(template.contains("%RUNGS%"), "template should have rungs placeholder");
    assert!(template.contains("%LEDGERS%"), "template should have ledgers placeholder");
}

#[test]
fn test_template_structure() {
    // Verify the template has the expected structure
    let template = get_template();

    assert!(template.contains(r#"\version"#), "should have version declaration");
    assert!(template.contains(r#"\include "format.ily""#), "should include format.ily");
    assert!(template.contains(r#"\include "elements.ily""#), "should include elements.ily");
    assert!(template.contains(r#"\include "entry.ily""#), "should include entry.ily");
    assert!(template.contains(r#"\score"#), "should have score block");
    assert!(template.contains(r#"\entry"#), "should have entry block");
}

#[test]
fn test_substitution_preserves_whitespace() {
    // Test that substitution preserves indentation and whitespace
    let morpheme = "c'4 d'4";
    let rungs = "#'(0)";
    let ledgers = "#'(0)";
    let result = substitute_template(morpheme, rungs, ledgers).unwrap();

    // The indented structure should be preserved
    // The markup should be indented within the entry block
    assert!(result.contains("  \\entry"));
}

#[test]
fn test_multiple_substitutions_independent() {
    // Test that multiple calls to substitute_template are independent
    let morpheme1 = "c'4 d'4";
    let morpheme2 = "e'4 f'4";
    let rungs1 = "#'(-3 0 3)";
    let rungs2 = "#'(-5 0 5)";
    let ledgers1 = "#'(-3 3)";
    let ledgers2 = "#'(-5 5)";

    let result1 = substitute_template(morpheme1, rungs1, ledgers1).unwrap();
    let result2 = substitute_template(morpheme2, rungs2, ledgers2).unwrap();

    assert!(result1.contains(morpheme1));
    assert!(result1.contains(rungs1));
    assert!(result1.contains(ledgers1));
    assert!(!result1.contains(morpheme2));
    assert!(!result1.contains(rungs2));
    assert!(!result1.contains(ledgers2));

    assert!(result2.contains(morpheme2));
    assert!(result2.contains(rungs2));
    assert!(result2.contains(ledgers2));
    assert!(!result2.contains(morpheme1));
    assert!(!result2.contains(rungs1));
    assert!(!result2.contains(ledgers1));
}

#[test]
fn test_substitution_with_actual_sequence_markup() {
    // Test with markup that would come from the actual transformation pipeline
    let morpheme = "f'4 f4 e4 b4";
    let rungs = "#'(-4 0 4)";
    let ledgers = "#'(-4 0 4)";

    let result = substitute_template(morpheme, rungs, ledgers);
    assert!(result.is_ok());

    let substituted = result.unwrap();

    // Verify it creates valid LilyPond syntax structure
    assert!(substituted.contains(r#"\entry {"#));
    assert!(substituted.contains(morpheme));
    assert!(substituted.contains(rungs));
    assert!(substituted.contains(ledgers));
    assert!(substituted.contains("}"));

    // The substituted values should be in the correct positions
    let entry_start = substituted.find(r#"\entry {"#).unwrap();
    let morpheme_pos = substituted.find(morpheme).unwrap();
    assert!(morpheme_pos > entry_start, "morpheme should be inside entry block");
}