Coverage Report

Created: 2025-09-05 15:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/home/noah/src/ruchy/src/backend/transpiler/patterns_tests.rs
Line
Count
Source
1
//! Comprehensive unit tests for patterns module
2
//! Target: Increase coverage from 33.33% to 80%+
3
4
#[cfg(test)]
5
mod tests {
6
    use super::super::*;
7
    use crate::frontend::ast::{Pattern, Literal, StructPatternField};
8
9
18
    fn create_transpiler() -> Transpiler {
10
18
        Transpiler::new()
11
18
    }
12
13
    #[test]
14
1
    fn test_wildcard_pattern() {
15
1
        let transpiler = create_transpiler();
16
1
        let pattern = Pattern::Wildcard;
17
        
18
1
        let result = transpiler.transpile_pattern(&pattern)
19
1
            .expect("Failed to transpile");
20
        
21
1
        let output = result.to_string();
22
1
        assert_eq!(output, "_");
23
1
    }
24
25
    #[test]
26
1
    fn test_identifier_pattern() {
27
1
        let transpiler = create_transpiler();
28
1
        let pattern = Pattern::Identifier("x".to_string());
29
        
30
1
        let result = transpiler.transpile_pattern(&pattern)
31
1
            .expect("Failed to transpile");
32
        
33
1
        let output = result.to_string();
34
1
        assert_eq!(output, "x");
35
1
    }
36
37
    #[test]
38
1
    fn test_literal_pattern_integer() {
39
1
        let transpiler = create_transpiler();
40
1
        let pattern = Pattern::Literal(Literal::Integer(42));
41
        
42
1
        let result = transpiler.transpile_pattern(&pattern)
43
1
            .expect("Failed to transpile");
44
        
45
1
        let output = result.to_string();
46
1
        assert!(output.contains("42"));
47
1
    }
48
49
    #[test]
50
1
    fn test_literal_pattern_string() {
51
1
        let transpiler = create_transpiler();
52
1
        let pattern = Pattern::Literal(Literal::String("hello".to_string()));
53
        
54
1
        let result = transpiler.transpile_pattern(&pattern)
55
1
            .expect("Failed to transpile");
56
        
57
1
        let output = result.to_string();
58
1
        assert!(output.contains("hello"));
59
1
    }
60
61
    #[test]
62
1
    fn test_literal_pattern_bool() {
63
1
        let transpiler = create_transpiler();
64
1
        let pattern = Pattern::Literal(Literal::Bool(true));
65
        
66
1
        let result = transpiler.transpile_pattern(&pattern)
67
1
            .expect("Failed to transpile");
68
        
69
1
        let output = result.to_string();
70
1
        assert_eq!(output, "true");
71
1
    }
72
73
    #[test]
74
1
    fn test_tuple_pattern_simple() {
75
1
        let transpiler = create_transpiler();
76
1
        let pattern = Pattern::Tuple(vec![
77
1
            Pattern::Identifier("a".to_string()),
78
1
            Pattern::Identifier("b".to_string()),
79
1
        ]);
80
        
81
1
        let result = transpiler.transpile_pattern(&pattern)
82
1
            .expect("Failed to transpile");
83
        
84
1
        let output = result.to_string();
85
1
        assert!(output.contains("("));
86
1
        assert!(output.contains("a"));
87
1
        assert!(output.contains("b"));
88
1
        assert!(output.contains(")"));
89
1
    }
90
91
    #[test]
92
1
    fn test_tuple_pattern_nested() {
93
1
        let transpiler = create_transpiler();
94
1
        let pattern = Pattern::Tuple(vec![
95
1
            Pattern::Tuple(vec![
96
1
                Pattern::Identifier("a".to_string()),
97
1
                Pattern::Identifier("b".to_string()),
98
1
            ]),
99
1
            Pattern::Identifier("c".to_string()),
100
1
        ]);
101
        
102
1
        let result = transpiler.transpile_pattern(&pattern)
103
1
            .expect("Failed to transpile");
104
        
105
1
        let output = result.to_string();
106
1
        assert!(output.contains("(("));
107
1
        assert!(output.contains("a"));
108
1
        assert!(output.contains("b"));
109
1
        assert!(output.contains("c"));
110
1
    }
111
112
    #[test]
113
1
    fn test_list_pattern_empty() {
114
1
        let transpiler = create_transpiler();
115
1
        let pattern = Pattern::List(vec![]);
116
        
117
1
        let result = transpiler.transpile_pattern(&pattern)
118
1
            .expect("Failed to transpile");
119
        
120
1
        let output = result.to_string();
121
1
        assert_eq!(output, "[]");
122
1
    }
123
124
    #[test]
125
1
    fn test_list_pattern_simple() {
126
1
        let transpiler = create_transpiler();
127
1
        let pattern = Pattern::List(vec![
128
1
            Pattern::Identifier("a".to_string()),
129
1
            Pattern::Identifier("b".to_string()),
130
1
            Pattern::Identifier("c".to_string()),
131
1
        ]);
132
        
133
1
        let result = transpiler.transpile_pattern(&pattern)
134
1
            .expect("Failed to transpile");
135
        
136
1
        let output = result.to_string();
137
1
        assert!(output.contains("["));
138
1
        assert!(output.contains("a"));
139
1
        assert!(output.contains("b"));
140
1
        assert!(output.contains("c"));
141
1
        assert!(output.contains("]"));
142
1
    }
143
144
    #[test]
145
1
    fn test_list_pattern_with_rest() {
146
1
        let transpiler = create_transpiler();
147
1
        let pattern = Pattern::List(vec![
148
1
            Pattern::Identifier("head".to_string()),
149
1
            Pattern::Rest,
150
1
        ]);
151
        
152
1
        let result = transpiler.transpile_pattern(&pattern)
153
1
            .expect("Failed to transpile");
154
        
155
1
        let output = result.to_string();
156
1
        assert!(output.contains("["));
157
1
        assert!(output.contains("head"));
158
1
        assert!(output.contains(".."));
159
1
        assert!(output.contains("]"));
160
1
    }
161
162
    #[test]
163
1
    fn test_list_pattern_with_named_rest() {
164
1
        let transpiler = create_transpiler();
165
1
        let pattern = Pattern::List(vec![
166
1
            Pattern::Identifier("head".to_string()),
167
1
            Pattern::RestNamed("tail".to_string()),
168
1
        ]);
169
        
170
1
        let result = transpiler.transpile_pattern(&pattern)
171
1
            .expect("Failed to transpile");
172
        
173
1
        let output = result.to_string();
174
1
        assert!(output.contains("["));
175
1
        assert!(output.contains("head"));
176
1
        assert!(output.contains(".."));
177
1
        assert!(output.contains("tail"));
178
1
        assert!(output.contains("]"));
179
1
    }
180
181
    #[test]
182
1
    fn test_struct_pattern_simple() {
183
1
        let transpiler = create_transpiler();
184
1
        let pattern = Pattern::Struct {
185
1
            name: "Point".to_string(),
186
1
            fields: vec![
187
1
                StructPatternField {
188
1
                    name: "x".to_string(),
189
1
                    pattern: Some(Pattern::Identifier("x_val".to_string())),
190
1
                },
191
1
                StructPatternField {
192
1
                    name: "y".to_string(),
193
1
                    pattern: Some(Pattern::Identifier("y_val".to_string())),
194
1
                },
195
1
            ],
196
1
            has_rest: false,
197
1
        };
198
        
199
1
        let result = transpiler.transpile_pattern(&pattern)
200
1
            .expect("Failed to transpile");
201
        
202
1
        let output = result.to_string();
203
1
        assert!(output.contains("Point"));
204
1
        assert!(output.contains("{"));
205
1
        assert!(output.contains("x"));
206
1
        assert!(output.contains("x_val"));
207
1
        assert!(output.contains("y"));
208
1
        assert!(output.contains("y_val"));
209
1
        assert!(output.contains("}"));
210
1
    }
211
212
    #[test]
213
1
    fn test_struct_pattern_with_rest() {
214
1
        let transpiler = create_transpiler();
215
1
        let pattern = Pattern::Struct {
216
1
            name: "Config".to_string(),
217
1
            fields: vec![
218
1
                StructPatternField {
219
1
                    name: "debug".to_string(),
220
1
                    pattern: Some(Pattern::Literal(Literal::Bool(true))),
221
1
                },
222
1
            ],
223
1
            has_rest: true,
224
1
        };
225
        
226
1
        let result = transpiler.transpile_pattern(&pattern)
227
1
            .expect("Failed to transpile");
228
        
229
1
        let output = result.to_string();
230
1
        assert!(output.contains("Config"));
231
1
        assert!(output.contains("debug"));
232
1
        assert!(output.contains("true"));
233
1
        assert!(output.contains(".."));
234
1
    }
235
236
    // Enum patterns are not in the current Pattern enum, skipping these tests
237
    // #[test]
238
    // fn test_enum_pattern() { ... }
239
    // #[test] 
240
    // fn test_enum_pattern_no_fields() { ... }
241
242
    #[test]
243
1
    fn test_qualified_name_pattern() {
244
1
        let transpiler = create_transpiler();
245
1
        let pattern = Pattern::QualifiedName(vec![
246
1
            "std".to_string(),
247
1
            "cmp".to_string(),
248
1
            "Ordering".to_string(),
249
1
            "Less".to_string(),
250
1
        ]);
251
        
252
1
        let result = transpiler.transpile_pattern(&pattern)
253
1
            .expect("Failed to transpile");
254
        
255
1
        let output = result.to_string();
256
1
        assert!(output.contains("std"));
257
1
        assert!(output.contains("cmp"));
258
1
        assert!(output.contains("Ordering"));
259
1
        assert!(output.contains("Less"));
260
1
    }
261
262
    #[test]
263
1
    fn test_range_pattern() {
264
1
        let transpiler = create_transpiler();
265
1
        let pattern = Pattern::Range {
266
1
            start: Box::new(Pattern::Literal(Literal::Integer(0))),
267
1
            end: Box::new(Pattern::Literal(Literal::Integer(10))),
268
1
            inclusive: false,
269
1
        };
270
        
271
1
        let result = transpiler.transpile_pattern(&pattern)
272
1
            .expect("Failed to transpile");
273
        
274
1
        let output = result.to_string();
275
1
        assert!(output.contains("0"));
276
1
        assert!(output.contains("10"));
277
1
    }
278
279
    #[test]
280
1
    fn test_range_pattern_inclusive() {
281
1
        let transpiler = create_transpiler();
282
1
        let pattern = Pattern::Range {
283
1
            start: Box::new(Pattern::Literal(Literal::Integer(1))),
284
1
            end: Box::new(Pattern::Literal(Literal::Integer(5))),
285
1
            inclusive: true,
286
1
        };
287
        
288
1
        let result = transpiler.transpile_pattern(&pattern)
289
1
            .expect("Failed to transpile");
290
        
291
1
        let output = result.to_string();
292
1
        assert!(output.contains("1"));
293
1
        assert!(output.contains("5"));
294
1
        assert!(output.contains("="));
295
1
    }
296
297
    #[test]
298
1
    fn test_or_pattern() {
299
1
        let transpiler = create_transpiler();
300
1
        let pattern = Pattern::Or(vec![
301
1
            Pattern::Literal(Literal::Integer(1)),
302
1
            Pattern::Literal(Literal::Integer(2)),
303
1
            Pattern::Literal(Literal::Integer(3)),
304
1
        ]);
305
        
306
1
        let result = transpiler.transpile_pattern(&pattern)
307
1
            .expect("Failed to transpile");
308
        
309
1
        let output = result.to_string();
310
1
        assert!(output.contains("1"));
311
1
        assert!(output.contains("|"));
312
1
        assert!(output.contains("2"));
313
1
        assert!(output.contains("|"));
314
1
        assert!(output.contains("3"));
315
1
    }
316
317
    #[test]
318
1
    fn test_complex_nested_pattern() {
319
1
        let transpiler = create_transpiler();
320
1
        let pattern = Pattern::Tuple(vec![
321
1
            Pattern::List(vec![
322
1
                Pattern::Identifier("first".to_string()),
323
1
                Pattern::Rest,
324
1
            ]),
325
1
            Pattern::Struct {
326
1
                name: "Config".to_string(),
327
1
                fields: vec![
328
1
                    StructPatternField {
329
1
                        name: "enabled".to_string(),
330
1
                        pattern: Some(Pattern::Literal(Literal::Bool(true))),
331
1
                    },
332
1
                ],
333
1
                has_rest: true,
334
1
            },
335
1
        ]);
336
        
337
1
        let result = transpiler.transpile_pattern(&pattern)
338
1
            .expect("Failed to transpile");
339
        
340
1
        let output = result.to_string();
341
1
        assert!(output.contains("("));
342
1
        assert!(output.contains("["));
343
1
        assert!(output.contains("first"));
344
1
        assert!(output.contains("Config"));
345
1
        assert!(output.contains("enabled"));
346
1
    }
347
}