Skip to content

Commit

Permalink
refactor: specs
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkeyl committed Jan 16, 2025
1 parent c3a1d82 commit 825b781
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
40 changes: 23 additions & 17 deletions specs/validate-font-file-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ import { describe, expect, test as spec } from 'bun:test';

import { validateFontFileName } from '#src/index.ts';

const positiveCases = [
const validTestCases = [
'FontFamily-UltraBlack.woff2',
'src/fonts/FontFamily-ExtraBlack.woff',
'Font-Heavy.otf',
'FontFamily-Black.ttf',
'FontFamily-UltraBoldItalic.woff2',
'FontFamily-ExtraBoldVariable.woff2',
'FontFamily-BoldItalicVariable.woff2',
'path/to/fonts/Roboto-Bold.otf',
'another/path/OpenSans-Light.ttf',
];

const positiveCustomCases = ['fontfamily-ultrablack.woff2'];
const validCustomCases = [
{ file: 'fontfamily-ultrablack.woff2', regex: /^[a-z]+-[a-z]+\.woff2$/ },
];

const negativeCases = [
const invalidTestCases = [
'',
'Font-Family-UltraBlack.woff2',
'src/fonts/FontFamily.woff',
Expand All @@ -34,34 +38,36 @@ const negativeCases = [
'FontFamily-.otf',
'-UltraLight.ttf',
'FontFamily.woff2',
'path/to/fonts/Roboto-Bold',
'another/path/OpenSans-Light.pdf',
];

const negativeCustomCases = ['FONTFAMILY-ULTRABLACK.WOFF2'];
const invalidCustomCases = [
{ file: 'FONTFAMILY-ULTRABLACK.WOFF2', regex: /^[a-z]+-[a-z]+\.woff2$/ },
];

describe('Validate Font File Name', async () => {
positiveCases.forEach(async (file) => {
describe('Validate Font File Name', () => {
validTestCases.forEach((file) => {
spec(`should validate ${file}`, async () => {
expect(await validateFontFileName({ file })).toEqual(true);
expect(await validateFontFileName({ file })).toBe(true);
});
});

negativeCases.forEach(async (file) => {
invalidTestCases.forEach((file) => {
spec(`should not validate ${file}`, async () => {
expect(await validateFontFileName({ file })).toEqual(false);
expect(await validateFontFileName({ file })).toBe(false);
});
});

positiveCustomCases.forEach(async (file) => {
spec(`should validate ${file}`, async () => {
expect(await validateFontFileName({ file, regex: /\w+/i })).toEqual(true);
validCustomCases.forEach(({ file, regex }) => {
spec(`should validate ${file} with custom regex`, async () => {
expect(await validateFontFileName({ file, regex })).toBe(true);
});
});

negativeCustomCases.forEach(async (file) => {
spec(`should not validate ${file}`, async () => {
expect(
await validateFontFileName({ file, regex: new RegExp('123', 'g') }),
).toEqual(false);
invalidCustomCases.forEach(({ file, regex }) => {
spec(`should not validate ${file} with custom regex`, async () => {
expect(await validateFontFileName({ file, regex })).toBe(false);
});
});
});
11 changes: 0 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ import { FONT_FILE_NAME_REGEX } from '@archoleat/reglib';

import type { Parameters } from '#types/parameters.ts';

/**
* Validates the font file name.
*
* @param {string} file - The file path of the font file.
*
* @param {string|RegExp} [regex=''] - The regular expression pattern to
* match the font file name.
*
* @return {boolean} - Return true if the font file name matches the
* specified or default regex pattern, false otherwise.
*/
const validateFontFileName = async (parameters: Parameters) => {
const { file, regex = '' } = parameters;

Expand Down

0 comments on commit 825b781

Please sign in to comment.