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 {
InputTest,
InputTestBatches,
InputTestInputs,
CallbackAppendArgs,
TestInputFunction,
InputTestBatchFinishCallback,
InputTestFailureCallback,
InputTestSuccessCallback,
} 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 = ({
excludeTestIds = [],
inputs = {},
isContinueOnFailure,
isIgnoreOnCallbacks,
isIgnoreOnCallbacks: isIgnoreAllOnCallbacks,
isTestAll = Object.keys(inputs).length === 0,
tests = {},
} = {}): boolean => {
let testsToRun: InputTestInputs = {};
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) {
Object.keys(tests).forEach((id: string) => {
testsToRun[id] = {};
@ -55,6 +73,7 @@ const testInput: TestInputFunction = ({
displayMin: dDisplayMin,
getCompare: dGetCompare,
getValue: dGetValue,
isIgnoreOnCallbacks: dIsIgnoreOnCallbacks = isIgnoreAllOnCallbacks,
max: dMax = 0,
min: dMin = 0,
onSuccess: dOnSuccess,
@ -67,6 +86,7 @@ const testInput: TestInputFunction = ({
const {
getCompare = dGetCompare,
getValue = dGetValue,
isIgnoreOnCallbacks = dIsIgnoreOnCallbacks,
max = dMax,
min = dMin,
compare = getCompare?.call(null) ?? dCompare,
@ -75,7 +95,10 @@ const testInput: TestInputFunction = ({
displayMin = dDisplayMin || String(min),
} = testsToRun[id];
const { cbFinishBatch } = setBatchCallback({ onFinishBatch });
const { cbFinishBatch, setTestCallbacks } = evalIsIgnoreOnCallbacks({
isIgnoreOnCallbacks,
onFinishBatch,
});
const runTest: (test: InputTest) => boolean = ({
onFailure,
@ -91,7 +114,7 @@ const testInput: TestInputFunction = ({
value,
});
const { cbFailure, cbSuccess } = setSingleCallback({
const { cbFailure, cbSuccess } = setTestCallbacks({
onFailure,
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 = {
compare?: InputTestValue[];
@ -6,13 +12,21 @@ export type InputTestArgs = {
displayMin?: string;
getCompare?: () => InputTestValue[];
getValue?: () => InputTestValue;
isIgnoreOnCallbacks?: boolean;
max?: bigint | number;
min?: bigint | number;
value?: InputTestValue;
};
export type MinimalInputTestArgs = Required<
Omit<InputTestArgs, 'displayMax' | 'displayMin' | 'getCompare' | 'getValue'>
Omit<
InputTestArgs,
| 'displayMax'
| 'displayMin'
| 'getCompare'
| 'getValue'
| 'isIgnoreOnCallbacks'
>
>;
export type CallbackAppendArgs = {
@ -21,10 +35,14 @@ export type CallbackAppendArgs = {
};
};
export type InputTestFailureCallback = (
args: InputTestArgs & CallbackAppendArgs,
) => void;
export type InputTestSuccessCallback = () => void;
export type InputTest = {
onFailure?: (args: InputTestArgs & CallbackAppendArgs) => void;
onFailure?: InputTestFailureCallback;
onSuccess?: InputTestSuccessCallback;
test: (args: MinimalInputTestArgs & CallbackAppendArgs) => boolean;
};
@ -33,12 +51,14 @@ export type InputTestInputs = {
[id: string]: Partial<InputTestArgs>;
};
export type InputTestBatchFinishCallback = () => void;
export type InputTestBatches = {
[id: string]: {
defaults?: InputTestArgs & {
onSuccess?: InputTestSuccessCallback;
};
onFinishBatch?: () => void;
onFinishBatch?: InputTestBatchFinishCallback;
optionalTests?: Array<InputTest>;
tests: Array<InputTest>;
};

Loading…
Cancel
Save