pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
//! TDD Tests for Accurate Complexity Analysis
//!
//! Sprint 63: Fixes complexity false positives using industry-standard algorithms
//! Following Toyota Way TDD principles with comprehensive coverage

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod accurate_complexity_tests {
    use crate::services::accurate_complexity_analyzer::AccurateComplexityAnalyzer;
    use std::fs;
    use tempfile::TempDir;

    /// Test cyclomatic complexity calculation matches industry standard
    #[tokio::test]
    async fn test_cyclomatic_complexity_calculation() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");

        // Function with if, else, match, loop = complexity 5
        fs::write(
            &test_file,
            r#"
            fn complex_function(x: i32) -> i32 {
                if x > 0 {                      // +1
                    if x > 10 {                 // +1
                        20
                    } else {                    
                        10
                    }
                } else if x < -10 {            // +1
                    match x {                   // +1 for match
                        -20 => 1,
                        -30 => 2,
                        _ => 3,
                    }
                } else {
                    for i in 0..5 {             // +1
                        println!("{}", i);
                    }
                    0
                }
            }
        "#,
        )
        .unwrap();

        let analyzer = AccurateComplexityAnalyzer::new();
        let result = analyzer.analyze_file(&test_file).await.unwrap();

        assert_eq!(result.functions.len(), 1);
        assert_eq!(
            result.functions[0].cyclomatic_complexity, 6,
            "Expected cyclomatic complexity of 6 (1 base + 5 decision points: if, nested if, else if, match, for loop)"
        );
    }

    /// Test cognitive complexity with nesting weights
    #[tokio::test]
    async fn test_cognitive_complexity_calculation() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");

        // Nested structures increase cognitive complexity
        fs::write(
            &test_file,
            r#"
            fn nested_function(x: i32) -> i32 {
                if x > 0 {                      // +1 (nesting 0)
                    for i in 0..x {             // +2 (nesting 1)
                        if i % 2 == 0 {         // +3 (nesting 2)
                            if i > 5 {          // +4 (nesting 3)
                                return i;
                            }
                        }
                    }
                }
                0
            }
        "#,
        )
        .unwrap();

        let analyzer = AccurateComplexityAnalyzer::new();
        let result = analyzer.analyze_file(&test_file).await.unwrap();

        assert_eq!(result.functions.len(), 1);
        assert!(
            result.functions[0].cognitive_complexity >= 4,
            "Cognitive complexity should be ≥4 for nested function with 4 control structures, got: {}",
            result.functions[0].cognitive_complexity
        );
    }

    /// Test that simple functions have low complexity
    #[tokio::test]
    async fn test_simple_function_low_complexity() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");

        fs::write(
            &test_file,
            r#"
            fn simple_add(a: i32, b: i32) -> i32 {
                a + b
            }
            
            fn simple_multiply(x: i32) -> i32 {
                x * 2
            }
        "#,
        )
        .unwrap();

        let analyzer = AccurateComplexityAnalyzer::new();
        let result = analyzer.analyze_file(&test_file).await.unwrap();

        assert_eq!(result.functions.len(), 2);
        for func in &result.functions {
            assert_eq!(
                func.cyclomatic_complexity, 1,
                "Simple functions should have complexity of 1"
            );
            assert_eq!(
                func.cognitive_complexity, 0,
                "Simple functions should have cognitive complexity of 0"
            );
        }
    }

    /// Test excluding test files
    #[tokio::test]
    async fn test_exclude_test_files() {
        let temp_dir = TempDir::new().unwrap();

        // Create test file
        let test_file = temp_dir.path().join("lib_test.rs");
        fs::write(
            &test_file,
            r#"
            #[test]
            fn test_something() {
                // Complex test code
                for i in 0..10 {
                    if i > 5 {
                        assert!(true);
                    }
                }
            }
        "#,
        )
        .unwrap();

        // Create non-test file
        let src_file = temp_dir.path().join("lib.rs");
        fs::write(
            &src_file,
            r#"
            pub fn actual_function() -> i32 {
                42
            }
        "#,
        )
        .unwrap();

        let analyzer = AccurateComplexityAnalyzer::new().exclude_tests(true);

        let project_result = analyzer.analyze_project(temp_dir.path()).await.unwrap();

        // Should only analyze lib.rs, not lib_test.rs
        assert_eq!(project_result.files_analyzed, 1, "Should exclude test file");
        assert!(project_result
            .file_metrics
            .iter()
            .any(|f| f.file_path.ends_with("lib.rs")));
        assert!(!project_result
            .file_metrics
            .iter()
            .any(|f| f.file_path.ends_with("lib_test.rs")));
    }

    /// Test annotation support for suppressing complexity warnings
    #[tokio::test]
    async fn test_annotation_support() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");

        fs::write(
            &test_file,
            r#"
            #[allow(complex_function)]
            fn intentionally_complex(x: i32) -> i32 {
                // Very complex function that should be ignored
                let mut result = 0;
                for i in 0..x {
                    if i % 2 == 0 {
                        for j in 0..i {
                            if j % 3 == 0 {
                                result += j;
                            }
                        }
                    }
                }
                result
            }
            
            fn normal_complex(x: i32) -> i32 {
                // This one should be reported
                let mut result = 0;
                for i in 0..x {
                    if i % 2 == 0 {
                        result += i;
                    }
                }
                result
            }
        "#,
        )
        .unwrap();

        let analyzer = AccurateComplexityAnalyzer::new().respect_annotations(true);
        let result = analyzer.analyze_file(&test_file).await.unwrap();

        // Should have both functions
        assert_eq!(result.functions.len(), 2);

        // Find annotated function
        let annotated = result
            .functions
            .iter()
            .find(|f| f.name.contains("intentionally_complex"))
            .unwrap();
        assert!(
            annotated.suppressed,
            "Annotated function should be marked as suppressed"
        );

        // Find normal function
        let normal = result
            .functions
            .iter()
            .find(|f| f.name.contains("normal_complex"))
            .unwrap();
        assert!(
            !normal.suppressed,
            "Normal function should not be suppressed"
        );
    }

    /// Test match expressions with multiple arms
    #[tokio::test]
    async fn test_match_complexity() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");

        fs::write(
            &test_file,
            r#"
            fn match_function(x: Option<i32>) -> i32 {
                match x {                       // +1 for match
                    Some(n) if n > 0 => n * 2, // +1 for guard
                    Some(n) if n < 0 => -n,    // +1 for guard
                    Some(0) => 0,
                    None => -1,
                }
            }
        "#,
        )
        .unwrap();

        let analyzer = AccurateComplexityAnalyzer::new();
        let result = analyzer.analyze_file(&test_file).await.unwrap();

        assert_eq!(
            result.functions[0].cyclomatic_complexity, 4,
            "Match with 2 guards should have complexity 4: base(1) + match(1) + 2 guards(2) = 4"
        );
    }

    /// Test boolean operators (&&, ||) add to complexity
    #[tokio::test]
    async fn test_boolean_operator_complexity() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");

        fs::write(
            &test_file,
            r#"
            fn boolean_logic(a: bool, b: bool, c: bool) -> bool {
                if a && b || c {    // +3 (if + && + ||)
                    true
                } else {
                    false
                }
            }
        "#,
        )
        .unwrap();

        let analyzer = AccurateComplexityAnalyzer::new();
        let result = analyzer.analyze_file(&test_file).await.unwrap();

        assert_eq!(
            result.functions[0].cyclomatic_complexity, 4,
            "Boolean operators should add to complexity: base(1) + if(1) + &&(1) + ||(1) = 4"
        );
    }

    /// Test ? operator adds to complexity
    #[tokio::test]
    async fn test_question_mark_operator() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");

        fs::write(
            &test_file,
            r#"
            fn try_function() -> Result<i32, String> {
                let x = some_operation()?;  // +1
                let y = another_op()?;       // +1
                Ok(x + y)
            }
            
            fn some_operation() -> Result<i32, String> {
                Ok(42)
            }
            
            fn another_op() -> Result<i32, String> {
                Ok(10)
            }
        "#,
        )
        .unwrap();

        let analyzer = AccurateComplexityAnalyzer::new();
        let result = analyzer.analyze_file(&test_file).await.unwrap();

        let try_fn = result
            .functions
            .iter()
            .find(|f| f.name.contains("try_function"))
            .unwrap();
        assert_eq!(
            try_fn.cyclomatic_complexity, 3,
            "? operator should add 1 to complexity per use"
        );
    }

    /// Test while and loop constructs
    #[tokio::test]
    async fn test_loop_complexity() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");

        fs::write(
            &test_file,
            r#"
            fn loop_function(mut x: i32) -> i32 {
                while x > 0 {           // +1
                    x -= 1;
                }
                
                loop {                  // +1
                    if x < -10 {        // +1
                        break;
                    }
                    x -= 1;
                }
                
                x
            }
        "#,
        )
        .unwrap();

        let analyzer = AccurateComplexityAnalyzer::new();
        let result = analyzer.analyze_file(&test_file).await.unwrap();

        assert_eq!(
            result.functions[0].cyclomatic_complexity, 4,
            "while and loop should each add 1 to complexity"
        );
    }

    /// Test recursion detection for cognitive complexity
    #[tokio::test]
    async fn test_recursion_cognitive_complexity() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");

        fs::write(
            &test_file,
            r#"
            fn factorial(n: u32) -> u32 {
                if n <= 1 {
                    1
                } else {
                    n * factorial(n - 1)  // Recursive call adds cognitive weight
                }
            }
        "#,
        )
        .unwrap();

        let analyzer = AccurateComplexityAnalyzer::new();
        let result = analyzer.analyze_file(&test_file).await.unwrap();

        assert!(
            result.functions[0].cognitive_complexity >= 2,
            "Recursive functions should have cognitive complexity ≥2 (if + recursion), got: {}",
            result.functions[0].cognitive_complexity
        );
    }
}