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
//! Output reporters — terminal table and JSON.
pub mod advice_summary;
pub mod csv;
pub mod html;
pub mod json;
pub mod markdown;
pub mod sarif;
pub mod scorecard_row;
pub mod table;
pub use advice_summary::render_summary as render_advice_summary;
pub use csv::format_csv;
pub use html::format_html;
pub use json::{JsonConfig, format_json};
pub use markdown::format_markdown;
pub use sarif::format_sarif;
pub use scorecard_row::format_scorecard_row;
pub use table::{format_table, format_table_with_explain};
#[cfg(test)]
pub(crate) mod test_fixtures {
use crate::domain::delta::{self, AnalysisDelta, DeltaView, DeltaViewSpec};
use crate::domain::types::{
AnalysisResult, AnalysisSummary, ComplexityContributor, CrapScore, FunctionIdentity,
FunctionVerdict, RiskDistribution, RiskLevel, ScoredFunction, SourceSpan,
};
use crate::domain::view::{self, AnalysisView, ViewSpec};
/// Build a default-spec view from the given analysis. Convenience for
/// reporter tests that just want to assert on the default-spec output
/// (the V1a walking-skeleton invariant).
pub fn make_view_default(result: &AnalysisResult) -> AnalysisView<'_> {
view::apply(result, ViewSpec::default())
}
/// Build a default-spec delta view. Mirrors `make_view_default` for
/// the delta sibling pipeline.
pub fn make_delta_view_default(delta: &AnalysisDelta) -> DeltaView<'_> {
delta::apply(delta, DeltaViewSpec::default())
}
/// Sample delta covering all three change kinds plus a regression
/// and a new violation. Used by reporter snapshot tests.
///
/// Baseline → current edits:
/// - `simple_fn` (low) → unchanged identity, score 3.0 → 3.0 (Modified, zero delta)
/// - `parse_record` (moderate) → score 15.0 → 22.0 (Modified, regression)
/// - `complex_fn` (high) baseline only (Removed)
/// - `new_fn` (current only) score 30.0, exceeds threshold → Added + new violation
pub fn make_sample_delta() -> AnalysisDelta {
let baseline = {
let mut r = make_multi_function_result();
// Drop complex_fn from current, but keep in baseline; alter parse_record CRAP
r.functions[1].scored.crap.value = 15.0;
r
};
let current = {
let v_simple =
make_verdict("simple_fn", "src/lib.rs", 2, 95.0, 3.0, RiskLevel::Low, 8.0);
let mut v_parse = make_verdict(
"parse_record",
"src/adapters/coverage/mod.rs",
6,
60.0,
22.0,
RiskLevel::High,
8.0,
);
v_parse.exceeds = true;
let mut v_new = make_verdict(
"new_fn",
"src/adapters/baseline.rs",
10,
40.0,
30.0,
RiskLevel::High,
8.0,
);
v_new.exceeds = true;
AnalysisResult {
functions: vec![v_simple, v_parse, v_new],
summary: AnalysisSummary {
total_functions: 3,
total_files: 3,
exceeding_threshold: 2,
average_crap: 18.33,
median_crap: 22.0,
max_crap: Some(CrapScore {
value: 30.0,
risk_level: RiskLevel::High,
}),
worst_function: Some(FunctionIdentity {
file_path: "src/adapters/baseline.rs".to_string(),
qualified_name: "new_fn".to_string(),
span: SourceSpan {
start_line: 1,
end_line: 10,
start_column: 0,
end_column: 0,
},
}),
distribution: RiskDistribution {
low: 1,
acceptable: 0,
moderate: 0,
high: 2,
},
..Default::default()
},
passed: false,
}
};
delta::compute(baseline, current)
}
pub fn make_verdict(
name: &str,
file: &str,
complexity: u32,
coverage_pct: f64,
crap_value: f64,
risk: RiskLevel,
threshold: f64,
) -> FunctionVerdict {
FunctionVerdict {
scored: ScoredFunction {
identity: FunctionIdentity {
file_path: file.to_string(),
qualified_name: name.to_string(),
span: SourceSpan {
start_line: 1,
end_line: 10,
start_column: 0,
end_column: 0,
},
},
complexity,
complexity_metric: crate::domain::types::ComplexityMetric::Cognitive,
coverage_percent: coverage_pct,
crap: CrapScore {
value: crap_value,
risk_level: risk,
},
contributors: vec![],
},
threshold,
exceeds: crap_value > threshold,
diagnostic: None,
}
}
pub fn make_verdict_with_contributors(
verdict: FunctionVerdict,
contributors: Vec<ComplexityContributor>,
) -> FunctionVerdict {
let mut v = verdict;
v.scored.contributors = contributors;
v
}
pub fn make_empty_result() -> AnalysisResult {
AnalysisResult {
functions: vec![],
summary: AnalysisSummary {
total_functions: 0,
total_files: 0,
exceeding_threshold: 0,
average_crap: 0.0,
median_crap: 0.0,
max_crap: None,
worst_function: None,
distribution: RiskDistribution {
low: 0,
acceptable: 0,
moderate: 0,
high: 0,
},
..Default::default()
},
passed: true,
}
}
pub fn make_single_function_result(
name: &str,
file: &str,
complexity: u32,
coverage_pct: f64,
crap_value: f64,
risk: RiskLevel,
threshold: f64,
) -> AnalysisResult {
let verdict = make_verdict(
name,
file,
complexity,
coverage_pct,
crap_value,
risk,
threshold,
);
let exceeds = verdict.exceeds;
AnalysisResult {
functions: vec![verdict],
summary: AnalysisSummary {
total_functions: 1,
total_files: 1,
exceeding_threshold: if exceeds { 1 } else { 0 },
average_crap: crap_value,
median_crap: crap_value,
max_crap: Some(CrapScore {
value: crap_value,
risk_level: risk,
}),
worst_function: Some(FunctionIdentity {
file_path: file.to_string(),
qualified_name: name.to_string(),
span: SourceSpan {
start_line: 1,
end_line: 10,
start_column: 0,
end_column: 0,
},
}),
distribution: RiskDistribution {
low: if risk == RiskLevel::Low { 1 } else { 0 },
acceptable: if risk == RiskLevel::Acceptable { 1 } else { 0 },
moderate: if risk == RiskLevel::Moderate { 1 } else { 0 },
high: if risk == RiskLevel::High { 1 } else { 0 },
},
..Default::default()
},
passed: !exceeds,
}
}
/// Three functions spanning Low, Moderate, and High risk levels.
/// Scores: Low=3.0, Moderate=15.0, High=45.2 — threshold 8.0.
pub fn make_multi_function_result() -> AnalysisResult {
let v1 = make_verdict("simple_fn", "src/lib.rs", 2, 95.0, 3.0, RiskLevel::Low, 8.0);
let v2 = make_verdict(
"parse_record",
"src/adapters/coverage/mod.rs",
6,
72.5,
15.0,
RiskLevel::Moderate,
8.0,
);
let v3 = make_verdict(
"complex_fn",
"src/domain/crap.rs",
20,
30.0,
45.2,
RiskLevel::High,
8.0,
);
let functions = vec![v1, v2, v3];
let summary = crate::domain::summary::compute_summary(&functions);
AnalysisResult {
functions,
summary,
passed: false,
}
}
}