Skip to content

Commit

Permalink
test: fix some test
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKreil committed Aug 27, 2024
1 parent 1078ee7 commit 3a21ac4
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/build_fonts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('build fonts', () => {
.toStrictEqual([['dist', { recursive: true }]]);

expect(jest.mocked(getFontSources).mock.calls)
.toStrictEqual([['font-sources']]);
.toStrictEqual([['fonts']]);

expect(jest.mocked(buildAllGlyphs).mock.calls)
.toStrictEqual([[[fontSource]]]);
Expand Down
30 changes: 15 additions & 15 deletions src/lib/fonts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { jest } from '@jest/globals';

const mockFiles: Record<string, string[]> = {
'/fonts': ['Family 1', 'Family 2', '_ignoredDirectory'],
'/fonts/Family 1': ['fonts.json', 'Family1_bold.otf', 'Family1_italic.ttf', 'Family1.otf'],
'/fonts/Family 2': ['Family2_bold.ttf', 'Family2_italic.otf'],
'/fonts/Family 1': ['fonts.json', 'Family 1 bold.otf', 'Family 1 - Italic.ttf', 'Family_1_regular.otf'],
'/fonts/Family 2': ['Family 2 bold.ttf', 'Family_2_italic.otf'],
};

jest.unstable_mockModule('node:fs', () => ({
Expand All @@ -16,7 +16,7 @@ jest.unstable_mockModule('node:fs', () => ({
})),
readFileSync: jest.fn((path: string) => {
if (path === '/fonts/family1/fonts.json') {
return JSON.stringify([{ name: 'fontFamily1', sources: ['font1.ttf', 'font2.otf'] }]);
return JSON.stringify([{ name: 'Family 1 - Regular', sources: ['font1.otf', 'font2.ttf'] }]);
}
return `content of ${path}`;
}),
Expand All @@ -27,67 +27,67 @@ const { getFontSources } = await import('./fonts.ts');
describe('getFontSources', () => {
it('should process font directories and ignore others', () => {
const sources = getFontSources('/fonts');

expect(sources).toStrictEqual([
{
fontFace: {
familyId: 'family_1',
familyName: 'Family 1',
fontId: 'family_1_bold',
fontName: 'Family 1 bold',
fontName: 'Family 1 - Bold',
italic: false,
styleName: 'Bold',
weight: 700,
},
'sources': ['content of /fonts/Family 1/Family1_bold.otf'],
'sources': ['content of /fonts/Family 1/Family 1 bold.otf'],
},
{
fontFace: {
familyId: 'family_1',
familyName: 'Family 1',
fontId: 'family_1_italic',
fontName: 'Family 1 italic',
fontName: 'Family 1 - Italic',
italic: true,
styleName: 'Italic',
weight: 400,
},
'sources': ['content of /fonts/Family 1/Family1_italic.ttf'],
'sources': ['content of /fonts/Family 1/Family 1 - Italic.ttf'],
},
{
fontFace: {
familyId: 'family_1',
familyName: 'Family 1',
fontId: 'family_1',
fontName: 'Family 1',
fontId: 'family_1_regular',
fontName: 'Family 1 - Regular',
italic: false,
styleName: 'Regular',
weight: 400,
},
'sources': ['content of /fonts/Family 1/Family1.otf'],
'sources': ['content of /fonts/Family 1/Family_1_regular.otf'],
},
{
fontFace: {
familyId: 'family_2',
familyName: 'Family 2',
fontId: 'family_2_bold',
fontName: 'Family 2 bold',
fontName: 'Family 2 - Bold',
italic: false,
styleName: 'Bold',
weight: 700,
},
'sources': ['content of /fonts/Family 2/Family2_bold.ttf'],
'sources': ['content of /fonts/Family 2/Family 2 bold.ttf'],
},
{
fontFace: {
familyId: 'family_2',
familyName: 'Family 2',
fontId: 'family_2_italic',
fontName: 'Family 2 italic',
fontName: 'Family 2 - Italic',
italic: true,
styleName: 'Italic',
weight: 400,
},
'sources': ['content of /fonts/Family 2/Family2_italic.otf'],
'sources': ['content of /fonts/Family 2/Family_2_italic.otf'],
},
]);
});
Expand Down
15 changes: 10 additions & 5 deletions src/lib/fonts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export function getFontSources(inputDir: string): FontSourcesWrapper[] {
// compatible font name generation with genfontgl
let name = basename(file);
name = name.replace(/\..*?$/, '');
name = name.replace(/\-/g, '');
name = name.replace(/\s+/, ' ').trim();
name = name.replace(/[-_\s]+/g, ' ').trim();
fonts.push({ name, sources: [basename(file)] });
}
});
Expand Down Expand Up @@ -83,11 +82,17 @@ function getFontFace(fontName: string, familyName: string): FontFace {
if (italic) styleParts.push('Italic');
const styleName = (styleParts.length > 0) ? styleParts.join(' ') : 'Regular';

const newFontName = familyName + ' - ' + styleName;
const textToId = (s: string): string => s.toLowerCase().replace(/[\s_-]+/g, '_');
if (textToId(newFontName) !== textToId(fontName)) {
throw Error(`font name "${fontName}" should be "${newFontName}"`);
}

const fontFace: FontFace = {
fontName,
fontId: fontName.toLowerCase().replace(/\s/g, '_'),
fontName: newFontName,
fontId: textToId(newFontName),
familyName,
familyId: familyName.toLowerCase().replace(/\s/g, '_'),
familyId: textToId(familyName),
styleName,
italic,
weight,
Expand Down
9 changes: 5 additions & 4 deletions src/lib/glyphs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ jest.unstable_mockModule('./progress.ts', () => ({
Progress: function () {
return {
increase: () => {
return;
return;
},
finish: () => {
return;
return;
},
};
},
Expand All @@ -20,15 +20,16 @@ const { getFontSources } = await import('./fonts.ts');
describe('buildAllGlyphs', () => {
it('should build all glyphs from font sources', async () => {
process.chdir(new URL('../../', import.meta.url).pathname);
const fontSources = getFontSources('font-sources')

const fontSources = getFontSources('fonts')
.filter(f => f.fontFace.fontId === 'fira_sans_bold');

const result = await buildAllGlyphs(fontSources);

expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(1);
expect(result[0].fontFace).toStrictEqual({
fontName: 'Fira Sans Bold',
fontName: 'Fira Sans - Bold',
fontId: 'fira_sans_bold',
familyName: 'Fira Sans',
familyId: 'fira_sans',
Expand Down

0 comments on commit 3a21ac4

Please sign in to comment.