Add comprehensive tests for parseConfiguration function, handle non-object parser results consistently (fixes #1523).

This commit is contained in:
David Anson 2025-03-01 18:23:48 -08:00
parent 1f237e6c54
commit 63147ee1eb
4 changed files with 97 additions and 11 deletions

View file

@ -13,12 +13,94 @@ const testObject = {
};
const successfulTestObjectResult = {
"config": testObject,
"message": ""
"message": null
};
const successfulParser = () => testObject;
const failingParser = () => {
throw new Error("failingParser");
};
const testObjectJsonString = JSON.stringify(testObject);
test("json object, default parsers", (t) => {
test("json object, default parser", (t) => {
t.plan(1);
const actual = parseConfiguration("name", testObjectJsonString);
const actual = parseConfiguration("name", JSON.stringify(testObject));
t.deepEqual(actual, successfulTestObjectResult);
});
test("invalid json, default parser", (t) => {
t.plan(1);
const actual = parseConfiguration("name", "{");
const expected = {
"config": null,
"message": "Unable to parse 'name'; Parser 0: Expected property name or '}' in JSON at position 1 (line 1 column 2)"
};
t.deepEqual(actual, expected);
});
test("parser gets passed content", (t) => {
t.plan(2);
const content = "content";
const parser = (text) => {
t.is(text, content);
return {};
};
const actual = parseConfiguration("name", content, [ parser ]);
const expected = {
"config": {},
"message": null
};
t.deepEqual(actual, expected);
});
test("parser returns undefined/null/number/string/array", (t) => {
t.plan(5);
const expected = {
"config": {},
"message": null
};
const testCases = [ undefined, null, 10, "string", [] ];
for (const testCase of testCases) {
/** @type {import("../lib/markdownlint.mjs").ConfigurationParser} */
const parser =
// @ts-ignore
() => testCase;
const actual = parseConfiguration("name", "content", [ parser ]);
t.deepEqual(actual, expected);
}
});
test("custom parsers, successful/failing", (t) => {
t.plan(1);
const actual = parseConfiguration("name", "", [ successfulParser, failingParser ]);
t.deepEqual(actual, successfulTestObjectResult);
});
test("custom parsers, failing/successful", (t) => {
t.plan(1);
const actual = parseConfiguration("name", "", [ failingParser, successfulParser ]);
t.deepEqual(actual, successfulTestObjectResult);
});
test("custom parsers, failing/successful(undefined)", (t) => {
t.plan(1);
const actual = parseConfiguration(
"name",
"",
// @ts-ignore
[ failingParser, () => undefined ]
);
const expected = {
"config": {},
"message": null
};
t.deepEqual(actual, expected);
});
test("custom parsers, failing/failing", (t) => {
t.plan(1);
const actual = parseConfiguration("name", "", [ failingParser, failingParser ]);
const expected = {
"config": null,
"message": "Unable to parse 'name'; Parser 0: failingParser; Parser 1: failingParser"
};
t.deepEqual(actual, expected);
});