Skip to content

Commit

Permalink
♻️ Move tests to src/
Browse files Browse the repository at this point in the history
Keeping tests next to the source files helps to find the corresponding
tests quicker, spot missing tests, and it results in simpler imports.
  • Loading branch information
ralfstx committed Oct 3, 2023
1 parent 122ddfa commit 0805184
Show file tree
Hide file tree
Showing 30 changed files with 85 additions and 89 deletions.
2 changes: 1 addition & 1 deletion test/binary-data.test.ts → src/binary-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import { parseBinaryData } from '../src/binary-data.js';
import { parseBinaryData } from './binary-data.js';

describe('binary-data', () => {
const data = Uint8Array.of(1, 183, 0);
Expand Down
2 changes: 1 addition & 1 deletion test/box.test.ts → src/box.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import { parseEdges, parseLength, subtractEdges } from '../src/box.js';
import { parseEdges, parseLength, subtractEdges } from './box.js';

describe('box', () => {
describe('subtractEdges', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/colors.test.ts → src/colors.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import { parseColor } from '../src/colors.js';
import { parseColor } from './colors.js';

const { closeTo } = expect;

Expand Down
2 changes: 1 addition & 1 deletion test/document.test.ts → src/document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, expect, it } from '@jest/globals';
import crypto from 'crypto';
import { PDFDict, PDFDocument, PDFHexString, PDFName, PDFStream, PDFString } from 'pdf-lib';

import { renderDocument } from '../src/document.js';
import { renderDocument } from './document.js';

global.crypto ??= (crypto as any).webcrypto;

Expand Down
4 changes: 2 additions & 2 deletions test/fonts.test.ts → src/fonts.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeEach, describe, expect, it } from '@jest/globals';

import { Font, loadFonts, readFonts, selectFont } from '../src/fonts.js';
import { fakeFont } from './test-utils.js';
import { Font, loadFonts, readFonts, selectFont } from './fonts.js';
import { fakeFont } from './test/test-utils.js';

describe('fonts', () => {
describe('readFonts', () => {
Expand Down
10 changes: 5 additions & 5 deletions test/images.test.ts → src/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import crypto from 'crypto';
import { readFileSync } from 'fs';
import { join } from 'path';

import { Image, loadImages, readImages, registerImage } from '../src/images.js';
import { fakePDFDocument, mkData } from './test-utils.js';
import { Image, loadImages, readImages, registerImage } from './images.js';
import { fakePDFDocument, mkData } from './test/test-utils.js';

global.crypto ??= (crypto as any).webcrypto;

Expand Down Expand Up @@ -59,15 +59,15 @@ describe('images', () => {
});

it('reads width and height from JPEG image', async () => {
const data = readFileSync(join(__dirname, 'resources/liberty.jpg'));
const data = readFileSync(join(__dirname, './test/resources/liberty.jpg'));

const images = await loadImages([{ name: 'liberty', data, format: 'jpeg' }]);

expect(images).toEqual([{ name: 'liberty', data, format: 'jpeg', width: 160, height: 240 }]);
});

it('reads width and height from PNG image', async () => {
const data = readFileSync(join(__dirname, 'resources/torus.png'));
const data = readFileSync(join(__dirname, './test/resources/torus.png'));

const images = await loadImages([{ name: 'torus', data, format: 'png' }]);

Expand All @@ -78,7 +78,7 @@ describe('images', () => {
describe('registerImage', () => {
it('embeds image in PDF document and attaches ref', () => {
const doc = fakePDFDocument();
const data = readFileSync(join(__dirname, 'resources/liberty.jpg'));
const data = readFileSync(join(__dirname, './test/resources/liberty.jpg'));
const image: Image = { name: 'foo', format: 'jpeg', data, width: 100, height: 200 };

const pdfRef = registerImage(image, doc);
Expand Down
10 changes: 5 additions & 5 deletions test/layout-columns.test.ts → src/layout-columns.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { beforeEach, describe, expect, it } from '@jest/globals';

import { Box } from '../src/box.js';
import { Document } from '../src/document.js';
import { layoutColumnsContent } from '../src/layout-columns.js';
import { Block } from '../src/read-block.js';
import { fakeFont, span } from './test-utils.js';
import { Box } from './box.js';
import { Document } from './document.js';
import { layoutColumnsContent } from './layout-columns.js';
import { Block } from './read-block.js';
import { fakeFont, span } from './test/test-utils.js';

const { objectContaining } = expect;

Expand Down
10 changes: 5 additions & 5 deletions test/layout-image.test.ts → src/layout-image.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { beforeEach, describe, expect, it } from '@jest/globals';

import { Box } from '../src/box.js';
import { Document } from '../src/document.js';
import { layoutImageContent } from '../src/layout-image.js';
import { ImageBlock } from '../src/read-block.js';
import { fakeImage } from './test-utils.js';
import { Box } from './box.js';
import { Document } from './document.js';
import { layoutImageContent } from './layout-image.js';
import { ImageBlock } from './read-block.js';
import { fakeImage } from './test/test-utils.js';

const { objectContaining } = expect;

Expand Down
12 changes: 6 additions & 6 deletions test/layout-rows.test.ts → src/layout-rows.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { beforeEach, describe, expect, it } from '@jest/globals';

import { Box } from '../src/box.js';
import { Document } from '../src/document.js';
import { Frame } from '../src/layout.js';
import { layoutRowsContent } from '../src/layout-rows.js';
import { Block } from '../src/read-block.js';
import { extractTextRows, fakeFont, range, span } from './test-utils.js';
import { Box } from './box.js';
import { Document } from './document.js';
import { Frame } from './layout.js';
import { layoutRowsContent } from './layout-rows.js';
import { Block } from './read-block.js';
import { extractTextRows, fakeFont, range, span } from './test/test-utils.js';

describe('layout-rows', () => {
let doc: Document, box: Box;
Expand Down
10 changes: 5 additions & 5 deletions test/layout-text.test.ts → src/layout-text.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { rgb } from 'pdf-lib';

import { Box } from '../src/box.js';
import { Document } from '../src/document.js';
import { layoutTextContent } from '../src/layout-text.js';
import { paperSizes } from '../src/page-sizes.js';
import { extractTextRows, fakeFont, range, span } from './test-utils.js';
import { Box } from './box.js';
import { Document } from './document.js';
import { layoutTextContent } from './layout-text.js';
import { paperSizes } from './page-sizes.js';
import { extractTextRows, fakeFont, range, span } from './test/test-utils.js';

const { objectContaining } = expect;

Expand Down
14 changes: 7 additions & 7 deletions test/layout.test.ts → src/layout.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { beforeEach, describe, expect, it } from '@jest/globals';

import { Box } from '../src/box.js';
import { Document } from '../src/document.js';
import { isBreakPossible, layoutBlock, layoutPages } from '../src/layout.js';
import { paperSizes } from '../src/page-sizes.js';
import { Block, TextAttrs, TextSpan } from '../src/read-block.js';
import { PageInfo, readDocumentDefinition } from '../src/read-document.js';
import { fakeFont, p, range } from './test-utils.js';
import { Box } from './box.js';
import { Document } from './document.js';
import { isBreakPossible, layoutBlock, layoutPages } from './layout.js';
import { paperSizes } from './page-sizes.js';
import { Block, TextAttrs, TextSpan } from './read-block.js';
import { PageInfo, readDocumentDefinition } from './read-document.js';
import { fakeFont, p, range } from './test/test-utils.js';

const { objectContaining } = expect;

Expand Down
2 changes: 1 addition & 1 deletion test/make-pdf.test.ts → src/make-pdf.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from '@jest/globals';
import crypto from 'crypto';

import { makePdf } from '../src/make-pdf.js';
import { makePdf } from './make-pdf.js';

global.crypto ??= (crypto as any).webcrypto;

Expand Down
7 changes: 1 addition & 6 deletions test/page-sizes.test.ts → src/page-sizes.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import {
applyOrientation,
paperSizes,
parseOrientation,
parsePageSize,
} from '../src/page-sizes.js';
import { applyOrientation, paperSizes, parseOrientation, parsePageSize } from './page-sizes.js';

describe('page-sizes', () => {
describe('parsePageSize', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/page.test.ts → src/page.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { PDFPage } from 'pdf-lib';

import { Font } from '../src/fonts.js';
import { addPageFont, getExtGraphicsState, Page } from '../src/page.js';
import { fakeFont, fakePDFPage } from './test-utils.js';
import { Font } from './fonts.js';
import { addPageFont, getExtGraphicsState, Page } from './page.js';
import { fakeFont, fakePDFPage } from './test/test-utils.js';

describe('page', () => {
let page: Page, pdfPage: PDFPage;
Expand Down
2 changes: 1 addition & 1 deletion test/print-value.test.ts → src/print-value.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import { printValue } from '../src/print-value.js';
import { printValue } from './print-value.js';

describe('print-value', () => {
describe('printValue', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/read-block.test.ts → src/read-block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
readRowsBlock,
readText,
readTextBlock,
} from '../src/read-block.js';
} from './read-block.js';

describe('read-block', () => {
describe('readBlock', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/read-document.test.ts → src/read-document.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it } from '@jest/globals';

import { PageInfo, readDocumentDefinition } from '../src/read-document.js';
import { PageInfo, readDocumentDefinition } from './read-document.js';

describe('read-document', () => {
describe('readDocumentDefinition', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/read-graphics.test.ts → src/read-graphics.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, it } from '@jest/globals';
import { rgb } from 'pdf-lib';

import { readShape } from '../src/read-graphics.js';
import { p } from './test-utils.js';
import { readShape } from './read-graphics.js';
import { p } from './test/test-utils.js';

const transformAttrs = {
translate: { x: 1, y: 2 },
Expand Down
10 changes: 5 additions & 5 deletions test/render-graphics.test.ts → src/render-graphics.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { rgb } from 'pdf-lib';

import { Size } from '../src/box.js';
import { Page } from '../src/page.js';
import { Size } from './box.js';
import { Page } from './page.js';
import {
CircleObject,
LineObject,
PathObject,
PolylineObject,
RectObject,
} from '../src/read-graphics.js';
import { renderGraphics } from '../src/render-graphics.js';
import { fakePDFPage, getContentStream, p } from './test-utils.js';
} from './read-graphics.js';
import { renderGraphics } from './render-graphics.js';
import { fakePDFPage, getContentStream, p } from './test/test-utils.js';

describe('render-graphics', () => {
let page: Page, size: Size;
Expand Down
12 changes: 6 additions & 6 deletions test/render-image.test.ts → src/render-image.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { beforeEach, describe, expect, it } from '@jest/globals';

import { Size } from '../src/box.js';
import { Image } from '../src/images.js';
import { ImageObject } from '../src/layout-image.js';
import { Page } from '../src/page.js';
import { renderImage } from '../src/render-image.js';
import { fakePDFPage, getContentStream } from './test-utils.js';
import { Size } from './box.js';
import { Image } from './images.js';
import { ImageObject } from './layout-image.js';
import { Page } from './page.js';
import { renderImage } from './render-image.js';
import { fakePDFPage, getContentStream } from './test/test-utils.js';

describe('render-image', () => {
let page: Page, size: Size, image: Image;
Expand Down
12 changes: 6 additions & 6 deletions test/render-page.test.ts → src/render-page.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import { PDFArray, PDFDict, PDFDocument, PDFName, PDFPage, PDFRef } from 'pdf-lib';

import { Size } from '../src/box.js';
import { Font } from '../src/fonts.js';
import { Frame } from '../src/layout.js';
import { Page } from '../src/page.js';
import { renderFrame, renderPage } from '../src/render-page.js';
import { fakeFont, fakePDFPage, getContentStream } from './test-utils.js';
import { Size } from './box.js';
import { Font } from './fonts.js';
import { Frame } from './layout.js';
import { Page } from './page.js';
import { renderFrame, renderPage } from './render-page.js';
import { fakeFont, fakePDFPage, getContentStream } from './test/test-utils.js';

describe('render-page', () => {
let pdfPage: PDFPage;
Expand Down
12 changes: 6 additions & 6 deletions test/render-text.test.ts → src/render-text.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { beforeEach, describe, expect, it } from '@jest/globals';

import { Size } from '../src/box.js';
import { Font } from '../src/fonts.js';
import { TextObject } from '../src/layout.js';
import { Page } from '../src/page.js';
import { renderText } from '../src/render-text.js';
import { fakeFont, fakePDFPage, getContentStream } from './test-utils.js';
import { Size } from './box.js';
import { Font } from './fonts.js';
import { TextObject } from './layout.js';
import { Page } from './page.js';
import { renderText } from './render-text.js';
import { fakeFont, fakePDFPage, getContentStream } from './test/test-utils.js';

describe('render-text', () => {
let page: Page, size: Size, font: Font;
Expand Down
2 changes: 1 addition & 1 deletion test/svg-paths.test.ts → src/svg-paths.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import { parseSvgPath, svgPathToPdfOps, tokenizeSvgPath } from '../src/svg-paths.js';
import { parseSvgPath, svgPathToPdfOps, tokenizeSvgPath } from './svg-paths.js';

describe('svg-paths', () => {
describe('tokenize', () => {
Expand Down
File renamed without changes
File renamed without changes
10 changes: 5 additions & 5 deletions test/test-utils.ts → src/test/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { PDFContext, PDFDocument, PDFFont, PDFName, PDFPage, PDFRef } from 'pdf-lib';

import { Font } from '../src/fonts.js';
import { Image } from '../src/images.js';
import { Frame } from '../src/layout.js';
import { Page } from '../src/page.js';
import { TextAttrs, TextSpan } from '../src/read-block.js';
import { Font } from '../fonts.js';
import { Image } from '../images.js';
import { Frame } from '../layout.js';
import { Page } from '../page.js';
import { TextAttrs, TextSpan } from '../read-block.js';

export function fakeFont(
name: string,
Expand Down
6 changes: 3 additions & 3 deletions test/text.test.ts → src/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { beforeEach, describe, expect, it } from '@jest/globals';
import { rgb } from 'pdf-lib';

import { Font } from '../src/fonts.js';
import { Font } from './fonts.js';
import { fakeFont } from './test/test-utils.js';
import {
breakLine,
extractTextSegments,
findLinebreakOpportunity,
flattenTextSegments,
splitChunks,
TextSegment,
} from '../src/text.js';
import { fakeFont } from './test-utils.js';
} from './text.js';

const { objectContaining } = expect;

Expand Down
2 changes: 1 addition & 1 deletion test/types.test.ts → src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
readString,
required,
types,
} from '../src/types.js';
} from './types.js';

describe('types', () => {
describe('pickDefined', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/utis.test.ts → src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from '@jest/globals';

import { round } from '../src/utils.js';
import { round } from './utils.js';

describe('utils', () => {
describe('round', () => {
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"incremental": true,
"strict": true
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.js"]
"include": ["src/**/*.ts", "src/**/*.d.ts"],
"exclude": ["node_modules", "dist", "src/**/test/", "src/**/*.test.ts"]
}

0 comments on commit 0805184

Please sign in to comment.