-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtransform-tests.ts
116 lines (94 loc) · 3.71 KB
/
transform-tests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import 'jasmine';
import {formatCode} from '../src/transform/format-code';
import {removeContentMarkers} from '../src/transform/remove-content-markers';
import {removeHeaders} from '../src/transform/remove-headers';
import {JS_BODY_MARKER} from '../src/util/js-utils';
describe('Eleventy transform', () => {
describe('format-code', () => {
async function testFormatting(input: string, ext: string) {
const formattedInput = await formatCode(input, `out.${ext}`);
// Ensure the formatter did something (not validating specific rules).
expect(input).not.toEqual(formattedInput);
// Ensure that character content hasn't changed.
expect(unformat(input)).toEqual(unformat(formattedInput));
}
function unformat(input: string) {
return input.replace(/[\n|\s]/g, '');
}
it('formats HTML', async () => {
await testFormatting(
'<html><head><title>test</title></head><body></body></html>', 'html');
});
it('formats JS', async () => {
await testFormatting('function foo() { return bar; }', 'js');
});
it('formats JS in HTML', async () => {
await testFormatting('function foo() { return bar; }', 'html');
});
it('formats TS', async () => {
await testFormatting(
'function foo(bar: string): boolean { return false; }', 'ts');
});
});
describe('remove-content-markers', () => {
const content = `return true;`;
const knownMarker =
`// [START ${JS_BODY_MARKER}]\n${content}\n// [END ${JS_BODY_MARKER}]`;
const unknownMarker =
`// [START other_marker]\n${content}\n// [END other_marker]`;
function testMarkerRemoval(
input: string, expectedOutput: string, ext: string) {
const output = removeContentMarkers(input, `out.${ext}`);
expect(output.trim()).toEqual(expectedOutput.trim());
}
it('leaves request_ads makers in JS', () => {
testMarkerRemoval(knownMarker, knownMarker, 'js');
});
it('removes request_ads marker from TS', () => {
testMarkerRemoval(knownMarker, content, 'ts');
});
it('ignores unrecognized markers in JS', () => {
const input = `${unknownMarker}\n${knownMarker}`;
testMarkerRemoval(input, input, 'js');
});
it('ignores unrecognized markers in TS', () => {
const input = `${unknownMarker}\n${knownMarker}`;
const expectedOutput = `${unknownMarker}\n${content}`;
testMarkerRemoval(input, expectedOutput, 'ts');
});
it('ignores indentation', () => {
const input = `${knownMarker.replace(/^(.*)$/gm, ' $1')}`;
testMarkerRemoval(input, content, 'ts');
});
});
describe('remove-headers', () => {
const licenseHeader = `
/**
* @license
* Copyright 2022 Google LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/`;
const tsStanza = `
// Using @types/google-publisher-tag
// https://www.npmjs.com/package/@types/google-publisher-tag`;
it('removes license header from JS files', () => {
const output = removeHeaders(licenseHeader, 'out.js');
expect(output.trim()).toEqual('');
});
it('removes TS stanza from JS files', () => {
const output = removeHeaders(tsStanza, 'out.js');
expect(output.trim()).toEqual('');
});
it('leaves headers in TS files', () => {
const input = `${licenseHeader}\n${tsStanza}`;
const output = removeHeaders(input, 'out.ts');
expect(output.trim()).toEqual(input.trim());
});
it('correctly handles non-header block comments', () => {
const blockComment = '/* test comment */';
const input = `${licenseHeader}\n${tsStanza}\n${blockComment}`;
const output = removeHeaders(input, 'out.js');
expect(output.trim()).toEqual(blockComment);
});
});
});