munin/source/helpers/test.ts

125 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-06-28 08:34:57 +02:00
/*
This file is part of »munin«.
Copyright 2025 'Fenris Wolf' <fenris@folksprak.org>
»munin« is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
»munin« is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with »munin«. If not, see <http://www.gnu.org/licenses/>.
*/
namespace _munin.helpers.test
{
/**
* @todo outsource
*/
type type_testcase_raw<type_input, type_output> = {
active ?: boolean;
name ?: string;
input : type_input;
output : type_output;
};
/**
* @todo outsource
*/
export type type_testcase<type_input, type_output> = {
name : string;
input : type_input;
output : type_output;
};
/**
* @todo outsource
*/
export async function get_data<type_input, type_output>(
path : string,
{
"default_active": default_active = true,
} : {
default_active ?: boolean;
} = {
}
) : Promise<Array<type_testcase<type_input, type_output>>>
{
const content : string = await lib_plankton.file.read(path);
const testcases_raw : Array<type_testcase_raw<type_input, type_output>> = (
lib_plankton.json.decode(content) as Array<type_testcase_raw<type_input, type_output>>
);
const testcases : Array<type_testcase<type_input, type_output>> = (
testcases_raw
.filter(
testcase_raw => (testcase_raw.active ?? default_active),
)
.map(
(testcase_raw, index) => ({
"name": (
testcase_raw.name
??
lib_plankton.string.coin(
"{{path}} | #{{index}}",
{
"path": path,
"index": (index + 1).toFixed(0),
}
)
),
"input": testcase_raw.input,
"output": testcase_raw.output,
})
)
);
return testcases;
}
/**
* @todo outsource
*/
export function start(
name : string
) : void
{
lib_plankton.log._info(
"test.run",
{
"details": {
"name": name,
}
}
);
}
/**
* @todo outsource
*/
export function fail(
name : string
) : void
{
lib_plankton.log._error(
"test.failed",
{
"details": {
"name": name,
}
}
);
}
}