-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathexample.ts
More file actions
307 lines (264 loc) · 7.2 KB
/
Copy pathexample.ts
File metadata and controls
307 lines (264 loc) · 7.2 KB
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
/**
* Example Usage of Analyzer Coverage Reporting
*
* This file demonstrates how to use the coverage reporting feature.
*/
import { CoverageReporter } from "./coverage-reporter";
import { RuleCoverageAnalyzer } from "../../analysis/coverage";
/**
* Example 1: Basic Usage
*/
async function basicExample() {
console.log("=== Example 1: Basic Usage ===\n");
// Create analyzer and perform analysis
const analyzer = new RuleCoverageAnalyzer();
// Simulate AST analysis
const mockAst = {
type: "Program",
id: "root",
children: [
{ type: "Function", id: "fn1", children: [] },
{ type: "Variable", id: "var1", children: [] },
{ type: "CallExpression", id: "call1", children: [] },
],
};
analyzer.registerAst(mockAst);
analyzer.markAnalyzed("fn1");
analyzer.markAnalyzed("var1");
analyzer.reportUncovered("CallExpression");
// Get coverage metrics
const metrics = analyzer.getMetrics();
// Create reporter and generate report
const reporter = new CoverageReporter();
const reportData = reporter.createReportData(
"ExampleProject",
"1.0.0",
metrics,
3,
5,
250,
);
// Generate text report
const textReport = reporter.generate(reportData, { format: "text" });
console.log(textReport);
}
/**
* Example 2: Generate Multiple Formats
*/
async function multipleFormatsExample() {
console.log("\n=== Example 2: Multiple Formats ===\n");
const analyzer = new RuleCoverageAnalyzer();
const reporter = new CoverageReporter();
// Simulate analysis
const metrics = {
totalNodes: 100,
analyzedNodes: 85,
coveragePercent: 85,
uncoveredPatterns: ["Pattern1", "Pattern2"],
};
const reportData = reporter.createReportData(
"MultiFormatExample",
"2.0.0",
metrics,
10,
12,
1500,
);
// Generate JSON for API consumption
const jsonReport = reporter.generate(reportData, { format: "json" });
console.log("JSON Report (truncated):");
console.log(jsonReport.substring(0, 200) + "...\n");
// Generate Markdown for documentation
const mdReport = reporter.generate(reportData, { format: "markdown" });
console.log("Markdown Report (first 300 chars):");
console.log(mdReport.substring(0, 300) + "...\n");
}
/**
* Example 3: Threshold Checking for CI/CD
*/
async function thresholdCheckExample() {
console.log("\n=== Example 3: Threshold Checking ===\n");
const reporter = new CoverageReporter();
// Scenario 1: Coverage meets threshold
const goodMetrics = {
totalNodes: 100,
analyzedNodes: 90,
coveragePercent: 90,
uncoveredPatterns: [],
};
const threshold = {
minCoveragePercent: 85,
failOnThreshold: true,
};
const meetsThreshold = reporter.checkThreshold(goodMetrics, threshold);
console.log(
`Coverage 90% >= Threshold 85%: ${meetsThreshold ? "PASS ✓" : "FAIL ✗"}`,
);
// Scenario 2: Coverage below threshold
const poorMetrics = {
totalNodes: 100,
analyzedNodes: 70,
coveragePercent: 70,
uncoveredPatterns: ["Pattern1", "Pattern2", "Pattern3"],
};
const failsThreshold = reporter.checkThreshold(poorMetrics, threshold);
console.log(
`Coverage 70% >= Threshold 85%: ${failsThreshold ? "PASS ✓" : "FAIL ✗"}`,
);
}
/**
* Example 4: Save Report to File
*/
async function saveReportExample() {
console.log("\n=== Example 4: Save Report to File ===\n");
const reporter = new CoverageReporter();
const metrics = {
totalNodes: 150,
analyzedNodes: 135,
coveragePercent: 90,
uncoveredPatterns: ["UncoveredPattern1"],
};
const reportData = reporter.createReportData(
"SaveReportExample",
"1.5.0",
metrics,
20,
25,
3000,
);
// Save as HTML with full details
const result = await reporter.saveReport(
reportData,
"./reports/coverage-report.html",
{
format: "html",
includeUncoveredDetails: true,
thresholdPercent: 85,
},
);
console.log("Report saved successfully!");
console.log(`Path: ${result.reportPath}`);
console.log(`Coverage: ${result.coveragePercent}%`);
console.log(`Threshold Met: ${result.thresholdMet}`);
console.log(`Summary: ${result.summary}`);
}
/**
* Example 5: Rule Breakdown
*/
async function ruleBreakdownExample() {
console.log("\n=== Example 5: Rule Breakdown ===\n");
const reporter = new CoverageReporter();
const reportData = {
projectName: "RuleBreakdownExample",
version: "3.0.0",
timestamp: new Date(),
coverage: {
totalNodes: 200,
analyzedNodes: 180,
coveragePercent: 90,
uncoveredPatterns: [],
},
filesCovered: 15,
totalFiles: 20,
analysisTimeMs: 4500,
rulesCoverage: [
{
ruleId: "R001",
ruleName: "Inefficient Storage Pattern",
nodesAnalyzed: 75,
nodeTypes: ["StorageCall", "StorageWrite"],
filesAffected: ["contract1.rs", "contract2.rs", "lib.rs"],
},
{
ruleId: "R002",
ruleName: "Redundant Computation",
nodesAnalyzed: 50,
nodeTypes: ["Function", "Loop"],
filesAffected: ["utils.rs", "math.rs"],
},
{
ruleId: "R003",
ruleName: "Unoptimized Loop",
nodesAnalyzed: 55,
nodeTypes: ["ForLoop", "WhileLoop"],
filesAffected: ["iterator.rs", "processor.rs"],
},
],
};
const report = reporter.generate(reportData, {
format: "text",
includeRuleBreakdown: true,
});
console.log(report);
}
/**
* Example 6: CI/CD Integration
*/
async function cicdIntegrationExample() {
console.log("\n=== Example 6: CI/CD Integration ===\n");
const reporter = new CoverageReporter();
const metrics = {
totalNodes: 300,
analyzedNodes: 240,
coveragePercent: 80,
uncoveredPatterns: ["Pattern1", "Pattern2"],
};
const reportData = reporter.createReportData(
"CI-CD-Project",
"1.0.0",
metrics,
30,
35,
5000,
);
// Generate JSON for CI/CD tools
const result = await reporter.saveReport(
reportData,
"./reports/coverage.json",
{
format: "json",
thresholdPercent: 75,
},
);
console.log("CI/CD Integration Result:");
console.log(`- Report saved: ${result.reportPath}`);
console.log(`- Coverage: ${result.coveragePercent}%`);
console.log(
`- Threshold (75%): ${result.thresholdMet ? "PASSED ✓" : "FAILED ✗"}`,
);
if (!result.thresholdMet) {
console.error("ERROR: Coverage below threshold! Build should fail.");
// In real CI/CD: process.exit(1);
} else {
console.log("SUCCESS: Coverage meets requirements. Build can proceed.");
}
}
/**
* Run all examples
*/
async function runAllExamples() {
try {
await basicExample();
await multipleFormatsExample();
await thresholdCheckExample();
// Skip file operations in example run
// await saveReportExample();
await ruleBreakdownExample();
await cicdIntegrationExample();
console.log("\n=== All Examples Completed Successfully ===");
} catch (error) {
console.error("Example execution failed:", error);
}
}
// Run examples if this file is executed directly
if (require.main === module) {
runAllExamples();
}
export {
basicExample,
multipleFormatsExample,
thresholdCheckExample,
saveReportExample,
ruleBreakdownExample,
cicdIntegrationExample,
};