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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! expr_gen.rs Coverage Expansion Tests
//!
//! DEPYLER-0151 Phase 2B: Property + Mutation testing for expression generation
//! Target: 64.15% → 75%+ coverage (492 missed lines)
//!
//! Test Structure (MANDATORY):
//! - Unit Tests: Basic expression transpilation validation
//! - Property Tests: Arbitrary input validation with proptest
//! - Mutation Tests: Documented mutation kill strategies
use depyler_core::DepylerPipeline;
// ============================================================================
// UNIT TESTS - Method Call Conversions
// ============================================================================
#[test]
fn test_list_method_append() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def test_list():
items = [1, 2, 3]
items.append(4)
return items
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
println!("Generated list append code:\n{}", rust_code);
// Should generate .push() for list.append()
assert!(
rust_code.contains(".push("),
"list.append() should transpile to .push()"
);
}
#[test]
fn test_dict_method_get_with_default() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def test_dict():
data = {"key": "value"}
result = data.get("key", "default")
return result
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
println!("Generated dict.get() code:\n{}", rust_code);
// Should generate .get() with unwrap_or
assert!(
rust_code.contains(".get(") && rust_code.contains("unwrap_or"),
"dict.get(key, default) should use .get().unwrap_or()"
);
}
#[test]
fn test_string_method_upper() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def test_str():
text = "hello"
return text.upper()
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
println!("Generated string.upper() code:\n{}", rust_code);
// Should generate .to_uppercase()
assert!(
rust_code.contains(".to_uppercase()"),
"str.upper() should transpile to .to_uppercase()"
);
}
// ============================================================================
// UNIT TESTS - Binary Operation Edge Cases
// ============================================================================
#[test]
fn test_floor_division_semantics() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def floor_div(a: int, b: int) -> int:
return a // b
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
println!("Generated floor division code:\n{}", rust_code);
// Should include Python floor division semantics (towards negative infinity)
assert!(
rust_code.contains("needs_adjustment") || rust_code.contains("signs_differ"),
"Floor division should implement Python semantics"
);
}
#[test]
fn test_power_operation_with_negative_exponent() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def power_calc():
return 2 ** -1
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
println!("Generated power with negative exp code:\n{}", rust_code);
// Should use .powf() for negative exponents
assert!(
rust_code.contains(".powf(") || rust_code.contains("as f64"),
"Negative exponent should use float power"
);
}
#[test]
fn test_set_literals_generate_hashset() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def set_create():
a = {1, 2, 3}
return a
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
println!("Generated set creation code:\n{}", rust_code);
// Should use HashSet
assert!(
rust_code.contains("HashSet"),
"Set literals should generate HashSet"
);
}
// ============================================================================
// UNIT TESTS - Slice Operations
// ============================================================================
#[test]
fn test_slice_with_step() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def slice_test():
arr = [1, 2, 3, 4, 5, 6]
return arr[::2]
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
println!("Generated slice with step code:\n{}", rust_code);
// Should use .step_by() for slice with step
assert!(
rust_code.contains(".step_by(") || rust_code.contains("step"),
"Slice with step should use .step_by()"
);
}
#[test]
fn test_slice_negative_step() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def reverse_slice():
arr = [1, 2, 3, 4, 5]
return arr[::-1]
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
println!("Generated reverse slice code:\n{}", rust_code);
// Should use .rev() for negative step
assert!(
rust_code.contains(".rev()"),
"Slice [::-1] should use .rev()"
);
}
// ============================================================================
// UNIT TESTS - Comprehensions
// ============================================================================
#[test]
fn test_list_comprehension_with_filter() {
let pipeline = DepylerPipeline::new();
let python_code = r#"
def list_comp():
return [x * 2 for x in range(10) if x > 5]
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
println!("Generated list comprehension code:\n{}", rust_code);
// Should use .filter() and .map()
assert!(
rust_code.contains(".filter(") && rust_code.contains(".map("),
"List comprehension with condition should use .filter().map()"
);
}
// ============================================================================
// PROPERTY TESTS
// ============================================================================
#[cfg(test)]
mod property_tests {
use super::*;
use proptest::prelude::*;
proptest! {
#![proptest_config(ProptestConfig::with_cases(10))]
#[test]
fn prop_integer_binary_operations_transpile(a in -100i32..100i32, b in 1i32..100i32) {
// Property: All basic binary operations should transpile without error
let pipeline = DepylerPipeline::new();
let python_code = format!(r#"
def binary_ops():
return {} + {}
"#, a, b);
let result = pipeline.transpile(&python_code);
prop_assert!(result.is_ok(), "Binary operation transpilation failed: {:?}", result.err());
}
#[test]
fn prop_list_operations_always_generate_vec(size in 1usize..10) {
// Property: List creation should always generate vec! macro (non-empty lists)
// Note: Empty lists may be optimized differently
let pipeline = DepylerPipeline::new();
let elements = (0..size).map(|i| i.to_string()).collect::<Vec<_>>().join(", ");
let python_code = format!(r#"
def make_list():
return [{}]
"#, elements);
let result = pipeline.transpile(&python_code);
prop_assert!(result.is_ok(), "List transpilation failed");
let rust_code = result.unwrap();
prop_assert!(
rust_code.contains("vec!") || rust_code.contains("vec !"),
"List should generate vec! macro"
);
}
#[test]
fn prop_dict_operations_require_hashmap(pairs in 0usize..5) {
// Property: Dict creation should always require HashMap import
let pipeline = DepylerPipeline::new();
let items = (0..pairs)
.map(|i| format!(r#""key{}": {}"#, i, i))
.collect::<Vec<_>>()
.join(", ");
let python_code = format!(r#"
def make_dict():
return {{{}}}
"#, items);
let result = pipeline.transpile(&python_code);
prop_assert!(result.is_ok(), "Dict transpilation failed");
let rust_code = result.unwrap();
prop_assert!(
rust_code.contains("HashMap"),
"Dict should require HashMap"
);
}
#[test]
fn prop_range_calls_generate_valid_ranges(n in 1usize..20) {
// Property: range(n) should generate valid Rust ranges
let pipeline = DepylerPipeline::new();
let python_code = format!(r#"
def use_range():
total = 0
for i in range({}):
total = total + i
return total
"#, n);
let result = pipeline.transpile(&python_code);
prop_assert!(result.is_ok(), "range() transpilation failed");
let rust_code = result.unwrap();
prop_assert!(
rust_code.contains("..") || rust_code.contains("range"),
"range() should generate Rust range syntax"
);
}
}
}
// ============================================================================
// MUTATION TESTS
// ============================================================================
#[cfg(test)]
mod mutation_tests {
use super::*;
#[test]
fn test_mutation_method_dispatch_correctness() {
// Target Mutations:
// 1. list.append → list.extend (wrong method selection)
// 2. .push() → .pop() (wrong Rust method)
// 3. Method parameter count (append takes 1 arg, not 0)
//
// Kill Strategy:
// - Verify correct Rust method is generated (.push not .pop)
// - Verify method takes correct number of parameters
// - Mutation changing method dispatch would fail
let pipeline = DepylerPipeline::new();
let python_code = r#"
def test_list_methods():
items = []
items.append(1)
items.append(2)
return items
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
// Mutation Kill: Changing .push() to .pop() would fail
assert!(
rust_code.matches(".push(").count() == 2,
"MUTATION KILL: Must use .push() exactly 2 times for 2 append calls (found {} times)",
rust_code.matches(".push(").count()
);
// Mutation Kill: Removing parameter would fail
assert!(
rust_code.contains(".push(1)") && rust_code.contains(".push(2)"),
"MUTATION KILL: Must pass correct arguments to .push()"
);
}
#[test]
fn test_mutation_floor_division_semantics() {
// Target Mutations:
// 1. Python // → Rust / (wrong: truncates towards zero, not floor)
// 2. Remove sign adjustment logic (wrong: breaks negative results)
// 3. Remove remainder check (wrong: adjusts when not needed)
//
// Kill Strategy:
// - Verify floor division includes sign/remainder checks
// - Verify adjustment logic is present
// - Mutation removing Python semantics would fail
let pipeline = DepylerPipeline::new();
let python_code = r#"
def floor_div_test(a: int, b: int) -> int:
return a // b
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
// Mutation Kill: Using simple / would fail for negative numbers
assert!(
rust_code.contains("needs_adjustment") || rust_code.contains("signs_differ"),
"MUTATION KILL: Must include Python floor division adjustment logic"
);
// Mutation Kill: Removing remainder check would break correctness
assert!(
rust_code.contains("r_nonzero") || rust_code.contains("r != 0"),
"MUTATION KILL: Must check remainder for adjustment decision"
);
// Mutation Kill: Removing sign check would break for mixed signs
assert!(
rust_code.contains("negative") || rust_code.contains("< 0"),
"MUTATION KILL: Must check signs for floor division"
);
}
#[test]
fn test_mutation_comprehension_filter_order() {
// Target Mutations:
// 1. .filter() → .map() order swap (wrong: map then filter vs filter then map)
// 2. Remove .filter() entirely (wrong: loses condition)
// 3. Remove .collect() (wrong: returns iterator not Vec)
//
// Kill Strategy:
// - Verify .filter() appears before .map() in chain
// - Verify .collect() converts to Vec
// - Mutation changing operation order would fail
let pipeline = DepylerPipeline::new();
let python_code = r#"
def filtered_comp():
return [x * 2 for x in range(10) if x > 5]
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
// Mutation Kill: Removing .filter() would include all elements
assert!(
rust_code.contains(".filter("),
"MUTATION KILL: Must include .filter() for comprehension condition"
);
// Mutation Kill: Removing .map() would not transform elements
assert!(
rust_code.contains(".map("),
"MUTATION KILL: Must include .map() for element transformation"
);
// Mutation Kill: Removing .collect() would return iterator
assert!(
rust_code.contains(".collect::<Vec<_>>()"),
"MUTATION KILL: Must collect into Vec for list comprehension"
);
// Mutation Kill: Swapping .filter() and .map() order breaks semantics
// Find positions of .filter and .map to verify order
let filter_pos = rust_code.find(".filter(").expect(".filter( must exist");
let map_pos = rust_code.find(".map(").expect(".map( must exist");
assert!(
filter_pos < map_pos,
"MUTATION KILL: .filter() must appear before .map() in chain (filter at {}, map at {})",
filter_pos,
map_pos
);
}
#[test]
fn test_mutation_set_creation() {
// Target Mutations:
// 1. HashSet::new() → Vec::new() (wrong collection type)
// 2. .insert() → .push() (wrong method for sets)
// 3. Remove HashSet import (would fail compilation)
//
// Kill Strategy:
// - Verify HashSet is used for set literals
// - Verify .insert() is used (not .push())
// - Mutation changing collection type would fail
let pipeline = DepylerPipeline::new();
let python_code = r#"
def make_set():
s = {1, 2, 3}
return s
"#;
let rust_code = pipeline.transpile(python_code).unwrap();
// Mutation Kill: Using Vec instead of HashSet would fail
assert!(
rust_code.contains("HashSet"),
"MUTATION KILL: Set literals must use HashSet"
);
// Mutation Kill: Using .push() instead of .insert() would fail
assert!(
rust_code.matches(".insert(").count() >= 3,
"MUTATION KILL: Must use .insert() for each set element (found {} times)",
rust_code.matches(".insert(").count()
);
// Mutation Kill: Not importing HashSet would fail compilation
assert!(
rust_code.contains("use std::collections::HashSet"),
"MUTATION KILL: Must import HashSet for set literals"
);
}
}