ruchy 4.1.2

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
Documentation
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
//! Refactored method call transpilation with reduced complexity
//! Original complexity: 58, Target: <20 per function
use crate::backend::Transpiler;
use crate::frontend::ast::Expr;
use anyhow::{bail, Result};
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
impl Transpiler {
    /// Main dispatcher for method calls (complexity: ~15)
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::backend::Transpiler;
    /// let mut transpiler = Transpiler::new();
    /// // Method call transpilation is handled internally
    /// ```
    pub fn transpile_method_call_refactored(
        &self,
        object: &Expr,
        method: &str,
        args: &[Expr],
    ) -> Result<TokenStream> {
        let obj_tokens = self.transpile_expr(object)?;

        // DEFECT-011 FIX: For contains() method, wrap field access args with &
        use crate::frontend::ast::ExprKind;
        let arg_tokens: Result<Vec<_>> = if method == "contains" {
            args.iter()
                .map(|a| {
                    let tokens = self.transpile_expr(a)?;
                    // Check if argument is a field access - if so, wrap with &
                    if matches!(&a.kind, ExprKind::FieldAccess { .. }) {
                        Ok(quote! { &#tokens })
                    } else {
                        Ok(tokens)
                    }
                })
                .collect()
        } else {
            args.iter().map(|a| self.transpile_expr(a)).collect()
        };
        let arg_tokens = arg_tokens?;

        // Dispatch to specialized handlers based on method category
        match method {
            // Iterator methods
            "map" | "filter" | "reduce" | "fold" | "any" | "all" | "find" => {
                self.transpile_iterator_method(&obj_tokens, method, &arg_tokens)
            }
            // HashMap/Dict methods
            "get" | "contains_key" | "keys" | "values" | "items" | "entry" => {
                self.transpile_hashmap_method(&obj_tokens, method, &arg_tokens)
            }
            // HashSet methods
            "contains" | "union" | "intersection" | "difference" | "symmetric_difference" => {
                self.transpile_hashset_method(&obj_tokens, method, &arg_tokens)
            }
            // Collection mutators
            "insert" | "remove" | "clear" | "push" | "pop" | "append" | "extend" => {
                self.transpile_collection_mutator(&obj_tokens, method, &arg_tokens)
            }
            // Collection accessors
            "len" | "is_empty" | "iter" | "slice" | "first" | "last" => {
                self.transpile_collection_accessor(&obj_tokens, method, &arg_tokens)
            }
            // String methods
            "to_s" | "to_string" | "to_upper" | "to_lower" | "length" | "trim" | "split"
            | "replace" | "starts_with" | "ends_with" => {
                self.transpile_string_method(&obj_tokens, method, &arg_tokens)
            }
            // DataFrame methods
            "select" | "groupby" | "agg" | "sort" | "mean" | "std" | "min" | "max" | "sum"
            | "count" | "drop_nulls" | "fill_null" | "pivot" | "melt" | "head" | "tail"
            | "sample" | "describe" => {
                self.transpile_dataframe_method_refactored(&obj_tokens, method, &arg_tokens)
            }
            // Default: pass through
            _ => {
                let method_ident = format_ident!("{}", method);
                Ok(quote! { #obj_tokens.#method_ident(#(#arg_tokens),*) })
            }
        }
    }
    /// Handle iterator methods (complexity: ~10)
    fn transpile_iterator_method(
        &self,
        obj: &TokenStream,
        method: &str,
        args: &[TokenStream],
    ) -> Result<TokenStream> {
        match method {
            "map" => Ok(quote! { #obj.iter().map(#(#args),*).collect::<Vec<_>>() }),
            // TRANSPILER-ITERATOR-001 FIX: Wrap closure to handle reference from filter
            "filter" => {
                let user_closure = &args[0];
                Ok(
                    quote! { #obj.into_iter().filter(|__x| { let __f = #user_closure; __f(*__x) }).collect::<Vec<_>>() },
                )
            }
            "reduce" => Ok(quote! { #obj.into_iter().reduce(#(#args),*) }),
            "fold" => {
                if args.len() != 2 {
                    bail!("fold requires exactly 2 arguments");
                }
                let init = &args[0];
                let func = &args[1];
                Ok(quote! { #obj.into_iter().fold(#init, #func) })
            }
            "any" => Ok(quote! { #obj.iter().any(#(#args),*) }),
            "all" => Ok(quote! { #obj.iter().all(#(#args),*) }),
            "find" => Ok(quote! { #obj.iter().find(#(#args),*).cloned() }),
            _ => unreachable!("Unknown iterator method: {}", method),
        }
    }
    /// Handle HashMap/Dict methods (complexity: ~10)
    fn transpile_hashmap_method(
        &self,
        obj: &TokenStream,
        method: &str,
        args: &[TokenStream],
    ) -> Result<TokenStream> {
        let method_ident = format_ident!("{}", method);
        match method {
            "get" => Ok(quote! { #obj.#method_ident(#(#args),*).cloned() }),
            "items" => Ok(quote! { #obj.iter().map(|(k, v)| (k.clone(), v.clone())) }),
            "contains_key" | "keys" | "values" | "entry" => {
                Ok(quote! { #obj.#method_ident(#(#args),*) })
            }
            _ => unreachable!("Unknown HashMap method: {}", method),
        }
    }
    /// Handle `HashSet` methods (complexity: ~12)
    fn transpile_hashset_method(
        &self,
        obj: &TokenStream,
        method: &str,
        args: &[TokenStream],
    ) -> Result<TokenStream> {
        match method {
            "contains" => {
                let method_ident = format_ident!("{}", method);
                Ok(quote! { #obj.#method_ident(#(#args),*) })
            }
            "union" | "intersection" | "difference" | "symmetric_difference" => {
                if args.len() != 1 {
                    bail!("{method} requires exactly 1 argument");
                }
                let other = &args[0];
                let method_ident = format_ident!("{}", method);
                Ok(quote! {
                                    {
                                        use std::collections::HashSet;
                #[cfg(test)]
                                        #obj.#method_ident(&#other).cloned().collect::<HashSet<_>>()
                                    }
                                })
            }
            _ => unreachable!("Unknown HashSet method: {}", method),
        }
    }
    /// Handle collection mutator methods (complexity: ~5)
    fn transpile_collection_mutator(
        &self,
        obj: &TokenStream,
        method: &str,
        args: &[TokenStream],
    ) -> Result<TokenStream> {
        let method_ident = format_ident!("{}", method);
        Ok(quote! { #obj.#method_ident(#(#args),*) })
    }
    /// Handle collection accessor methods (complexity: ~10)
    fn transpile_collection_accessor(
        &self,
        obj: &TokenStream,
        method: &str,
        args: &[TokenStream],
    ) -> Result<TokenStream> {
        match method {
            "slice" => {
                if args.len() != 2 {
                    bail!("slice requires exactly 2 arguments");
                }
                let start = &args[0];
                let end = &args[1];
                Ok(quote! { #obj[#start..#end].to_vec() })
            }
            "first" => Ok(quote! { #obj.first().cloned() }),
            "last" => Ok(quote! { #obj.last().cloned() }),
            _ => {
                let method_ident = format_ident!("{}", method);
                Ok(quote! { #obj.#method_ident(#(#args),*) })
            }
        }
    }
    /// Handle string methods (complexity: ~12)
    fn transpile_string_method(
        &self,
        obj: &TokenStream,
        method: &str,
        args: &[TokenStream],
    ) -> Result<TokenStream> {
        match method {
            "to_s" | "to_string" => Ok(quote! { #obj }),
            "to_upper" => Ok(quote! { #obj.to_uppercase(#(#args),*) }),
            "to_lower" => Ok(quote! { #obj.to_lowercase(#(#args),*) }),
            "length" => Ok(quote! { #obj.len(#(#args),*) }),
            "trim" => Ok(quote! { #obj.trim(#(#args),*).to_string() }),
            "split" => {
                if args.is_empty() {
                    Ok(quote! { #obj.split_whitespace().map(String::from).collect::<Vec<_>>() })
                } else {
                    Ok(quote! { #obj.split(#(#args),*).map(String::from).collect::<Vec<_>>() })
                }
            }
            "replace" => {
                if args.len() != 2 {
                    bail!("replace requires exactly 2 arguments");
                }
                Ok(quote! { #obj.replace(#(#args),*) })
            }
            "starts_with" | "ends_with" => {
                let method_ident = format_ident!("{}", method);
                Ok(quote! { #obj.#method_ident(#(#args),*) })
            }
            _ => unreachable!("Unknown string method: {}", method),
        }
    }
    /// Handle `DataFrame` methods (complexity: ~5)
    fn transpile_dataframe_method_refactored(
        &self,
        obj: &TokenStream,
        method: &str,
        args: &[TokenStream],
    ) -> Result<TokenStream> {
        let method_ident = format_ident!("{}", method);
        Ok(quote! { #obj.#method_ident(#(#args),*) })
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::backend::Transpiler;
    use crate::frontend::ast::{Expr, ExprKind, Literal};
    use proc_macro2::TokenStream;

    fn setup_transpiler() -> Transpiler {
        Transpiler::new()
    }

    fn make_ident_expr(name: &str) -> Expr {
        Expr {
            kind: ExprKind::Identifier(name.to_string()),
            span: Default::default(),
            attributes: Vec::new(),
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    fn make_string_expr(s: &str) -> Expr {
        Expr {
            kind: ExprKind::Literal(Literal::String(s.to_string())),
            span: Default::default(),
            attributes: Vec::new(),
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    #[test]
    fn test_iterator_methods() {
        let t = setup_transpiler();
        let obj = make_ident_expr("vec");
        let arg = make_ident_expr("x");

        // Test map method
        let result = t.transpile_method_call_refactored(&obj, "map", std::slice::from_ref(&arg));
        assert!(result.is_ok());

        // Test filter method
        let result = t.transpile_method_call_refactored(&obj, "filter", std::slice::from_ref(&arg));
        assert!(result.is_ok());

        // Test reduce method
        let result = t.transpile_method_call_refactored(&obj, "reduce", &[arg]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_hashmap_methods() {
        let t = setup_transpiler();
        let obj = make_ident_expr("map");
        let key = make_string_expr("key");

        // Test get method
        let result = t.transpile_method_call_refactored(&obj, "get", std::slice::from_ref(&key));
        assert!(result.is_ok());

        // Test contains_key method
        let result = t.transpile_method_call_refactored(&obj, "contains_key", &[key]);
        assert!(result.is_ok());

        // Test keys method
        let result = t.transpile_method_call_refactored(&obj, "keys", &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_hashset_methods() {
        let t = setup_transpiler();
        let obj = make_ident_expr("set");
        let val = make_string_expr("value");

        // Test contains method
        let result = t.transpile_method_call_refactored(&obj, "contains", &[val]);
        assert!(result.is_ok());

        // Test union method
        let other = make_ident_expr("other_set");
        let result = t.transpile_method_call_refactored(&obj, "union", &[other]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_collection_mutators() {
        let t = setup_transpiler();
        let obj = make_ident_expr("vec");
        let val = make_string_expr("item");

        // Test push method
        let result = t.transpile_method_call_refactored(&obj, "push", &[val]);
        assert!(result.is_ok());

        // Test pop method
        let result = t.transpile_method_call_refactored(&obj, "pop", &[]);
        assert!(result.is_ok());

        // Test clear method
        let result = t.transpile_method_call_refactored(&obj, "clear", &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_collection_accessors() {
        let t = setup_transpiler();
        let obj = make_ident_expr("vec");

        // Test len method
        let result = t.transpile_method_call_refactored(&obj, "len", &[]);
        assert!(result.is_ok());

        // Test is_empty method
        let result = t.transpile_method_call_refactored(&obj, "is_empty", &[]);
        assert!(result.is_ok());

        // Test first method
        let result = t.transpile_method_call_refactored(&obj, "first", &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_string_methods() {
        let t = setup_transpiler();
        let obj = make_string_expr("hello");

        // Test to_upper method
        let result = t.transpile_method_call_refactored(&obj, "to_upper", &[]);
        assert!(result.is_ok());

        // Test to_lower method
        let result = t.transpile_method_call_refactored(&obj, "to_lower", &[]);
        assert!(result.is_ok());

        // Test length method
        let result = t.transpile_method_call_refactored(&obj, "length", &[]);
        assert!(result.is_ok());

        // Test trim method
        let result = t.transpile_method_call_refactored(&obj, "trim", &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_dataframe_methods() {
        let t = setup_transpiler();
        let obj = make_ident_expr("df");
        let col = make_string_expr("column");

        // Test select method
        let result = t.transpile_method_call_refactored(&obj, "select", &[col]);
        assert!(result.is_ok());

        // Test mean method
        let result = t.transpile_method_call_refactored(&obj, "mean", &[]);
        assert!(result.is_ok());

        // Test sum method
        let result = t.transpile_method_call_refactored(&obj, "sum", &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_default_method() {
        let t = setup_transpiler();
        let obj = make_ident_expr("obj");
        let arg = make_string_expr("arg");

        // Test unknown method falls through to default
        let result = t.transpile_method_call_refactored(&obj, "unknown_method", &[arg]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_iterator_method_implementations() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { vec };
        let args = vec![quote! { |x| x * 2 }];

        // Test transpile_iterator_method directly
        let result = t.transpile_iterator_method(&tokens, "map", &args);
        assert!(result.is_ok());

        // Test with filter
        let result = t.transpile_iterator_method(&tokens, "filter", &[quote! { |x| x > 0 }]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_hashmap_method_implementations() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { hashmap };
        let key_arg = vec![quote! { "key" }];

        // Test transpile_hashmap_method directly
        let result = t.transpile_hashmap_method(&tokens, "get", &key_arg);
        assert!(result.is_ok());

        // Test keys method with no args
        let result = t.transpile_hashmap_method(&tokens, "keys", &[]);
        assert!(result.is_ok());
    }

    #[test]
    fn test_string_method_implementations() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { "hello" };

        // Test various string methods
        let result = t.transpile_string_method(&tokens, "to_upper", &[]);
        assert!(result.is_ok());

        let split_arg = vec![quote! { " " }];
        let result = t.transpile_string_method(&tokens, "split", &split_arg);
        assert!(result.is_ok());
    }

    // Test 1: fold with wrong number of args (error path)
    #[test]
    fn test_fold_wrong_args() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { vec };
        let result = t.transpile_iterator_method(&tokens, "fold", &[quote! { 0 }]);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("fold requires exactly 2 arguments"));
    }

    // Test 2: slice with wrong number of args (error path)
    #[test]
    fn test_slice_wrong_args() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { vec };
        let result = t.transpile_collection_accessor(&tokens, "slice", &[quote! { 0 }]);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("slice requires exactly 2 arguments"));
    }

    // Test 3: replace with wrong number of args (error path)
    #[test]
    fn test_replace_wrong_args() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { "hello" };
        let result = t.transpile_string_method(&tokens, "replace", &[quote! { "h" }]);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("replace requires exactly 2 arguments"));
    }

    // Test 4: hashset union with wrong number of args (error path)
    #[test]
    fn test_hashset_union_wrong_args() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { set };
        let result = t.transpile_hashset_method(&tokens, "union", &[]);
        assert!(result.is_err());
        assert!(result
            .unwrap_err()
            .to_string()
            .contains("union requires exactly 1 argument"));
    }

    // Test 5: iterator reduce method
    #[test]
    fn test_iterator_reduce() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { vec };
        let args = vec![quote! { |a, b| a + b }];
        let result = t.transpile_iterator_method(&tokens, "reduce", &args);
        assert!(result.is_ok());
        assert!(result
            .expect("operation should succeed in test")
            .to_string()
            .contains("reduce"));
    }

    // Test 6: iterator any method
    #[test]
    fn test_iterator_any() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { vec };
        let args = vec![quote! { |x| x > 0 }];
        let result = t.transpile_iterator_method(&tokens, "any", &args);
        assert!(result.is_ok());
        assert!(result
            .expect("operation should succeed in test")
            .to_string()
            .contains("any"));
    }

    // Test 7: iterator all method
    #[test]
    fn test_iterator_all() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { vec };
        let args = vec![quote! { |x| x > 0 }];
        let result = t.transpile_iterator_method(&tokens, "all", &args);
        assert!(result.is_ok());
        assert!(result
            .expect("operation should succeed in test")
            .to_string()
            .contains("all"));
    }

    // Test 8: iterator find method
    #[test]
    fn test_iterator_find() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { vec };
        let args = vec![quote! { |x| x == 5 }];
        let result = t.transpile_iterator_method(&tokens, "find", &args);
        assert!(result.is_ok());
        let output = result
            .expect("operation should succeed in test")
            .to_string();
        assert!(output.contains("find"));
        assert!(output.contains("cloned"));
    }

    // Test 9: hashset intersection method
    #[test]
    fn test_hashset_intersection() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { set };
        let args = vec![quote! { other_set }];
        let result = t.transpile_hashset_method(&tokens, "intersection", &args);
        assert!(result.is_ok());
        assert!(result
            .expect("operation should succeed in test")
            .to_string()
            .contains("intersection"));
    }

    // Test 10: hashset difference method
    #[test]
    fn test_hashset_difference() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { set };
        let args = vec![quote! { other_set }];
        let result = t.transpile_hashset_method(&tokens, "difference", &args);
        assert!(result.is_ok());
        assert!(result
            .expect("operation should succeed in test")
            .to_string()
            .contains("difference"));
    }

    // Test 11: hashset symmetric_difference method
    #[test]
    fn test_hashset_symmetric_difference() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { set };
        let args = vec![quote! { other_set }];
        let result = t.transpile_hashset_method(&tokens, "symmetric_difference", &args);
        assert!(result.is_ok());
        assert!(result
            .expect("operation should succeed in test")
            .to_string()
            .contains("symmetric_difference"));
    }

    // Test 12: collection accessor first method
    #[test]
    fn test_collection_first() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { vec };
        let result = t.transpile_collection_accessor(&tokens, "first", &[]);
        assert!(result.is_ok());
        let output = result
            .expect("operation should succeed in test")
            .to_string();
        assert!(output.contains("first"));
        assert!(output.contains("cloned"));
    }

    // Test 13: collection accessor last method
    #[test]
    fn test_collection_last() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { vec };
        let result = t.transpile_collection_accessor(&tokens, "last", &[]);
        assert!(result.is_ok());
        let output = result
            .expect("operation should succeed in test")
            .to_string();
        assert!(output.contains("last"));
        assert!(output.contains("cloned"));
    }

    // Test 14: collection accessor slice method (valid args)
    #[test]
    fn test_collection_slice_valid() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { vec };
        let args = vec![quote! { 0 }, quote! { 5 }];
        let result = t.transpile_collection_accessor(&tokens, "slice", &args);
        assert!(result.is_ok());
        let output = result
            .expect("operation should succeed in test")
            .to_string();
        assert!(output.contains(".."));
        assert!(output.contains("to_vec"));
    }

    // Test 15: string to_s method
    #[test]
    fn test_string_to_s() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { "hello" };
        let result = t.transpile_string_method(&tokens, "to_s", &[]);
        assert!(result.is_ok());
    }

    // Test 16: string trim method
    #[test]
    fn test_string_trim() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { "  hello  " };
        let result = t.transpile_string_method(&tokens, "trim", &[]);
        assert!(result.is_ok());
        let output = result
            .expect("operation should succeed in test")
            .to_string();
        assert!(output.contains("trim"));
        assert!(output.contains("to_string"));
    }

    // Test 17: string split with no args (whitespace)
    #[test]
    fn test_string_split_whitespace() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { "hello world" };
        let result = t.transpile_string_method(&tokens, "split", &[]);
        assert!(result.is_ok());
        let output = result
            .expect("operation should succeed in test")
            .to_string();
        assert!(output.contains("split_whitespace"));
    }

    // Test 18: string replace (valid args)
    #[test]
    fn test_string_replace_valid() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { "hello" };
        let args = vec![quote! { "h" }, quote! { "H" }];
        let result = t.transpile_string_method(&tokens, "replace", &args);
        assert!(result.is_ok());
        assert!(result
            .expect("operation should succeed in test")
            .to_string()
            .contains("replace"));
    }

    // Test 19: hashmap items method
    #[test]
    fn test_hashmap_items() {
        let t = setup_transpiler();
        let tokens: TokenStream = quote! { map };
        let result = t.transpile_hashmap_method(&tokens, "items", &[]);
        assert!(result.is_ok());
        let output = result
            .expect("operation should succeed in test")
            .to_string();
        assert!(output.contains("iter"));
        assert!(output.contains("clone"));
    }

    // Test 20: DEFECT-011 validation - contains with field access
    #[test]
    fn test_contains_field_access_defect_011() {
        let t = setup_transpiler();
        let obj = make_ident_expr("set");
        let field_access = Expr {
            kind: ExprKind::FieldAccess {
                object: Box::new(make_ident_expr("obj")),
                field: "name".to_string(),
            },
            span: Default::default(),
            attributes: Vec::new(),
            leading_comments: vec![],
            trailing_comment: None,
        };
        let result = t.transpile_method_call_refactored(&obj, "contains", &[field_access]);
        assert!(result.is_ok());
        // Verify that field access argument is wrapped with &
        let output = result
            .expect("operation should succeed in test")
            .to_string();
        assert!(output.contains('&'));
    }
}