From dbca0e98b0177ce62a1b46feaff4e8a137804c5e Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Fri, 22 Nov 2024 10:52:01 -0700 Subject: [PATCH] Give total number of tests in result as well. --- .ncurc | 3 ++- lib/index.js | 23 ++++++++++++++++++++--- test/index.test.js | 5 +++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/.ncurc b/.ncurc index 7f738d6..a64aed5 100644 --- a/.ncurc +++ b/.ncurc @@ -1,3 +1,4 @@ { - "dep": ["prod", "dev", "packageManager"] + "dep": ["prod", "dev", "packageManager"], + "reject": ["typescript"] } diff --git a/lib/index.js b/lib/index.js index 23e6453..db0587a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -17,6 +17,7 @@ let counter = 0; * @typedef {object} TestCounts * @prop {number} valid * @prop {number} invalid + * @prop {number} total * @prop {string} grammarPath * @prop {string} modifiedPath */ @@ -61,6 +62,20 @@ let counter = 0; * @typedef {import('peggy').Location} Location */ +/** + * Add the formatted information to the given error. + * + * @param {unknown} e + * @param {string} source + * @param {string} text + */ +function format(e, source, text) { + const er = /** @type {import('peggy').parser.SyntaxError} */(e); + if (typeof er?.format === "function") { + er.message = er.format([{ source, text }]); + } +} + /** * @template T * @param {Parser} grammar @@ -101,9 +116,8 @@ function checkParserStarts(grammar, starts, modified, counts) { ...options, }); } catch (er) { - const e = /** @type {import('peggy').parser.SyntaxError} */ (er); - e.message = e.format([{ source, text: start.validInput }]); - throw e; + format(er, source, start.validInput); + throw er; } if (typeof expected === "string") { equal(res, expected, `${source} (eq): "${start.validInput}"`); @@ -175,6 +189,7 @@ function checkParserStarts(grammar, starts, modified, counts) { })); } counts.valid++; + counts.total++; } if (typeof start.invalidInput === "string") { @@ -221,6 +236,7 @@ function checkParserStarts(grammar, starts, modified, counts) { equal(typeof fmt, "string"); } counts.invalid++; + counts.total++; } } } @@ -270,6 +286,7 @@ export async function testPeggy(grammarUrl, starts, opts) { const counts = { valid: 0, invalid: 0, + total: 0, grammarPath, modifiedPath, }; diff --git a/test/index.test.js b/test/index.test.js index d1e20d9..a95d453 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -84,6 +84,7 @@ test("test peggy coverage", async() => { deepEqual(counts, { valid: 11, invalid: 9, + total: 20, }); }); @@ -101,6 +102,7 @@ test("noGenerate", async() => { deepEqual(counts, { valid: 1, invalid: 1, + total: 2, }); }); @@ -118,6 +120,7 @@ test("noMap", async() => { deepEqual(counts, { valid: 2, invalid: 2, + total: 4, }); }); @@ -140,6 +143,7 @@ test("skip", async() => { deepEqual(counts, { valid: 2, invalid: 0, + total: 2, }); }); @@ -158,6 +162,7 @@ test("skip", async() => { deepEqual(counts, { valid: 2, invalid: 0, + total: 2, }); });