ruchy 4.2.0

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
#![cfg(test)]
#![allow(warnings)]
#![allow(clippy::assertions_on_constants)]
#![allow(clippy::unreadable_literal)]
#![allow(clippy::unwrap_used)]
//! Tests for the lints module
//!
//! This test suite targets the lints module that had 120 lines with 0% coverage.
//! It tests custom lint rules for Ruchy code quality analysis.

use ruchy::frontend::ast::{BinaryOp, Expr, ExprKind, Literal, MatchArm, Pattern, Span};
use ruchy::lints::{LintRule, LintViolation, RuchyLinter, Severity};

/// Create a simple test expression
fn create_test_expr(kind: ExprKind) -> Expr {
    Expr::new(kind, Span::new(0, 10))
}

/// Test basic linter creation and initialization
#[test]
fn test_linter_creation() {
    let _linter = RuchyLinter::new();

    // Should create successfully with default rules
    // Linter creation verified by no panic

    // Check Default trait
    let _default_linter = RuchyLinter::default();
    // Default creation verified by no panic
}

/// Test adding custom rules to linter
#[test]
fn test_linter_add_custom_rule() {
    let mut linter = RuchyLinter::new();

    // Create a simple custom rule
    struct TestRule;
    impl LintRule for TestRule {
        fn name(&self) -> &'static str {
            "test_rule"
        }

        fn check_expression(&self, _expr: &Expr) -> Vec<LintViolation> {
            vec![]
        }
    }

    // Add the custom rule
    linter.add_rule(Box::new(TestRule));

    // Should add without error
    // Linter created successfully - no panic means test passed
}

/// Test linting simple expressions
#[test]
fn test_lint_simple_expressions() {
    let linter = RuchyLinter::new();

    // Check integer literal (should have no violations)
    let expr = create_test_expr(ExprKind::Literal(Literal::Integer(42)));
    let violations = linter.lint(&expr);

    // Simple expressions should have no violations
    assert!(violations.is_empty());
}

/// Test linting string expressions
#[test]
fn test_lint_string_expressions() {
    let linter = RuchyLinter::new();

    // Check string literal
    let expr = create_test_expr(ExprKind::Literal(Literal::String("hello".to_string())));
    let violations = linter.lint(&expr);

    // String literals should have no violations
    assert!(violations.is_empty());
}

/// Test linting boolean expressions
#[test]
fn test_lint_boolean_expressions() {
    let linter = RuchyLinter::new();

    // Check boolean literal
    let expr = create_test_expr(ExprKind::Literal(Literal::Bool(true)));
    let violations = linter.lint(&expr);

    // Boolean literals should have no violations
    assert!(violations.is_empty());
}

/// Test complexity rule with simple if expression
#[test]
fn test_complexity_rule_simple_if() {
    let linter = RuchyLinter::new();

    // Create a simple if expression (low complexity)
    let condition = Box::new(create_test_expr(ExprKind::Literal(Literal::Bool(true))));
    let then_branch = Box::new(create_test_expr(ExprKind::Literal(Literal::Integer(1))));
    let else_branch = Some(Box::new(create_test_expr(ExprKind::Literal(
        Literal::Integer(2),
    ))));

    let if_expr = create_test_expr(ExprKind::If {
        condition,
        then_branch,
        else_branch,
    });

    let violations = linter.lint(&if_expr);

    // Simple if should not violate complexity rules
    assert!(violations.is_empty());
}

/// Test complexity rule with nested complex expression
#[test]
fn test_complexity_rule_nested_complexity() {
    let linter = RuchyLinter::new();

    // Create a highly nested if expression to trigger complexity warning
    let mut nested_expr = create_test_expr(ExprKind::Literal(Literal::Integer(1)));

    // Create multiple nested if expressions to increase complexity
    for _ in 0..12 {
        let condition = Box::new(create_test_expr(ExprKind::Literal(Literal::Bool(true))));
        let then_branch = Box::new(nested_expr.clone());
        let else_branch = Some(Box::new(create_test_expr(ExprKind::Literal(
            Literal::Integer(2),
        ))));

        nested_expr = create_test_expr(ExprKind::If {
            condition,
            then_branch,
            else_branch,
        });
    }

    let violations = linter.lint(&nested_expr);

    // Should have complexity violations
    assert!(!violations.is_empty());

    // Check that it's a complexity violation
    let first_violation = &violations[0];
    let LintViolation::Violation {
        message, severity, ..
    } = first_violation;
    assert!(message.contains("complexity"));
    assert!(matches!(severity, Severity::Warning));
}

