-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathrun_spec.js
More file actions
123 lines (108 loc) · 3.13 KB
/
run_spec.js
File metadata and controls
123 lines (108 loc) · 3.13 KB
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
117
118
119
120
121
122
123
"use strict";
const fs = require("fs");
const { extname } = require("path");
const prettier = require("prettier");
const { AST_COMPARE } = process.env;
function run_spec(dirname, parsers, options) {
options = Object.assign(
{
plugins: ["."],
},
options
);
/* istanbul ignore if */
if (!parsers || !parsers.length) {
throw new Error(`No parsers were specified for ${dirname}`);
}
fs.readdirSync(dirname).forEach((filename) => {
const path = `${dirname}/${filename}`;
if (
extname(filename) !== ".snap" &&
fs.lstatSync(path).isFile() &&
filename[0] !== "." &&
filename !== "jsfmt.spec.js"
) {
let source = read(path);
if (!options.keepEOL) {
source = source.replace(/\r\n/g, "\n");
}
const mergedOptions = Object.assign(mergeDefaultOptions(options || {}), {
parser: parsers[0],
});
const output = prettyprint(source, path, mergedOptions);
test(`${filename} - ${mergedOptions.parser}-verify`, () => {
expect(
raw(`${source + "~".repeat(mergedOptions.printWidth)}\n${output}`)
).toMatchSnapshot(filename);
});
parsers.slice(1).forEach((parserName) => {
test(`${filename} - ${parserName}-verify`, () => {
const verifyOptions = Object.assign(mergedOptions, {
parser: parserName,
});
const verifyOutput = prettyprint(source, path, verifyOptions);
expect(output).toEqual(verifyOutput);
});
});
if (AST_COMPARE && parsers.slice(0) === "lua") {
const compareOptions = Object.assign({}, mergedOptions);
const astMassaged = parse(source, compareOptions);
let ppastMassaged;
let pperr = null;
expect(() => {
ppastMassaged = parse(
prettyprint(source, path, compareOptions),
compareOptions
);
}).not.toThrow();
expect(ppastMassaged).toBeDefined();
test(`${path} parse`, () => {
expect(pperr).toBe(null);
expect(ppastMassaged).toBeDefined();
if (!astMassaged.errors || astMassaged.errors.length === 0) {
expect(astMassaged).toEqual(ppastMassaged);
}
});
}
}
});
}
global.run_spec = run_spec;
function parse(string, opts) {
return prettier.__debug.parse(string, opts, /* massage */ true).ast;
}
function prettyprint(src, filename, options) {
return prettier.format(
src,
Object.assign(
{
filepath: filename,
},
options
)
);
}
function read(filename) {
return fs.readFileSync(filename, "utf8");
}
/**
* Wraps a string in a marker object that is used by `./raw-serializer.js` to
* directly print that string in a snapshot without escaping all double quotes.
* Backticks will still be escaped.
*/
function raw(string) {
if (typeof string !== "string") {
throw new Error("Raw snapshots have to be strings.");
}
return { [Symbol.for("raw")]: string };
}
function mergeDefaultOptions(parserConfig) {
return Object.assign(
{
printWidth: 80,
trailingComma: "none",
semi: false,
},
parserConfig
);
}