pmat 3.15.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
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
// Tests for language analyzer
// Extracted for file health compliance (CB-040)

use super::*;

mod tests {
    use super::*;

    #[tokio::test]
    async fn test_language_analyzer_basic() {
        let analyzer = LanguageAnalyzer::new();
        assert!(analyzer.supported_languages().len() >= 50);
    }

    #[tokio::test]
    async fn test_analysis_support() {
        let analyzer = LanguageAnalyzer::new();

        assert!(analyzer.supports_analysis(Language::Rust, &AnalysisType::Complexity));
        assert!(analyzer.supports_analysis(Language::Python, &AnalysisType::Satd));
        assert!(!analyzer.supports_analysis(Language::JSON, &AnalysisType::Complexity));
        assert!(analyzer.supports_analysis(Language::Markdown, &AnalysisType::Documentation));
    }

    #[test]
    fn test_comment_detection() {
        let analyzer = LanguageAnalyzer::new();

        assert!(analyzer.is_comment_line("// This is a comment", Language::Rust));
        assert!(analyzer.is_comment_line("# This is a comment", Language::Python));
        assert!(analyzer.is_comment_line("/* Comment */", Language::Java));
        assert!(!analyzer.is_comment_line("let x = 5;", Language::Rust));
    }
}


mod property_tests {
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn basic_property_stability(_input in ".*") {
            // Basic property test for coverage
            prop_assert!(true);
        }

        #[test]
        fn module_consistency_check(_x in 0u32..1000) {
            // Module consistency verification
            prop_assert!(_x < 1001);
        }
    }
}

/// Comprehensive coverage tests for LanguageAnalyzer
mod coverage_tests {
    use super::*;
    use std::io::Write;
    use std::path::Path;
    use tempfile::NamedTempFile;

    // Test Fixtures

    /// Create a temporary file with the given content and extension
    fn create_temp_file(content: &str, extension: &str) -> NamedTempFile {
        let mut file = tempfile::Builder::new()
            .suffix(extension)
            .tempfile()
            .unwrap();
        file.write_all(content.as_bytes()).unwrap();
        file.flush().unwrap();
        file
    }

    /// Sample Rust code for testing
    fn sample_rust_code() -> &'static str {
        r#"// This is a Rust file
use std::io;

/// A simple function
fn main() {
    // TODO: Add more features
    if true {
        println!("Hello");
    } else {
        println!("World");
    }
    for i in 0..10 {
        match i {
            0 => println!("zero"),
            _ => println!("{}", i),
        }
    }
}

fn helper() {
    while true {
        break;
    }
}
"#
    }

    /// Sample Python code for testing
    fn sample_python_code() -> &'static str {
        r#"# This is a Python file
import os
from typing import List

def main():
    # FIXME: This needs work
    if True:
        print("Hello")
    elif False:
        print("World")
    else:
        print("!")

    for i in range(10):
        try:
            print(i)
        except Exception:
            pass

def helper():
    while True:
        break
"#
    }

    /// Sample JavaScript code for testing
    fn sample_javascript_code() -> &'static str {
        r#"// This is a JavaScript file
import { something } from 'module';
const axios = require('axios');

function main() {
    // HACK: temporary fix
    if (true) {
        console.log("Hello");
    } else {
        console.log("World");
    }
    for (let i = 0; i < 10; i++) {
        switch (i) {
            case 0:
                console.log("zero");
                break;
            default:
                console.log(i);
        }
    }
    try {
        doSomething();
    } catch (e) {
        console.error(e);
    }
}

const arrow = () => console.log("arrow");
"#
    }

    /// Sample TypeScript code for testing
    fn sample_typescript_code() -> &'static str {
        r#"// TypeScript file
import { Component } from '@angular/core';

interface User {
    name: string;
    age: number;
}

function processUser(user: User): void {
    // XXX: review this
    if (user.age > 18) {
        console.log("Adult");
    } else {
        console.log("Minor");
    }
}

const arrowFn = (x: number): number => x * 2;
"#
    }

    /// Sample Java code for testing
    fn sample_java_code() -> &'static str {
        r#"// Java file
import java.util.List;

public class Main {
    // BUG: Memory leak here
    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.println("Has args");
        } else {
            System.out.println("No args");
        }
        for (int i = 0; i < 10; i++) {
            switch (i) {
                case 0:
                    System.out.println("zero");
                    break;
                default:
                    System.out.println(i);
            }
        }
        try {
            doSomething();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void helper() {
        while (true) {
            break;
        }
    }
}
"#
    }

    /// Sample Go code for testing
    fn sample_go_code() -> &'static str {
        r#"// Go file
