blob: 68c7a7775d4c6d0ec8ffbd676c117bc2d0cb363f [file] [log] [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as Common from '../../core/common/common.js';
import * as Formatter from '../formatter/formatter.js';
describe('ScriptFormatter', () => {
const indentString = ' ';
after(() => {
Formatter.FormatterWorkerPool.formatterWorkerPool().dispose();
});
describe('JSON formatting', () => {
it('can format a JSON document via format()', async () => {
// Bug fix test: JSON files should be formatted when using the format() function.
// Previously, format() only formatted documents/scripts/stylesheets, skipping JSON.
const originalContent = '{"a":{"b":{"c":1}}}';
const {formattedContent} = await Formatter.ScriptFormatter.format(
Common.ResourceType.ResourceType.fromMimeType('application/json'), 'application/json', originalContent,
indentString);
const expectedContent = `{
"a": {
"b": {
"c": 1
}
}
}`;
assert.strictEqual(formattedContent, expectedContent);
});
it('can format a JSON document via formatScriptContent()', async () => {
const originalContent = '{"a":{"b":{"c":1}}}';
const {formattedContent} =
await Formatter.ScriptFormatter.formatScriptContent('application/json', originalContent, indentString);
const expectedContent = `{
"a": {
"b": {
"c": 1
}
}
}`;
assert.strictEqual(formattedContent, expectedContent);
});
it('can toggle JSON formatting (format then restore original)', async () => {
// Test that demonstrates the reversibility requirement from issue 378870233
const originalContent = '{"keys":[{"k1":"v1"},{"k2":"v2"}]}';
const {formattedContent, formattedMapping} = await Formatter.ScriptFormatter.format(
Common.ResourceType.ResourceType.fromMimeType('application/json'), 'application/json', originalContent,
indentString);
// Should be formatted
assert.notStrictEqual(formattedContent, originalContent);
assert.include(formattedContent, '\n');
// Mapping should work correctly
assert.deepEqual(formattedMapping.originalToFormatted(0, 0), [0, 0]);
});
});
it('can format a HTML document', async () => {
const {formattedContent} = await Formatter.ScriptFormatter.format(
Common.ResourceType.ResourceType.fromMimeType('text/html'), 'text/html',
'<html><head></head><body></body></html>', indentString);
assert.strictEqual(formattedContent, `<html>
<head></head>
<body></body>
</html>
`);
});
it('can map original locations to formatted locations for HTML documents', async () => {
const {formattedMapping} = await Formatter.ScriptFormatter.format(
Common.ResourceType.ResourceType.fromMimeType('text/html'), 'text/html',
'<html><head></head><body></body></html>', indentString);
// The start of <head>
assert.deepEqual(formattedMapping.originalToFormatted(0, 6), [1, 2]);
});
it('can map original lines to formatted locations for HTML documents', async () => {
const {formattedMapping} = await Formatter.ScriptFormatter.format(
Common.ResourceType.ResourceType.fromMimeType('text/html'), 'text/html', `<html><head>
</head><body></body></html>`,
indentString);
// The start of </head>
assert.deepEqual(formattedMapping.originalToFormatted(1), [1, 8]);
});
it('can map formatted locations to original locations for HTML documents', async () => {
const {formattedMapping} = await Formatter.ScriptFormatter.format(
Common.ResourceType.ResourceType.fromMimeType('text/html'), 'text/html',
'<html><head></head><body></body></html>', indentString);
// The start of <head>
assert.deepEqual(formattedMapping.formattedToOriginal(1, 2), [0, 6]);
});
it('can map formatted lines to original locations for HTML documents', async () => {
const {formattedMapping} = await Formatter.ScriptFormatter.format(
Common.ResourceType.ResourceType.fromMimeType('text/html'), 'text/html',
'<html><head></head><body></body></html>', indentString);
// The start of <head>
assert.deepEqual(formattedMapping.formattedToOriginal(1), [0, 6]);
});
describe('for documents that cant be formatted', () => {
// Technically we can format SVG files, but for this test we pretend its
// mimetype is an image, which we consider unformattable.
const originalContent = `<svg>
<rect x="10" y="-10" /></svg>`;
const mimeType = 'image/svg';
const resourceType = Common.ResourceType.ResourceType.fromMimeType(mimeType);
it('returns the original content', async () => {
const {formattedContent} =
await Formatter.ScriptFormatter.format(resourceType, mimeType, originalContent, indentString);
assert.deepEqual(formattedContent, originalContent);
});
it('maps to the same locations from formatted locations', async () => {
const {formattedMapping} =
await Formatter.ScriptFormatter.format(resourceType, mimeType, originalContent, indentString);
assert.deepEqual(formattedMapping.formattedToOriginal(1, 2), [1, 2]);
});
it('defaults column number to zero from formatted locations', async () => {
const {formattedMapping} =
await Formatter.ScriptFormatter.format(resourceType, mimeType, originalContent, indentString);
assert.deepEqual(formattedMapping.formattedToOriginal(1), [1, 0]);
});
it('maps to the same locations from original locations', async () => {
const {formattedMapping} =
await Formatter.ScriptFormatter.format(resourceType, mimeType, originalContent, indentString);
assert.deepEqual(formattedMapping.originalToFormatted(1, 4), [1, 4]);
});
it('defaults column number to zero from original locations', async () => {
const {formattedMapping} =
await Formatter.ScriptFormatter.format(resourceType, mimeType, originalContent, indentString);
assert.deepEqual(formattedMapping.originalToFormatted(1), [1, 0]);
});
});
});