Skip to content

Commit

Permalink
fix(tests): use .each convention for tests
Browse files Browse the repository at this point in the history
closes #11
  • Loading branch information
kalisjoshua committed Dec 3, 2024
1 parent 702bc47 commit 12ea115
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 239 deletions.
46 changes: 46 additions & 0 deletions packages/constants/src/__fixtures__/boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2024 Hypergiant Galactic Systems Inc. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
type Callable = (...a: any[]) => any;
type Fixture = [string, boolean, Callable, Values][];
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
type Values = any[];

export const truthy: Values = [
1,
'1',
'on',
'true',
'yes',
true,
'ON',
'YES',
'TRUE',
];
export const falsey: Values = [
'',
0,
'0',
'off',
'false',
'no',
false,
[],
{},
'OFF',
'NO',
'FALSE',
];

export const createFixture = (bool: boolean, ...rest: [Values, Callable][]) =>
rest.map(([values, fn]) => [fn.name, bool, fn, values]) as Fixture;
17 changes: 7 additions & 10 deletions packages/converters/src/boolean-to-number/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
import { describe, expect, it } from 'vitest';
import { booleanToNumber } from './';

const testPairs: [boolean, number][] = [
[true, 1],
[false, 0],
];

describe('bool to number', () => {
for (const [input, result] of testPairs) {
it(`should convert ${input} to ${result}`, () => {
expect(booleanToNumber(input)).toEqual(result);
});
}
it('should convert true to 1', () => {
expect(booleanToNumber(true)).toEqual(1);
});

it('should convert false to 0', () => {
expect(booleanToNumber(false)).toEqual(0);
});
});
43 changes: 17 additions & 26 deletions packages/converters/src/to-boolean/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,25 @@
* governing permissions and limitations under the License.
*/

import { expect, it, describe } from 'vitest';
import { toBoolean } from './';
import { describe, expect, it } from 'vitest';

const truthy = [1, '1', 'on', 'true', 'yes', true, 'ON', 'YES', 'TRUE'];
const falsey = [
0,
'0',
'off',
'false',
'no',
false,
[],
{},
'OFF',
'NO',
'FALSE',
];
import {
createFixture,
falsey,
truthy,
} from '../../../constants/src/__fixtures__/boolean';

describe('toBoolean', () => {
for (const item of truthy) {
it(`should return true for ${item}`, () => {
expect(toBoolean(item)).toBeTruthy();
});
}
import { toBoolean } from './';

for (const item of falsey) {
it(`should return false for ${item}`, () => {
expect(toBoolean(item)).not.toBeTruthy();
describe('toBoolean', () => {
describe.each([
// positive cases
...createFixture(true, [truthy, toBoolean]),
// negative cases
...createFixture(false, [falsey, toBoolean]),
])('%s', (_name, expected, fn, values) => {
it.each(values)(`should return "${expected}" for %j`, (value) => {
expect(fn(value)).toBe(expected);
});
}
});
});
2 changes: 1 addition & 1 deletion packages/predicates/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export { isBbox } from './is-bbox';
export { isLatitude } from './is-latitude';
export { isLongitude } from './is-longitude';
export { isNothing } from './is-nothing';
export { isFalse, isNo, isTrue, isYes } from './is-noyes';
export { isFalse, isNo, isOff, isOn, isTrue, isYes } from './is-noyes';
export {
isFiniteNumber,
isFiniteNumeric,
Expand Down
51 changes: 33 additions & 18 deletions packages/predicates/src/is-noyes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,41 @@
* governing permissions and limitations under the License.
*/

import { describe, it, expect } from 'vitest';
import { isTrue, isYes, isFalse, isNo, isOn, isOff } from './';
import { describe, expect, it } from 'vitest';

const truthy = [1, '1', 'on', 'true', 'yes', true, 'ON', 'YES', 'TRUE'];
const falsey = [0, '0', 'off', 'false', 'no', false, 'OFF', 'NO', 'FALSE'];
import {
createFixture,
falsey,
truthy,
} from '../../../constants/src/__fixtures__/boolean';

describe('boolean validators', () => {
for (const item of truthy) {
it(`should return true for ${item}`, () => {
expect(isOn(item)).toBeTruthy();
expect(isTrue(item)).toBeTruthy();
expect(isYes(item)).toBeTruthy();
});
}
import { isFalse, isNo, isOff, isOn, isTrue, isYes } from './';

for (const item of falsey) {
it(`should return false for ${item}`, () => {
expect(isFalse(item)).toBeTruthy();
expect(isOff(item)).toBeTruthy();
expect(isNo(item)).toBeTruthy();
describe('boolean validators', () => {
describe.each([
// positive cases
...createFixture(
true,
[falsey, isFalse],
[falsey, isNo],
[falsey, isOff],
[truthy, isOn],
[truthy, isTrue],
[truthy, isYes],
),
// negative cases
...createFixture(
false,
[truthy, isFalse],
[truthy, isNo],
[truthy, isOff],
[falsey, isOn],
[falsey, isTrue],
[falsey, isYes],
),
])('%s', (_name, expected, fn, values) => {
it.each(values)(`should return "${expected}" for %j`, (value) => {
expect(fn(value)).toBe(expected);
});
}
});
});
9 changes: 3 additions & 6 deletions packages/predicates/src/is-noyes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
* governing permissions and limitations under the License.
*/

// const trueRegex = /^(?:y|yes|true|1|on)$/i;
// const falseRegex = /^(?:n|no|false|0|off)$/i;

// const test = (r: RegExp, val: unknown) => r.test(`${val}`.trim());

const falseValues = ['0', 'false', 'n', 'no', 'off'];
// NOTE: a stringify'd empty array will result in ''; a stringify'd object will
// result in '[object Object]' which would then need to be lower cased as below
const falseValues = ['', '0', 'false', 'n', 'no', 'off', '[object object]'];
const trueValues = ['1', 'true', 'y', 'yes', 'on'];

const test = (list: string[], val: unknown) =>
Expand Down
Loading

0 comments on commit 12ea115

Please sign in to comment.