package main

import "fmt"

func main() {
    // KLUDGE: hack for now
    if true {
        fmt.Println("Hello")
    } else {
        fmt.Println("World")
    }
    for i := 0; i < 10; i++ {
        switch i {
        case 0:
            fmt.Println("zero")
        default:
            fmt.Println(i)
        }
    }
}

func helper() {
    for {
        break
    }
}
"#
    }

    /// Sample Kotlin code for testing
    fn sample_kotlin_code() -> &'static str {
        r#"// Kotlin file
import kotlin.collections.List

fun main() {
    // TODO: refactor
    if (true) {
        println("Hello")
    } else {
        println("World")
    }
    for (i in 0..10) {
        when (i) {
            0 -> println("zero")
            else -> println(i)
        }
    }
    try {
        doSomething()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

private fun helper() {
    while (true) {
        break
    }
}
"#
    }

    /// Sample SQL code for testing
    fn sample_sql_code() -> &'static str {
        r#"-- SQL file
-- This is a comment
SELECT * FROM users WHERE id = 1;
UPDATE users SET name = 'test' WHERE id = 1;
DELETE FROM users WHERE id = 1;
DROP TABLE IF EXISTS temp;
"#
    }

    /// Sample Python code with documentation
    fn sample_python_with_docs() -> &'static str {
        r#"# Module docstring
# This module does things

def function_one():
    # Comment line
    # Another comment
    pass

def function_two():
    # Inline comment
    x = 5  # trailing comment
    return x

# More comments
# Even more comments
# And more

def function_three():
    pass
