cargo-mate 1.8.0

Rust development companion that enhances cargo with intelligent workflows, state management, performance optimization, and comprehensive project monitoring.
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
use super::{Tool, Result, ToolError, common_options, parse_output_format, OutputFormat};
use clap::{Arg, ArgMatches, Command};
use colored::*;
use std::path::Path;
use std::fs;
use syn::{parse_file, FnArg, Pat, ReturnType, visit::Visit};
use quote::ToTokens;
#[derive(Debug, Clone)]
pub struct TestGenTool;
#[derive(Debug)]
struct FunctionInfo {
    name: String,
    params: Vec<ParamInfo>,
    return_type: Option<String>,
    is_async: bool,
    visibility: String,
}
#[derive(Debug)]
struct ParamInfo {
    name: String,
    ty: String,
    is_reference: bool,
}
#[derive(Debug, serde::Serialize)]
struct GeneratedTest {
    function_name: String,
    test_name: String,
    test_code: String,
}
impl TestGenTool {
    pub fn new() -> Self {
        Self
    }
    fn parse_rust_file(&self, file_path: &Path) -> Result<Vec<FunctionInfo>> {
        let content = fs::read_to_string(file_path)
            .map_err(|e| ToolError::ExecutionFailed(
                format!("Failed to read file: {}", e),
            ))?;
        let syntax = parse_file(&content)
            .map_err(|e| ToolError::ExecutionFailed(
                format!("Failed to parse Rust code: {}", e),
            ))?;
        let mut visitor = FunctionVisitor::new();
        visitor.visit_file(&syntax);
        Ok(visitor.functions)
    }
    fn generate_test_for_function(
        &self,
        func: &FunctionInfo,
        test_type: &str,
    ) -> GeneratedTest {
        let test_name = format!("test_{}_{}", func.name, test_type);
        let test_code = match test_type {
            "unit" => self.generate_unit_test(func),
            "integration" => self.generate_integration_test(func),
            "property" => self.generate_property_test(func),
            _ => self.generate_unit_test(func),
        };
        GeneratedTest {
            function_name: func.name.clone(),
            test_name,
            test_code,
        }
    }
    fn generate_unit_test(&self, func: &FunctionInfo) -> String {
        let mut code = format!("/// Unit test for `{}`\n", func.name);
        code.push_str("#[test]\n");
        if func.is_async {
            code.push_str("#[tokio::test]\n");
        }
        code.push_str(
            &format!(
                "fn {}() {{\n", self.snake_to_pascal(& format!("test_{}", func.name))
            ),
        );
        for param in &func.params {
            let mock_value = self.generate_mock_value(&param.ty);
            code.push_str(&format!("    let {} = {};\n", param.name, mock_value));
        }
        let params_str = func
            .params
            .iter()
            .map(|p| {
                if p.is_reference { format!("&{}", p.name) } else { p.name.clone() }
            })
            .collect::<Vec<_>>()
            .join(", ");
        if func.is_async {
            code.push_str(
                &format!("    let result = {}({}).await;\n", func.name, params_str),
            );
        } else {
            code.push_str(&format!("    let result = {}({});\n", func.name, params_str));
        }
        if let Some(return_type) = &func.return_type {
            if return_type.contains("Result") {
                code.push_str("    assert!(result.is_ok());\n");
            } else if return_type.contains("Option") {
                code.push_str("    assert!(result.is_some());\n");
            } else if return_type == "bool" {
                code.push_str("    assert!(result);\n");
            } else if return_type.contains("Vec") || return_type.contains("HashMap") {
                code.push_str("    assert!(!result.is_empty());\n");
            } else {
                code.push_str("    // Add your assertions here\n");
                code.push_str("    assert!(true); // Placeholder assertion\n");
            }
        } else {
            code.push_str(
                "    // Function returns nothing - add your test logic here\n",
            );
            code.push_str("    assert!(true); // Placeholder assertion\n");
        }
        code.push_str("}\n");
        code
    }
    fn generate_integration_test(&self, func: &FunctionInfo) -> String {
        let mut code = format!("/// Integration test for `{}`\n", func.name);
        code.push_str("#[test]\n");
        if func.is_async {
            code.push_str("#[tokio::test]\n");
        }
        code.push_str(
            &format!(
                "fn {}() {{\n", self.snake_to_pascal(& format!("integration_test_{}",
                func.name))
            ),
        );
        code.push_str("    // Setup test environment\n");
        for param in &func.params {
            let mock_value = self.generate_integration_mock_value(&param.ty);
            code.push_str(&format!("    let {} = {};\n", param.name, mock_value));
        }
        let params_str = func
            .params
            .iter()
            .map(|p| {
                if p.is_reference { format!("&{}", p.name) } else { p.name.clone() }
            })
            .collect::<Vec<_>>()
            .join(", ");
        if func.is_async {
            code.push_str(
                &format!("    let result = {}({}).await;\n", func.name, params_str),
            );
        } else {
            code.push_str(&format!("    let result = {}({});\n", func.name, params_str));
        }
        code.push_str("    // Verify the result\n");
        if let Some(return_type) = &func.return_type {
            if return_type.contains("Result") {
                code.push_str("    match result {\n");
                code.push_str("        Ok(value) => {\n");
                code.push_str("            // Add your success case assertions here\n");
                code.push_str("            assert!(true); // Placeholder\n");
                code.push_str("        }\n");
                code.push_str("        Err(e) => {\n");
                code.push_str(
                    "            panic!(\"Integration test failed: {}\", e);\n",
                );
                code.push_str("        }\n");
                code.push_str("    }\n");
            } else {
                code.push_str("    // Add your integration test assertions here\n");
                code.push_str("    assert!(true); // Placeholder assertion\n");
            }
        }
        code.push_str("}\n");
        code
    }
    fn generate_property_test(&self, func: &FunctionInfo) -> String {
        let mut code = format!("/// Property-based test for `{}`\n", func.name);
        code.push_str("#[cfg(test)]\n");
        code.push_str("mod property_tests {\n");
        code.push_str("    use super::*;\n");
        code.push_str("    use proptest::prelude::*;\n\n");
        let has_numeric_params = func
            .params
            .iter()
            .any(|p| {
                let ty = p.ty.to_lowercase();
                ty.contains("i32") || ty.contains("i64") || ty.contains("u32")
                    || ty.contains("u64") || ty.contains("f32") || ty.contains("f64")
                    || ty.contains("usize") || ty.contains("isize")
            });
        if has_numeric_params {
            code.push_str(&format!("    proptest! {{\n"));
            code.push_str(&format!("        #[test]\n"));
            code.push_str(
                &format!(
                    "        fn {}({}) {{\n", func.name, self.generate_proptest_params(&
                    func.params)
                ),
            );
            let params_str = func
                .params
                .iter()
                .map(|p| {
                    if p.is_reference { format!("&{}", p.name) } else { p.name.clone() }
                })
                .collect::<Vec<_>>()
                .join(", ");
            if func.is_async {
                code.push_str(
                    &format!(
                        "            let result = {}({}).await;\n", func.name, params_str
                    ),
                );
            } else {
                code.push_str(
                    &format!("            let result = {}({});\n", func.name, params_str),
                );
            }
            if let Some(return_type) = &func.return_type {
                if return_type.contains("Result") {
                    code.push_str("            prop_assert!(result.is_ok());\n");
                } else if return_type.contains("bool") {
                    code.push_str(
                        "            // Add property-based assertions for boolean results\n",
                    );
                    code.push_str("            prop_assert!(true); // Placeholder\n");
                } else {
                    code.push_str(
                        "            // Add your property-based assertions here\n",
                    );
                    code.push_str("            prop_assert!(true); // Placeholder\n");
                }
            }
            code.push_str("        }\n");
            code.push_str("    }\n");
        } else {
            code.push_str(
                &format!(
                    "    // Property-based testing not applicable for this function\n"
                ),
            );
            code.push_str(&format!("    // Function doesn't have numeric parameters\n"));
        }
        code.push_str("}\n");
        code
    }
    fn generate_mock_value(&self, ty: &str) -> String {
        let ty_lower = ty.to_lowercase();
        if ty_lower.contains("string") {
            "\"test_value\".to_string()".to_string()
        } else if ty_lower.contains("i32") {
            "42".to_string()
        } else if ty_lower.contains("i64") {
            "42i64".to_string()
        } else if ty_lower.contains("u32") {
            "42u32".to_string()
        } else if ty_lower.contains("u64") {
            "42u64".to_string()
        } else if ty_lower.contains("f32") {
            "3.14f32".to_string()
        } else if ty_lower.contains("f64") {
            "3.14".to_string()
        } else if ty_lower.contains("bool") {
            "true".to_string()
        } else if ty_lower.contains("vec") {
            "vec![1, 2, 3]".to_string()
        } else if ty_lower.contains("hashmap") {
            "HashMap::new()".to_string()
        } else if ty_lower.contains("option") {
            "Some(\"test\".to_string())".to_string()
        } else if ty_lower.contains("result") {
            "Ok(\"success\".to_string())".to_string()
        } else {
            format!("{}::default()", ty)
        }
    }
    fn generate_integration_mock_value(&self, ty: &str) -> String {
        let ty_lower = ty.to_lowercase();
        if ty_lower.contains("string") {
            "\"integration_test_value\".to_string()".to_string()
        } else if ty_lower.contains("i32") {
            "100".to_string()
        } else if ty_lower.contains("i64") {
            "1000i64".to_string()
        } else if ty_lower.contains("u32") {
            "100u32".to_string()
        } else if ty_lower.contains("u64") {
            "1000u64".to_string()
        } else if ty_lower.contains("f32") {
            "1.414f32".to_string()
        } else if ty_lower.contains("f64") {
            "2.718".to_string()
        } else if ty_lower.contains("bool") {
            "false".to_string()
        } else if ty_lower.contains("vec") {
            "vec![10, 20, 30, 40, 50]".to_string()
        } else if ty_lower.contains("hashmap") {
            "{\n        let mut map = HashMap::new();\n        map.insert(\"key1\".to_string(), \"value1\".to_string());\n        map\n    }"
                .to_string()
        } else if ty_lower.contains("option") {
            "None".to_string()
        } else if ty_lower.contains("result") {
            "Err(\"integration test error\".to_string())".to_string()
        } else {
            format!("{}::default()", ty)
        }
    }
    fn generate_proptest_params(&self, params: &[ParamInfo]) -> String {
        params
            .iter()
            .filter_map(|p| {
                let ty_lower = p.ty.to_lowercase();
                if ty_lower.contains("i32") {
                    Some(format!("{} in 0..1000i32", p.name))
                } else if ty_lower.contains("i64") {
                    Some(format!("{} in 0..10000i64", p.name))
                } else if ty_lower.contains("u32") {
                    Some(format!("{} in 0..1000u32", p.name))
                } else if ty_lower.contains("u64") {
                    Some(format!("{} in 0..10000u64", p.name))
                } else if ty_lower.contains("f32") {
                    Some(format!("{} in 0.0..1000.0f32", p.name))
                } else if ty_lower.contains("f64") {
                    Some(format!("{} in 0.0..1000.0", p.name))
                } else {
                    None
                }
            })
            .collect::<Vec<_>>()
            .join(", ")
    }
    fn snake_to_pascal(&self, snake_case: &str) -> String {
        snake_case
            .split('_')
            .map(|word| {
                let mut chars = word.chars();
                match chars.next() {
                    None => String::new(),
                    Some(first) => {
                        first.to_uppercase().chain(chars.as_str().chars()).collect()
                    }
                }
            })
            .collect()
    }
    fn display_generated_tests(&self, tests: &[GeneratedTest], format: OutputFormat) {
        match format {
            OutputFormat::Json => {
                println!("{}", serde_json::to_string_pretty(tests).unwrap());
            }
            OutputFormat::Table => {
                println!("{:<25} {:<30}", "Function", "Generated Test");
                println!("{}", "โ”€".repeat(55));
                for test in tests {
                    println!("{:<25} {:<30}", test.function_name, test.test_name);
                }
            }
            OutputFormat::Human => {
                println!("{}", "๐Ÿงช Generated Tests".bold().blue());
                println!("{}", "โ•".repeat(50).blue());
                for test in tests {
                    println!(
                        "๐Ÿ“ {} -> {}", test.function_name.green(), test.test_name
                        .cyan()
                    );
                    println!("```rust");
                    println!("{}", test.test_code.trim());
                    println!("```");
                    println!();
                }
                println!("๐Ÿ’ก {} tests generated successfully!", tests.len());
                println!(
                    "๐Ÿ“ Add these tests to your test module or create a new test file"
                );
            }
        }
    }
}
struct FunctionVisitor {
    functions: Vec<FunctionInfo>,
}
impl FunctionVisitor {
    fn new() -> Self {
        Self { functions: Vec::new() }
    }
}
impl<'ast> Visit<'ast> for FunctionVisitor {
    fn visit_item_fn(&mut self, node: &'ast syn::ItemFn) {
        let fn_name = node.sig.ident.to_string();
        let is_async = node.sig.asyncness.is_some();
        let params = node
            .sig
            .inputs
            .iter()
            .filter_map(|arg| {
                match arg {
                    FnArg::Receiver(_) => None,
                    FnArg::Typed(pat_type) => {
                        if let Pat::Ident(pat_ident) = &*pat_type.pat {
                            let param_name = pat_ident.ident.to_string();
                            let param_type = pat_type.ty.to_token_stream().to_string();
                            let is_reference = param_type.contains('&');
                            Some(ParamInfo {
                                name: param_name,
                                ty: param_type
                                    .replace('&', "")
                                    .replace("mut", "")
                                    .trim()
                                    .to_string(),
                                is_reference,
                            })
                        } else {
                            None
                        }
                    }
                }
            })
            .collect();
        let return_type = match &node.sig.output {
            ReturnType::Default => None,
            ReturnType::Type(_, ty) => Some(ty.to_token_stream().to_string()),
        };
        let visibility = match &node.vis {
            syn::Visibility::Public(_) => "public".to_string(),
            _ => "private".to_string(),
        };
        self.functions
            .push(FunctionInfo {
                name: fn_name,
                params,
                return_type,
                is_async,
                visibility,
            });
    }
}
impl Tool for TestGenTool {
    fn name(&self) -> &'static str {
        "test-gen"
    }
    fn description(&self) -> &'static str {
        "Generate test boilerplate from functions"
    }
    fn command(&self) -> Command {
        Command::new(self.name())
            .about(self.description())
            .long_about(
                "Parse Rust AST to find functions and generate test templates with edge cases",
            )
            .args(
                &[
                    Arg::new("file")
                        .long("file")
                        .short('f')
                        .help("Path to Rust source file")
                        .required(true),
                    Arg::new("module")
                        .long("module")
                        .short('m')
                        .help("Module name for generated tests"),
                    Arg::new("type")
                        .long("type")
                        .short('t')
                        .help("Test type to generate")
                        .value_parser(["unit", "integration", "property"])
                        .default_value("unit"),
                    Arg::new("output")
                        .long("output")
                        .short('o')
                        .help("Output file path for generated tests"),
                ],
            )
            .args(&common_options())
    }
    fn execute(&self, matches: &ArgMatches) -> Result<()> {
        let file_path = matches.get_one::<String>("file").unwrap();
        let test_type = matches.get_one::<String>("type").unwrap();
        let module_name = matches.get_one::<String>("module");
        let output_file = matches.get_one::<String>("output");
        let output_format = parse_output_format(matches);
        let verbose = matches.get_flag("verbose");
        let path = Path::new(file_path);
        if !path.exists() {
            return Err(
                ToolError::ExecutionFailed(format!("File not found: {}", file_path)),
            );
        }
        println!("๐Ÿงช {} - Generating tests", "CargoMate TestGen".bold().blue());
        println!(
            "   File: {} | Type: {} | Format: {:?}", file_path, test_type, output_format
        );
        let functions = self.parse_rust_file(path)?;
        if functions.is_empty() {
            println!("โš ๏ธ  No functions found in {}", file_path);
            return Ok(());
        }
        if verbose {
            println!("๐Ÿ“Š Found {} functions", functions.len());
        }
        let mut generated_tests = Vec::new();
        for func in &functions {
            if verbose {
                println!("   Generating {} test for: {}", test_type, func.name);
            }
            let test = self.generate_test_for_function(func, test_type);
            generated_tests.push(test);
        }
        if let Some(output_path) = output_file {
            let mut output_content = String::new();
            if let Some(mod_name) = module_name {
                output_content
                    .push_str(
                        &format!(
                            "#[cfg(test)]\nmod {} {{\n    use super::*;\n\n", mod_name
                        ),
                    );
            } else {
                output_content
                    .push_str(
                        "#[cfg(test)]\nmod generated_tests {\n    use super::*;\n\n",
                    );
            }
            for test in &generated_tests {
                output_content.push_str(&test.test_code);
                output_content.push_str("\n");
            }
            output_content.push_str("}\n");
            fs::write(output_path, output_content)
                .map_err(|e| ToolError::ExecutionFailed(
                    format!("Failed to write output file: {}", e),
                ))?;
            println!("๐Ÿ’พ Tests saved to: {}", output_path.cyan());
        } else {
            self.display_generated_tests(&generated_tests, output_format);
        }
        Ok(())
    }
}
impl Default for TestGenTool {
    fn default() -> Self {
        Self::new()
    }
}