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
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
// Unit tests for Swift source analysis and complexity metrics

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    const SIMPLE_SWIFT_SOURCE: &str = r#"
import Foundation

print("Hello, World!")
"#;

    const SWIFT_SOURCE_WITH_FUNCTIONS: &str = r#"
import Foundation

// Simple function
func printHello() {
    print("Hello World!")
}

// Function with parameters
func calculateScore(value: Int, isBonus: Bool) -> Int {
    if value < 0 {
        return 0
    }

    if isBonus {
        if value > 100 {
            return value * 2
        } else {
            return value + 50
        }
    }

    return value
}

// Function with loop
func sumArray(arr: [Int]) -> Int {
    var sum = 0
    for num in arr {
        if num > 0 {
            sum += num
        }
    }
    return sum
}
"#;

    const SWIFT_SOURCE_WITH_CLASSES: &str = r#"
import Foundation

class Calculator {
    func add(_ a: Int, _ b: Int) -> Int {
        return a + b
    }

    func multiply(_ a: Int, _ b: Int) -> Int {
        return a * b
    }

    private func complexOperation(_ x: Int) -> Int {
        if x < 0 {
            return -1
        } else if x == 0 {
            return 0
        }
        return x * x
    }
}
"#;

    #[test]
    fn test_simple_swift_source_analysis() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("simple.swift"));
        let items = analyzer
            .analyze_swift_source(SIMPLE_SWIFT_SOURCE)
            .expect("Should parse simple Swift source");

        // Simple script may not have functions
        assert!(
            items.is_empty() || !items.is_empty(),
            "Should handle simple Swift source"
        );
    }

    #[test]
    fn test_swift_functions_analysis() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("functions.swift"));
        let items = analyzer
            .analyze_swift_source(SWIFT_SOURCE_WITH_FUNCTIONS)
            .expect("Should parse Swift source with functions");

        let function_items: Vec<_> = items
            .iter()
            .filter(|item| matches!(item, AstItem::Function { .. }))
            .collect();

        assert!(
            function_items.len() >= 3,
            "Should extract printHello, calculateScore, and sumArray functions"
        );

        // Check function names
        let function_names: Vec<_> = function_items
            .iter()
            .filter_map(|item| match item {
                AstItem::Function { name, .. } => Some(name.as_str()),
                _ => None,
            })
            .collect();

        assert!(function_names
            .iter()
            .any(|&name| name.contains("printHello")));
        assert!(function_names
            .iter()
            .any(|&name| name.contains("calculateScore")));
        assert!(function_names.iter().any(|&name| name.contains("sumArray")));
    }

    #[test]
    fn test_swift_class_analysis() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("classes.swift"));
        let items = analyzer
            .analyze_swift_source(SWIFT_SOURCE_WITH_CLASSES)
            .expect("Should parse Swift source with classes");

        let class_items: Vec<_> = items
            .iter()
            .filter(|item| matches!(item, AstItem::Struct { .. }))
            .collect();

        assert!(!class_items.is_empty(), "Should extract Calculator class");

        let method_items: Vec<_> = items
            .iter()
            .filter(|item| matches!(item, AstItem::Function { .. }))
            .collect();

        assert!(
            method_items.len() >= 3,
            "Should extract add, multiply, and complexOperation methods"
        );
    }

    #[test]
    fn test_swift_complexity_analysis() {
        let mut analyzer = SwiftComplexityAnalyzer::new();
        let (cyclomatic, cognitive) = analyzer
            .analyze_complexity(SWIFT_SOURCE_WITH_FUNCTIONS)
            .expect("Should analyze Swift complexity");

        assert!(
            cyclomatic >= 3,
            "Source with conditionals should have cyclomatic complexity >= 3"
        );
        assert!(
            cognitive >= 3,
            "Source with conditionals should have cognitive complexity >= 3"
        );
    }

    #[test]
    fn test_empty_swift_source() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("empty.swift"));
        let items = analyzer
            .analyze_swift_source("")
            .expect("Should handle empty source");

        assert!(items.is_empty(), "Empty source should produce no AST items");
    }

    // Additional unit tests for coverage

    #[test]
    fn test_swift_analyzer_new() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("test.swift"));
        assert_eq!(analyzer.source_name, "test");
        assert_eq!(analyzer.function_count, 0);
        assert_eq!(analyzer.class_count, 0);
        assert_eq!(analyzer.method_count, 0);
    }

    #[test]
    fn test_swift_analyzer_source_name_extraction() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("/path/to/ViewController.swift"));
        assert_eq!(analyzer.source_name, "ViewController");
    }

    #[test]
    fn test_swift_analyzer_qualified_name() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("api.swift"));
        let qualified = analyzer.get_qualified_name("handleRequest");
        assert_eq!(qualified, "api::handleRequest");
    }

    #[test]
    fn test_swift_analyzer_extract_function_name() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("test.swift"));

        assert_eq!(
            analyzer.extract_function_name("func myFunc() {"),
            Some("myFunc".to_string())
        );
        assert_eq!(
            analyzer.extract_function_name("func compute(_ a: Int, _ b: Int) -> Int {"),
            Some("compute".to_string())
        );
        assert_eq!(analyzer.extract_function_name("not a function"), None);
    }

    #[test]
    fn test_swift_analyzer_extract_class_name() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("test.swift"));

        assert_eq!(
            analyzer.extract_class_name("class MyClass {"),
            Some("MyClass".to_string())
        );
        assert_eq!(
            analyzer.extract_class_name("class BaseController: UIViewController {"),
            Some("BaseController".to_string())
        );
        assert_eq!(
            analyzer.extract_class_name("struct Point {"),
            Some("Point".to_string())
        );
        assert_eq!(analyzer.extract_class_name("not a class"), None);
    }

    #[test]
    fn test_swift_analyzer_extract_visibility() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("test.swift"));

        assert_eq!(
            analyzer.extract_visibility("private func helper()"),
            "private"
        );
        assert_eq!(analyzer.extract_visibility("public func api()"), "public");
        assert_eq!(
            analyzer.extract_visibility("internal func internal_fn()"),
            "internal"
        );
        assert_eq!(analyzer.extract_visibility("func default_fn()"), "internal");
    }

    #[test]
    fn test_swift_complexity_analyzer_new() {
        let analyzer = SwiftComplexityAnalyzer::new();
        assert_eq!(analyzer.cyclomatic_complexity, 0);
        assert_eq!(analyzer.cognitive_complexity, 0);
    }

    #[test]
    fn test_swift_complexity_analyzer_default() {
        let analyzer = SwiftComplexityAnalyzer::default();
        assert_eq!(analyzer.cyclomatic_complexity, 0);
        assert_eq!(analyzer.cognitive_complexity, 0);
    }

    #[test]
    fn test_swift_complexity_simple_code() {
        let mut analyzer = SwiftComplexityAnalyzer::new();
        let code = r#"
func hello() {
    print("Hello")
}
"#;
        let (cyclomatic, cognitive) = analyzer.analyze_complexity(code).unwrap();
        assert_eq!(cyclomatic, 1);
        assert_eq!(cognitive, 1);
    }

    #[test]
    fn test_swift_complexity_with_if() {
        let mut analyzer = SwiftComplexityAnalyzer::new();
        let code = r#"
if x > 0 {
    print("positive")
}
"#;
        let (cyclomatic, cognitive) = analyzer.analyze_complexity(code).unwrap();
        assert!(cyclomatic >= 2);
        assert!(cognitive >= 2);
    }

    #[test]
    fn test_swift_complexity_with_loops() {
        let mut analyzer = SwiftComplexityAnalyzer::new();
        let code = r#"
for i in 0..<10 {
    while j < i {
        for item in arr {
            print(item)
        }
    }
}
"#;
        let (cyclomatic, cognitive) = analyzer.analyze_complexity(code).unwrap();
        assert!(cyclomatic >= 4);
        assert!(cognitive >= 4);
    }

    #[test]
    fn test_swift_complexity_with_switch() {
        let mut analyzer = SwiftComplexityAnalyzer::new();
        let code = r#"
switch value {
case 1:
    print("one")
case 2:
    print("two")
case 3:
    print("three")
}
"#;
        let (cyclomatic, cognitive) = analyzer.analyze_complexity(code).unwrap();
        assert!(cyclomatic >= 4);
        assert!(cognitive >= 4);
    }

    #[test]
    fn test_swift_complexity_with_guard() {
        let mut analyzer = SwiftComplexityAnalyzer::new();
        let code = r#"
guard let value = optional else {
    return
}
"#;
        let (cyclomatic, cognitive) = analyzer.analyze_complexity(code).unwrap();
        assert!(cyclomatic >= 2);
        assert!(cognitive >= 2);
    }

    #[test]
    fn test_swift_complexity_with_ternary() {
        let mut analyzer = SwiftComplexityAnalyzer::new();
        let code = r#"
let result = x > 0 ? "positive" : "non-positive"
let value = a ? b : c
"#;
        let (cyclomatic, cognitive) = analyzer.analyze_complexity(code).unwrap();
        assert!(cyclomatic >= 3);
        assert!(cognitive >= 3);
    }

    #[test]
    fn test_swift_complexity_with_else_if() {
        let mut analyzer = SwiftComplexityAnalyzer::new();
        let code = r#"
if x > 10 {
    print("big")
} else if x > 5 {
    print("medium")
} else if x > 0 {
    print("small")
} else {
    print("zero or negative")
}
"#;
        let (cyclomatic, cognitive) = analyzer.analyze_complexity(code).unwrap();
        assert!(cyclomatic >= 4);
        assert!(cognitive >= 4);
    }

    #[test]
    fn test_swift_whitespace_handling() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("test.swift"));
        let items = analyzer
            .analyze_swift_source("   \n\n  \t  \n  ")
            .expect("Should handle whitespace-only");

        assert!(items.is_empty());
    }

    #[test]
    fn test_swift_struct_parsing() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("test.swift"));
        let code = r#"
