Skip to content

Commit

Permalink
Rework tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 17, 2022
1 parent 181adb4 commit 854a61b
Show file tree
Hide file tree
Showing 121 changed files with 774 additions and 2,548 deletions.
19 changes: 2 additions & 17 deletions tests/attributes/boolean-attributes/boolean-attributes.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from 'src/index';
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Attributes', () => {
it('should shorten truthy attributes', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted.pug'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted.pug'),
'utf8',
);
const actual: string = format(code, {
parser: 'pug',
plugins: [plugin],
});

const { expected, actual } = compareFiles(__dirname);
expect(actual).toBe(expected);
});
});
20 changes: 3 additions & 17 deletions tests/attributes/class-attributes/class-attributes.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from 'src/index';
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Attributes', () => {
it('should handle class attributes', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted.pug'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted.pug'),
'utf8',
);
const actual: string = format(code, {
parser: 'pug',
plugins: [plugin],
semi: false,
const { actual, expected } = compareFiles(__dirname, {
formatOptions: { semi: false },
});

expect(actual).toBe(expected);
});
});
19 changes: 2 additions & 17 deletions tests/attributes/no-attributes/no-attributes.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from 'src/index';
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Attributes', () => {
it('should handle no attributes', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted.pug'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted.pug'),
'utf8',
);
const actual: string = format(code, {
parser: 'pug',
plugins: [plugin],
});

const { expected, actual } = compareFiles(__dirname);
expect(actual).toBe(expected);
});
});
19 changes: 2 additions & 17 deletions tests/attributes/style-attributes/style-attributes.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from 'src/index';
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Attributes', () => {
it('should handle style attributes', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted.pug'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted.pug'),
'utf8',
);
const actual: string = format(code, {
parser: 'pug',
plugins: [plugin],
});

const { expected, actual } = compareFiles(__dirname);
expect(actual).toBe(expected);
});
});
21 changes: 3 additions & 18 deletions tests/comments/comments.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from 'src/index';
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Comments', () => {
it('should handle comments', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted.pug'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted.pug'),
'utf8',
);
const actual: string = format(code, {
parser: 'pug',
plugins: [plugin],

pugCommentPreserveSpaces: 'trim-all',
const { actual, expected } = compareFiles(__dirname, {
formatOptions: { pugCommentPreserveSpaces: 'trim-all' },
});

expect(actual).toBe(expected);
});
});
80 changes: 80 additions & 0 deletions tests/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import type { Options } from 'prettier';
import { format } from 'prettier';
import type { AttributeToken } from 'pug-lexer';
import { plugin } from 'src/index';

/**
* Creates a fake attribute token.
Expand All @@ -22,3 +27,78 @@ export function createAttributeToken(
},
};
}

/**
* Options for `compareFiles`.
*/
export interface CompareFilesOptions {
/**
* Name of the source code file. Default `'unformatted.pug'`.
*/
source?: string;
/**
* Name of the target code file. Default `'formatted.pug'`.
*
* Pass `null` to explicitly only check the given source.
*/
target?: string | null;
/**
* Further format options.
*
* You can also override the `parser` and `plugins` key.
*
* @default { parser: 'pug', plugins: [plugin] }
*/
formatOptions?: Options;
}

/**
* Result of the comparison.
*/
export interface CompareFilesResult {
/**
* The code of the target file.
*
* `null` if target was also `null`.
*/
readonly expected: string | null;
/**
* The code of the source file.
*/
readonly code: string;
/**
* The actual formatted result.
*/
readonly actual: string;
}

/**
* Compare two files with each other and returns the result to be passed in expect calls.
*
* @param dirname Pass `__dirname`, so the function can relative resolve the files.
* @param param1 Compare options.
* @param param1.source The source file. Default `'unformatted.pug'`.
* @param param1.target The target file. Default `'formatted.pug'`. Pass `null` to explicitly only check the given source.
* @param param1.formatOptions Further format options. Default `{ parser: 'pug', plugins: [plugin] }`. You can also override the parser and plugins key.
* @returns The result to be passed in expect calls.
*/
export function compareFiles(
dirname: string,
{
source = 'unformatted.pug',
target = 'formatted.pug',
formatOptions = {},
}: CompareFilesOptions = {},
): CompareFilesResult {
const expected: string | null = target
? readFileSync(resolve(dirname, target), 'utf8')
: null;
const code: string = readFileSync(resolve(dirname, source), 'utf8');
const actual: string = format(code, {
parser: 'pug',
plugins: [plugin],
...formatOptions,
});

return { expected, code, actual };
}
19 changes: 2 additions & 17 deletions tests/conditionals/conditionals.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from 'src/index';
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Comments', () => {
it('should handle conditionals', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted.pug'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted.pug'),
'utf8',
);
const actual: string = format(code, {
parser: 'pug',
plugins: [plugin],
});

const { expected, actual } = compareFiles(__dirname);
expect(actual).toBe(expected);
});
});
21 changes: 5 additions & 16 deletions tests/embed/md/embed.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from 'src/index';
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Embedded', () => {
it('should format when embedded in markdown', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted.md'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted.md'),
'utf8',
);
const actual: string = format(code, {
parser: 'markdown',
plugins: [plugin],
const { actual, expected } = compareFiles(__dirname, {
source: 'unformatted.md',
target: 'formatted.md',
formatOptions: { parser: 'markdown' },
});

expect(actual).toBe(expected);
});
});
59 changes: 19 additions & 40 deletions tests/embed/vue/embed.test.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,37 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from 'src/index';
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Embedded', () => {
it('should format when embedded in vue', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted.vue'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted.vue'),
'utf8',
);
const actual: string = format(code, {
parser: 'vue',
plugins: [plugin],
const { actual, expected } = compareFiles(__dirname, {
source: 'unformatted.vue',
target: 'formatted.vue',
formatOptions: {
parser: 'vue',
},
});

expect(actual).toBe(expected);
});

it('should format when embedded in vue html reference', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted-html-reference.vue'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted-html-reference.vue'),
'utf8',
);
const actual: string = format(code, {
parser: 'vue',
plugins: [plugin],
const { actual, expected } = compareFiles(__dirname, {
source: 'unformatted-html-reference.vue',
target: 'formatted-html-reference.vue',
formatOptions: {
parser: 'vue',
},
});

expect(actual).toBe(expected);
});

it('should format when embedded in vue empty template', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted-empty-template.vue'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted-empty-template.vue'),
'utf8',
);
const actual: string = format(code, {
parser: 'vue',
plugins: [plugin],
const { actual, expected } = compareFiles(__dirname, {
source: 'unformatted-empty-template.vue',
target: 'formatted-empty-template.vue',
formatOptions: {
parser: 'vue',
},
});

expect(actual).toBe(expected);
});
});
20 changes: 3 additions & 17 deletions tests/frameworks/svelte/svelte.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { format } from 'prettier';
import { plugin } from 'src/index';
import { compareFiles } from 'tests/common';
import { describe, expect, it } from 'vitest';

describe('Frameworks', () => {
describe('Svelte', () => {
it('should format svelte', () => {
const expected: string = readFileSync(
resolve(__dirname, 'formatted.pug'),
'utf8',
);
const code: string = readFileSync(
resolve(__dirname, 'unformatted.pug'),
'utf8',
);
const actual: string = format(code, {
parser: 'pug',
plugins: [plugin],

pugFramework: 'svelte',
const { actual, expected } = compareFiles(__dirname, {
formatOptions: { pugFramework: 'svelte' },
});

expect(actual).toBe(expected);
Expand Down
Loading

0 comments on commit 854a61b

Please sign in to comment.