Commit 2bae1d00 authored by David Sveningsson's avatar David Sveningsson

feat(reporter): add {error,warning}Count summary to Report object

parent bc657d03
......@@ -5,16 +5,16 @@ describe("Reporter", () => {
describe("merge()", () => {
it("should set valid only if all reports are valid", () => {
const none = Reporter.merge([
{ valid: false, results: [] },
{ valid: false, results: [] },
{ valid: false, results: [], errorCount: 1, warningCount: 2 },
{ valid: false, results: [], errorCount: 3, warningCount: 4 },
]);
const one = Reporter.merge([
{ valid: true, results: [] },
{ valid: false, results: [] },
{ valid: true, results: [], errorCount: 0, warningCount: 0 },
{ valid: false, results: [], errorCount: 1, warningCount: 0 },
]);
const all = Reporter.merge([
{ valid: true, results: [] },
{ valid: true, results: [] },
{ valid: true, results: [], errorCount: 0, warningCount: 0 },
{ valid: true, results: [], errorCount: 0, warningCount: 0 },
]);
expect(none.valid).toBeFalsy();
expect(one.valid).toBeFalsy();
......@@ -29,10 +29,14 @@ describe("Reporter", () => {
createResult("foo", ["fred", "barney"]),
createResult("bar", ["spam"]),
],
errorCount: 3,
warningCount: 0,
},
{
valid: false,
results: [createResult("foo", ["wilma"])],
errorCount: 1,
warningCount: 0,
},
]);
expect(merged.results).toHaveLength(2);
......@@ -46,6 +50,8 @@ describe("Reporter", () => {
expect(merged.results[1].filePath).toEqual("bar");
expect(merged.results[1].messages.map(x => x.message)).toEqual(["spam"]);
expect(merged.results[1].errorCount).toEqual(1);
expect(merged.errorCount).toEqual(4);
expect(merged.warningCount).toEqual(0);
});
});
......
......@@ -52,6 +52,12 @@ export interface Report {
/** Detailed results per validated source */
results: Result[];
/** Total number of errors across all sources */
errorCount: number;
/** Total warnings of errors across all sources */
warningCount: number;
}
export class Reporter {
......@@ -89,6 +95,8 @@ export class Reporter {
return {
valid,
results,
errorCount: sumErrors(results),
warningCount: sumWarnings(results),
};
}
......@@ -122,7 +130,7 @@ export class Reporter {
}
public save(sources?: Source[]): Report {
return {
const report: Report = {
valid: this.isValid(),
results: Object.keys(this.result).map(filePath => {
const messages = [].concat(this.result[filePath]).sort(messageSort);
......@@ -137,7 +145,12 @@ export class Reporter {
source: source ? source.originalData || source.data : null,
};
}),
errorCount: 0,
warningCount: 0,
};
report.errorCount = sumErrors(report.results);
report.warningCount = sumWarnings(report.results);
return report;
}
protected isValid(): boolean {
......@@ -156,6 +169,18 @@ function countWarnings(messages: Message[]): number {
return messages.filter(m => m.severity === Severity.WARN).length;
}
function sumErrors(results: Result[]): number {
return results.reduce((sum: number, result: Result) => {
return sum + result.errorCount;
}, 0);
}
function sumWarnings(results: Result[]): number {
return results.reduce((sum: number, result: Result) => {
return sum + result.warningCount;
}, 0);
}
function messageSort(a: Message, b: Message): number {
if (a.line < b.line) {
return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment