2022-11-18 22:05:06 +03:00
|
|
|
import { existsSync, readdirSync, readFileSync } from "fs";
|
|
|
|
import { join } from "path";
|
|
|
|
import { format, Options } from "prettier";
|
|
|
|
import * as jinjaPlugin from "../src/index";
|
|
|
|
|
|
|
|
const prettify = (code: string, options: Options) =>
|
|
|
|
format(code, {
|
|
|
|
parser: "jinja-template",
|
|
|
|
plugins: [jinjaPlugin],
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
|
|
|
|
const testFolder = join(__dirname, "cases");
|
|
|
|
const tests = readdirSync(testFolder);
|
|
|
|
|
|
|
|
tests.forEach((test) => {
|
|
|
|
if (test.startsWith("_")) {
|
|
|
|
return;
|
|
|
|
}
|
2023-08-12 21:32:28 +03:00
|
|
|
return it(test, async () => {
|
2022-11-18 22:05:06 +03:00
|
|
|
const path = join(testFolder, test);
|
|
|
|
const input = readFileSync(join(path, "input.html")).toString();
|
|
|
|
const expected = readFileSync(join(path, "expected.html")).toString();
|
|
|
|
|
|
|
|
const configPath = join(path, "config.json");
|
|
|
|
const configString =
|
|
|
|
existsSync(configPath) && readFileSync(configPath)?.toString();
|
|
|
|
const configObject = configString ? JSON.parse(configString) : {};
|
|
|
|
|
|
|
|
const format = () => prettify(input, configObject);
|
|
|
|
|
|
|
|
const expectedError = expected.match(/Error\(["'`](?<message>.*)["'`]\)/)
|
|
|
|
?.groups?.message;
|
|
|
|
|
|
|
|
if (expectedError) {
|
|
|
|
jest.spyOn(console, "error").mockImplementation(() => {});
|
2023-08-12 21:32:28 +03:00
|
|
|
await expect(format()).rejects.toThrow(expectedError);
|
2022-11-18 22:05:06 +03:00
|
|
|
} else {
|
2023-08-12 21:32:28 +03:00
|
|
|
const result = await format();
|
2022-11-18 22:05:06 +03:00
|
|
|
expect(result).toEqual(expected);
|
2023-08-12 21:32:28 +03:00
|
|
|
expect(await prettify(result, configObject)).toEqual(expected);
|
2022-11-18 22:05:06 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|