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/method_call_refactored_tests.rs
Line
Count
Source
1
//! Comprehensive unit tests for method_call_refactored module
2
//! Target: Increase coverage from 15.58% to 80%+
3
4
#[cfg(test)]
5
mod tests {
6
    use super::super::*;
7
    use crate::frontend::parser::Parser;
8
    use crate::frontend::ast::{Expr, ExprKind};
9
10
22
    fn create_transpiler() -> Transpiler {
11
22
        Transpiler::new()
12
22
    }
13
14
22
    fn parse_and_extract_method_call(code: &str) -> (Box<Expr>, String, Vec<Expr>) {
15
22
        let mut parser = Parser::new(code);
16
22
        let expr = parser.parse_expr().expect("Failed to parse");
17
        
18
22
        if let ExprKind::MethodCall { receiver: object, method, args } = expr.kind {
19
22
            (object, method, args)
20
        } else {
21
0
            panic!("Not a method call expression");
22
        }
23
22
    }
24
25
    #[test]
26
1
    fn test_map_method() {
27
1
        let transpiler = create_transpiler();
28
1
        let (obj, method, args) = parse_and_extract_method_call("[1,2,3].map(|x| x * 2)");
29
        
30
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
31
1
            .expect("Failed to transpile");
32
        
33
1
        let output = result.to_string();
34
1
        assert!(output.contains("iter"));
35
1
        assert!(output.contains("map"));
36
1
        assert!(output.contains("collect"));
37
1
    }
38
39
    #[test]
40
1
    fn test_filter_method() {
41
1
        let transpiler = create_transpiler();
42
1
        let (obj, method, args) = parse_and_extract_method_call("[1,2,3].filter(|x| x > 1)");
43
        
44
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
45
1
            .expect("Failed to transpile");
46
        
47
1
        let output = result.to_string();
48
1
        assert!(output.contains("into_iter"));
49
1
        assert!(output.contains("filter"));
50
1
        assert!(output.contains("collect"));
51
1
    }
52
53
    #[test]
54
1
    fn test_reduce_method() {
55
1
        let transpiler = create_transpiler();
56
1
        let (obj, method, args) = parse_and_extract_method_call("[1,2,3].reduce(|a,b| a + b)");
57
        
58
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
59
1
            .expect("Failed to transpile");
60
        
61
1
        let output = result.to_string();
62
1
        assert!(output.contains("reduce") || 
output.contains("fold")0
);
63
1
    }
64
65
    #[test]
66
1
    fn test_hashmap_get() {
67
1
        let transpiler = create_transpiler();
68
1
        let (obj, method, args) = parse_and_extract_method_call("map.get(\"key\")");
69
        
70
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
71
1
            .expect("Failed to transpile");
72
        
73
1
        let output = result.to_string();
74
1
        assert!(output.contains("get"));
75
1
        assert!(output.contains("cloned"));
76
1
    }
77
78
    #[test]
79
1
    fn test_hashmap_contains_key() {
80
1
        let transpiler = create_transpiler();
81
1
        let (obj, method, args) = parse_and_extract_method_call("map.contains_key(\"key\")");
82
        
83
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
84
1
            .expect("Failed to transpile");
85
        
86
1
        let output = result.to_string();
87
1
        assert!(output.contains("contains_key"));
88
1
    }
89
90
    #[test]
91
1
    fn test_hashmap_items() {
92
1
        let transpiler = create_transpiler();
93
1
        let (obj, method, args) = parse_and_extract_method_call("map.items()");
94
        
95
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
96
1
            .expect("Failed to transpile");
97
        
98
1
        let output = result.to_string();
99
1
        assert!(output.contains("iter"));
100
1
        assert!(output.contains("clone"));
101
1
    }
102
103
    #[test]
104
1
    fn test_hashset_contains() {
105
1
        let transpiler = create_transpiler();
106
1
        let (obj, method, args) = parse_and_extract_method_call("set.contains(42)");
107
        
108
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
109
1
            .expect("Failed to transpile");
110
        
111
1
        let output = result.to_string();
112
1
        assert!(output.contains("contains"));
113
1
    }
114
115
    #[test]
116
1
    fn test_hashset_union() {
117
1
        let transpiler = create_transpiler();
118
1
        let (obj, method, args) = parse_and_extract_method_call("set1.union(set2)");
119
        
120
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
121
1
            .expect("Failed to transpile");
122
        
123
1
        let output = result.to_string();
124
1
        assert!(output.contains("union"));
125
1
        assert!(output.contains("HashSet"));
126
1
    }
127
128
    #[test]
129
1
    fn test_collection_push() {
130
1
        let transpiler = create_transpiler();
131
1
        let (obj, method, args) = parse_and_extract_method_call("vec.push(42)");
132
        
133
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
134
1
            .expect("Failed to transpile");
135
        
136
1
        let output = result.to_string();
137
1
        assert!(output.contains("push"));
138
1
    }
139
140
    #[test]
141
1
    fn test_collection_pop() {
142
1
        let transpiler = create_transpiler();
143
1
        let (obj, method, args) = parse_and_extract_method_call("vec.pop()");
144
        
145
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
146
1
            .expect("Failed to transpile");
147
        
148
1
        let output = result.to_string();
149
1
        assert!(output.contains("pop"));
150
1
    }
151
152
    #[test]
153
1
    fn test_collection_len() {
154
1
        let transpiler = create_transpiler();
155
1
        let (obj, method, args) = parse_and_extract_method_call("vec.len()");
156
        
157
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
158
1
            .expect("Failed to transpile");
159
        
160
1
        let output = result.to_string();
161
1
        assert!(output.contains("len"));
162
1
    }
163
164
    #[test]
165
1
    fn test_collection_is_empty() {
166
1
        let transpiler = create_transpiler();
167
1
        let (obj, method, args) = parse_and_extract_method_call("vec.is_empty()");
168
        
169
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
170
1
            .expect("Failed to transpile");
171
        
172
1
        let output = result.to_string();
173
1
        assert!(output.contains("is_empty"));
174
1
    }
175
176
    #[test]
177
1
    fn test_string_to_upper() {
178
1
        let transpiler = create_transpiler();
179
1
        let (obj, method, args) = parse_and_extract_method_call("\"hello\".to_upper()");
180
        
181
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
182
1
            .expect("Failed to transpile");
183
        
184
1
        let output = result.to_string();
185
1
        assert!(output.contains("to_uppercase"));
186
1
    }
187
188
    #[test]
189
1
    fn test_string_trim() {
190
1
        let transpiler = create_transpiler();
191
1
        let (obj, method, args) = parse_and_extract_method_call("\"  hello  \".trim()");
192
        
193
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
194
1
            .expect("Failed to transpile");
195
        
196
1
        let output = result.to_string();
197
1
        assert!(output.contains("trim"));
198
1
    }
199
200
    #[test]
201
1
    fn test_string_split() {
202
1
        let transpiler = create_transpiler();
203
1
        let (obj, method, args) = parse_and_extract_method_call("\"a,b,c\".split(\",\")");
204
        
205
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
206
1
            .expect("Failed to transpile");
207
        
208
1
        let output = result.to_string();
209
1
        assert!(output.contains("split"));
210
1
    }
211
212
    #[test]
213
1
    fn test_string_replace() {
214
1
        let transpiler = create_transpiler();
215
1
        let (obj, method, args) = parse_and_extract_method_call("\"hello\".replace(\"l\", \"r\")");
216
        
217
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
218
1
            .expect("Failed to transpile");
219
        
220
1
        let output = result.to_string();
221
1
        assert!(output.contains("replace"));
222
1
    }
223
224
    #[test]
225
1
    fn test_numeric_round() {
226
1
        let transpiler = create_transpiler();
227
1
        let (obj, method, args) = parse_and_extract_method_call("3.14.round()");
228
        
229
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
230
1
            .expect("Failed to transpile");
231
        
232
1
        let output = result.to_string();
233
1
        assert!(output.contains("round"));
234
1
    }
235
236
    #[test]
237
1
    fn test_numeric_abs() {
238
1
        let transpiler = create_transpiler();
239
1
        let (obj, method, args) = parse_and_extract_method_call("(-5).abs()");
240
        
241
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
242
1
            .expect("Failed to transpile");
243
        
244
1
        let output = result.to_string();
245
1
        assert!(output.contains("abs"));
246
1
    }
247
248
    #[test]
249
    #[ignore = "Flatten method not fully implemented"]
250
0
    fn test_advanced_flatten() {
251
0
        let transpiler = create_transpiler();
252
0
        let (obj, method, args) = parse_and_extract_method_call("[[1],[2]].flatten()");
253
        
254
0
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
255
0
            .expect("Failed to transpile");
256
        
257
0
        let output = result.to_string();
258
0
        assert!(output.contains("flatten"));
259
0
        assert!(output.contains("collect"));
260
0
    }
261
262
    #[test]
263
    #[ignore = "Unique method not fully implemented"]
264
0
    fn test_advanced_unique() {
265
0
        let transpiler = create_transpiler();
266
0
        let (obj, method, args) = parse_and_extract_method_call("[1,2,2,3].unique()");
267
        
268
0
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
269
0
            .expect("Failed to transpile");
270
        
271
0
        let output = result.to_string();
272
0
        assert!(output.contains("HashSet"));
273
0
        assert!(output.contains("collect"));
274
0
    }
275
276
    #[test]
277
1
    fn test_default_method_call() {
278
1
        let transpiler = create_transpiler();
279
1
        let (obj, method, args) = parse_and_extract_method_call("obj.custom_method(1, 2)");
280
        
281
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
282
1
            .expect("Failed to transpile");
283
        
284
1
        let output = result.to_string();
285
1
        assert!(output.contains("custom_method"));
286
1
    }
287
288
    #[test]
289
1
    fn test_iterator_any() {
290
1
        let transpiler = create_transpiler();
291
1
        let (obj, method, args) = parse_and_extract_method_call("[1,2,3].any(|x| x > 2)");
292
        
293
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
294
1
            .expect("Failed to transpile");
295
        
296
1
        let output = result.to_string();
297
1
        assert!(output.contains("any"));
298
1
    }
299
300
    #[test]
301
1
    fn test_iterator_all() {
302
1
        let transpiler = create_transpiler();
303
1
        let (obj, method, args) = parse_and_extract_method_call("[1,2,3].all(|x| x > 0)");
304
        
305
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
306
1
            .expect("Failed to transpile");
307
        
308
1
        let output = result.to_string();
309
1
        assert!(output.contains("all"));
310
1
    }
311
312
    #[test]
313
1
    fn test_iterator_find() {
314
1
        let transpiler = create_transpiler();
315
1
        let (obj, method, args) = parse_and_extract_method_call("[1,2,3].find(|x| x == 2)");
316
        
317
1
        let result = transpiler.transpile_method_call_refactored(&obj, &method, &args)
318
1
            .expect("Failed to transpile");
319
        
320
1
        let output = result.to_string();
321
1
        assert!(output.contains("find"));
322
1
    }
323
}