fix(striker-ui): allow each test batch to override isIgnoreOnCallbacks

main
Tsu-ba-me 2 years ago
parent 5c0f06be00
commit c496b90b62
  1. 67
      striker-ui/lib/test_input/testInput.ts
  2. 28
      striker-ui/types/TestInputFunction.ts

@ -1,40 +1,58 @@
import { import {
InputTest, InputTest,
InputTestBatches,
InputTestInputs, InputTestInputs,
CallbackAppendArgs, CallbackAppendArgs,
TestInputFunction, TestInputFunction,
InputTestBatchFinishCallback,
InputTestFailureCallback,
InputTestSuccessCallback,
} from '../../types/TestInputFunction'; } from '../../types/TestInputFunction';
type TestCallbacks = Pick<InputTest, 'onFailure' | 'onSuccess'>;
const cbEmptySetter = () => ({});
const cbSetter = ({
onFailure,
onSuccess,
}: Pick<InputTest, 'onFailure' | 'onSuccess'>) => ({
cbFailure: onFailure,
cbSuccess: onSuccess,
});
const evalIsIgnoreOnCallbacks = ({
isIgnoreOnCallbacks,
onFinishBatch,
}: {
isIgnoreOnCallbacks?: boolean;
onFinishBatch?: InputTestBatchFinishCallback;
}): {
cbFinishBatch?: InputTestBatchFinishCallback;
setTestCallbacks: (testCallbacks: TestCallbacks) => {
cbFailure?: InputTestFailureCallback;
cbSuccess?: InputTestSuccessCallback;
};
} =>
isIgnoreOnCallbacks
? {
setTestCallbacks: cbEmptySetter,
}
: {
cbFinishBatch: onFinishBatch,
setTestCallbacks: cbSetter,
};
const testInput: TestInputFunction = ({ const testInput: TestInputFunction = ({
excludeTestIds = [], excludeTestIds = [],
inputs = {}, inputs = {},
isContinueOnFailure, isContinueOnFailure,
isIgnoreOnCallbacks, isIgnoreOnCallbacks: isIgnoreAllOnCallbacks,
isTestAll = Object.keys(inputs).length === 0, isTestAll = Object.keys(inputs).length === 0,
tests = {}, tests = {},
} = {}): boolean => { } = {}): boolean => {
let testsToRun: InputTestInputs = {}; let testsToRun: InputTestInputs = {};
let allResult = true; let allResult = true;
let setBatchCallback: (batch?: Partial<InputTestBatches[string]>) => {
cbFinishBatch: InputTestBatches[string]['onFinishBatch'];
} = () => ({ cbFinishBatch: undefined });
let setSingleCallback: (test?: Partial<InputTest>) => {
cbFailure: InputTest['onFailure'];
cbSuccess: InputTest['onSuccess'];
} = () => ({ cbFailure: undefined, cbSuccess: undefined });
if (!isIgnoreOnCallbacks) {
setBatchCallback = ({ onFinishBatch } = {}) => ({
cbFinishBatch: onFinishBatch,
});
setSingleCallback = ({ onFailure, onSuccess } = {}) => ({
cbFailure: onFailure,
cbSuccess: onSuccess,
});
}
if (isTestAll) { if (isTestAll) {
Object.keys(tests).forEach((id: string) => { Object.keys(tests).forEach((id: string) => {
testsToRun[id] = {}; testsToRun[id] = {};
@ -55,6 +73,7 @@ const testInput: TestInputFunction = ({
displayMin: dDisplayMin, displayMin: dDisplayMin,
getCompare: dGetCompare, getCompare: dGetCompare,
getValue: dGetValue, getValue: dGetValue,
isIgnoreOnCallbacks: dIsIgnoreOnCallbacks = isIgnoreAllOnCallbacks,
max: dMax = 0, max: dMax = 0,
min: dMin = 0, min: dMin = 0,
onSuccess: dOnSuccess, onSuccess: dOnSuccess,
@ -67,6 +86,7 @@ const testInput: TestInputFunction = ({
const { const {
getCompare = dGetCompare, getCompare = dGetCompare,
getValue = dGetValue, getValue = dGetValue,
isIgnoreOnCallbacks = dIsIgnoreOnCallbacks,
max = dMax, max = dMax,
min = dMin, min = dMin,
compare = getCompare?.call(null) ?? dCompare, compare = getCompare?.call(null) ?? dCompare,
@ -75,7 +95,10 @@ const testInput: TestInputFunction = ({
displayMin = dDisplayMin || String(min), displayMin = dDisplayMin || String(min),
} = testsToRun[id]; } = testsToRun[id];
const { cbFinishBatch } = setBatchCallback({ onFinishBatch }); const { cbFinishBatch, setTestCallbacks } = evalIsIgnoreOnCallbacks({
isIgnoreOnCallbacks,
onFinishBatch,
});
const runTest: (test: InputTest) => boolean = ({ const runTest: (test: InputTest) => boolean = ({
onFailure, onFailure,
@ -91,7 +114,7 @@ const testInput: TestInputFunction = ({
value, value,
}); });
const { cbFailure, cbSuccess } = setSingleCallback({ const { cbFailure, cbSuccess } = setTestCallbacks({
onFailure, onFailure,
onSuccess, onSuccess,
}); });

@ -1,4 +1,10 @@
export type InputTestValue = bigint | number | null | string | undefined; export type InputTestValue =
| bigint
| boolean
| number
| null
| string
| undefined;
export type InputTestArgs = { export type InputTestArgs = {
compare?: InputTestValue[]; compare?: InputTestValue[];
@ -6,13 +12,21 @@ export type InputTestArgs = {
displayMin?: string; displayMin?: string;
getCompare?: () => InputTestValue[]; getCompare?: () => InputTestValue[];
getValue?: () => InputTestValue; getValue?: () => InputTestValue;
isIgnoreOnCallbacks?: boolean;
max?: bigint | number; max?: bigint | number;
min?: bigint | number; min?: bigint | number;
value?: InputTestValue; value?: InputTestValue;
}; };
export type MinimalInputTestArgs = Required< export type MinimalInputTestArgs = Required<
Omit<InputTestArgs, 'displayMax' | 'displayMin' | 'getCompare' | 'getValue'> Omit<
InputTestArgs,
| 'displayMax'
| 'displayMin'
| 'getCompare'
| 'getValue'
| 'isIgnoreOnCallbacks'
>
>; >;
export type CallbackAppendArgs = { export type CallbackAppendArgs = {
@ -21,10 +35,14 @@ export type CallbackAppendArgs = {
}; };
}; };
export type InputTestFailureCallback = (
args: InputTestArgs & CallbackAppendArgs,
) => void;
export type InputTestSuccessCallback = () => void; export type InputTestSuccessCallback = () => void;
export type InputTest = { export type InputTest = {
onFailure?: (args: InputTestArgs & CallbackAppendArgs) => void; onFailure?: InputTestFailureCallback;
onSuccess?: InputTestSuccessCallback; onSuccess?: InputTestSuccessCallback;
test: (args: MinimalInputTestArgs & CallbackAppendArgs) => boolean; test: (args: MinimalInputTestArgs & CallbackAppendArgs) => boolean;
}; };
@ -33,12 +51,14 @@ export type InputTestInputs = {
[id: string]: Partial<InputTestArgs>; [id: string]: Partial<InputTestArgs>;
}; };
export type InputTestBatchFinishCallback = () => void;
export type InputTestBatches = { export type InputTestBatches = {
[id: string]: { [id: string]: {
defaults?: InputTestArgs & { defaults?: InputTestArgs & {
onSuccess?: InputTestSuccessCallback; onSuccess?: InputTestSuccessCallback;
}; };
onFinishBatch?: () => void; onFinishBatch?: InputTestBatchFinishCallback;
optionalTests?: Array<InputTest>; optionalTests?: Array<InputTest>;
tests: Array<InputTest>; tests: Array<InputTest>;
}; };

Loading…
Cancel
Save