/// Test no debug print rule with regular function calls
#[test]
fn test_no_debug_print_rule_regular_calls() {
    let linter = RuchyLinter::new();

    // Create a regular function call (not debug)
    let func = Box::new(create_test_expr(ExprKind::Identifier(
        "println".to_string(),
    )));
    let args = vec![];

    let call_expr = create_test_expr(ExprKind::Call { func, args });
    let violations = linter.lint(&call_expr);

    // Regular function calls should not violate debug print rule
    assert!(
        violations.is_empty()
            || !violations.iter().any(|v| {
                let LintViolation::Violation { message, .. } = v;
                message.contains("debug")
            })
    );
}

/// Test no debug print rule with debug calls
#[test]
fn test_no_debug_print_rule_debug_calls() {
    let linter = RuchyLinter::new();

    // Create a debug function call
    let func = Box::new(create_test_expr(ExprKind::Identifier("dbg".to_string())));
    let args = vec![create_test_expr(ExprKind::Literal(Literal::Integer(42)))];

    let debug_call_expr = create_test_expr(ExprKind::Call { func, args });
    let violations = linter.lint(&debug_call_expr);

    // Debug calls should violate the rule
    println!("Debug: Violations found: {violations:?}");
    assert!(!violations.is_empty());

    // Check that it's a debug print violation
    let has_debug_violation = violations.iter().any(|v| {
        let LintViolation::Violation { message, .. } = v;
        // Debug: Checking message
        message.contains("debug") || message.contains("Debug")
    });
    assert!(has_debug_violation);
}

/// Test no debug print rule with `debug_print` calls
#[test]
fn test_no_debug_print_rule_debug_print_calls() {
    let linter = RuchyLinter::new();

    // Create a debug_print function call
    let func = Box::new(create_test_expr(ExprKind::Identifier(
        "debug_print".to_string(),
    )));
    let args = vec![create_test_expr(ExprKind::Literal(Literal::String(
        "debug".to_string(),
    )))];

    let debug_print_expr = create_test_expr(ExprKind::Call { func, args });
    let violations = linter.lint(&debug_print_expr);

    // debug_print calls should violate the rule
    assert!(!violations.is_empty());
}

/// Test match expression complexity
#[test]
fn test_complexity_rule_match_expression() {
    let linter = RuchyLinter::new();

    // Create a match expression
    let expr = Box::new(create_test_expr(ExprKind::Literal(Literal::Integer(42))));

    let arms = vec![
        MatchArm {
            pattern: Pattern::Literal(Literal::Integer(1)),
            guard: None,
            body: Box::new(create_test_expr(ExprKind::Literal(Literal::String(
                "one".to_string(),
            )))),
            span: Span::new(0, 10),
        },
        MatchArm {
            pattern: Pattern::Literal(Literal::Integer(2)),
            guard: None,
            body: Box::new(create_test_expr(ExprKind::Literal(Literal::String(
                "two".to_string(),
            )))),
            span: Span::new(0, 10),
        },
    ];

    let match_expr = create_test_expr(ExprKind::Match { expr, arms });
    let violations = linter.lint(&match_expr);

    // Simple match should not violate complexity rules
    assert!(violations.is_empty());
}

