summaryrefslogtreecommitdiff
path: root/morphology-img/tests/template.rs
blob: 60ef20ef85579092681ae5229133dd93ebef0ddc (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
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_placeholder() {
    // Verify the template contains the placeholder
    let template = get_template();
    assert!(template.contains("%%%"), "template should contain '%%%' placeholder");
}

#[test]
fn test_successful_substitution() {
    // Test successful substitution with simple markup
    let markup = "c'4 d'4 e'4";
    let result = substitute_template(markup);

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

    let substituted = result.unwrap();

    // Verify the markup is present in the result
    assert!(substituted.contains(markup), "result should contain the markup");

    // Verify the placeholder is no longer present
    assert!(!substituted.contains("%%%"), "result should not contain 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 markup string
    let markup = "";
    let result = substitute_template(markup);

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

    let substituted = result.unwrap();

    // Placeholder should be replaced with empty string
    assert!(!substituted.contains("%%%"));
}

#[test]
fn test_error_when_placeholder_missing() {
    // This test simulates what would happen if the placeholder was missing
    // We'll test this by creating a mock template without the placeholder

    // Note: We can't easily modify the embedded template at runtime,
    // so we'll just verify the error handling works as designed
    // by checking that our current template DOES have the placeholder
    let template = get_template();

    if !template.contains("%%%") {
        // If this branch were taken, substitute_template would return an error
        let result = substitute_template("test");
        assert!(result.is_err());

        if let Err(RenderError::Template(msg)) = result {
            assert!(msg.contains("placeholder"));
        } else {
            panic!("Expected RenderError::Template");
        }
    } else {
        // Current template has placeholder, which is correct
        assert!(template.contains("%%%"));
    }
}

#[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 markup = "c'4 d'4";
    let result = substitute_template(markup).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 markup1 = "c'4 d'4";
    let markup2 = "e'4 f'4";

    let result1 = substitute_template(markup1).unwrap();
    let result2 = substitute_template(markup2).unwrap();

    assert!(result1.contains(markup1));
    assert!(!result1.contains(markup2));

    assert!(result2.contains(markup2));
    assert!(!result2.contains(markup1));
}

#[test]
fn test_substitution_with_actual_sequence_markup() {
    // Test with markup that would come from the actual transformation pipeline
    // This is the example from the development plan
    let markup = "f'4 f4 e4 b4";

    let result = substitute_template(markup);
    assert!(result.is_ok());

    let substituted = result.unwrap();

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

    // The substituted markup should be between the entry braces
    let entry_start = substituted.find(r#"\entry { "#).unwrap();
    let markup_pos = substituted.find(markup).unwrap();
    assert!(markup_pos > entry_start, "markup should be inside entry block");
}