"#
    }

    // LanguageAnalyzer Creation Tests

    #[test]
    fn test_language_analyzer_new() {
        let analyzer = LanguageAnalyzer::new();
        assert!(!analyzer.supported_languages().is_empty());
    }

    #[test]
    fn test_language_analyzer_default() {
        let analyzer = LanguageAnalyzer::default();
        assert!(!analyzer.supported_languages().is_empty());
    }

    #[test]
    fn test_supported_languages_count() {
        let analyzer = LanguageAnalyzer::new();
        // Should support many languages
        assert!(analyzer.supported_languages().len() >= 50);
    }

    // Analysis Support Tests

    #[test]
    fn test_supports_analysis_complexity() {
        let analyzer = LanguageAnalyzer::new();

        // Languages that support complexity
        assert!(analyzer.supports_analysis(Language::Rust, &AnalysisType::Complexity));
        assert!(analyzer.supports_analysis(Language::Python, &AnalysisType::Complexity));
        assert!(analyzer.supports_analysis(Language::Java, &AnalysisType::Complexity));
        assert!(analyzer.supports_analysis(Language::TypeScript, &AnalysisType::Complexity));

        // Languages that don't support complexity
        assert!(!analyzer.supports_analysis(Language::JSON, &AnalysisType::Complexity));
        assert!(!analyzer.supports_analysis(Language::YAML, &AnalysisType::Complexity));
    }

    #[test]
    fn test_supports_analysis_satd() {
        let analyzer = LanguageAnalyzer::new();

        // SATD is supported for all languages
        assert!(analyzer.supports_analysis(Language::Rust, &AnalysisType::Satd));
        assert!(analyzer.supports_analysis(Language::Python, &AnalysisType::Satd));
        assert!(analyzer.supports_analysis(Language::JSON, &AnalysisType::Satd));
        assert!(analyzer.supports_analysis(Language::Markdown, &AnalysisType::Satd));
    }

    #[test]
    fn test_supports_analysis_dead_code() {
        let analyzer = LanguageAnalyzer::new();

        // Languages with AST support - Markdown now has AST support (structure parsing)
        assert!(analyzer.supports_analysis(Language::Rust, &AnalysisType::DeadCode));
        assert!(analyzer.supports_analysis(Language::Python, &AnalysisType::DeadCode));
        assert!(analyzer.supports_analysis(Language::Markdown, &AnalysisType::DeadCode));
    }

    #[test]
    fn test_supports_analysis_security() {
        let analyzer = LanguageAnalyzer::new();

        // Security analysis needs AST (same as complexity)
        assert!(analyzer.supports_analysis(Language::JavaScript, &AnalysisType::Security));
        assert!(analyzer.supports_analysis(Language::Python, &AnalysisType::Security));
    }

    #[test]
    fn test_supports_analysis_style() {
        let analyzer = LanguageAnalyzer::new();

        // Style needs AST support - Markdown has AST support (structure parsing)
        assert!(analyzer.supports_analysis(Language::Rust, &AnalysisType::Style));
        assert!(analyzer.supports_analysis(Language::Markdown, &AnalysisType::Style));
    }

    #[test]
    fn test_supports_analysis_documentation() {
        let analyzer = LanguageAnalyzer::new();

        // Documentation is supported for doc languages
        assert!(analyzer.supports_analysis(Language::Markdown, &AnalysisType::Documentation));
        assert!(analyzer.supports_analysis(Language::LaTeX, &AnalysisType::Documentation));
        assert!(analyzer.supports_analysis(Language::AsciiDoc, &AnalysisType::Documentation));
        assert!(analyzer.supports_analysis(Language::Unknown, &AnalysisType::Documentation));

        // Not for code languages
        assert!(!analyzer.supports_analysis(Language::Rust, &AnalysisType::Documentation));
    }

    #[test]
    fn test_supports_analysis_dependencies() {
        let analyzer = LanguageAnalyzer::new();

        // Dependencies needs AST support - Markdown has AST support (structure parsing)
        assert!(analyzer.supports_analysis(Language::Rust, &AnalysisType::Dependencies));
        assert!(analyzer.supports_analysis(Language::Python, &AnalysisType::Dependencies));
        assert!(analyzer.supports_analysis(Language::Markdown, &AnalysisType::Dependencies));
    }

    #[test]
    fn test_supports_analysis_metrics() {
        let analyzer = LanguageAnalyzer::new();

        // Metrics is supported for all
        assert!(analyzer.supports_analysis(Language::Rust, &AnalysisType::Metrics));
        assert!(analyzer.supports_analysis(Language::JSON, &AnalysisType::Metrics));
        assert!(analyzer.supports_analysis(Language::Markdown, &AnalysisType::Metrics));
    }

    // Comment Detection Tests

    #[test]
    fn test_is_comment_line_c_style() {
        let analyzer = LanguageAnalyzer::new();

        // C-style comments
        assert!(analyzer.is_comment_line("// comment", Language::Rust));
        assert!(analyzer.is_comment_line("// comment", Language::C));
        assert!(analyzer.is_comment_line("// comment", Language::Cpp));
        assert!(analyzer.is_comment_line("// comment", Language::Go));
        assert!(analyzer.is_comment_line("// comment", Language::Java));
        assert!(analyzer.is_comment_line("// comment", Language::JavaScript));
        assert!(analyzer.is_comment_line("// comment", Language::TypeScript));
        assert!(analyzer.is_comment_line("// comment", Language::CSharp));
        assert!(analyzer.is_comment_line("// comment", Language::Swift));
        assert!(analyzer.is_comment_line("// comment", Language::Kotlin));
        assert!(analyzer.is_comment_line("// comment", Language::Scala));

        // Block comment start
        assert!(analyzer.is_comment_line("/* comment */", Language::Java));
        assert!(analyzer.is_comment_line("/* comment", Language::C));

        // Asterisk continuation
        assert!(analyzer.is_comment_line("* continuation", Language::Java));
    }

    #[test]
    fn test_is_comment_line_hash() {
        let analyzer = LanguageAnalyzer::new();

        // Hash comments
        assert!(analyzer.is_comment_line("# comment", Language::Python));
        assert!(analyzer.is_comment_line("# comment", Language::Ruby));
        assert!(analyzer.is_comment_line("# comment", Language::Bash));
        assert!(analyzer.is_comment_line("# comment", Language::Zsh));
        assert!(analyzer.is_comment_line("# comment", Language::Fish));
        assert!(analyzer.is_comment_line("# comment", Language::Perl));
        assert!(analyzer.is_comment_line("# comment", Language::R));
        assert!(analyzer.is_comment_line("# comment", Language::YAML));
        assert!(analyzer.is_comment_line("# comment", Language::TOML));
        assert!(analyzer.is_comment_line("# comment", Language::Makefile));
    }

    #[test]
    fn test_is_comment_line_semicolon() {
        let analyzer = LanguageAnalyzer::new();

        // Semicolon comments
        assert!(analyzer.is_comment_line("; comment", Language::Clojure));
    }

    #[test]
    fn test_is_comment_line_percent() {
        let analyzer = LanguageAnalyzer::new();

        // Percent comments
        assert!(analyzer.is_comment_line("% comment", Language::Erlang));
        assert!(analyzer.is_comment_line("% comment", Language::Matlab));
    }

    #[test]
    fn test_is_comment_line_double_dash() {
        let analyzer = LanguageAnalyzer::new();