struct Point {
    var x: Double
    var y: Double

    func distance() -> Double {
        return sqrt(x*x + y*y)
    }
}
"#;
        let items = analyzer.analyze_swift_source(code).expect("Should parse");

        let structs: Vec<_> = items
            .iter()
            .filter(|i| matches!(i, AstItem::Struct { .. }))
            .collect();
        assert!(!structs.is_empty());
    }

    #[test]
    fn test_swift_multiple_classes() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("models.swift"));
        let code = r#"
class User {
    func getName() -> String { return name }
}

class Order {
    func getTotal() -> Double { return total }
}

struct Product {
    func getPrice() -> Double { return price }
}
"#;
        let items = analyzer.analyze_swift_source(code).expect("Should parse");

        let types: Vec<_> = items
            .iter()
            .filter(|i| matches!(i, AstItem::Struct { .. }))
            .collect();
        assert_eq!(types.len(), 3);
    }

    #[test]
    fn test_swift_async_function() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("api.swift"));
        let code = r#"
func fetchData() async throws -> Data {
    return try await URLSession.shared.data(from: url)
}
"#;
        let items = analyzer.analyze_swift_source(code).expect("Should parse");

        let functions: Vec<_> = items
            .iter()
            .filter_map(|i| {
                if let AstItem::Function { is_async, .. } = i {
                    Some(*is_async)
                } else {
                    None
                }
            })
            .collect();

        assert!(
            functions.iter().any(|&is_async| is_async),
            "Should detect async function"
        );
    }

    #[test]
    fn test_swift_private_function() {
        let analyzer = SwiftSourceAnalyzer::new(Path::new("helper.swift"));
        // The extract_functions method looks for "func " at the start of line
        let code = r#"
func helperFunction() {
    print("helper")
}
"#;
        let items = analyzer.analyze_swift_source(code).expect("Should parse");

        // Verify function is found
        let functions: Vec<_> = items
            .iter()
            .filter(|i| matches!(i, AstItem::Function { .. }))
            .collect();
        assert!(!functions.is_empty(), "Should detect function");
    }
}