/// Test while loop complexity
#[test]
fn test_complexity_rule_while_loop() {
    let linter = RuchyLinter::new();

    // Create a while loop
    let condition = Box::new(create_test_expr(ExprKind::Literal(Literal::Bool(true))));
    let body = Box::new(create_test_expr(ExprKind::Literal(Literal::Integer(1))));

    let while_expr = create_test_expr(ExprKind::While { condition, body });
    let violations = linter.lint(&while_expr);

    // Simple while loop should not violate complexity rules
    assert!(violations.is_empty());
}

/// Test for loop complexity
#[test]
fn test_complexity_rule_for_loop() {
    let linter = RuchyLinter::new();

    // Create a for loop
    let var = "i".to_string();
    let pattern = Some(Pattern::Identifier("i".to_string()));
    let iter = Box::new(create_test_expr(ExprKind::Literal(Literal::Integer(10))));
    let body = Box::new(create_test_expr(ExprKind::Literal(Literal::Integer(1))));

    let for_expr = create_test_expr(ExprKind::For {
        var,
        pattern,
        iter,
        body,
    });
    let violations = linter.lint(&for_expr);

    // Simple for loop should not violate complexity rules
    assert!(violations.is_empty());
}

/// Test binary expression complexity
#[test]
fn test_complexity_rule_binary_expression() {
    let linter = RuchyLinter::new();

    // Create a binary expression
    let left = Box::new(create_test_expr(ExprKind::Literal(Literal::Integer(1))));
    let right = Box::new(create_test_expr(ExprKind::Literal(Literal::Integer(2))));

    let binary_expr = create_test_expr(ExprKind::Binary {
        left,
        op: BinaryOp::Add,
        right,
    });

    let violations = linter.lint(&binary_expr);

    // Simple binary expressions should not violate complexity rules
    assert!(violations.is_empty());
}

/// Test lint violation formatting
#[test]
fn test_lint_violation_formatting() {
    // Create a lint violation
    let violation = LintViolation::Violation {
        location: "line 10".to_string(),
        message: "Test violation".to_string(),
        severity: Severity::Error,
        suggestion: Some("Fix this".to_string()),
    };

    // Check formatting
    let formatted = format!("{violation}");
    assert!(formatted.contains("line 10"));
    assert!(formatted.contains("Test violation"));
    assert!(formatted.contains("Error"));
}

/// Test severity enum
#[test]
fn test_severity_enum() {
    // Check all severity levels
    let error = Severity::Error;
    let warning = Severity::Warning;
    let info = Severity::Info;

    // Check equality
    assert_eq!(error, Severity::Error);
    assert_eq!(warning, Severity::Warning);
    assert_eq!(info, Severity::Info);

    // Check inequality
    assert_ne!(error, warning);
    assert_ne!(warning, info);
    assert_ne!(error, info);
}

/// Test custom lint rule implementation
#[test]
fn test_custom_lint_rule() {
    struct AlwaysViolatesRule;

    impl LintRule for AlwaysViolatesRule {
        fn name(&self) -> &'static str {
            "always_violates"
        }

        fn check_expression(&self, expr: &Expr) -> Vec<LintViolation> {
            vec![LintViolation::Violation {
                location: format!("position {}", expr.span.start),
                message: "This rule always triggers".to_string(),
                severity: Severity::Info,
                suggestion: None,
            }]
        }
    }

    let mut linter = RuchyLinter::new();
    linter.add_rule(Box::new(AlwaysViolatesRule));

    let expr = create_test_expr(ExprKind::Literal(Literal::Integer(42)));
    let violations = linter.lint(&expr);

    // Should have our custom violation plus any default rule violations
    let has_custom_violation = violations.iter().any(|v| {
        let LintViolation::Violation { message, .. } = v;
        message.contains("always triggers")
    });

    assert!(has_custom_violation);
}

/// Test lint rule name method
#[test]
fn test_lint_rule_names() {
    struct TestRule;
    impl LintRule for TestRule {
        fn name(&self) -> &'static str {
            "test_rule_name"
        }

        fn check_expression(&self, _expr: &Expr) -> Vec<LintViolation> {
            vec![]
        }
    }

    let rule = TestRule;
    assert_eq!(rule.name(), "test_rule_name");
}