/home/noah/src/ruchy/src/backend/transpiler/type_conversion_refactored_tests.rs
Line | Count | Source |
1 | | //! Comprehensive unit tests for type_conversion_refactored module |
2 | | //! Target: Increase coverage from 6.38% 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, StringPart, Literal}; |
9 | | use crate::frontend::Span; |
10 | | |
11 | 19 | fn create_transpiler() -> Transpiler { |
12 | 19 | Transpiler::new() |
13 | 19 | } |
14 | | |
15 | 23 | fn parse_expr(code: &str) -> Expr { |
16 | 23 | let mut parser = Parser::new(code); |
17 | 23 | parser.parse_expr().expect("Failed to parse expression") |
18 | 23 | } |
19 | | |
20 | | #[test] |
21 | 1 | fn test_str_conversion_integer() { |
22 | 1 | let transpiler = create_transpiler(); |
23 | 1 | let expr = parse_expr("str(42)"); |
24 | | |
25 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
26 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
27 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
28 | 1 | .expect("Conversion failed"); |
29 | 1 | assert!(result.is_some()); |
30 | 1 | let tokens = result.unwrap(); |
31 | 1 | let output = tokens.to_string(); |
32 | 1 | assert!(output.contains("format")); |
33 | 0 | } |
34 | 0 | } |
35 | 1 | } |
36 | | |
37 | | #[test] |
38 | 1 | fn test_str_conversion_float() { |
39 | 1 | let transpiler = create_transpiler(); |
40 | 1 | let expr = parse_expr("str(3.14)"); |
41 | | |
42 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
43 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
44 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
45 | 1 | .expect("Conversion failed"); |
46 | 1 | assert!(result.is_some()); |
47 | 1 | let tokens = result.unwrap(); |
48 | 1 | let output = tokens.to_string(); |
49 | 1 | assert!(output.contains("format")); |
50 | 0 | } |
51 | 0 | } |
52 | 1 | } |
53 | | |
54 | | #[test] |
55 | 1 | fn test_str_conversion_bool() { |
56 | 1 | let transpiler = create_transpiler(); |
57 | 1 | let expr = parse_expr("str(true)"); |
58 | | |
59 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
60 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
61 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
62 | 1 | .expect("Conversion failed"); |
63 | 1 | assert!(result.is_some()); |
64 | 0 | } |
65 | 0 | } |
66 | 1 | } |
67 | | |
68 | | #[test] |
69 | 1 | fn test_int_conversion_string_literal() { |
70 | 1 | let transpiler = create_transpiler(); |
71 | 1 | let expr = parse_expr("int(\"123\")"); |
72 | | |
73 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
74 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
75 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
76 | 1 | .expect("Conversion failed"); |
77 | 1 | assert!(result.is_some()); |
78 | 1 | let tokens = result.unwrap(); |
79 | 1 | let output = tokens.to_string(); |
80 | 1 | assert!(output.contains("parse")); |
81 | 1 | assert!(output.contains("i64")); |
82 | 0 | } |
83 | 0 | } |
84 | 1 | } |
85 | | |
86 | | #[test] |
87 | 1 | fn test_int_conversion_float() { |
88 | 1 | let transpiler = create_transpiler(); |
89 | 1 | let expr = parse_expr("int(3.14)"); |
90 | | |
91 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
92 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
93 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
94 | 1 | .expect("Conversion failed"); |
95 | 1 | assert!(result.is_some()); |
96 | 1 | let tokens = result.unwrap(); |
97 | 1 | let output = tokens.to_string(); |
98 | 1 | assert!(output.contains("as i64")); |
99 | 0 | } |
100 | 0 | } |
101 | 1 | } |
102 | | |
103 | | #[test] |
104 | 1 | fn test_float_conversion_string() { |
105 | 1 | let transpiler = create_transpiler(); |
106 | 1 | let expr = parse_expr("float(\"3.14\")"); |
107 | | |
108 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
109 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
110 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
111 | 1 | .expect("Conversion failed"); |
112 | 1 | assert!(result.is_some()); |
113 | 1 | let tokens = result.unwrap(); |
114 | 1 | let output = tokens.to_string(); |
115 | 1 | assert!(output.contains("parse")); |
116 | 1 | assert!(output.contains("f64")); |
117 | 0 | } |
118 | 0 | } |
119 | 1 | } |
120 | | |
121 | | #[test] |
122 | 1 | fn test_float_conversion_int() { |
123 | 1 | let transpiler = create_transpiler(); |
124 | 1 | let expr = parse_expr("float(42)"); |
125 | | |
126 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
127 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
128 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
129 | 1 | .expect("Conversion failed"); |
130 | 1 | assert!(result.is_some()); |
131 | 1 | let tokens = result.unwrap(); |
132 | 1 | let output = tokens.to_string(); |
133 | 1 | assert!(output.contains("as f64")); |
134 | 0 | } |
135 | 0 | } |
136 | 1 | } |
137 | | |
138 | | #[test] |
139 | 1 | fn test_bool_conversion_zero() { |
140 | 1 | let transpiler = create_transpiler(); |
141 | 1 | let expr = parse_expr("bool(0)"); |
142 | | |
143 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
144 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
145 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
146 | 1 | .expect("Conversion failed"); |
147 | 1 | assert!(result.is_some()); |
148 | 1 | let tokens = result.unwrap(); |
149 | 1 | let output = tokens.to_string(); |
150 | 1 | assert!(output.contains("!= 0")); |
151 | 0 | } |
152 | 0 | } |
153 | 1 | } |
154 | | |
155 | | #[test] |
156 | 1 | fn test_bool_conversion_nonzero() { |
157 | 1 | let transpiler = create_transpiler(); |
158 | 1 | let expr = parse_expr("bool(42)"); |
159 | | |
160 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
161 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
162 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
163 | 1 | .expect("Conversion failed"); |
164 | 1 | assert!(result.is_some()); |
165 | 1 | let tokens = result.unwrap(); |
166 | 1 | let output = tokens.to_string(); |
167 | 1 | assert!(output.contains("!= 0")); |
168 | 0 | } |
169 | 0 | } |
170 | 1 | } |
171 | | |
172 | | #[test] |
173 | 1 | fn test_bool_conversion_empty_string() { |
174 | 1 | let transpiler = create_transpiler(); |
175 | 1 | let expr = parse_expr("bool(\"\")"); |
176 | | |
177 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
178 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
179 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
180 | 1 | .expect("Conversion failed"); |
181 | 1 | assert!(result.is_some()); |
182 | 1 | let tokens = result.unwrap(); |
183 | 1 | let output = tokens.to_string(); |
184 | 1 | assert!(output.contains("is_empty")); |
185 | 0 | } |
186 | 0 | } |
187 | 1 | } |
188 | | |
189 | | #[test] |
190 | 1 | fn test_bool_conversion_nonempty_string() { |
191 | 1 | let transpiler = create_transpiler(); |
192 | 1 | let expr = parse_expr("bool(\"hello\")"); |
193 | | |
194 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
195 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
196 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
197 | 1 | .expect("Conversion failed"); |
198 | 1 | assert!(result.is_some()); |
199 | 1 | let tokens = result.unwrap(); |
200 | 1 | let output = tokens.to_string(); |
201 | 1 | assert!(output.contains("is_empty")); |
202 | 0 | } |
203 | 0 | } |
204 | 1 | } |
205 | | |
206 | | #[test] |
207 | 1 | fn test_list_conversion_string() { |
208 | 1 | let transpiler = create_transpiler(); |
209 | 1 | let expr = parse_expr("list(\"hello\")"); |
210 | | |
211 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
212 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
213 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
214 | 1 | .expect("Conversion failed"); |
215 | 1 | assert!(result.is_some()); |
216 | 1 | let tokens = result.unwrap(); |
217 | 1 | let output = tokens.to_string(); |
218 | 1 | assert!(output.contains("chars") || output.contains("collect")0 ); |
219 | 0 | } |
220 | 0 | } |
221 | 1 | } |
222 | | |
223 | | #[test] |
224 | 1 | fn test_list_conversion_range() { |
225 | 1 | let transpiler = create_transpiler(); |
226 | 1 | let expr = parse_expr("list(0..10)"); |
227 | | |
228 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
229 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
230 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
231 | 1 | .expect("Conversion failed"); |
232 | 1 | assert!(result.is_some()); |
233 | 1 | let tokens = result.unwrap(); |
234 | 1 | let output = tokens.to_string(); |
235 | 1 | assert!(output.contains("collect")); |
236 | 0 | } |
237 | 0 | } |
238 | 1 | } |
239 | | |
240 | | #[test] |
241 | 1 | fn test_set_conversion_list() { |
242 | 1 | let transpiler = create_transpiler(); |
243 | 1 | let expr = parse_expr("set([1, 2, 3])"); |
244 | | |
245 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
246 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
247 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
248 | 1 | .expect("Conversion failed"); |
249 | 1 | assert!(result.is_some()); |
250 | 1 | let tokens = result.unwrap(); |
251 | 1 | let output = tokens.to_string(); |
252 | 1 | assert!(output.contains("HashSet")); |
253 | 0 | } |
254 | 0 | } |
255 | 1 | } |
256 | | |
257 | | #[test] |
258 | 1 | fn test_not_type_conversion() { |
259 | 1 | let transpiler = create_transpiler(); |
260 | 1 | let expr = parse_expr("print(42)"); |
261 | | |
262 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
263 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
264 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
265 | 1 | .expect("Should not fail"); |
266 | 1 | assert!(result.is_none()); // Not a type conversion |
267 | 0 | } |
268 | 0 | } |
269 | 1 | } |
270 | | |
271 | | #[test] |
272 | 1 | fn test_invalid_arg_count() { |
273 | 1 | let transpiler = create_transpiler(); |
274 | 1 | let expr = parse_expr("int(1, 2)"); |
275 | | |
276 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
277 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
278 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args); |
279 | 1 | assert!(result.is_err()); // Should fail with wrong arg count |
280 | 0 | } |
281 | 0 | } |
282 | 1 | } |
283 | | |
284 | | #[test] |
285 | 1 | fn test_string_interpolation_to_bool() { |
286 | 1 | let transpiler = create_transpiler(); |
287 | | // Create a string interpolation expression manually |
288 | 1 | let expr = Expr { |
289 | 1 | kind: ExprKind::Call { |
290 | 1 | func: Box::new(Expr { |
291 | 1 | kind: ExprKind::Identifier("bool".to_string()), |
292 | 1 | span: Span::new(0, 0), |
293 | 1 | attributes: vec![], |
294 | 1 | }), |
295 | 1 | args: vec![Expr { |
296 | 1 | kind: ExprKind::StringInterpolation { |
297 | 1 | parts: vec![StringPart::Text("test".to_string())], |
298 | 1 | }, |
299 | 1 | span: Span::new(0, 0), |
300 | 1 | attributes: vec![], |
301 | 1 | }], |
302 | 1 | }, |
303 | 1 | span: Span::new(0, 0), |
304 | 1 | attributes: vec![], |
305 | 1 | }; |
306 | | |
307 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
308 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
309 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
310 | 1 | .expect("Conversion failed"); |
311 | 1 | assert!(result.is_some()); |
312 | 0 | } |
313 | 0 | } |
314 | 1 | } |
315 | | |
316 | | #[test] |
317 | 1 | fn test_dict_conversion() { |
318 | 1 | let transpiler = create_transpiler(); |
319 | | // Create a simple expression for dict conversion |
320 | 1 | let expr = Expr { |
321 | 1 | kind: ExprKind::Call { |
322 | 1 | func: Box::new(Expr { |
323 | 1 | kind: ExprKind::Identifier("dict".to_string()), |
324 | 1 | span: Span::new(0, 0), |
325 | 1 | attributes: vec![], |
326 | 1 | }), |
327 | 1 | args: vec![Expr { |
328 | 1 | kind: ExprKind::List(vec![]), |
329 | 1 | span: Span::new(0, 0), |
330 | 1 | attributes: vec![], |
331 | 1 | }], |
332 | 1 | }, |
333 | 1 | span: Span::new(0, 0), |
334 | 1 | attributes: vec![], |
335 | 1 | }; |
336 | | |
337 | 1 | if let ExprKind::Call { func, args } = &expr.kind { |
338 | 1 | if let ExprKind::Identifier(name) = &func.kind { |
339 | 1 | let result = transpiler.try_transpile_type_conversion_refactored(name, args) |
340 | 1 | .expect("Conversion failed"); |
341 | 1 | assert!(result.is_some()); |
342 | 1 | let tokens = result.unwrap(); |
343 | 1 | let output = tokens.to_string(); |
344 | 1 | assert!(output.contains("HashMap")); |
345 | 0 | } |
346 | 0 | } |
347 | 1 | } |
348 | | |
349 | | #[test] |
350 | 1 | fn test_all_type_conversions_exist() { |
351 | 1 | let transpiler = create_transpiler(); |
352 | 1 | let conversions = vec!["str", "int", "float", "bool", "list", "set", "dict"]; |
353 | | |
354 | 8 | for conv7 in conversions { |
355 | 7 | let code = format!("{}(42)", conv); |
356 | 7 | let expr = parse_expr(&code); |
357 | | |
358 | 7 | if let ExprKind::Call { func, args } = &expr.kind { |
359 | 7 | if let ExprKind::Identifier(name) = &func.kind { |
360 | 7 | let result = transpiler.try_transpile_type_conversion_refactored(name, args); |
361 | 7 | assert!(result.is_ok(), "Failed for {}"0 , conv); |
362 | 7 | assert!(result.unwrap().is_some(), "None for {}"0 , conv); |
363 | 0 | } |
364 | 0 | } |
365 | | } |
366 | 1 | } |
367 | | } |